Skip to content

Commit

Permalink
tweak our upsert code
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyrot committed Mar 4, 2021
1 parent eb4bd15 commit 083bd11
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
5 changes: 2 additions & 3 deletions nick.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ public function store_data(): void
/**
* If $firstseen is empty there have been no lines, actions or events for this
* nick. The only two possibilities left are; this nick has been slapped, or
* this nick had its streak interrupted. Update the appropriate values.
* this nick had its streak interrupted. Update appropriate values.
*/
$queryparts = $this->get_queryparts(['monologues', 'topmonologue', 'slapped']);
db::query_exec('UPDATE uid_lines SET '.$queryparts['update_assignments'].' WHERE uid = '.$uid);
db::query_exec('UPDATE uid_lines SET slapped = slapped + '.$this->slapped.', monologues = monologues + '.$this->monologues.', topmonologue = CASE WHEN '.$this->topmonologue.' > topmonologue THEN '.$this->topmonologue.' ELSE topmonologue END WHERE uid = '.$uid);
return;
}

Expand Down
30 changes: 12 additions & 18 deletions queryparts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

/**
* Trait with code for creating parts of the SQLite query.
* Trait with code for creating parts of SQLite UPSERT queries.
*/
trait queryparts
{
Expand All @@ -17,33 +17,27 @@ private function get_queryparts(array $columns): ?array
$insert_columns[] = $var;
$insert_values[] = $this->$var;

/**
* $topmonologue is a high value and should be treated as such.
*/
if ($var === 'topmonologue') {
/**
* $topmonologue is a high value and should be treated as such.
*/
$update_assignments[] = 'topmonologue = CASE WHEN '.$this->topmonologue.' > topmonologue THEN '.$this->topmonologue.' ELSE topmonologue END';
$update_assignments[] = 'topmonologue = CASE WHEN excluded.topmonologue > topmonologue THEN excluded.topmonologue ELSE topmonologue END';
} else {
$update_assignments[] = $var.' = '.$var.' + '.$this->$var;
$update_assignments[] = $var.' = '.$var.' + excluded.'.$var;
}
}
} elseif (is_string($this->$var)) {
if ($this->$var !== '') {
if ($var === 'lasttalked') {
$value = 'DATETIME(\''.$this->lasttalked.'\')';
} else {
$value = '\''.preg_replace('/\'/', '\'\'', $this->$var).'\'';
}

$insert_columns[] = $var;
$insert_values[] = $value;
$insert_values[] = ($var === 'lasttalked' ? 'DATETIME(\''.$this->lasttalked.'\')' : '\''.preg_replace('/\'/', '\'\'', $this->$var).'\'');

/**
* Don't update a quote if that means its length will fall below 3 words.
*/
if (($var === 'quote' || $var === 'ex_exclamations' || $var === 'ex_questions' || $var === 'ex_uppercased' || 'ex_actions') && substr_count($this->$var, ' ') < 2) {
/**
* Don't update a quote if that means its length will fall below 3 words.
*/
$update_assignments[] = $var.' = CASE WHEN '.$var.' IS NULL OR '.$var.' NOT LIKE \'% % %\' THEN '.$value.' ELSE '.$var.' END';
$update_assignments[] = $var.' = CASE WHEN '.$var.' IS NULL OR '.$var.' NOT LIKE \'% % %\' THEN excluded.'.$var.' ELSE '.$var.' END';
} else {
$update_assignments[] = $var.' = '.$value;
$update_assignments[] = $var.' = excluded.'.$var;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion url.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function store_data(): void
}

foreach ($this->uses as $nick => ['firstused' => $firstused, 'lastused' => $lastused, 'total' => $total]) {
db::query_exec('INSERT INTO uid_urls (uid, lid, firstused, lastused, total) VALUES ((SELECT uid FROM uid_details WHERE csnick = \''.$nick.'\'), '.$lid.', DATETIME(\''.$firstused.'\'), DATETIME(\''.$lastused.'\'), '.$total.') ON CONFLICT (uid, lid) DO UPDATE SET lastused = DATETIME(\''.$lastused.'\'), total = total + '.$total);
db::query_exec('INSERT INTO uid_urls (uid, lid, firstused, lastused, total) VALUES ((SELECT uid FROM uid_details WHERE csnick = \''.$nick.'\'), '.$lid.', DATETIME(\''.$firstused.'\'), DATETIME(\''.$lastused.'\'), '.$total.') ON CONFLICT (uid, lid) DO UPDATE SET lastused = excluded.lastused, total = total + excluded.total');
}
}
}
2 changes: 1 addition & 1 deletion word.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function store_data(): void
/**
* Store data in database table "words".
*/
db::query_exec('INSERT INTO words (word, length, total, firstused) VALUES (\''.$this->word.'\', LENGTH(\''.$this->word.'\'), '.$this->total.', \''.$this->firstused.'\') ON CONFLICT (word) DO UPDATE SET total = total + '.$this->total);
db::query_exec('INSERT INTO words (word, length, total, firstused) VALUES (\''.$this->word.'\', LENGTH(\''.$this->word.'\'), '.$this->total.', \''.$this->firstused.'\') ON CONFLICT (word) DO UPDATE SET total = total + excluded.total');
}
}

0 comments on commit 083bd11

Please sign in to comment.