-
Notifications
You must be signed in to change notification settings - Fork 12
/
queryparts.php
55 lines (49 loc) · 1.72 KB
/
queryparts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php declare(strict_types=1);
/**
* Copyright (c) 2020-2021, Jos de Ruijter <jos@dutnie.nl>
*/
/**
* Trait with code for creating parts of SQLite UPSERT queries.
*/
trait queryparts
{
private function get_queryparts(array $columns): ?array
{
foreach ($columns as $var) {
if (is_int($this->$var)) {
if ($this->$var !== 0) {
$insert_columns[] = $var;
$insert_values[] = $this->$var;
/**
* $topmonologue is a high value and should be treated as such.
*/
if ($var === 'topmonologue') {
$update_assignments[] = 'topmonologue = CASE WHEN excluded.topmonologue > topmonologue THEN excluded.topmonologue ELSE topmonologue END';
} else {
$update_assignments[] = $var.' = '.$var.' + excluded.'.$var;
}
}
} elseif (is_string($this->$var)) {
if ($this->$var !== '') {
$insert_columns[] = $var;
$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) {
$update_assignments[] = $var.' = CASE WHEN '.$var.' IS NULL OR '.$var.' NOT LIKE \'% % %\' THEN excluded.'.$var.' ELSE '.$var.' END';
} else {
$update_assignments[] = $var.' = excluded.'.$var;
}
}
}
}
if (!isset($insert_columns)) {
return null;
}
return [
'insert_columns' => implode(', ', $insert_columns),
'insert_values' => implode(', ', $insert_values),
'update_assignments' => implode(', ', $update_assignments)];
}
}