@@ 73-143 (lines=71) @@ | ||
70 | private $impl; |
|
71 | } |
|
72 | ||
73 | class WhereRule extends OrderByRule |
|
74 | { |
|
75 | public function __construct(Context $context, $isTheFirst = true) |
|
76 | { |
|
77 | parent::__construct($context); |
|
78 | $this->isTheFirst = $isTheFirst; |
|
79 | } |
|
80 | ||
81 | /** |
|
82 | * where('a=?', 1) => "WHERE a=1" |
|
83 | * where('a=?', Sql::raw('now()')) => "WHERE a=now()" |
|
84 | * where('a IN (?)', [1, 2]) => "WHERE a IN (1,2)" |
|
85 | * where([ |
|
86 | * 'a'=>1, |
|
87 | * 'b'=>['IN'=>[1,2]] |
|
88 | * 'c'=>['BETWEEN'=>[1,2]] |
|
89 | * 'd'=>['<>'=>1] |
|
90 | * ]) |
|
91 | * => |
|
92 | * "WHERE a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1" |
|
93 | * |
|
94 | * @param string|array|callable $conditions |
|
95 | * @param mixed $_ |
|
96 | * @return WhereRule |
|
97 | */ |
|
98 | public function where($conditions=null, $_=null) { |
|
99 | if(is_callable($conditions)){ |
|
100 | $callback = function ($context)use($conditions){ |
|
101 | $rule = new SubQuery($context); |
|
102 | $conditions($rule); |
|
103 | }; |
|
104 | $conditions = $callback; |
|
105 | } |
|
106 | if($this->isTheFirst){ |
|
107 | WhereImpl::where($this->context, 'WHERE' ,$conditions, array_slice(func_get_args(), 1)); |
|
108 | }else{ |
|
109 | WhereImpl::where($this->context, 'AND', $conditions, array_slice(func_get_args(), 1)); |
|
110 | } |
|
111 | return new WhereRule($this->context, false); |
|
112 | } |
|
113 | ||
114 | /** |
|
115 | * orWhere('a=?', 1) => "OR a=1" |
|
116 | * orWhere('a=?', Sql::raw('now()')) => "OR a=now()" |
|
117 | * orWhere('a IN (?)', [1, 2]) => "OR a IN (1,2)" |
|
118 | * orWhere([ |
|
119 | * 'a'=>1, |
|
120 | * 'b'=>['IN'=>[1,2]] |
|
121 | * 'c'=>['BETWEEN'=>[1,2]] |
|
122 | * 'd'=>['<>'=>1] |
|
123 | * ]) |
|
124 | * => |
|
125 | * "OR (a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1)" |
|
126 | * |
|
127 | * @param string|array|callable $conditions |
|
128 | * @param mixed $_ |
|
129 | * @return WhereRule |
|
130 | */ |
|
131 | public function orWhere($conditions=null, $_=null) { |
|
132 | if(is_callable($conditions)){ |
|
133 | $callback = function ($context)use($conditions){ |
|
134 | $rule = new SubQuery($context); |
|
135 | $conditions($rule); |
|
136 | }; |
|
137 | $conditions = $callback; |
|
138 | } |
|
139 | WhereImpl::where($this->context, 'OR', $conditions, array_slice(func_get_args(), 1)); |
|
140 | return new WhereRule($this->context, false); |
|
141 | } |
|
142 | private $isTheFirst; |
|
143 | } |
|
144 | ||
145 | class SubQuery extends BasicRule |
|
146 | { |
|
@@ 145-192 (lines=48) @@ | ||
142 | private $isTheFirst; |
|
143 | } |
|
144 | ||
145 | class SubQuery extends BasicRule |
|
146 | { |
|
147 | ||
148 | public function __construct(Context $context, $isTheFirst = true) |
|
149 | { |
|
150 | parent::__construct($context); |
|
151 | $this->isTheFirst = $isTheFirst; |
|
152 | } |
|
153 | ||
154 | /** |
|
155 | * @param $expr |
|
156 | * @param null $_ |
|
157 | * @return SubQuery |
|
158 | */ |
|
159 | public function where($expr, $_= null){ |
|
160 | if(is_callable($expr)){ |
|
161 | $callback = function ($context)use($expr){ |
|
162 | $rule = new SubQuery($context, true); |
|
163 | $expr($rule); |
|
164 | }; |
|
165 | $expr = $callback; |
|
166 | } |
|
167 | if($this->isTheFirst){ |
|
168 | WhereImpl::where($this->context, '', $expr, array_slice(func_get_args(), 1)); |
|
169 | }else{ |
|
170 | WhereImpl::where($this->context, 'AND', $expr, array_slice(func_get_args(), 1)); |
|
171 | } |
|
172 | return new SubQuery($this->context, false); |
|
173 | } |
|
174 | /** |
|
175 | * @param $expr |
|
176 | * @param null $_ |
|
177 | * @return SubQuery |
|
178 | */ |
|
179 | public function orWhere($expr, $_= null){ |
|
180 | if(is_callable($expr)){ |
|
181 | $callback = function ($context)use($expr){ |
|
182 | $rule = new SubQuery($context, true); |
|
183 | $expr($rule); |
|
184 | }; |
|
185 | $expr = $callback; |
|
186 | } |
|
187 | WhereImpl::where($this->context, 'OR', $expr, array_slice(func_get_args(), 1)); |
|
188 | return new SubQuery($this->context, false); |
|
189 | } |
|
190 | ||
191 | private $isTheFirst; |
|
192 | } |
@@ 150-227 (lines=78) @@ | ||
147 | private $order; |
|
148 | } |
|
149 | ||
150 | class HavingRule extends OrderByRule |
|
151 | { |
|
152 | public function __construct(Context $context, $isTheFirst = true) |
|
153 | { |
|
154 | parent::__construct($context); |
|
155 | $this->isTheFirst = $isTheFirst; |
|
156 | } |
|
157 | /** |
|
158 | * |
|
159 | * having('SUM(a)=?', 1) => "HAVING SUM(a)=1" |
|
160 | * having('a>?', Sql::raw('now()')) => "HAVING a>now()" |
|
161 | * having('a IN (?)', [1, 2]) => "HAVING a IN (1,2)" |
|
162 | * |
|
163 | * having([ |
|
164 | * 'a'=>1, |
|
165 | * 'b'=>['IN'=>[1,2]] |
|
166 | * 'c'=>['BETWEEN'=>[1,2]] |
|
167 | * 'd'=>['<>'=>1] |
|
168 | * ]) |
|
169 | * |
|
170 | * => |
|
171 | * "HAVING a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1" |
|
172 | * |
|
173 | * @param string|array|callable $expr |
|
174 | * @param string $_ |
|
175 | * @return \PhpBoot\DB\rules\select\HavingRule |
|
176 | */ |
|
177 | public function having($expr, $_=null) { |
|
178 | if(is_callable($expr)){ |
|
179 | $callback = function ($context)use($expr){ |
|
180 | $rule = new SubQuery($context); |
|
181 | $expr($rule); |
|
182 | }; |
|
183 | $expr = $callback; |
|
184 | } |
|
185 | if($this->isTheFirst){ |
|
186 | WhereImpl::where($this->context, 'HAVING', $expr, array_slice(func_get_args(), 1)); |
|
187 | }else{ |
|
188 | WhereImpl::where($this->context, 'AND', $expr, array_slice(func_get_args(), 1)); |
|
189 | } |
|
190 | ||
191 | return new HavingRule($this->context, false); |
|
192 | } |
|
193 | ||
194 | /** |
|
195 | * |
|
196 | * orHaving('SUM(a)=?', 1) => "OR SUM(a)=1" |
|
197 | * orHaving('a>?', Sql::raw('now()')) => "OR a>now()" |
|
198 | * orHaving('a IN (?)', [1, 2]) => "OR a IN (1,2)" |
|
199 | * |
|
200 | * orHaving([ |
|
201 | * 'a'=>1, |
|
202 | * 'b'=>['IN'=>[1,2]] |
|
203 | * 'c'=>['BETWEEN'=>[1,2]] |
|
204 | * 'd'=>['<>'=>1] |
|
205 | * ]) |
|
206 | * |
|
207 | * => |
|
208 | * "OR (a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1)" |
|
209 | * |
|
210 | * @param string|array|callable $expr |
|
211 | * @param string $_ |
|
212 | * @return \PhpBoot\DB\rules\select\HavingRule |
|
213 | */ |
|
214 | public function orHaving($expr, $_=null) { |
|
215 | if(is_callable($expr)){ |
|
216 | $callback = function ($context)use($expr){ |
|
217 | $rule = new SubQuery($context); |
|
218 | $expr($rule); |
|
219 | }; |
|
220 | $expr = $callback; |
|
221 | } |
|
222 | WhereImpl::where($this->context, 'OR', $expr, array_slice(func_get_args(), 1)); |
|
223 | return new HavingRule($this->context, false); |
|
224 | } |
|
225 | ||
226 | private $isTheFirst; |
|
227 | } |
|
228 | class GroupByRule extends OrderByRule |
|
229 | { |
|
230 | /** |
|
@@ 241-311 (lines=71) @@ | ||
238 | } |
|
239 | } |
|
240 | ||
241 | class WhereRule extends GroupByRule |
|
242 | { |
|
243 | public function __construct(Context $context, $isTheFirst = true) |
|
244 | { |
|
245 | parent::__construct($context); |
|
246 | $this->isTheFirst = $isTheFirst; |
|
247 | } |
|
248 | ||
249 | /** |
|
250 | * where('a=?', 1) => "WHERE a=1" |
|
251 | * where('a=?', Sql::raw('now()')) => "WHERE a=now()" |
|
252 | * where('a IN (?)', [1, 2]) => "WHERE a IN (1,2)" |
|
253 | * where([ |
|
254 | * 'a'=>1, |
|
255 | * 'b'=>['IN'=>[1,2]] |
|
256 | * 'c'=>['BETWEEN'=>[1,2]] |
|
257 | * 'd'=>['<>'=>1] |
|
258 | * ]) |
|
259 | * => |
|
260 | * "WHERE a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1" |
|
261 | * |
|
262 | * @param string|array|callable $conditions |
|
263 | * @param mixed $_ |
|
264 | * @return \PhpBoot\DB\rules\select\WhereRule |
|
265 | */ |
|
266 | public function where($conditions=null, $_=null) { |
|
267 | if(is_callable($conditions)){ |
|
268 | $callback = function ($context)use($conditions){ |
|
269 | $rule = new SubQuery($context); |
|
270 | $conditions($rule); |
|
271 | }; |
|
272 | $conditions = $callback; |
|
273 | } |
|
274 | if($this->isTheFirst){ |
|
275 | WhereImpl::where($this->context, 'WHERE' ,$conditions, array_slice(func_get_args(), 1)); |
|
276 | }else{ |
|
277 | WhereImpl::where($this->context, 'AND', $conditions, array_slice(func_get_args(), 1)); |
|
278 | } |
|
279 | return new WhereRule($this->context, false); |
|
280 | } |
|
281 | ||
282 | /** |
|
283 | * orWhere('a=?', 1) => "OR a=1" |
|
284 | * orWhere('a=?', Sql::raw('now()')) => "OR a=now()" |
|
285 | * orWhere('a IN (?)', [1, 2]) => "OR a IN (1,2)" |
|
286 | * orWhere([ |
|
287 | * 'a'=>1, |
|
288 | * 'b'=>['IN'=>[1,2]] |
|
289 | * 'c'=>['BETWEEN'=>[1,2]] |
|
290 | * 'd'=>['<>'=>1] |
|
291 | * ]) |
|
292 | * => |
|
293 | * "OR (a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1)" |
|
294 | * |
|
295 | * @param string|array|callable $conditions |
|
296 | * @param mixed $_ |
|
297 | * @return \PhpBoot\DB\rules\select\WhereRule |
|
298 | */ |
|
299 | public function orWhere($conditions=null, $_=null) { |
|
300 | if(is_callable($conditions)){ |
|
301 | $callback = function ($context)use($conditions){ |
|
302 | $rule = new SubQuery($context); |
|
303 | $conditions($rule); |
|
304 | }; |
|
305 | $conditions = $callback; |
|
306 | } |
|
307 | WhereImpl::where($this->context, 'OR', $conditions, array_slice(func_get_args(), 1)); |
|
308 | return new WhereRule($this->context, false); |
|
309 | } |
|
310 | private $isTheFirst; |
|
311 | } |
|
312 | ||
313 | class JoinRule extends WhereRule |
|
314 | { |