|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Trait MySQLWhereTrait |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource MySQLWhereTrait.php |
|
6
|
|
|
* @created 07.06.2017 |
|
7
|
|
|
* @package chillerlan\Database\Query\Dialects\Firebird |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2017 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\Database\Query\Dialects\Firebird; |
|
14
|
|
|
|
|
15
|
|
|
use chillerlan\Database\Query\StatementInterface; |
|
16
|
|
|
use chillerlan\Database\Query\Traits\WhereTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @method quote(string $str):string |
|
20
|
|
|
*/ |
|
21
|
|
View Code Duplication |
trait FirebirdWhereTrait{ |
|
|
|
|
|
|
22
|
|
|
use WhereTrait; |
|
23
|
|
|
|
|
24
|
|
|
protected $where = []; |
|
25
|
|
|
protected $bindValues = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param $val1 |
|
29
|
|
|
* @param null $val2 |
|
30
|
|
|
* @param null $operator |
|
31
|
|
|
* @param bool $bind |
|
32
|
|
|
* @param string $join |
|
33
|
|
|
* |
|
34
|
|
|
* @return $this |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function _addWhere($val1, $val2 = null, $operator = null, $bind = true, $join = 'AND'){ |
|
37
|
|
|
$operator = strtoupper(trim($operator)); |
|
38
|
|
|
$join = strtoupper(trim($join)); |
|
39
|
|
|
|
|
40
|
|
|
$operators = [ |
|
41
|
|
|
'=', '>=', '>', '<=', '<', '<>', '!=', |
|
42
|
|
|
'|', '&', '<<', '>>', '+', '-', '*', '/', '%', '^', '<=>', '~', '!', 'DIV', 'MOD', |
|
43
|
|
|
'IS', 'IS NOT', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
if(in_array($operator, $operators, true)){ |
|
47
|
|
|
$where = [$this->quote($val1)]; |
|
48
|
|
|
$values = []; |
|
49
|
|
|
|
|
50
|
|
|
if(in_array($operator, ['IN', 'NOT IN'], true)){ |
|
51
|
|
|
|
|
52
|
|
|
if(is_array($val2)){ |
|
53
|
|
|
if($bind){ |
|
54
|
|
|
$where[] = 'IN('.implode(',', array_fill(0, count($val2), '?')).')'; |
|
55
|
|
|
$values = array_merge($values, $val2); |
|
56
|
|
|
} |
|
57
|
|
|
else{ |
|
58
|
|
|
$where[] = 'IN('.implode(',', $val2).')'; // @todo: quote |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
else if($val2 instanceof StatementInterface){ |
|
62
|
|
|
$where[] = 'IN('.$val2->sql().')'; |
|
63
|
|
|
$values = array_merge($values, $val2->bindValues()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
else if(in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)){ |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
else{ |
|
71
|
|
|
$where[] = $operator; |
|
72
|
|
|
|
|
73
|
|
|
if(is_null($val2)){ |
|
74
|
|
|
$where[] = 'NULL'; |
|
75
|
|
|
} |
|
76
|
|
|
else if(is_bool($val2)){ |
|
77
|
|
|
$where[] = $val2 ? 'TRUE' : 'FALSE'; |
|
78
|
|
|
} |
|
79
|
|
|
else if(in_array(strtolower($val2), ['null', 'false', 'true', 'unknown'])){ |
|
80
|
|
|
$where[] = strtoupper($val2); |
|
81
|
|
|
} |
|
82
|
|
|
else if($val2 instanceof StatementInterface){ |
|
83
|
|
|
$where[] = '('.$val2->sql().')'; |
|
84
|
|
|
$values = array_merge($values, $val2->bindValues()); |
|
85
|
|
|
} |
|
86
|
|
|
else{ |
|
87
|
|
|
if($bind){ |
|
88
|
|
|
$where[] = '?'; |
|
89
|
|
|
$values[] = $val2; |
|
90
|
|
|
} |
|
91
|
|
|
else{ |
|
92
|
|
|
if(!empty($val2)){ |
|
93
|
|
|
$where[] = $this->quote($val2); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->bindValues = array_merge($this->bindValues, $values); |
|
101
|
|
|
$this->where[] = [ |
|
102
|
|
|
'join' => in_array($join, ['AND', 'OR', 'XOR']) ? $join : 'AND', |
|
103
|
|
|
'stmt' => implode(' ', $where), |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
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.