|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* @filesource PostgresWhereTrait.php |
|
5
|
|
|
* @created 12.06.2017 |
|
6
|
|
|
* @package chillerlan\Database\Query\Dialects\Postgres |
|
7
|
|
|
* @author Smiley <[email protected]> |
|
8
|
|
|
* @copyright 2017 Smiley |
|
9
|
|
|
* @license MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace chillerlan\Database\Query\Dialects\Postgres; |
|
13
|
|
|
|
|
14
|
|
|
use chillerlan\Database\Query\StatementInterface; |
|
15
|
|
|
use chillerlan\Database\Query\Traits\WhereTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Trait PostgresWhereTrait |
|
19
|
|
|
*/ |
|
20
|
|
View Code Duplication |
trait PostgresWhereTrait{ |
|
|
|
|
|
|
21
|
|
|
use WhereTrait; |
|
22
|
|
|
|
|
23
|
|
|
protected function _addWhere($val1, $val2 = null, $operator = null, $bind = true, $join = 'AND'){ |
|
24
|
|
|
$operator = strtoupper(trim($operator)); |
|
25
|
|
|
$join = strtoupper(trim($join)); |
|
26
|
|
|
|
|
27
|
|
|
$operators = [ |
|
28
|
|
|
'=', '>=', '>', '<=', '<', '<>', '!=', |
|
29
|
|
|
'|', '&', '<<', '>>', '+', '-', '*', '/', '%', '^', '<=>', '~', '!', 'DIV', 'MOD', |
|
30
|
|
|
'IS', 'IS NOT', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
if(in_array($operator, $operators, true)){ |
|
34
|
|
|
$where = [$this->quote($val1)]; |
|
|
|
|
|
|
35
|
|
|
$values = []; |
|
36
|
|
|
|
|
37
|
|
|
if(in_array($operator, ['IN', 'NOT IN'], true)){ |
|
38
|
|
|
|
|
39
|
|
|
if(is_array($val2)){ |
|
40
|
|
|
if($bind){ |
|
41
|
|
|
$where[] = 'IN('.implode(',', array_fill(0, count($val2), '?')).')'; |
|
42
|
|
|
$values = array_merge($values, $val2); |
|
43
|
|
|
} |
|
44
|
|
|
else{ |
|
45
|
|
|
$where[] = 'IN('.implode(',', $val2).')'; // @todo: quote |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
else if($val2 instanceof StatementInterface){ |
|
49
|
|
|
$where[] = 'IN('.$val2->sql().')'; |
|
50
|
|
|
$values = array_merge($values, $val2->bindValues()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
else if(in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)){ |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
else{ |
|
58
|
|
|
$where[] = $operator; |
|
59
|
|
|
|
|
60
|
|
|
if(is_null($val2)){ |
|
61
|
|
|
$where[] = 'NULL'; |
|
62
|
|
|
} |
|
63
|
|
|
else if(is_bool($val2)){ |
|
64
|
|
|
$where[] = $val2 ? 'TRUE' : 'FALSE'; |
|
65
|
|
|
} |
|
66
|
|
|
else if(in_array(strtolower($val2), ['null', 'false', 'true', 'unknown'])){ |
|
67
|
|
|
$where[] = strtoupper($val2); |
|
68
|
|
|
} |
|
69
|
|
|
else if($val2 instanceof StatementInterface){ |
|
70
|
|
|
$where[] = '('.$val2->sql().')'; |
|
71
|
|
|
$values = array_merge($values, $val2->bindValues()); |
|
72
|
|
|
} |
|
73
|
|
|
else{ |
|
74
|
|
|
if($bind){ |
|
75
|
|
|
$where[] = '?'; |
|
76
|
|
|
$values[] = $val2; |
|
77
|
|
|
} |
|
78
|
|
|
else{ |
|
79
|
|
|
if(!empty($val2)){ |
|
80
|
|
|
$where[] = $this->quote($val2); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->bindValues = array_merge($this->bindValues, $values); |
|
|
|
|
|
|
88
|
|
|
$this->where[] = [ |
|
89
|
|
|
'join' => in_array($join, ['AND', 'OR', 'XOR']) ? $join : 'AND', |
|
90
|
|
|
'stmt' => implode(' ', $where), |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.