1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JAQB\Query\Traits; |
4
|
|
|
|
5
|
|
|
trait Where |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var \JAQB\Statement\WhereStatement |
9
|
|
|
*/ |
10
|
|
|
protected $where; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Sets the where conditions for the query. |
14
|
|
|
* |
15
|
|
|
* @param array|string $field |
16
|
|
|
* @param string|bool $condition condition value (optional) |
17
|
|
|
* @param string $operator operator (optional) |
18
|
|
|
* |
19
|
|
|
* @return self |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public function where($field, $condition = false, $operator = '=') |
22
|
|
|
{ |
23
|
|
|
if (func_num_args() >= 2) { |
24
|
|
|
$this->where->addCondition($field, $condition, $operator); |
25
|
|
|
} else { |
26
|
|
|
$this->where->addCondition($field); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Adds a where or condition to the query. |
34
|
|
|
* |
35
|
|
|
* @param array|string $field |
36
|
|
|
* @param string $condition condition value (optional) |
37
|
|
|
* @param string $operator operator (optional) |
38
|
|
|
* |
39
|
|
|
* @return self |
40
|
|
|
*/ |
41
|
|
|
public function orWhere($field, $condition = false, $operator = '=') |
42
|
|
|
{ |
43
|
|
|
if (func_num_args() >= 2) { |
44
|
|
|
$this->where->addOrCondition($field, $condition, $operator); |
45
|
|
|
} else { |
46
|
|
|
$this->where->addOrCondition($field); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Sets the where conditions for the query with infix style arguments. |
54
|
|
|
* |
55
|
|
|
* @param array|string $field |
56
|
|
|
* @param string $operator operator (optional) |
57
|
|
|
* @param string|bool $condition condition value (optional) |
58
|
|
|
* |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function whereInfix($field, $operator = '=', $condition = false) |
62
|
|
|
{ |
63
|
|
|
$numArgs = func_num_args(); |
64
|
|
|
if ($numArgs > 2) { |
65
|
|
|
$this->where->addCondition($field, $condition, $operator); |
66
|
|
|
} elseif ($numArgs == 2) { |
67
|
|
|
$this->where->addCondition($field, $operator, '='); |
68
|
|
|
} else { |
69
|
|
|
$this->where->addCondition($field); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Adds a where or condition to the query with infix style arguments. |
77
|
|
|
* |
78
|
|
|
* @param array|string $field |
79
|
|
|
* @param string $operator operator (optional) |
80
|
|
|
* @param string $condition condition value (optional) |
81
|
|
|
* |
82
|
|
|
* @return self |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function orWhereInfix($field, $operator = '=', $condition = false) |
85
|
|
|
{ |
86
|
|
|
$numArgs = func_num_args(); |
87
|
|
|
if ($numArgs > 2) { |
88
|
|
|
$this->where->addOrCondition($field, $condition, $operator); |
89
|
|
|
} elseif ($numArgs == 2) { |
90
|
|
|
$this->where->addOrCondition($field, $operator, '='); |
91
|
|
|
} else { |
92
|
|
|
$this->where->addOrCondition($field); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Adds a where not condition to the query. |
100
|
|
|
* |
101
|
|
|
* @param string $field |
102
|
|
|
* @param string $condition condition value (optional) |
103
|
|
|
* |
104
|
|
|
* @return self |
105
|
|
|
*/ |
106
|
|
|
public function not($field, $condition = true) |
107
|
|
|
{ |
108
|
|
|
$this->where->addCondition($field, $condition, '<>'); |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Adds a where between condition to the query. |
115
|
|
|
* |
116
|
|
|
* @param string $field |
117
|
|
|
* @param mixed $a first between value |
118
|
|
|
* @param mixed $b second between value |
119
|
|
|
* |
120
|
|
|
* @return self |
121
|
|
|
*/ |
122
|
|
|
public function between($field, $a, $b) |
123
|
|
|
{ |
124
|
|
|
$this->where->addBetweenCondition($field, $a, $b); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Adds a where not between condition to the query. |
131
|
|
|
* |
132
|
|
|
* @param string $field |
133
|
|
|
* @param mixed $a first between value |
134
|
|
|
* @param mixed $b second between value |
135
|
|
|
* |
136
|
|
|
* @return self |
137
|
|
|
*/ |
138
|
|
|
public function notBetween($field, $a, $b) |
139
|
|
|
{ |
140
|
|
|
$this->where->addNotBetweenCondition($field, $a, $b); |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Adds an exists condition to the query. |
147
|
|
|
* |
148
|
|
|
* @param callable $f |
149
|
|
|
* |
150
|
|
|
* @return self |
151
|
|
|
*/ |
152
|
|
|
public function exists(callable $f) |
153
|
|
|
{ |
154
|
|
|
$this->where->addExistsCondition($f); |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Adds a not exists condition to the query. |
161
|
|
|
* |
162
|
|
|
* @param callable $f |
163
|
|
|
* |
164
|
|
|
* @return self |
165
|
|
|
*/ |
166
|
|
|
public function notExists(callable $f) |
167
|
|
|
{ |
168
|
|
|
$this->where->addNotExistsCondition($f); |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Gets the where statement for the query. |
175
|
|
|
* |
176
|
|
|
* @return \JAQB\Statement\WhereStatement |
177
|
|
|
*/ |
178
|
|
|
public function getWhere() |
179
|
|
|
{ |
180
|
|
|
return $this->where; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|