@@ -24,1505 +24,1505 @@ |
||
24 | 24 | |
25 | 25 | class QueryBuilderHandler implements HasConnection |
26 | 26 | { |
27 | - /** |
|
28 | - * @method add |
|
29 | - */ |
|
30 | - use TablePrefixer; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var \Viocon\Container |
|
34 | - */ |
|
35 | - protected $container; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var Connection |
|
39 | - */ |
|
40 | - protected $connection; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var array<string, mixed[]|mixed> |
|
44 | - */ |
|
45 | - protected $statements = []; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var wpdb |
|
49 | - */ |
|
50 | - protected $dbInstance; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var string|string[]|null |
|
54 | - */ |
|
55 | - protected $sqlStatement = null; |
|
56 | - |
|
57 | - /** |
|
58 | - * @var string|null |
|
59 | - */ |
|
60 | - protected $tablePrefix = null; |
|
61 | - |
|
62 | - /** |
|
63 | - * @var WPDBAdapter |
|
64 | - */ |
|
65 | - protected $adapterInstance; |
|
66 | - |
|
67 | - /** |
|
68 | - * The mode to return results as. |
|
69 | - * Accepts WPDB constants or class names. |
|
70 | - * |
|
71 | - * @var string |
|
72 | - */ |
|
73 | - protected $fetchMode; |
|
74 | - |
|
75 | - /** |
|
76 | - * Custom args used to construct models for hydrator |
|
77 | - * |
|
78 | - * @var array<int, mixed>|null |
|
79 | - */ |
|
80 | - protected $hydratorConstructorArgs; |
|
81 | - |
|
82 | - /** |
|
83 | - * Handler for Json Selectors |
|
84 | - * |
|
85 | - * @var JsonHandler |
|
86 | - */ |
|
87 | - protected $jsonHandler; |
|
88 | - |
|
89 | - /** |
|
90 | - * @param \Pixie\Connection|null $connection |
|
91 | - * @param string $fetchMode |
|
92 | - * @param mixed[] $hydratorConstructorArgs |
|
93 | - * |
|
94 | - * @throws Exception if no connection passed and not previously established |
|
95 | - */ |
|
96 | - final public function __construct( |
|
97 | - Connection $connection = null, |
|
98 | - string $fetchMode = \OBJECT, |
|
99 | - ?array $hydratorConstructorArgs = null |
|
100 | - ) { |
|
101 | - if (is_null($connection)) { |
|
102 | - // throws if connection not already established. |
|
103 | - $connection = Connection::getStoredConnection(); |
|
104 | - } |
|
105 | - |
|
106 | - // Set all dependencies from connection. |
|
107 | - $this->connection = $connection; |
|
108 | - $this->container = $this->connection->getContainer(); |
|
109 | - $this->dbInstance = $this->connection->getDbInstance(); |
|
110 | - $this->setAdapterConfig($this->connection->getAdapterConfig()); |
|
111 | - |
|
112 | - // Set up optional hydration details. |
|
113 | - $this->setFetchMode($fetchMode); |
|
114 | - $this->hydratorConstructorArgs = $hydratorConstructorArgs; |
|
115 | - |
|
116 | - // Query builder adapter instance |
|
117 | - $this->adapterInstance = $this->container->build( |
|
118 | - WPDBAdapter::class, |
|
119 | - [$this->connection] |
|
120 | - ); |
|
121 | - |
|
122 | - // Setup JSON Selector handler. |
|
123 | - $this->jsonHandler = new JsonHandler($connection); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Sets the config for WPDB |
|
128 | - * |
|
129 | - * @param array<string, mixed> $adapterConfig |
|
130 | - * |
|
131 | - * @return void |
|
132 | - */ |
|
133 | - protected function setAdapterConfig(array $adapterConfig): void |
|
134 | - { |
|
135 | - if (isset($adapterConfig[Connection::PREFIX])) { |
|
136 | - $this->tablePrefix = $adapterConfig[Connection::PREFIX]; |
|
137 | - } |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Fetch query results as object of specified type |
|
142 | - * |
|
143 | - * @param string $className |
|
144 | - * @param array<int, mixed> $constructorArgs |
|
145 | - * @return static |
|
146 | - */ |
|
147 | - public function asObject($className, $constructorArgs = array()): self |
|
148 | - { |
|
149 | - return $this->setFetchMode($className, $constructorArgs); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Set the fetch mode |
|
154 | - * |
|
155 | - * @param string $mode |
|
156 | - * @param array<int, mixed>|null $constructorArgs |
|
157 | - * |
|
158 | - * @return static |
|
159 | - */ |
|
160 | - public function setFetchMode(string $mode, ?array $constructorArgs = null): self |
|
161 | - { |
|
162 | - $this->fetchMode = $mode; |
|
163 | - $this->hydratorConstructorArgs = $constructorArgs; |
|
164 | - |
|
165 | - return $this; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @param Connection|null $connection |
|
170 | - * |
|
171 | - * @return static |
|
172 | - * |
|
173 | - * @throws Exception |
|
174 | - */ |
|
175 | - public function newQuery(Connection $connection = null): self |
|
176 | - { |
|
177 | - if (is_null($connection)) { |
|
178 | - $connection = $this->connection; |
|
179 | - } |
|
180 | - |
|
181 | - $newQuery = $this->constructCurrentBuilderClass($connection); |
|
182 | - $newQuery->setFetchMode($this->getFetchMode(), $this->hydratorConstructorArgs); |
|
183 | - |
|
184 | - return $newQuery; |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Returns a new instance of the current, with the passed connection. |
|
189 | - * |
|
190 | - * @param \Pixie\Connection $connection |
|
191 | - * |
|
192 | - * @return static |
|
193 | - */ |
|
194 | - protected function constructCurrentBuilderClass(Connection $connection): self |
|
195 | - { |
|
196 | - return new static($connection); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Interpolates a query |
|
201 | - * |
|
202 | - * @param string $query |
|
203 | - * @param array<mixed> $bindings |
|
204 | - * @return string |
|
205 | - */ |
|
206 | - public function interpolateQuery(string $query, array $bindings = []): string |
|
207 | - { |
|
208 | - return $this->adapterInstance->interpolateQuery($query, $bindings); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @param string $sql |
|
213 | - * @param array<int,mixed> $bindings |
|
214 | - * |
|
215 | - * @return static |
|
216 | - */ |
|
217 | - public function query($sql, $bindings = []): self |
|
218 | - { |
|
219 | - list($this->sqlStatement) = $this->statement($sql, $bindings); |
|
220 | - |
|
221 | - return $this; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * @param string $sql |
|
226 | - * @param array<int,mixed> $bindings |
|
227 | - * |
|
228 | - * @return array{0:string, 1:float} |
|
229 | - */ |
|
230 | - public function statement(string $sql, $bindings = []): array |
|
231 | - { |
|
232 | - $start = microtime(true); |
|
233 | - $sqlStatement = empty($bindings) ? $sql : $this->interpolateQuery($sql, $bindings); |
|
234 | - |
|
235 | - if (!is_string($sqlStatement)) { |
|
236 | - throw new Exception('Could not interpolate query', 1); |
|
237 | - } |
|
238 | - |
|
239 | - return [$sqlStatement, microtime(true) - $start]; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Get all rows |
|
244 | - * |
|
245 | - * @return array<mixed,mixed>|null |
|
246 | - * |
|
247 | - * @throws Exception |
|
248 | - */ |
|
249 | - public function get() |
|
250 | - { |
|
251 | - $eventResult = $this->fireEvents('before-select'); |
|
252 | - if (!is_null($eventResult)) { |
|
253 | - return $eventResult; |
|
254 | - } |
|
255 | - $executionTime = 0; |
|
256 | - if (is_null($this->sqlStatement)) { |
|
257 | - $queryObject = $this->getQuery('select'); |
|
258 | - $statement = $this->statement( |
|
259 | - $queryObject->getSql(), |
|
260 | - $queryObject->getBindings() |
|
261 | - ); |
|
262 | - |
|
263 | - $this->sqlStatement = $statement[0]; |
|
264 | - $executionTime = $statement[1]; |
|
265 | - } |
|
266 | - |
|
267 | - $start = microtime(true); |
|
268 | - $result = $this->dbInstance()->get_results( |
|
269 | - is_array($this->sqlStatement) ? (end($this->sqlStatement) ?: '') : $this->sqlStatement, |
|
270 | - // If we are using the hydrator, return as OBJECT and let the hydrator map the correct model. |
|
271 | - $this->useHydrator() ? OBJECT : $this->getFetchMode() |
|
272 | - ); |
|
273 | - $executionTime += microtime(true) - $start; |
|
274 | - $this->sqlStatement = null; |
|
275 | - |
|
276 | - // Ensure we have an array of results. |
|
277 | - if (!is_array($result) && null !== $result) { |
|
278 | - $result = [$result]; |
|
279 | - } |
|
280 | - |
|
281 | - // Maybe hydrate the results. |
|
282 | - if (null !== $result && $this->useHydrator()) { |
|
283 | - $result = $this->getHydrator()->fromMany($result); |
|
284 | - } |
|
285 | - |
|
286 | - $this->fireEvents('after-select', $result, $executionTime); |
|
287 | - |
|
288 | - return $result; |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * Returns a populated instance of the Hydrator. |
|
293 | - * |
|
294 | - * @return Hydrator |
|
295 | - */ |
|
296 | - protected function getHydrator(): Hydrator /* @phpstan-ignore-line */ |
|
297 | - { |
|
298 | - $hydrator = new Hydrator($this->getFetchMode(), $this->hydratorConstructorArgs ?? []); /* @phpstan-ignore-line */ |
|
299 | - |
|
300 | - return $hydrator; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Checks if the results should be mapped via the hydrator |
|
305 | - * |
|
306 | - * @return bool |
|
307 | - */ |
|
308 | - protected function useHydrator(): bool |
|
309 | - { |
|
310 | - return !in_array($this->getFetchMode(), [\ARRAY_A, \ARRAY_N, \OBJECT, \OBJECT_K]); |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * Find all matching a simple where condition. |
|
315 | - * |
|
316 | - * Shortcut of ->where('key','=','value')->limit(1)->get(); |
|
317 | - * |
|
318 | - * @return \stdClass\array<mixed,mixed>|object|null Can return any object using hydrator |
|
319 | - */ |
|
320 | - public function first() |
|
321 | - { |
|
322 | - $this->limit(1); |
|
323 | - $result = $this->get(); |
|
324 | - |
|
325 | - return empty($result) ? null : $result[0]; |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Find all matching a simple where condition. |
|
330 | - * |
|
331 | - * Shortcut of ->where('key','=','value')->get(); |
|
332 | - * |
|
333 | - * @param string $fieldName |
|
334 | - * @param mixed $value |
|
335 | - * |
|
336 | - * @return array<mixed,mixed>|null Can return any object using hydrator |
|
337 | - */ |
|
338 | - public function findAll($fieldName, $value) |
|
339 | - { |
|
340 | - $this->where($fieldName, '=', $value); |
|
341 | - |
|
342 | - return $this->get(); |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * @param string $fieldName |
|
347 | - * @param mixed $value |
|
348 | - * |
|
349 | - * @return \stdClass\array<mixed,mixed>|object|null Can return any object using hydrator |
|
350 | - */ |
|
351 | - public function find($value, $fieldName = 'id') |
|
352 | - { |
|
353 | - $this->where($fieldName, '=', $value); |
|
354 | - |
|
355 | - return $this->first(); |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * @param string $fieldName |
|
360 | - * @param mixed $value |
|
361 | - * |
|
362 | - * @return \stdClass\array<mixed,mixed>|object Can return any object using hydrator |
|
363 | - * @throws Exception If fails to find |
|
364 | - */ |
|
365 | - public function findOrFail($value, $fieldName = 'id') |
|
366 | - { |
|
367 | - $result = $this->find($value, $fieldName); |
|
368 | - if (null === $result) { |
|
369 | - throw new Exception("Failed to find {$fieldName}={$value}", 1); |
|
370 | - } |
|
371 | - return $result; |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * Used to handle all aggregation method. |
|
376 | - * |
|
377 | - * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
378 | - * |
|
379 | - * @param string $type |
|
380 | - * @param string|Raw $field |
|
381 | - * |
|
382 | - * @return float |
|
383 | - */ |
|
384 | - protected function aggregate(string $type, $field = '*'): float |
|
385 | - { |
|
386 | - // Parse a raw expression. |
|
387 | - if ($field instanceof Raw) { |
|
388 | - $field = $this->adapterInstance->parseRaw($field); |
|
389 | - } |
|
390 | - |
|
391 | - // Potentialy cast field from JSON |
|
392 | - if ($this->jsonHandler->isJsonSelector($field)) { |
|
393 | - $field = $this->jsonHandler->extractAndUnquoteFromJsonSelector($field); |
|
394 | - } |
|
395 | - |
|
396 | - // Verify that field exists |
|
397 | - if ('*' !== $field && true === isset($this->statements['selects']) && false === \in_array($field, $this->statements['selects'], true)) { |
|
398 | - throw new \Exception(sprintf('Failed %s query - the column %s hasn\'t been selected in the query.', $type, $field)); |
|
399 | - } |
|
400 | - |
|
401 | - |
|
402 | - if (false === isset($this->statements['tables'])) { |
|
403 | - throw new Exception('No table selected'); |
|
404 | - } |
|
405 | - |
|
406 | - $count = $this |
|
407 | - ->table($this->subQuery($this, 'count')) |
|
408 | - ->select([$this->raw(sprintf('%s(%s) AS field', strtoupper($type), $field))]) |
|
409 | - ->first(); |
|
410 | - |
|
411 | - return true === isset($count->field) ? (float)$count->field : 0; |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Get count of all the rows for the current query |
|
416 | - * |
|
417 | - * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
418 | - * |
|
419 | - * @param string|Raw $field |
|
420 | - * |
|
421 | - * @return int |
|
422 | - * |
|
423 | - * @throws Exception |
|
424 | - */ |
|
425 | - public function count($field = '*'): int |
|
426 | - { |
|
427 | - return (int)$this->aggregate('count', $field); |
|
428 | - } |
|
429 | - |
|
430 | - /** |
|
431 | - * Get the sum for a field in the current query |
|
432 | - * |
|
433 | - * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
434 | - * |
|
435 | - * @param string|Raw $field |
|
436 | - * |
|
437 | - * @return float |
|
438 | - * |
|
439 | - * @throws Exception |
|
440 | - */ |
|
441 | - public function sum($field): float |
|
442 | - { |
|
443 | - return $this->aggregate('sum', $field); |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Get the average for a field in the current query |
|
448 | - * |
|
449 | - * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
450 | - * |
|
451 | - * @param string|Raw $field |
|
452 | - * |
|
453 | - * @return float |
|
454 | - * |
|
455 | - * @throws Exception |
|
456 | - */ |
|
457 | - public function average($field): float |
|
458 | - { |
|
459 | - return $this->aggregate('avg', $field); |
|
460 | - } |
|
461 | - |
|
462 | - /** |
|
463 | - * Get the minimum for a field in the current query |
|
464 | - * |
|
465 | - * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
466 | - * |
|
467 | - * @param string|Raw $field |
|
468 | - * |
|
469 | - * @return float |
|
470 | - * |
|
471 | - * @throws Exception |
|
472 | - */ |
|
473 | - public function min($field): float |
|
474 | - { |
|
475 | - return $this->aggregate('min', $field); |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * Get the maximum for a field in the current query |
|
480 | - * |
|
481 | - * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
482 | - * |
|
483 | - * @param string|Raw $field |
|
484 | - * |
|
485 | - * @return float |
|
486 | - * |
|
487 | - * @throws Exception |
|
488 | - */ |
|
489 | - public function max($field): float |
|
490 | - { |
|
491 | - return $this->aggregate('max', $field); |
|
492 | - } |
|
493 | - |
|
494 | - /** |
|
495 | - * @param string $type |
|
496 | - * @param bool|array<mixed, mixed> $dataToBePassed |
|
497 | - * |
|
498 | - * @return mixed |
|
499 | - * |
|
500 | - * @throws Exception |
|
501 | - */ |
|
502 | - public function getQuery(string $type = 'select', $dataToBePassed = []) |
|
503 | - { |
|
504 | - $allowedTypes = ['select', 'insert', 'insertignore', 'replace', 'delete', 'update', 'criteriaonly']; |
|
505 | - if (!in_array(strtolower($type), $allowedTypes)) { |
|
506 | - throw new Exception($type . ' is not a known type.', 2); |
|
507 | - } |
|
508 | - |
|
509 | - $queryArr = $this->adapterInstance->$type($this->statements, $dataToBePassed); |
|
510 | - |
|
511 | - return $this->container->build( |
|
512 | - QueryObject::class, |
|
513 | - [$queryArr['sql'], $queryArr['bindings'], $this->dbInstance] |
|
514 | - ); |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * @param QueryBuilderHandler $queryBuilder |
|
519 | - * @param string|null $alias |
|
520 | - * |
|
521 | - * @return Raw |
|
522 | - */ |
|
523 | - public function subQuery(QueryBuilderHandler $queryBuilder, ?string $alias = null) |
|
524 | - { |
|
525 | - $sql = '(' . $queryBuilder->getQuery()->getRawSql() . ')'; |
|
526 | - if (is_string($alias) && 0 !== mb_strlen($alias)) { |
|
527 | - $sql = $sql . ' as ' . $alias; |
|
528 | - } |
|
529 | - |
|
530 | - return $queryBuilder->raw($sql); |
|
531 | - } |
|
532 | - |
|
533 | - /** |
|
534 | - * Handles the various insert operations based on the type. |
|
535 | - * |
|
536 | - * @param array<int|string, mixed|mixed[]> $data |
|
537 | - * @param string $type |
|
538 | - * |
|
539 | - * @return int|int[]|mixed|null can return a single row id, array of row ids, null (for failed) or any other value short circuited from event |
|
540 | - */ |
|
541 | - private function doInsert(array $data, string $type) |
|
542 | - { |
|
543 | - $eventResult = $this->fireEvents('before-insert'); |
|
544 | - if (!is_null($eventResult)) { |
|
545 | - return $eventResult; |
|
546 | - } |
|
547 | - |
|
548 | - // If first value is not an array () not a batch insert) |
|
549 | - if (!is_array(current($data))) { |
|
550 | - $queryObject = $this->getQuery($type, $data); |
|
551 | - |
|
552 | - list($preparedQuery, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); |
|
553 | - $this->dbInstance->get_results($preparedQuery); |
|
554 | - |
|
555 | - // Check we have a result. |
|
556 | - $return = 1 === $this->dbInstance->rows_affected ? $this->dbInstance->insert_id : null; |
|
557 | - } else { |
|
558 | - // Its a batch insert |
|
559 | - $return = []; |
|
560 | - $executionTime = 0; |
|
561 | - foreach ($data as $subData) { |
|
562 | - $queryObject = $this->getQuery($type, $subData); |
|
563 | - |
|
564 | - list($preparedQuery, $time) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); |
|
565 | - $this->dbInstance->get_results($preparedQuery); |
|
566 | - $executionTime += $time; |
|
567 | - |
|
568 | - if (1 === $this->dbInstance->rows_affected) { |
|
569 | - $return[] = $this->dbInstance->insert_id; |
|
570 | - } |
|
571 | - } |
|
572 | - } |
|
573 | - |
|
574 | - $this->fireEvents('after-insert', $return, $executionTime); |
|
575 | - |
|
576 | - return $return; |
|
577 | - } |
|
578 | - |
|
579 | - /** |
|
580 | - * @param array<int|string, mixed|mixed[]> $data either key=>value array for single or array of arrays for bulk |
|
581 | - * |
|
582 | - * @return int|int[]|mixed|null can return a single row id, array of row ids, null (for failed) or any other value short circuited from event |
|
583 | - */ |
|
584 | - public function insert($data) |
|
585 | - { |
|
586 | - return $this->doInsert($data, 'insert'); |
|
587 | - } |
|
588 | - |
|
589 | - /** |
|
590 | - * |
|
591 | - * @param array<int|string, mixed|mixed[]> $data either key=>value array for single or array of arrays for bulk |
|
592 | - * |
|
593 | - * @return int|int[]|mixed|null can return a single row id, array of row ids, null (for failed) or any other value short circuited from event |
|
594 | - */ |
|
595 | - public function insertIgnore($data) |
|
596 | - { |
|
597 | - return $this->doInsert($data, 'insertignore'); |
|
598 | - } |
|
599 | - |
|
600 | - /** |
|
601 | - * |
|
602 | - * @param array<int|string, mixed|mixed[]> $data either key=>value array for single or array of arrays for bulk |
|
603 | - * |
|
604 | - * @return int|int[]|mixed|null can return a single row id, array of row ids, null (for failed) or any other value short circuited from event |
|
605 | - */ |
|
606 | - public function replace($data) |
|
607 | - { |
|
608 | - return $this->doInsert($data, 'replace'); |
|
609 | - } |
|
610 | - |
|
611 | - /** |
|
612 | - * @param array<string, mixed> $data |
|
613 | - * |
|
614 | - * @return int|null Number of row effected, null for none. |
|
615 | - */ |
|
616 | - public function update(array $data): ?int |
|
617 | - { |
|
618 | - $eventResult = $this->fireEvents('before-update'); |
|
619 | - if (!is_null($eventResult)) { |
|
620 | - return $eventResult; |
|
621 | - } |
|
622 | - $queryObject = $this->getQuery('update', $data); |
|
623 | - $r = $this->statement($queryObject->getSql(), $queryObject->getBindings()); |
|
624 | - list($preparedQuery, $executionTime) = $r; |
|
625 | - $this->dbInstance()->get_results($preparedQuery); |
|
626 | - $this->fireEvents('after-update', $queryObject, $executionTime); |
|
627 | - |
|
628 | - return 0 !== (int) $this->dbInstance()->rows_affected |
|
629 | - ? (int) $this->dbInstance()->rows_affected |
|
630 | - : null; |
|
631 | - } |
|
632 | - |
|
633 | - /** |
|
634 | - * Update or Insert based on the attributes. |
|
635 | - * |
|
636 | - * @param array<string, mixed> $attributes Conditions to check |
|
637 | - * @param array<string, mixed> $values Values to add/update |
|
638 | - * |
|
639 | - * @return int|int[]|null will return row id(s) for insert and null for success/fail on update |
|
640 | - */ |
|
641 | - public function updateOrInsert(array $attributes, array $values = []) |
|
642 | - { |
|
643 | - // Check if existing post exists. |
|
644 | - $query = clone $this; |
|
645 | - foreach ($attributes as $column => $value) { |
|
646 | - $query->where($column, $value); |
|
647 | - } |
|
648 | - |
|
649 | - return null !== $query->first() |
|
650 | - ? $this->update(array_merge($values, $attributes)) |
|
651 | - : $this->insert(array_merge($values, $attributes)); |
|
652 | - } |
|
653 | - |
|
654 | - /** |
|
655 | - * @param array<string, mixed> $data |
|
656 | - * |
|
657 | - * @return static |
|
658 | - */ |
|
659 | - public function onDuplicateKeyUpdate($data) |
|
660 | - { |
|
661 | - $this->addStatement('onduplicate', $data); |
|
662 | - |
|
663 | - return $this; |
|
664 | - } |
|
665 | - |
|
666 | - /** |
|
667 | - * @return mixed number of rows effected or shortcircuited response |
|
668 | - */ |
|
669 | - public function delete() |
|
670 | - { |
|
671 | - $eventResult = $this->fireEvents('before-delete'); |
|
672 | - if (!is_null($eventResult)) { |
|
673 | - return $eventResult; |
|
674 | - } |
|
675 | - |
|
676 | - $queryObject = $this->getQuery('delete'); |
|
677 | - |
|
678 | - list($preparedQuery, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); |
|
679 | - $this->dbInstance()->get_results($preparedQuery); |
|
680 | - $this->fireEvents('after-delete', $queryObject, $executionTime); |
|
681 | - |
|
682 | - return $this->dbInstance()->rows_affected; |
|
683 | - } |
|
684 | - |
|
685 | - /** |
|
686 | - * @param string|Raw ...$tables Single table or array of tables |
|
687 | - * |
|
688 | - * @return static |
|
689 | - * |
|
690 | - * @throws Exception |
|
691 | - */ |
|
692 | - public function table(...$tables) |
|
693 | - { |
|
694 | - $instance = $this->constructCurrentBuilderClass($this->connection); |
|
695 | - $this->setFetchMode($this->getFetchMode(), $this->hydratorConstructorArgs); |
|
696 | - $tables = $this->addTablePrefix($tables, false); |
|
697 | - $instance->addStatement('tables', $tables); |
|
698 | - |
|
699 | - return $instance; |
|
700 | - } |
|
701 | - |
|
702 | - /** |
|
703 | - * @param string|Raw ...$tables Single table or array of tables |
|
704 | - * |
|
705 | - * @return static |
|
706 | - */ |
|
707 | - public function from(...$tables): self |
|
708 | - { |
|
709 | - $tables = $this->addTablePrefix($tables, false); |
|
710 | - $this->addStatement('tables', $tables); |
|
711 | - |
|
712 | - return $this; |
|
713 | - } |
|
714 | - |
|
715 | - /** |
|
716 | - * @param string|string[]|Raw[]|array<string, string> $fields |
|
717 | - * |
|
718 | - * @return static |
|
719 | - */ |
|
720 | - public function select($fields): self |
|
721 | - { |
|
722 | - if (!is_array($fields)) { |
|
723 | - $fields = func_get_args(); |
|
724 | - } |
|
725 | - |
|
726 | - foreach ($fields as $field => $alias) { |
|
727 | - // If we have a JSON expression |
|
728 | - if ($this->jsonHandler->isJsonSelector($field)) { |
|
729 | - $field = $this->jsonHandler->extractAndUnquoteFromJsonSelector($field); |
|
730 | - } |
|
731 | - |
|
732 | - // If no alias passed, but field is for JSON. thrown an exception. |
|
733 | - if (is_numeric($field) && is_string($alias) && $this->jsonHandler->isJsonSelector($alias)) { |
|
734 | - throw new Exception("An alias must be used if you wish to select from JSON Object", 1); |
|
735 | - } |
|
736 | - |
|
737 | - // Treat each array as a single table, to retain order added |
|
738 | - $field = is_numeric($field) |
|
739 | - ? $field = $alias // If single colum |
|
740 | - : $field = [$field => $alias]; // Has alias |
|
741 | - |
|
742 | - $field = $this->addTablePrefix($field); |
|
743 | - $this->addStatement('selects', $field); |
|
744 | - } |
|
745 | - |
|
746 | - return $this; |
|
747 | - } |
|
748 | - |
|
749 | - /** |
|
750 | - * @param string|string[]|Raw[]|array<string, string> $fields |
|
751 | - * |
|
752 | - * @return static |
|
753 | - */ |
|
754 | - public function selectDistinct($fields) |
|
755 | - { |
|
756 | - $this->select($fields); |
|
757 | - $this->addStatement('distinct', true); |
|
758 | - |
|
759 | - return $this; |
|
760 | - } |
|
761 | - |
|
762 | - /** |
|
763 | - * @param string|string[] $field either the single field or an array of fields |
|
764 | - * |
|
765 | - * @return static |
|
766 | - */ |
|
767 | - public function groupBy($field): self |
|
768 | - { |
|
769 | - $field = $this->addTablePrefix($field); |
|
770 | - $this->addStatement('groupBys', $field); |
|
771 | - |
|
772 | - return $this; |
|
773 | - } |
|
774 | - |
|
775 | - /** |
|
776 | - * @param string|array<string|int, mixed> $fields |
|
777 | - * @param string $defaultDirection |
|
778 | - * |
|
779 | - * @return static |
|
780 | - */ |
|
781 | - public function orderBy($fields, string $defaultDirection = 'ASC'): self |
|
782 | - { |
|
783 | - if (!is_array($fields)) { |
|
784 | - $fields = [$fields]; |
|
785 | - } |
|
786 | - |
|
787 | - foreach ($fields as $key => $value) { |
|
788 | - $field = $key; |
|
789 | - $type = $value; |
|
790 | - if (is_int($key)) { |
|
791 | - $field = $value; |
|
792 | - $type = $defaultDirection; |
|
793 | - } |
|
794 | - |
|
795 | - if ($this->jsonHandler->isJsonSelector($field)) { |
|
796 | - $field = $this->jsonHandler->extractAndUnquoteFromJsonSelector($field); |
|
797 | - } |
|
798 | - |
|
799 | - if (!$field instanceof Raw) { |
|
800 | - $field = $this->addTablePrefix($field); |
|
801 | - } |
|
802 | - $this->statements['orderBys'][] = compact('field', 'type'); |
|
803 | - } |
|
804 | - |
|
805 | - return $this; |
|
806 | - } |
|
807 | - |
|
808 | - /** |
|
809 | - * @param string|Raw $key The database column which holds the JSON value |
|
810 | - * @param string|Raw|string[] $jsonKey The json key/index to search |
|
811 | - * @param string $defaultDirection |
|
812 | - * @return static |
|
813 | - */ |
|
814 | - public function orderByJson($key, $jsonKey, string $defaultDirection = 'ASC'): self |
|
815 | - { |
|
816 | - $key = $this->jsonHandler->jsonExpressionFactory()->extractAndUnquote($key, $jsonKey); |
|
817 | - return $this->orderBy($key, $defaultDirection); |
|
818 | - } |
|
819 | - |
|
820 | - /** |
|
821 | - * @param int $limit |
|
822 | - * |
|
823 | - * @return static |
|
824 | - */ |
|
825 | - public function limit(int $limit): self |
|
826 | - { |
|
827 | - $this->statements['limit'] = $limit; |
|
828 | - |
|
829 | - return $this; |
|
830 | - } |
|
831 | - |
|
832 | - /** |
|
833 | - * @param int $offset |
|
834 | - * |
|
835 | - * @return static |
|
836 | - */ |
|
837 | - public function offset(int $offset): self |
|
838 | - { |
|
839 | - $this->statements['offset'] = $offset; |
|
840 | - |
|
841 | - return $this; |
|
842 | - } |
|
843 | - |
|
844 | - /** |
|
845 | - * @param string|string[]|Raw|Raw[] $key |
|
846 | - * @param string $operator |
|
847 | - * @param mixed $value |
|
848 | - * @param string $joiner |
|
849 | - * |
|
850 | - * @return static |
|
851 | - */ |
|
852 | - public function having($key, string $operator, $value, string $joiner = 'AND') |
|
853 | - { |
|
854 | - $key = $this->addTablePrefix($key); |
|
855 | - $this->statements['havings'][] = compact('key', 'operator', 'value', 'joiner'); |
|
856 | - |
|
857 | - return $this; |
|
858 | - } |
|
859 | - |
|
860 | - /** |
|
861 | - * @param string|string[]|Raw|Raw[] $key |
|
862 | - * @param string $operator |
|
863 | - * @param mixed $value |
|
864 | - * |
|
865 | - * @return static |
|
866 | - */ |
|
867 | - public function orHaving($key, $operator, $value) |
|
868 | - { |
|
869 | - return $this->having($key, $operator, $value, 'OR'); |
|
870 | - } |
|
871 | - |
|
872 | - /** |
|
873 | - * @param string|Raw $key |
|
874 | - * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
875 | - * @param mixed|null $value |
|
876 | - * |
|
877 | - * @return static |
|
878 | - */ |
|
879 | - public function where($key, $operator = null, $value = null): self |
|
880 | - { |
|
881 | - // If two params are given then assume operator is = |
|
882 | - if (2 === func_num_args()) { |
|
883 | - $value = $operator; |
|
884 | - $operator = '='; |
|
885 | - } |
|
886 | - |
|
887 | - return $this->whereHandler($key, $operator, $value); |
|
888 | - } |
|
889 | - |
|
890 | - /** |
|
891 | - * @param string|Raw|\Closure(QueryBuilderHandler):void $key |
|
892 | - * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
893 | - * @param mixed|null $value |
|
894 | - * |
|
895 | - * @return static |
|
896 | - */ |
|
897 | - public function orWhere($key, $operator = null, $value = null): self |
|
898 | - { |
|
899 | - // If two params are given then assume operator is = |
|
900 | - if (2 === func_num_args()) { |
|
901 | - $value = $operator; |
|
902 | - $operator = '='; |
|
903 | - } |
|
904 | - |
|
905 | - return $this->whereHandler($key, $operator, $value, 'OR'); |
|
906 | - } |
|
907 | - |
|
908 | - /** |
|
909 | - * @param string|Raw $key |
|
910 | - * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
911 | - * @param mixed|null $value |
|
912 | - * |
|
913 | - * @return static |
|
914 | - */ |
|
915 | - public function whereNot($key, $operator = null, $value = null): self |
|
916 | - { |
|
917 | - // If two params are given then assume operator is = |
|
918 | - if (2 === func_num_args()) { |
|
919 | - $value = $operator; |
|
920 | - $operator = '='; |
|
921 | - } |
|
922 | - |
|
923 | - return $this->whereHandler($key, $operator, $value, 'AND NOT'); |
|
924 | - } |
|
925 | - |
|
926 | - /** |
|
927 | - * @param string|Raw $key |
|
928 | - * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
929 | - * @param mixed|null $value |
|
930 | - * |
|
931 | - * @return static |
|
932 | - */ |
|
933 | - public function orWhereNot($key, $operator = null, $value = null) |
|
934 | - { |
|
935 | - // If two params are given then assume operator is = |
|
936 | - if (2 === func_num_args()) { |
|
937 | - $value = $operator; |
|
938 | - $operator = '='; |
|
939 | - } |
|
940 | - |
|
941 | - return $this->whereHandler($key, $operator, $value, 'OR NOT'); |
|
942 | - } |
|
943 | - |
|
944 | - /** |
|
945 | - * @param string|Raw $key |
|
946 | - * @param mixed[]|string|Raw $values |
|
947 | - * |
|
948 | - * @return static |
|
949 | - */ |
|
950 | - public function whereIn($key, $values): self |
|
951 | - { |
|
952 | - return $this->whereHandler($key, 'IN', $values, 'AND'); |
|
953 | - } |
|
954 | - |
|
955 | - /** |
|
956 | - * @param string|Raw $key |
|
957 | - * @param mixed[]|string|Raw $values |
|
958 | - * |
|
959 | - * @return static |
|
960 | - */ |
|
961 | - public function whereNotIn($key, $values): self |
|
962 | - { |
|
963 | - return $this->whereHandler($key, 'NOT IN', $values, 'AND'); |
|
964 | - } |
|
965 | - |
|
966 | - /** |
|
967 | - * @param string|Raw $key |
|
968 | - * @param mixed[]|string|Raw $values |
|
969 | - * |
|
970 | - * @return static |
|
971 | - */ |
|
972 | - public function orWhereIn($key, $values): self |
|
973 | - { |
|
974 | - return $this->whereHandler($key, 'IN', $values, 'OR'); |
|
975 | - } |
|
976 | - |
|
977 | - /** |
|
978 | - * @param string|Raw $key |
|
979 | - * @param mixed[]|string|Raw $values |
|
980 | - * |
|
981 | - * @return static |
|
982 | - */ |
|
983 | - public function orWhereNotIn($key, $values): self |
|
984 | - { |
|
985 | - return $this->whereHandler($key, 'NOT IN', $values, 'OR'); |
|
986 | - } |
|
987 | - |
|
988 | - /** |
|
989 | - * @param string|Raw $key |
|
990 | - * @param mixed $valueFrom |
|
991 | - * @param mixed $valueTo |
|
992 | - * |
|
993 | - * @return static |
|
994 | - */ |
|
995 | - public function whereBetween($key, $valueFrom, $valueTo): self |
|
996 | - { |
|
997 | - return $this->whereHandler($key, 'BETWEEN', [$valueFrom, $valueTo], 'AND'); |
|
998 | - } |
|
999 | - |
|
1000 | - /** |
|
1001 | - * @param string|Raw $key |
|
1002 | - * @param mixed $valueFrom |
|
1003 | - * @param mixed $valueTo |
|
1004 | - * |
|
1005 | - * @return static |
|
1006 | - */ |
|
1007 | - public function orWhereBetween($key, $valueFrom, $valueTo): self |
|
1008 | - { |
|
1009 | - return $this->whereHandler($key, 'BETWEEN', [$valueFrom, $valueTo], 'OR'); |
|
1010 | - } |
|
1011 | - |
|
1012 | - /** |
|
1013 | - * Handles all function call based where conditions |
|
1014 | - * |
|
1015 | - * @param string|Raw $key |
|
1016 | - * @param string $function |
|
1017 | - * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
1018 | - * @param mixed|null $value |
|
1019 | - * @return static |
|
1020 | - */ |
|
1021 | - protected function whereFunctionCallHandler($key, $function, $operator, $value): self |
|
1022 | - { |
|
1023 | - $key = \sprintf('%s(%s)', $function, $this->addTablePrefix($key)); |
|
1024 | - return $this->where($key, $operator, $value); |
|
1025 | - } |
|
1026 | - |
|
1027 | - /** |
|
1028 | - * @param string|Raw $key |
|
1029 | - * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
1030 | - * @param mixed|null $value |
|
1031 | - * @return self |
|
1032 | - */ |
|
1033 | - public function whereMonth($key, $operator = null, $value = null): self |
|
1034 | - { |
|
1035 | - // If two params are given then assume operator is = |
|
1036 | - if (2 === func_num_args()) { |
|
1037 | - $value = $operator; |
|
1038 | - $operator = '='; |
|
1039 | - } |
|
1040 | - return $this->whereFunctionCallHandler($key, 'MONTH', $operator, $value); |
|
1041 | - } |
|
1042 | - |
|
1043 | - /** |
|
1044 | - * @param string|Raw $key |
|
1045 | - * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
1046 | - * @param mixed|null $value |
|
1047 | - * @return self |
|
1048 | - */ |
|
1049 | - public function whereDay($key, $operator = null, $value = null): self |
|
1050 | - { |
|
1051 | - // If two params are given then assume operator is = |
|
1052 | - if (2 === func_num_args()) { |
|
1053 | - $value = $operator; |
|
1054 | - $operator = '='; |
|
1055 | - } |
|
1056 | - return $this->whereFunctionCallHandler($key, 'DAY', $operator, $value); |
|
1057 | - } |
|
1058 | - |
|
1059 | - /** |
|
1060 | - * @param string|Raw $key |
|
1061 | - * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
1062 | - * @param mixed|null $value |
|
1063 | - * @return self |
|
1064 | - */ |
|
1065 | - public function whereYear($key, $operator = null, $value = null): self |
|
1066 | - { |
|
1067 | - // If two params are given then assume operator is = |
|
1068 | - if (2 === func_num_args()) { |
|
1069 | - $value = $operator; |
|
1070 | - $operator = '='; |
|
1071 | - } |
|
1072 | - return $this->whereFunctionCallHandler($key, 'YEAR', $operator, $value); |
|
1073 | - } |
|
1074 | - |
|
1075 | - /** |
|
1076 | - * @param string|Raw $key |
|
1077 | - * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
1078 | - * @param mixed|null $value |
|
1079 | - * @return self |
|
1080 | - */ |
|
1081 | - public function whereDate($key, $operator = null, $value = null): self |
|
1082 | - { |
|
1083 | - // If two params are given then assume operator is = |
|
1084 | - if (2 === func_num_args()) { |
|
1085 | - $value = $operator; |
|
1086 | - $operator = '='; |
|
1087 | - } |
|
1088 | - return $this->whereFunctionCallHandler($key, 'DATE', $operator, $value); |
|
1089 | - } |
|
1090 | - |
|
1091 | - /** |
|
1092 | - * @param string|Raw $key |
|
1093 | - * |
|
1094 | - * @return static |
|
1095 | - */ |
|
1096 | - public function whereNull($key): self |
|
1097 | - { |
|
1098 | - return $this->whereNullHandler($key); |
|
1099 | - } |
|
1100 | - |
|
1101 | - /** |
|
1102 | - * @param string|Raw $key |
|
1103 | - * |
|
1104 | - * @return static |
|
1105 | - */ |
|
1106 | - public function whereNotNull($key): self |
|
1107 | - { |
|
1108 | - return $this->whereNullHandler($key, 'NOT'); |
|
1109 | - } |
|
1110 | - |
|
1111 | - /** |
|
1112 | - * @param string|Raw $key |
|
1113 | - * |
|
1114 | - * @return static |
|
1115 | - */ |
|
1116 | - public function orWhereNull($key): self |
|
1117 | - { |
|
1118 | - return $this->whereNullHandler($key, '', 'or'); |
|
1119 | - } |
|
1120 | - |
|
1121 | - /** |
|
1122 | - * @param string|Raw $key |
|
1123 | - * |
|
1124 | - * @return static |
|
1125 | - */ |
|
1126 | - public function orWhereNotNull($key): self |
|
1127 | - { |
|
1128 | - return $this->whereNullHandler($key, 'NOT', 'or'); |
|
1129 | - } |
|
1130 | - |
|
1131 | - /** |
|
1132 | - * @param string|Raw $key |
|
1133 | - * @param string $prefix |
|
1134 | - * @param string $operator |
|
1135 | - * |
|
1136 | - * @return static |
|
1137 | - */ |
|
1138 | - protected function whereNullHandler($key, string $prefix = '', $operator = ''): self |
|
1139 | - { |
|
1140 | - $prefix = 0 === mb_strlen($prefix) ? '' : " {$prefix}"; |
|
1141 | - |
|
1142 | - if ($key instanceof Raw) { |
|
1143 | - $key = $this->adapterInstance->parseRaw($key); |
|
1144 | - } |
|
1145 | - |
|
1146 | - $key = $this->addTablePrefix($key); |
|
1147 | - if ($key instanceof Closure) { |
|
1148 | - throw new Exception('Key used for whereNull condition must be a string or raw exrpession.', 1); |
|
1149 | - } |
|
1150 | - |
|
1151 | - return $this->{$operator . 'Where'}($this->raw("{$key} IS{$prefix} NULL")); |
|
1152 | - } |
|
1153 | - |
|
1154 | - |
|
1155 | - /** |
|
1156 | - * Runs a transaction |
|
1157 | - * |
|
1158 | - * @param \Closure(Transaction):void $callback |
|
1159 | - * |
|
1160 | - * @return static |
|
1161 | - */ |
|
1162 | - public function transaction(Closure $callback): self |
|
1163 | - { |
|
1164 | - try { |
|
1165 | - // Begin the transaction |
|
1166 | - $this->dbInstance->query('START TRANSACTION'); |
|
1167 | - |
|
1168 | - // Get the Transaction class |
|
1169 | - $transaction = $this->container->build(Transaction::class, [$this->connection]); |
|
1170 | - |
|
1171 | - $this->handleTransactionCall($callback, $transaction); |
|
1172 | - |
|
1173 | - // If no errors have been thrown or the transaction wasn't completed within |
|
1174 | - $this->dbInstance->query('COMMIT'); |
|
1175 | - |
|
1176 | - return $this; |
|
1177 | - } catch (TransactionHaltException $e) { |
|
1178 | - // Commit or rollback behavior has been handled in the closure, so exit |
|
1179 | - return $this; |
|
1180 | - } catch (\Exception $e) { |
|
1181 | - // something happened, rollback changes |
|
1182 | - $this->dbInstance->query('ROLLBACK'); |
|
1183 | - |
|
1184 | - return $this; |
|
1185 | - } |
|
1186 | - } |
|
1187 | - |
|
1188 | - /** |
|
1189 | - * Handles the transaction call. |
|
1190 | - * Catches any WPDB Errors (printed) |
|
1191 | - * |
|
1192 | - * @param Closure $callback |
|
1193 | - * @param Transaction $transaction |
|
1194 | - * |
|
1195 | - * @return void |
|
1196 | - * @throws Exception |
|
1197 | - */ |
|
1198 | - protected function handleTransactionCall(Closure $callback, Transaction $transaction): void |
|
1199 | - { |
|
1200 | - try { |
|
1201 | - ob_start(); |
|
1202 | - $callback($transaction); |
|
1203 | - $output = ob_get_clean() ?: ''; |
|
1204 | - } catch (Throwable $th) { |
|
1205 | - ob_end_clean(); |
|
1206 | - throw $th; |
|
1207 | - } |
|
1208 | - |
|
1209 | - // If we caught an error, throw an exception. |
|
1210 | - if (0 !== mb_strlen($output)) { |
|
1211 | - throw new Exception($output); |
|
1212 | - } |
|
1213 | - } |
|
1214 | - |
|
1215 | - /*************************************************************************/ |
|
1216 | - /*************************************************************************/ |
|
1217 | - /*************************************************************************/ |
|
1218 | - /** JOIN JOIN **/ |
|
1219 | - /** JOIN **/ |
|
1220 | - /** JOIN JOIN **/ |
|
1221 | - /*************************************************************************/ |
|
1222 | - /*************************************************************************/ |
|
1223 | - /*************************************************************************/ |
|
1224 | - |
|
1225 | - /** |
|
1226 | - * @param string|Raw $table |
|
1227 | - * @param string|Raw|Closure $key |
|
1228 | - * @param string|null $operator |
|
1229 | - * @param mixed $value |
|
1230 | - * @param string $type |
|
1231 | - * |
|
1232 | - * @return static |
|
1233 | - */ |
|
1234 | - public function join($table, $key, ?string $operator = null, $value = null, $type = 'inner') |
|
1235 | - { |
|
1236 | - // Potentially cast key from JSON |
|
1237 | - if ($this->jsonHandler->isJsonSelector($key)) { |
|
1238 | - /** @var string $key */ |
|
1239 | - $key = $this->jsonHandler->extractAndUnquoteFromJsonSelector($key); /** @phpstan-ignore-line */ |
|
1240 | - } |
|
1241 | - |
|
1242 | - // Potentially cast value from json |
|
1243 | - if ($this->jsonHandler->isJsonSelector($value)) { |
|
1244 | - /** @var string $value */ |
|
1245 | - $value = $this->jsonHandler->extractAndUnquoteFromJsonSelector($value); |
|
1246 | - } |
|
1247 | - |
|
1248 | - if (!$key instanceof Closure) { |
|
1249 | - $key = function ($joinBuilder) use ($key, $operator, $value) { |
|
1250 | - $joinBuilder->on($key, $operator, $value); |
|
1251 | - }; |
|
1252 | - } |
|
1253 | - |
|
1254 | - // Build a new JoinBuilder class, keep it by reference so any changes made |
|
1255 | - // in the closure should reflect here |
|
1256 | - $joinBuilder = $this->container->build(JoinBuilder::class, [$this->connection]); |
|
1257 | - $joinBuilder = &$joinBuilder; |
|
1258 | - // Call the closure with our new joinBuilder object |
|
1259 | - $key($joinBuilder); |
|
1260 | - $table = $this->addTablePrefix($table, false); |
|
1261 | - // Get the criteria only query from the joinBuilder object |
|
1262 | - $this->statements['joins'][] = compact('type', 'table', 'joinBuilder'); |
|
1263 | - return $this; |
|
1264 | - } |
|
1265 | - |
|
1266 | - /** |
|
1267 | - * @param string|Raw $table |
|
1268 | - * @param string|Raw|Closure $key |
|
1269 | - * @param string|null $operator |
|
1270 | - * @param mixed $value |
|
1271 | - * |
|
1272 | - * @return static |
|
1273 | - */ |
|
1274 | - public function leftJoin($table, $key, $operator = null, $value = null) |
|
1275 | - { |
|
1276 | - return $this->join($table, $key, $operator, $value, 'left'); |
|
1277 | - } |
|
1278 | - |
|
1279 | - /** |
|
1280 | - * @param string|Raw $table |
|
1281 | - * @param string|Raw|Closure $key |
|
1282 | - * @param string|null $operator |
|
1283 | - * @param mixed $value |
|
1284 | - * |
|
1285 | - * @return static |
|
1286 | - */ |
|
1287 | - public function rightJoin($table, $key, $operator = null, $value = null) |
|
1288 | - { |
|
1289 | - return $this->join($table, $key, $operator, $value, 'right'); |
|
1290 | - } |
|
1291 | - |
|
1292 | - /** |
|
1293 | - * @param string|Raw $table |
|
1294 | - * @param string|Raw|Closure $key |
|
1295 | - * @param string|null $operator |
|
1296 | - * @param mixed $value |
|
1297 | - * |
|
1298 | - * @return static |
|
1299 | - */ |
|
1300 | - public function innerJoin($table, $key, $operator = null, $value = null) |
|
1301 | - { |
|
1302 | - return $this->join($table, $key, $operator, $value, 'inner'); |
|
1303 | - } |
|
1304 | - |
|
1305 | - /** |
|
1306 | - * @param string|Raw $table |
|
1307 | - * @param string|Raw|Closure $key |
|
1308 | - * @param string|null $operator |
|
1309 | - * @param mixed $value |
|
1310 | - * |
|
1311 | - * @return static |
|
1312 | - */ |
|
1313 | - public function crossJoin($table, $key, $operator = null, $value = null) |
|
1314 | - { |
|
1315 | - return $this->join($table, $key, $operator, $value, 'cross'); |
|
1316 | - } |
|
1317 | - |
|
1318 | - /** |
|
1319 | - * @param string|Raw $table |
|
1320 | - * @param string|Raw|Closure $key |
|
1321 | - * @param string|null $operator |
|
1322 | - * @param mixed $value |
|
1323 | - * |
|
1324 | - * @return static |
|
1325 | - */ |
|
1326 | - public function outerJoin($table, $key, $operator = null, $value = null) |
|
1327 | - { |
|
1328 | - return $this->join($table, $key, $operator, $value, 'full outer'); |
|
1329 | - } |
|
1330 | - |
|
1331 | - /** |
|
1332 | - * Shortcut to join 2 tables on the same key name with equals |
|
1333 | - * |
|
1334 | - * @param string $table |
|
1335 | - * @param string $key |
|
1336 | - * @param string $type |
|
1337 | - * @return self |
|
1338 | - * @throws Exception If base table is set as more than 1 or 0 |
|
1339 | - */ |
|
1340 | - public function joinUsing(string $table, string $key, string $type = 'INNER'): self |
|
1341 | - { |
|
1342 | - if (!array_key_exists('tables', $this->statements) || count($this->statements['tables']) !== 1) { |
|
1343 | - throw new Exception("JoinUsing can only be used with a single table set as the base of the query", 1); |
|
1344 | - } |
|
1345 | - $baseTable = end($this->statements['tables']); |
|
1346 | - |
|
1347 | - // Potentialy cast key from JSON |
|
1348 | - if ($this->jsonHandler->isJsonSelector($key)) { |
|
1349 | - $key = $this->jsonHandler->extractAndUnquoteFromJsonSelector($key); |
|
1350 | - } |
|
1351 | - |
|
1352 | - $remoteKey = $table = $this->addTablePrefix("{$table}.{$key}", true); |
|
1353 | - $localKey = $table = $this->addTablePrefix("{$baseTable}.{$key}", true); |
|
1354 | - return $this->join($table, $remoteKey, '=', $localKey, $type); |
|
1355 | - } |
|
1356 | - |
|
1357 | - /** |
|
1358 | - * Add a raw query |
|
1359 | - * |
|
1360 | - * @param string|Raw $value |
|
1361 | - * @param mixed|mixed[] $bindings |
|
1362 | - * |
|
1363 | - * @return Raw |
|
1364 | - */ |
|
1365 | - public function raw($value, $bindings = []): Raw |
|
1366 | - { |
|
1367 | - return new Raw($value, $bindings); |
|
1368 | - } |
|
1369 | - |
|
1370 | - /** |
|
1371 | - * Return wpdb instance |
|
1372 | - * |
|
1373 | - * @return wpdb |
|
1374 | - */ |
|
1375 | - public function dbInstance(): wpdb |
|
1376 | - { |
|
1377 | - return $this->dbInstance; |
|
1378 | - } |
|
1379 | - |
|
1380 | - /** |
|
1381 | - * @param Connection $connection |
|
1382 | - * |
|
1383 | - * @return static |
|
1384 | - */ |
|
1385 | - public function setConnection(Connection $connection): self |
|
1386 | - { |
|
1387 | - $this->connection = $connection; |
|
1388 | - |
|
1389 | - return $this; |
|
1390 | - } |
|
1391 | - |
|
1392 | - /** |
|
1393 | - * @return Connection |
|
1394 | - */ |
|
1395 | - public function getConnection(): Connection |
|
1396 | - { |
|
1397 | - return $this->connection; |
|
1398 | - } |
|
1399 | - |
|
1400 | - /** |
|
1401 | - * @param string|Raw|Closure $key |
|
1402 | - * @param string|null $operator |
|
1403 | - * @param mixed|null $value |
|
1404 | - * @param string $joiner |
|
1405 | - * |
|
1406 | - * @return static |
|
1407 | - */ |
|
1408 | - protected function whereHandler($key, $operator = null, $value = null, $joiner = 'AND') |
|
1409 | - { |
|
1410 | - $key = $this->addTablePrefix($key); |
|
1411 | - if ($key instanceof Raw) { |
|
1412 | - $key = $this->adapterInstance->parseRaw($key); |
|
1413 | - } |
|
1414 | - |
|
1415 | - if ($this->jsonHandler->isJsonSelector($key)) { |
|
1416 | - $key = $this->jsonHandler->extractAndUnquoteFromJsonSelector($key); |
|
1417 | - } |
|
1418 | - |
|
1419 | - $this->statements['wheres'][] = compact('key', 'operator', 'value', 'joiner'); |
|
1420 | - return $this; |
|
1421 | - } |
|
1422 | - |
|
1423 | - |
|
1424 | - |
|
1425 | - /** |
|
1426 | - * @param string $key |
|
1427 | - * @param mixed|mixed[]|bool $value |
|
1428 | - * |
|
1429 | - * @return void |
|
1430 | - */ |
|
1431 | - protected function addStatement($key, $value) |
|
1432 | - { |
|
1433 | - if (!is_array($value)) { |
|
1434 | - $value = [$value]; |
|
1435 | - } |
|
1436 | - |
|
1437 | - if (!array_key_exists($key, $this->statements)) { |
|
1438 | - $this->statements[$key] = $value; |
|
1439 | - } else { |
|
1440 | - $this->statements[$key] = array_merge($this->statements[$key], $value); |
|
1441 | - } |
|
1442 | - } |
|
1443 | - |
|
1444 | - /** |
|
1445 | - * @param string $event |
|
1446 | - * @param string|Raw $table |
|
1447 | - * |
|
1448 | - * @return callable|null |
|
1449 | - */ |
|
1450 | - public function getEvent(string $event, $table = ':any'): ?callable |
|
1451 | - { |
|
1452 | - return $this->connection->getEventHandler()->getEvent($event, $table); |
|
1453 | - } |
|
1454 | - |
|
1455 | - /** |
|
1456 | - * @param string $event |
|
1457 | - * @param string|Raw $table |
|
1458 | - * @param Closure $action |
|
1459 | - * |
|
1460 | - * @return void |
|
1461 | - */ |
|
1462 | - public function registerEvent($event, $table, Closure $action): void |
|
1463 | - { |
|
1464 | - $table = $table ?: ':any'; |
|
1465 | - |
|
1466 | - if (':any' != $table) { |
|
1467 | - $table = $this->addTablePrefix($table, false); |
|
1468 | - } |
|
1469 | - |
|
1470 | - $this->connection->getEventHandler()->registerEvent($event, $table, $action); |
|
1471 | - } |
|
1472 | - |
|
1473 | - /** |
|
1474 | - * @param string $event |
|
1475 | - * @param string|Raw $table |
|
1476 | - * |
|
1477 | - * @return void |
|
1478 | - */ |
|
1479 | - public function removeEvent(string $event, $table = ':any') |
|
1480 | - { |
|
1481 | - if (':any' != $table) { |
|
1482 | - $table = $this->addTablePrefix($table, false); |
|
1483 | - } |
|
1484 | - |
|
1485 | - $this->connection->getEventHandler()->removeEvent($event, $table); |
|
1486 | - } |
|
1487 | - |
|
1488 | - /** |
|
1489 | - * @param string $event |
|
1490 | - * |
|
1491 | - * @return mixed |
|
1492 | - */ |
|
1493 | - public function fireEvents(string $event) |
|
1494 | - { |
|
1495 | - $params = func_get_args(); // @todo Replace this with an easier to read alteratnive |
|
1496 | - array_unshift($params, $this); |
|
1497 | - |
|
1498 | - return call_user_func_array([$this->connection->getEventHandler(), 'fireEvents'], $params); |
|
1499 | - } |
|
1500 | - |
|
1501 | - /** |
|
1502 | - * @return array<string, mixed[]> |
|
1503 | - */ |
|
1504 | - public function getStatements() |
|
1505 | - { |
|
1506 | - return $this->statements; |
|
1507 | - } |
|
1508 | - |
|
1509 | - /** |
|
1510 | - * @return string will return WPDB Fetch mode |
|
1511 | - */ |
|
1512 | - public function getFetchMode() |
|
1513 | - { |
|
1514 | - return null !== $this->fetchMode |
|
1515 | - ? $this->fetchMode |
|
1516 | - : \OBJECT; |
|
1517 | - } |
|
1518 | - |
|
1519 | - /** |
|
1520 | - * Returns an NEW instance of the JSON builder populated with the same connection and hydrator details. |
|
1521 | - * |
|
1522 | - * @return JsonQueryBuilder |
|
1523 | - */ |
|
1524 | - public function jsonBuilder(): JsonQueryBuilder |
|
1525 | - { |
|
1526 | - return new JsonQueryBuilder($this->getConnection(), $this->getFetchMode(), $this->hydratorConstructorArgs); |
|
1527 | - } |
|
27 | + /** |
|
28 | + * @method add |
|
29 | + */ |
|
30 | + use TablePrefixer; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var \Viocon\Container |
|
34 | + */ |
|
35 | + protected $container; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var Connection |
|
39 | + */ |
|
40 | + protected $connection; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var array<string, mixed[]|mixed> |
|
44 | + */ |
|
45 | + protected $statements = []; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var wpdb |
|
49 | + */ |
|
50 | + protected $dbInstance; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var string|string[]|null |
|
54 | + */ |
|
55 | + protected $sqlStatement = null; |
|
56 | + |
|
57 | + /** |
|
58 | + * @var string|null |
|
59 | + */ |
|
60 | + protected $tablePrefix = null; |
|
61 | + |
|
62 | + /** |
|
63 | + * @var WPDBAdapter |
|
64 | + */ |
|
65 | + protected $adapterInstance; |
|
66 | + |
|
67 | + /** |
|
68 | + * The mode to return results as. |
|
69 | + * Accepts WPDB constants or class names. |
|
70 | + * |
|
71 | + * @var string |
|
72 | + */ |
|
73 | + protected $fetchMode; |
|
74 | + |
|
75 | + /** |
|
76 | + * Custom args used to construct models for hydrator |
|
77 | + * |
|
78 | + * @var array<int, mixed>|null |
|
79 | + */ |
|
80 | + protected $hydratorConstructorArgs; |
|
81 | + |
|
82 | + /** |
|
83 | + * Handler for Json Selectors |
|
84 | + * |
|
85 | + * @var JsonHandler |
|
86 | + */ |
|
87 | + protected $jsonHandler; |
|
88 | + |
|
89 | + /** |
|
90 | + * @param \Pixie\Connection|null $connection |
|
91 | + * @param string $fetchMode |
|
92 | + * @param mixed[] $hydratorConstructorArgs |
|
93 | + * |
|
94 | + * @throws Exception if no connection passed and not previously established |
|
95 | + */ |
|
96 | + final public function __construct( |
|
97 | + Connection $connection = null, |
|
98 | + string $fetchMode = \OBJECT, |
|
99 | + ?array $hydratorConstructorArgs = null |
|
100 | + ) { |
|
101 | + if (is_null($connection)) { |
|
102 | + // throws if connection not already established. |
|
103 | + $connection = Connection::getStoredConnection(); |
|
104 | + } |
|
105 | + |
|
106 | + // Set all dependencies from connection. |
|
107 | + $this->connection = $connection; |
|
108 | + $this->container = $this->connection->getContainer(); |
|
109 | + $this->dbInstance = $this->connection->getDbInstance(); |
|
110 | + $this->setAdapterConfig($this->connection->getAdapterConfig()); |
|
111 | + |
|
112 | + // Set up optional hydration details. |
|
113 | + $this->setFetchMode($fetchMode); |
|
114 | + $this->hydratorConstructorArgs = $hydratorConstructorArgs; |
|
115 | + |
|
116 | + // Query builder adapter instance |
|
117 | + $this->adapterInstance = $this->container->build( |
|
118 | + WPDBAdapter::class, |
|
119 | + [$this->connection] |
|
120 | + ); |
|
121 | + |
|
122 | + // Setup JSON Selector handler. |
|
123 | + $this->jsonHandler = new JsonHandler($connection); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Sets the config for WPDB |
|
128 | + * |
|
129 | + * @param array<string, mixed> $adapterConfig |
|
130 | + * |
|
131 | + * @return void |
|
132 | + */ |
|
133 | + protected function setAdapterConfig(array $adapterConfig): void |
|
134 | + { |
|
135 | + if (isset($adapterConfig[Connection::PREFIX])) { |
|
136 | + $this->tablePrefix = $adapterConfig[Connection::PREFIX]; |
|
137 | + } |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Fetch query results as object of specified type |
|
142 | + * |
|
143 | + * @param string $className |
|
144 | + * @param array<int, mixed> $constructorArgs |
|
145 | + * @return static |
|
146 | + */ |
|
147 | + public function asObject($className, $constructorArgs = array()): self |
|
148 | + { |
|
149 | + return $this->setFetchMode($className, $constructorArgs); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Set the fetch mode |
|
154 | + * |
|
155 | + * @param string $mode |
|
156 | + * @param array<int, mixed>|null $constructorArgs |
|
157 | + * |
|
158 | + * @return static |
|
159 | + */ |
|
160 | + public function setFetchMode(string $mode, ?array $constructorArgs = null): self |
|
161 | + { |
|
162 | + $this->fetchMode = $mode; |
|
163 | + $this->hydratorConstructorArgs = $constructorArgs; |
|
164 | + |
|
165 | + return $this; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @param Connection|null $connection |
|
170 | + * |
|
171 | + * @return static |
|
172 | + * |
|
173 | + * @throws Exception |
|
174 | + */ |
|
175 | + public function newQuery(Connection $connection = null): self |
|
176 | + { |
|
177 | + if (is_null($connection)) { |
|
178 | + $connection = $this->connection; |
|
179 | + } |
|
180 | + |
|
181 | + $newQuery = $this->constructCurrentBuilderClass($connection); |
|
182 | + $newQuery->setFetchMode($this->getFetchMode(), $this->hydratorConstructorArgs); |
|
183 | + |
|
184 | + return $newQuery; |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Returns a new instance of the current, with the passed connection. |
|
189 | + * |
|
190 | + * @param \Pixie\Connection $connection |
|
191 | + * |
|
192 | + * @return static |
|
193 | + */ |
|
194 | + protected function constructCurrentBuilderClass(Connection $connection): self |
|
195 | + { |
|
196 | + return new static($connection); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Interpolates a query |
|
201 | + * |
|
202 | + * @param string $query |
|
203 | + * @param array<mixed> $bindings |
|
204 | + * @return string |
|
205 | + */ |
|
206 | + public function interpolateQuery(string $query, array $bindings = []): string |
|
207 | + { |
|
208 | + return $this->adapterInstance->interpolateQuery($query, $bindings); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @param string $sql |
|
213 | + * @param array<int,mixed> $bindings |
|
214 | + * |
|
215 | + * @return static |
|
216 | + */ |
|
217 | + public function query($sql, $bindings = []): self |
|
218 | + { |
|
219 | + list($this->sqlStatement) = $this->statement($sql, $bindings); |
|
220 | + |
|
221 | + return $this; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * @param string $sql |
|
226 | + * @param array<int,mixed> $bindings |
|
227 | + * |
|
228 | + * @return array{0:string, 1:float} |
|
229 | + */ |
|
230 | + public function statement(string $sql, $bindings = []): array |
|
231 | + { |
|
232 | + $start = microtime(true); |
|
233 | + $sqlStatement = empty($bindings) ? $sql : $this->interpolateQuery($sql, $bindings); |
|
234 | + |
|
235 | + if (!is_string($sqlStatement)) { |
|
236 | + throw new Exception('Could not interpolate query', 1); |
|
237 | + } |
|
238 | + |
|
239 | + return [$sqlStatement, microtime(true) - $start]; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Get all rows |
|
244 | + * |
|
245 | + * @return array<mixed,mixed>|null |
|
246 | + * |
|
247 | + * @throws Exception |
|
248 | + */ |
|
249 | + public function get() |
|
250 | + { |
|
251 | + $eventResult = $this->fireEvents('before-select'); |
|
252 | + if (!is_null($eventResult)) { |
|
253 | + return $eventResult; |
|
254 | + } |
|
255 | + $executionTime = 0; |
|
256 | + if (is_null($this->sqlStatement)) { |
|
257 | + $queryObject = $this->getQuery('select'); |
|
258 | + $statement = $this->statement( |
|
259 | + $queryObject->getSql(), |
|
260 | + $queryObject->getBindings() |
|
261 | + ); |
|
262 | + |
|
263 | + $this->sqlStatement = $statement[0]; |
|
264 | + $executionTime = $statement[1]; |
|
265 | + } |
|
266 | + |
|
267 | + $start = microtime(true); |
|
268 | + $result = $this->dbInstance()->get_results( |
|
269 | + is_array($this->sqlStatement) ? (end($this->sqlStatement) ?: '') : $this->sqlStatement, |
|
270 | + // If we are using the hydrator, return as OBJECT and let the hydrator map the correct model. |
|
271 | + $this->useHydrator() ? OBJECT : $this->getFetchMode() |
|
272 | + ); |
|
273 | + $executionTime += microtime(true) - $start; |
|
274 | + $this->sqlStatement = null; |
|
275 | + |
|
276 | + // Ensure we have an array of results. |
|
277 | + if (!is_array($result) && null !== $result) { |
|
278 | + $result = [$result]; |
|
279 | + } |
|
280 | + |
|
281 | + // Maybe hydrate the results. |
|
282 | + if (null !== $result && $this->useHydrator()) { |
|
283 | + $result = $this->getHydrator()->fromMany($result); |
|
284 | + } |
|
285 | + |
|
286 | + $this->fireEvents('after-select', $result, $executionTime); |
|
287 | + |
|
288 | + return $result; |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * Returns a populated instance of the Hydrator. |
|
293 | + * |
|
294 | + * @return Hydrator |
|
295 | + */ |
|
296 | + protected function getHydrator(): Hydrator /* @phpstan-ignore-line */ |
|
297 | + { |
|
298 | + $hydrator = new Hydrator($this->getFetchMode(), $this->hydratorConstructorArgs ?? []); /* @phpstan-ignore-line */ |
|
299 | + |
|
300 | + return $hydrator; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Checks if the results should be mapped via the hydrator |
|
305 | + * |
|
306 | + * @return bool |
|
307 | + */ |
|
308 | + protected function useHydrator(): bool |
|
309 | + { |
|
310 | + return !in_array($this->getFetchMode(), [\ARRAY_A, \ARRAY_N, \OBJECT, \OBJECT_K]); |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * Find all matching a simple where condition. |
|
315 | + * |
|
316 | + * Shortcut of ->where('key','=','value')->limit(1)->get(); |
|
317 | + * |
|
318 | + * @return \stdClass\array<mixed,mixed>|object|null Can return any object using hydrator |
|
319 | + */ |
|
320 | + public function first() |
|
321 | + { |
|
322 | + $this->limit(1); |
|
323 | + $result = $this->get(); |
|
324 | + |
|
325 | + return empty($result) ? null : $result[0]; |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Find all matching a simple where condition. |
|
330 | + * |
|
331 | + * Shortcut of ->where('key','=','value')->get(); |
|
332 | + * |
|
333 | + * @param string $fieldName |
|
334 | + * @param mixed $value |
|
335 | + * |
|
336 | + * @return array<mixed,mixed>|null Can return any object using hydrator |
|
337 | + */ |
|
338 | + public function findAll($fieldName, $value) |
|
339 | + { |
|
340 | + $this->where($fieldName, '=', $value); |
|
341 | + |
|
342 | + return $this->get(); |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * @param string $fieldName |
|
347 | + * @param mixed $value |
|
348 | + * |
|
349 | + * @return \stdClass\array<mixed,mixed>|object|null Can return any object using hydrator |
|
350 | + */ |
|
351 | + public function find($value, $fieldName = 'id') |
|
352 | + { |
|
353 | + $this->where($fieldName, '=', $value); |
|
354 | + |
|
355 | + return $this->first(); |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * @param string $fieldName |
|
360 | + * @param mixed $value |
|
361 | + * |
|
362 | + * @return \stdClass\array<mixed,mixed>|object Can return any object using hydrator |
|
363 | + * @throws Exception If fails to find |
|
364 | + */ |
|
365 | + public function findOrFail($value, $fieldName = 'id') |
|
366 | + { |
|
367 | + $result = $this->find($value, $fieldName); |
|
368 | + if (null === $result) { |
|
369 | + throw new Exception("Failed to find {$fieldName}={$value}", 1); |
|
370 | + } |
|
371 | + return $result; |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * Used to handle all aggregation method. |
|
376 | + * |
|
377 | + * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
378 | + * |
|
379 | + * @param string $type |
|
380 | + * @param string|Raw $field |
|
381 | + * |
|
382 | + * @return float |
|
383 | + */ |
|
384 | + protected function aggregate(string $type, $field = '*'): float |
|
385 | + { |
|
386 | + // Parse a raw expression. |
|
387 | + if ($field instanceof Raw) { |
|
388 | + $field = $this->adapterInstance->parseRaw($field); |
|
389 | + } |
|
390 | + |
|
391 | + // Potentialy cast field from JSON |
|
392 | + if ($this->jsonHandler->isJsonSelector($field)) { |
|
393 | + $field = $this->jsonHandler->extractAndUnquoteFromJsonSelector($field); |
|
394 | + } |
|
395 | + |
|
396 | + // Verify that field exists |
|
397 | + if ('*' !== $field && true === isset($this->statements['selects']) && false === \in_array($field, $this->statements['selects'], true)) { |
|
398 | + throw new \Exception(sprintf('Failed %s query - the column %s hasn\'t been selected in the query.', $type, $field)); |
|
399 | + } |
|
400 | + |
|
401 | + |
|
402 | + if (false === isset($this->statements['tables'])) { |
|
403 | + throw new Exception('No table selected'); |
|
404 | + } |
|
405 | + |
|
406 | + $count = $this |
|
407 | + ->table($this->subQuery($this, 'count')) |
|
408 | + ->select([$this->raw(sprintf('%s(%s) AS field', strtoupper($type), $field))]) |
|
409 | + ->first(); |
|
410 | + |
|
411 | + return true === isset($count->field) ? (float)$count->field : 0; |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Get count of all the rows for the current query |
|
416 | + * |
|
417 | + * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
418 | + * |
|
419 | + * @param string|Raw $field |
|
420 | + * |
|
421 | + * @return int |
|
422 | + * |
|
423 | + * @throws Exception |
|
424 | + */ |
|
425 | + public function count($field = '*'): int |
|
426 | + { |
|
427 | + return (int)$this->aggregate('count', $field); |
|
428 | + } |
|
429 | + |
|
430 | + /** |
|
431 | + * Get the sum for a field in the current query |
|
432 | + * |
|
433 | + * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
434 | + * |
|
435 | + * @param string|Raw $field |
|
436 | + * |
|
437 | + * @return float |
|
438 | + * |
|
439 | + * @throws Exception |
|
440 | + */ |
|
441 | + public function sum($field): float |
|
442 | + { |
|
443 | + return $this->aggregate('sum', $field); |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Get the average for a field in the current query |
|
448 | + * |
|
449 | + * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
450 | + * |
|
451 | + * @param string|Raw $field |
|
452 | + * |
|
453 | + * @return float |
|
454 | + * |
|
455 | + * @throws Exception |
|
456 | + */ |
|
457 | + public function average($field): float |
|
458 | + { |
|
459 | + return $this->aggregate('avg', $field); |
|
460 | + } |
|
461 | + |
|
462 | + /** |
|
463 | + * Get the minimum for a field in the current query |
|
464 | + * |
|
465 | + * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
466 | + * |
|
467 | + * @param string|Raw $field |
|
468 | + * |
|
469 | + * @return float |
|
470 | + * |
|
471 | + * @throws Exception |
|
472 | + */ |
|
473 | + public function min($field): float |
|
474 | + { |
|
475 | + return $this->aggregate('min', $field); |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * Get the maximum for a field in the current query |
|
480 | + * |
|
481 | + * @see Taken from the pecee-pixie library - https://github.com/skipperbent/pecee-pixie/ |
|
482 | + * |
|
483 | + * @param string|Raw $field |
|
484 | + * |
|
485 | + * @return float |
|
486 | + * |
|
487 | + * @throws Exception |
|
488 | + */ |
|
489 | + public function max($field): float |
|
490 | + { |
|
491 | + return $this->aggregate('max', $field); |
|
492 | + } |
|
493 | + |
|
494 | + /** |
|
495 | + * @param string $type |
|
496 | + * @param bool|array<mixed, mixed> $dataToBePassed |
|
497 | + * |
|
498 | + * @return mixed |
|
499 | + * |
|
500 | + * @throws Exception |
|
501 | + */ |
|
502 | + public function getQuery(string $type = 'select', $dataToBePassed = []) |
|
503 | + { |
|
504 | + $allowedTypes = ['select', 'insert', 'insertignore', 'replace', 'delete', 'update', 'criteriaonly']; |
|
505 | + if (!in_array(strtolower($type), $allowedTypes)) { |
|
506 | + throw new Exception($type . ' is not a known type.', 2); |
|
507 | + } |
|
508 | + |
|
509 | + $queryArr = $this->adapterInstance->$type($this->statements, $dataToBePassed); |
|
510 | + |
|
511 | + return $this->container->build( |
|
512 | + QueryObject::class, |
|
513 | + [$queryArr['sql'], $queryArr['bindings'], $this->dbInstance] |
|
514 | + ); |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * @param QueryBuilderHandler $queryBuilder |
|
519 | + * @param string|null $alias |
|
520 | + * |
|
521 | + * @return Raw |
|
522 | + */ |
|
523 | + public function subQuery(QueryBuilderHandler $queryBuilder, ?string $alias = null) |
|
524 | + { |
|
525 | + $sql = '(' . $queryBuilder->getQuery()->getRawSql() . ')'; |
|
526 | + if (is_string($alias) && 0 !== mb_strlen($alias)) { |
|
527 | + $sql = $sql . ' as ' . $alias; |
|
528 | + } |
|
529 | + |
|
530 | + return $queryBuilder->raw($sql); |
|
531 | + } |
|
532 | + |
|
533 | + /** |
|
534 | + * Handles the various insert operations based on the type. |
|
535 | + * |
|
536 | + * @param array<int|string, mixed|mixed[]> $data |
|
537 | + * @param string $type |
|
538 | + * |
|
539 | + * @return int|int[]|mixed|null can return a single row id, array of row ids, null (for failed) or any other value short circuited from event |
|
540 | + */ |
|
541 | + private function doInsert(array $data, string $type) |
|
542 | + { |
|
543 | + $eventResult = $this->fireEvents('before-insert'); |
|
544 | + if (!is_null($eventResult)) { |
|
545 | + return $eventResult; |
|
546 | + } |
|
547 | + |
|
548 | + // If first value is not an array () not a batch insert) |
|
549 | + if (!is_array(current($data))) { |
|
550 | + $queryObject = $this->getQuery($type, $data); |
|
551 | + |
|
552 | + list($preparedQuery, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); |
|
553 | + $this->dbInstance->get_results($preparedQuery); |
|
554 | + |
|
555 | + // Check we have a result. |
|
556 | + $return = 1 === $this->dbInstance->rows_affected ? $this->dbInstance->insert_id : null; |
|
557 | + } else { |
|
558 | + // Its a batch insert |
|
559 | + $return = []; |
|
560 | + $executionTime = 0; |
|
561 | + foreach ($data as $subData) { |
|
562 | + $queryObject = $this->getQuery($type, $subData); |
|
563 | + |
|
564 | + list($preparedQuery, $time) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); |
|
565 | + $this->dbInstance->get_results($preparedQuery); |
|
566 | + $executionTime += $time; |
|
567 | + |
|
568 | + if (1 === $this->dbInstance->rows_affected) { |
|
569 | + $return[] = $this->dbInstance->insert_id; |
|
570 | + } |
|
571 | + } |
|
572 | + } |
|
573 | + |
|
574 | + $this->fireEvents('after-insert', $return, $executionTime); |
|
575 | + |
|
576 | + return $return; |
|
577 | + } |
|
578 | + |
|
579 | + /** |
|
580 | + * @param array<int|string, mixed|mixed[]> $data either key=>value array for single or array of arrays for bulk |
|
581 | + * |
|
582 | + * @return int|int[]|mixed|null can return a single row id, array of row ids, null (for failed) or any other value short circuited from event |
|
583 | + */ |
|
584 | + public function insert($data) |
|
585 | + { |
|
586 | + return $this->doInsert($data, 'insert'); |
|
587 | + } |
|
588 | + |
|
589 | + /** |
|
590 | + * |
|
591 | + * @param array<int|string, mixed|mixed[]> $data either key=>value array for single or array of arrays for bulk |
|
592 | + * |
|
593 | + * @return int|int[]|mixed|null can return a single row id, array of row ids, null (for failed) or any other value short circuited from event |
|
594 | + */ |
|
595 | + public function insertIgnore($data) |
|
596 | + { |
|
597 | + return $this->doInsert($data, 'insertignore'); |
|
598 | + } |
|
599 | + |
|
600 | + /** |
|
601 | + * |
|
602 | + * @param array<int|string, mixed|mixed[]> $data either key=>value array for single or array of arrays for bulk |
|
603 | + * |
|
604 | + * @return int|int[]|mixed|null can return a single row id, array of row ids, null (for failed) or any other value short circuited from event |
|
605 | + */ |
|
606 | + public function replace($data) |
|
607 | + { |
|
608 | + return $this->doInsert($data, 'replace'); |
|
609 | + } |
|
610 | + |
|
611 | + /** |
|
612 | + * @param array<string, mixed> $data |
|
613 | + * |
|
614 | + * @return int|null Number of row effected, null for none. |
|
615 | + */ |
|
616 | + public function update(array $data): ?int |
|
617 | + { |
|
618 | + $eventResult = $this->fireEvents('before-update'); |
|
619 | + if (!is_null($eventResult)) { |
|
620 | + return $eventResult; |
|
621 | + } |
|
622 | + $queryObject = $this->getQuery('update', $data); |
|
623 | + $r = $this->statement($queryObject->getSql(), $queryObject->getBindings()); |
|
624 | + list($preparedQuery, $executionTime) = $r; |
|
625 | + $this->dbInstance()->get_results($preparedQuery); |
|
626 | + $this->fireEvents('after-update', $queryObject, $executionTime); |
|
627 | + |
|
628 | + return 0 !== (int) $this->dbInstance()->rows_affected |
|
629 | + ? (int) $this->dbInstance()->rows_affected |
|
630 | + : null; |
|
631 | + } |
|
632 | + |
|
633 | + /** |
|
634 | + * Update or Insert based on the attributes. |
|
635 | + * |
|
636 | + * @param array<string, mixed> $attributes Conditions to check |
|
637 | + * @param array<string, mixed> $values Values to add/update |
|
638 | + * |
|
639 | + * @return int|int[]|null will return row id(s) for insert and null for success/fail on update |
|
640 | + */ |
|
641 | + public function updateOrInsert(array $attributes, array $values = []) |
|
642 | + { |
|
643 | + // Check if existing post exists. |
|
644 | + $query = clone $this; |
|
645 | + foreach ($attributes as $column => $value) { |
|
646 | + $query->where($column, $value); |
|
647 | + } |
|
648 | + |
|
649 | + return null !== $query->first() |
|
650 | + ? $this->update(array_merge($values, $attributes)) |
|
651 | + : $this->insert(array_merge($values, $attributes)); |
|
652 | + } |
|
653 | + |
|
654 | + /** |
|
655 | + * @param array<string, mixed> $data |
|
656 | + * |
|
657 | + * @return static |
|
658 | + */ |
|
659 | + public function onDuplicateKeyUpdate($data) |
|
660 | + { |
|
661 | + $this->addStatement('onduplicate', $data); |
|
662 | + |
|
663 | + return $this; |
|
664 | + } |
|
665 | + |
|
666 | + /** |
|
667 | + * @return mixed number of rows effected or shortcircuited response |
|
668 | + */ |
|
669 | + public function delete() |
|
670 | + { |
|
671 | + $eventResult = $this->fireEvents('before-delete'); |
|
672 | + if (!is_null($eventResult)) { |
|
673 | + return $eventResult; |
|
674 | + } |
|
675 | + |
|
676 | + $queryObject = $this->getQuery('delete'); |
|
677 | + |
|
678 | + list($preparedQuery, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); |
|
679 | + $this->dbInstance()->get_results($preparedQuery); |
|
680 | + $this->fireEvents('after-delete', $queryObject, $executionTime); |
|
681 | + |
|
682 | + return $this->dbInstance()->rows_affected; |
|
683 | + } |
|
684 | + |
|
685 | + /** |
|
686 | + * @param string|Raw ...$tables Single table or array of tables |
|
687 | + * |
|
688 | + * @return static |
|
689 | + * |
|
690 | + * @throws Exception |
|
691 | + */ |
|
692 | + public function table(...$tables) |
|
693 | + { |
|
694 | + $instance = $this->constructCurrentBuilderClass($this->connection); |
|
695 | + $this->setFetchMode($this->getFetchMode(), $this->hydratorConstructorArgs); |
|
696 | + $tables = $this->addTablePrefix($tables, false); |
|
697 | + $instance->addStatement('tables', $tables); |
|
698 | + |
|
699 | + return $instance; |
|
700 | + } |
|
701 | + |
|
702 | + /** |
|
703 | + * @param string|Raw ...$tables Single table or array of tables |
|
704 | + * |
|
705 | + * @return static |
|
706 | + */ |
|
707 | + public function from(...$tables): self |
|
708 | + { |
|
709 | + $tables = $this->addTablePrefix($tables, false); |
|
710 | + $this->addStatement('tables', $tables); |
|
711 | + |
|
712 | + return $this; |
|
713 | + } |
|
714 | + |
|
715 | + /** |
|
716 | + * @param string|string[]|Raw[]|array<string, string> $fields |
|
717 | + * |
|
718 | + * @return static |
|
719 | + */ |
|
720 | + public function select($fields): self |
|
721 | + { |
|
722 | + if (!is_array($fields)) { |
|
723 | + $fields = func_get_args(); |
|
724 | + } |
|
725 | + |
|
726 | + foreach ($fields as $field => $alias) { |
|
727 | + // If we have a JSON expression |
|
728 | + if ($this->jsonHandler->isJsonSelector($field)) { |
|
729 | + $field = $this->jsonHandler->extractAndUnquoteFromJsonSelector($field); |
|
730 | + } |
|
731 | + |
|
732 | + // If no alias passed, but field is for JSON. thrown an exception. |
|
733 | + if (is_numeric($field) && is_string($alias) && $this->jsonHandler->isJsonSelector($alias)) { |
|
734 | + throw new Exception("An alias must be used if you wish to select from JSON Object", 1); |
|
735 | + } |
|
736 | + |
|
737 | + // Treat each array as a single table, to retain order added |
|
738 | + $field = is_numeric($field) |
|
739 | + ? $field = $alias // If single colum |
|
740 | + : $field = [$field => $alias]; // Has alias |
|
741 | + |
|
742 | + $field = $this->addTablePrefix($field); |
|
743 | + $this->addStatement('selects', $field); |
|
744 | + } |
|
745 | + |
|
746 | + return $this; |
|
747 | + } |
|
748 | + |
|
749 | + /** |
|
750 | + * @param string|string[]|Raw[]|array<string, string> $fields |
|
751 | + * |
|
752 | + * @return static |
|
753 | + */ |
|
754 | + public function selectDistinct($fields) |
|
755 | + { |
|
756 | + $this->select($fields); |
|
757 | + $this->addStatement('distinct', true); |
|
758 | + |
|
759 | + return $this; |
|
760 | + } |
|
761 | + |
|
762 | + /** |
|
763 | + * @param string|string[] $field either the single field or an array of fields |
|
764 | + * |
|
765 | + * @return static |
|
766 | + */ |
|
767 | + public function groupBy($field): self |
|
768 | + { |
|
769 | + $field = $this->addTablePrefix($field); |
|
770 | + $this->addStatement('groupBys', $field); |
|
771 | + |
|
772 | + return $this; |
|
773 | + } |
|
774 | + |
|
775 | + /** |
|
776 | + * @param string|array<string|int, mixed> $fields |
|
777 | + * @param string $defaultDirection |
|
778 | + * |
|
779 | + * @return static |
|
780 | + */ |
|
781 | + public function orderBy($fields, string $defaultDirection = 'ASC'): self |
|
782 | + { |
|
783 | + if (!is_array($fields)) { |
|
784 | + $fields = [$fields]; |
|
785 | + } |
|
786 | + |
|
787 | + foreach ($fields as $key => $value) { |
|
788 | + $field = $key; |
|
789 | + $type = $value; |
|
790 | + if (is_int($key)) { |
|
791 | + $field = $value; |
|
792 | + $type = $defaultDirection; |
|
793 | + } |
|
794 | + |
|
795 | + if ($this->jsonHandler->isJsonSelector($field)) { |
|
796 | + $field = $this->jsonHandler->extractAndUnquoteFromJsonSelector($field); |
|
797 | + } |
|
798 | + |
|
799 | + if (!$field instanceof Raw) { |
|
800 | + $field = $this->addTablePrefix($field); |
|
801 | + } |
|
802 | + $this->statements['orderBys'][] = compact('field', 'type'); |
|
803 | + } |
|
804 | + |
|
805 | + return $this; |
|
806 | + } |
|
807 | + |
|
808 | + /** |
|
809 | + * @param string|Raw $key The database column which holds the JSON value |
|
810 | + * @param string|Raw|string[] $jsonKey The json key/index to search |
|
811 | + * @param string $defaultDirection |
|
812 | + * @return static |
|
813 | + */ |
|
814 | + public function orderByJson($key, $jsonKey, string $defaultDirection = 'ASC'): self |
|
815 | + { |
|
816 | + $key = $this->jsonHandler->jsonExpressionFactory()->extractAndUnquote($key, $jsonKey); |
|
817 | + return $this->orderBy($key, $defaultDirection); |
|
818 | + } |
|
819 | + |
|
820 | + /** |
|
821 | + * @param int $limit |
|
822 | + * |
|
823 | + * @return static |
|
824 | + */ |
|
825 | + public function limit(int $limit): self |
|
826 | + { |
|
827 | + $this->statements['limit'] = $limit; |
|
828 | + |
|
829 | + return $this; |
|
830 | + } |
|
831 | + |
|
832 | + /** |
|
833 | + * @param int $offset |
|
834 | + * |
|
835 | + * @return static |
|
836 | + */ |
|
837 | + public function offset(int $offset): self |
|
838 | + { |
|
839 | + $this->statements['offset'] = $offset; |
|
840 | + |
|
841 | + return $this; |
|
842 | + } |
|
843 | + |
|
844 | + /** |
|
845 | + * @param string|string[]|Raw|Raw[] $key |
|
846 | + * @param string $operator |
|
847 | + * @param mixed $value |
|
848 | + * @param string $joiner |
|
849 | + * |
|
850 | + * @return static |
|
851 | + */ |
|
852 | + public function having($key, string $operator, $value, string $joiner = 'AND') |
|
853 | + { |
|
854 | + $key = $this->addTablePrefix($key); |
|
855 | + $this->statements['havings'][] = compact('key', 'operator', 'value', 'joiner'); |
|
856 | + |
|
857 | + return $this; |
|
858 | + } |
|
859 | + |
|
860 | + /** |
|
861 | + * @param string|string[]|Raw|Raw[] $key |
|
862 | + * @param string $operator |
|
863 | + * @param mixed $value |
|
864 | + * |
|
865 | + * @return static |
|
866 | + */ |
|
867 | + public function orHaving($key, $operator, $value) |
|
868 | + { |
|
869 | + return $this->having($key, $operator, $value, 'OR'); |
|
870 | + } |
|
871 | + |
|
872 | + /** |
|
873 | + * @param string|Raw $key |
|
874 | + * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
875 | + * @param mixed|null $value |
|
876 | + * |
|
877 | + * @return static |
|
878 | + */ |
|
879 | + public function where($key, $operator = null, $value = null): self |
|
880 | + { |
|
881 | + // If two params are given then assume operator is = |
|
882 | + if (2 === func_num_args()) { |
|
883 | + $value = $operator; |
|
884 | + $operator = '='; |
|
885 | + } |
|
886 | + |
|
887 | + return $this->whereHandler($key, $operator, $value); |
|
888 | + } |
|
889 | + |
|
890 | + /** |
|
891 | + * @param string|Raw|\Closure(QueryBuilderHandler):void $key |
|
892 | + * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
893 | + * @param mixed|null $value |
|
894 | + * |
|
895 | + * @return static |
|
896 | + */ |
|
897 | + public function orWhere($key, $operator = null, $value = null): self |
|
898 | + { |
|
899 | + // If two params are given then assume operator is = |
|
900 | + if (2 === func_num_args()) { |
|
901 | + $value = $operator; |
|
902 | + $operator = '='; |
|
903 | + } |
|
904 | + |
|
905 | + return $this->whereHandler($key, $operator, $value, 'OR'); |
|
906 | + } |
|
907 | + |
|
908 | + /** |
|
909 | + * @param string|Raw $key |
|
910 | + * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
911 | + * @param mixed|null $value |
|
912 | + * |
|
913 | + * @return static |
|
914 | + */ |
|
915 | + public function whereNot($key, $operator = null, $value = null): self |
|
916 | + { |
|
917 | + // If two params are given then assume operator is = |
|
918 | + if (2 === func_num_args()) { |
|
919 | + $value = $operator; |
|
920 | + $operator = '='; |
|
921 | + } |
|
922 | + |
|
923 | + return $this->whereHandler($key, $operator, $value, 'AND NOT'); |
|
924 | + } |
|
925 | + |
|
926 | + /** |
|
927 | + * @param string|Raw $key |
|
928 | + * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
929 | + * @param mixed|null $value |
|
930 | + * |
|
931 | + * @return static |
|
932 | + */ |
|
933 | + public function orWhereNot($key, $operator = null, $value = null) |
|
934 | + { |
|
935 | + // If two params are given then assume operator is = |
|
936 | + if (2 === func_num_args()) { |
|
937 | + $value = $operator; |
|
938 | + $operator = '='; |
|
939 | + } |
|
940 | + |
|
941 | + return $this->whereHandler($key, $operator, $value, 'OR NOT'); |
|
942 | + } |
|
943 | + |
|
944 | + /** |
|
945 | + * @param string|Raw $key |
|
946 | + * @param mixed[]|string|Raw $values |
|
947 | + * |
|
948 | + * @return static |
|
949 | + */ |
|
950 | + public function whereIn($key, $values): self |
|
951 | + { |
|
952 | + return $this->whereHandler($key, 'IN', $values, 'AND'); |
|
953 | + } |
|
954 | + |
|
955 | + /** |
|
956 | + * @param string|Raw $key |
|
957 | + * @param mixed[]|string|Raw $values |
|
958 | + * |
|
959 | + * @return static |
|
960 | + */ |
|
961 | + public function whereNotIn($key, $values): self |
|
962 | + { |
|
963 | + return $this->whereHandler($key, 'NOT IN', $values, 'AND'); |
|
964 | + } |
|
965 | + |
|
966 | + /** |
|
967 | + * @param string|Raw $key |
|
968 | + * @param mixed[]|string|Raw $values |
|
969 | + * |
|
970 | + * @return static |
|
971 | + */ |
|
972 | + public function orWhereIn($key, $values): self |
|
973 | + { |
|
974 | + return $this->whereHandler($key, 'IN', $values, 'OR'); |
|
975 | + } |
|
976 | + |
|
977 | + /** |
|
978 | + * @param string|Raw $key |
|
979 | + * @param mixed[]|string|Raw $values |
|
980 | + * |
|
981 | + * @return static |
|
982 | + */ |
|
983 | + public function orWhereNotIn($key, $values): self |
|
984 | + { |
|
985 | + return $this->whereHandler($key, 'NOT IN', $values, 'OR'); |
|
986 | + } |
|
987 | + |
|
988 | + /** |
|
989 | + * @param string|Raw $key |
|
990 | + * @param mixed $valueFrom |
|
991 | + * @param mixed $valueTo |
|
992 | + * |
|
993 | + * @return static |
|
994 | + */ |
|
995 | + public function whereBetween($key, $valueFrom, $valueTo): self |
|
996 | + { |
|
997 | + return $this->whereHandler($key, 'BETWEEN', [$valueFrom, $valueTo], 'AND'); |
|
998 | + } |
|
999 | + |
|
1000 | + /** |
|
1001 | + * @param string|Raw $key |
|
1002 | + * @param mixed $valueFrom |
|
1003 | + * @param mixed $valueTo |
|
1004 | + * |
|
1005 | + * @return static |
|
1006 | + */ |
|
1007 | + public function orWhereBetween($key, $valueFrom, $valueTo): self |
|
1008 | + { |
|
1009 | + return $this->whereHandler($key, 'BETWEEN', [$valueFrom, $valueTo], 'OR'); |
|
1010 | + } |
|
1011 | + |
|
1012 | + /** |
|
1013 | + * Handles all function call based where conditions |
|
1014 | + * |
|
1015 | + * @param string|Raw $key |
|
1016 | + * @param string $function |
|
1017 | + * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
1018 | + * @param mixed|null $value |
|
1019 | + * @return static |
|
1020 | + */ |
|
1021 | + protected function whereFunctionCallHandler($key, $function, $operator, $value): self |
|
1022 | + { |
|
1023 | + $key = \sprintf('%s(%s)', $function, $this->addTablePrefix($key)); |
|
1024 | + return $this->where($key, $operator, $value); |
|
1025 | + } |
|
1026 | + |
|
1027 | + /** |
|
1028 | + * @param string|Raw $key |
|
1029 | + * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
1030 | + * @param mixed|null $value |
|
1031 | + * @return self |
|
1032 | + */ |
|
1033 | + public function whereMonth($key, $operator = null, $value = null): self |
|
1034 | + { |
|
1035 | + // If two params are given then assume operator is = |
|
1036 | + if (2 === func_num_args()) { |
|
1037 | + $value = $operator; |
|
1038 | + $operator = '='; |
|
1039 | + } |
|
1040 | + return $this->whereFunctionCallHandler($key, 'MONTH', $operator, $value); |
|
1041 | + } |
|
1042 | + |
|
1043 | + /** |
|
1044 | + * @param string|Raw $key |
|
1045 | + * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
1046 | + * @param mixed|null $value |
|
1047 | + * @return self |
|
1048 | + */ |
|
1049 | + public function whereDay($key, $operator = null, $value = null): self |
|
1050 | + { |
|
1051 | + // If two params are given then assume operator is = |
|
1052 | + if (2 === func_num_args()) { |
|
1053 | + $value = $operator; |
|
1054 | + $operator = '='; |
|
1055 | + } |
|
1056 | + return $this->whereFunctionCallHandler($key, 'DAY', $operator, $value); |
|
1057 | + } |
|
1058 | + |
|
1059 | + /** |
|
1060 | + * @param string|Raw $key |
|
1061 | + * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
1062 | + * @param mixed|null $value |
|
1063 | + * @return self |
|
1064 | + */ |
|
1065 | + public function whereYear($key, $operator = null, $value = null): self |
|
1066 | + { |
|
1067 | + // If two params are given then assume operator is = |
|
1068 | + if (2 === func_num_args()) { |
|
1069 | + $value = $operator; |
|
1070 | + $operator = '='; |
|
1071 | + } |
|
1072 | + return $this->whereFunctionCallHandler($key, 'YEAR', $operator, $value); |
|
1073 | + } |
|
1074 | + |
|
1075 | + /** |
|
1076 | + * @param string|Raw $key |
|
1077 | + * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
1078 | + * @param mixed|null $value |
|
1079 | + * @return self |
|
1080 | + */ |
|
1081 | + public function whereDate($key, $operator = null, $value = null): self |
|
1082 | + { |
|
1083 | + // If two params are given then assume operator is = |
|
1084 | + if (2 === func_num_args()) { |
|
1085 | + $value = $operator; |
|
1086 | + $operator = '='; |
|
1087 | + } |
|
1088 | + return $this->whereFunctionCallHandler($key, 'DATE', $operator, $value); |
|
1089 | + } |
|
1090 | + |
|
1091 | + /** |
|
1092 | + * @param string|Raw $key |
|
1093 | + * |
|
1094 | + * @return static |
|
1095 | + */ |
|
1096 | + public function whereNull($key): self |
|
1097 | + { |
|
1098 | + return $this->whereNullHandler($key); |
|
1099 | + } |
|
1100 | + |
|
1101 | + /** |
|
1102 | + * @param string|Raw $key |
|
1103 | + * |
|
1104 | + * @return static |
|
1105 | + */ |
|
1106 | + public function whereNotNull($key): self |
|
1107 | + { |
|
1108 | + return $this->whereNullHandler($key, 'NOT'); |
|
1109 | + } |
|
1110 | + |
|
1111 | + /** |
|
1112 | + * @param string|Raw $key |
|
1113 | + * |
|
1114 | + * @return static |
|
1115 | + */ |
|
1116 | + public function orWhereNull($key): self |
|
1117 | + { |
|
1118 | + return $this->whereNullHandler($key, '', 'or'); |
|
1119 | + } |
|
1120 | + |
|
1121 | + /** |
|
1122 | + * @param string|Raw $key |
|
1123 | + * |
|
1124 | + * @return static |
|
1125 | + */ |
|
1126 | + public function orWhereNotNull($key): self |
|
1127 | + { |
|
1128 | + return $this->whereNullHandler($key, 'NOT', 'or'); |
|
1129 | + } |
|
1130 | + |
|
1131 | + /** |
|
1132 | + * @param string|Raw $key |
|
1133 | + * @param string $prefix |
|
1134 | + * @param string $operator |
|
1135 | + * |
|
1136 | + * @return static |
|
1137 | + */ |
|
1138 | + protected function whereNullHandler($key, string $prefix = '', $operator = ''): self |
|
1139 | + { |
|
1140 | + $prefix = 0 === mb_strlen($prefix) ? '' : " {$prefix}"; |
|
1141 | + |
|
1142 | + if ($key instanceof Raw) { |
|
1143 | + $key = $this->adapterInstance->parseRaw($key); |
|
1144 | + } |
|
1145 | + |
|
1146 | + $key = $this->addTablePrefix($key); |
|
1147 | + if ($key instanceof Closure) { |
|
1148 | + throw new Exception('Key used for whereNull condition must be a string or raw exrpession.', 1); |
|
1149 | + } |
|
1150 | + |
|
1151 | + return $this->{$operator . 'Where'}($this->raw("{$key} IS{$prefix} NULL")); |
|
1152 | + } |
|
1153 | + |
|
1154 | + |
|
1155 | + /** |
|
1156 | + * Runs a transaction |
|
1157 | + * |
|
1158 | + * @param \Closure(Transaction):void $callback |
|
1159 | + * |
|
1160 | + * @return static |
|
1161 | + */ |
|
1162 | + public function transaction(Closure $callback): self |
|
1163 | + { |
|
1164 | + try { |
|
1165 | + // Begin the transaction |
|
1166 | + $this->dbInstance->query('START TRANSACTION'); |
|
1167 | + |
|
1168 | + // Get the Transaction class |
|
1169 | + $transaction = $this->container->build(Transaction::class, [$this->connection]); |
|
1170 | + |
|
1171 | + $this->handleTransactionCall($callback, $transaction); |
|
1172 | + |
|
1173 | + // If no errors have been thrown or the transaction wasn't completed within |
|
1174 | + $this->dbInstance->query('COMMIT'); |
|
1175 | + |
|
1176 | + return $this; |
|
1177 | + } catch (TransactionHaltException $e) { |
|
1178 | + // Commit or rollback behavior has been handled in the closure, so exit |
|
1179 | + return $this; |
|
1180 | + } catch (\Exception $e) { |
|
1181 | + // something happened, rollback changes |
|
1182 | + $this->dbInstance->query('ROLLBACK'); |
|
1183 | + |
|
1184 | + return $this; |
|
1185 | + } |
|
1186 | + } |
|
1187 | + |
|
1188 | + /** |
|
1189 | + * Handles the transaction call. |
|
1190 | + * Catches any WPDB Errors (printed) |
|
1191 | + * |
|
1192 | + * @param Closure $callback |
|
1193 | + * @param Transaction $transaction |
|
1194 | + * |
|
1195 | + * @return void |
|
1196 | + * @throws Exception |
|
1197 | + */ |
|
1198 | + protected function handleTransactionCall(Closure $callback, Transaction $transaction): void |
|
1199 | + { |
|
1200 | + try { |
|
1201 | + ob_start(); |
|
1202 | + $callback($transaction); |
|
1203 | + $output = ob_get_clean() ?: ''; |
|
1204 | + } catch (Throwable $th) { |
|
1205 | + ob_end_clean(); |
|
1206 | + throw $th; |
|
1207 | + } |
|
1208 | + |
|
1209 | + // If we caught an error, throw an exception. |
|
1210 | + if (0 !== mb_strlen($output)) { |
|
1211 | + throw new Exception($output); |
|
1212 | + } |
|
1213 | + } |
|
1214 | + |
|
1215 | + /*************************************************************************/ |
|
1216 | + /*************************************************************************/ |
|
1217 | + /*************************************************************************/ |
|
1218 | + /** JOIN JOIN **/ |
|
1219 | + /** JOIN **/ |
|
1220 | + /** JOIN JOIN **/ |
|
1221 | + /*************************************************************************/ |
|
1222 | + /*************************************************************************/ |
|
1223 | + /*************************************************************************/ |
|
1224 | + |
|
1225 | + /** |
|
1226 | + * @param string|Raw $table |
|
1227 | + * @param string|Raw|Closure $key |
|
1228 | + * @param string|null $operator |
|
1229 | + * @param mixed $value |
|
1230 | + * @param string $type |
|
1231 | + * |
|
1232 | + * @return static |
|
1233 | + */ |
|
1234 | + public function join($table, $key, ?string $operator = null, $value = null, $type = 'inner') |
|
1235 | + { |
|
1236 | + // Potentially cast key from JSON |
|
1237 | + if ($this->jsonHandler->isJsonSelector($key)) { |
|
1238 | + /** @var string $key */ |
|
1239 | + $key = $this->jsonHandler->extractAndUnquoteFromJsonSelector($key); /** @phpstan-ignore-line */ |
|
1240 | + } |
|
1241 | + |
|
1242 | + // Potentially cast value from json |
|
1243 | + if ($this->jsonHandler->isJsonSelector($value)) { |
|
1244 | + /** @var string $value */ |
|
1245 | + $value = $this->jsonHandler->extractAndUnquoteFromJsonSelector($value); |
|
1246 | + } |
|
1247 | + |
|
1248 | + if (!$key instanceof Closure) { |
|
1249 | + $key = function ($joinBuilder) use ($key, $operator, $value) { |
|
1250 | + $joinBuilder->on($key, $operator, $value); |
|
1251 | + }; |
|
1252 | + } |
|
1253 | + |
|
1254 | + // Build a new JoinBuilder class, keep it by reference so any changes made |
|
1255 | + // in the closure should reflect here |
|
1256 | + $joinBuilder = $this->container->build(JoinBuilder::class, [$this->connection]); |
|
1257 | + $joinBuilder = &$joinBuilder; |
|
1258 | + // Call the closure with our new joinBuilder object |
|
1259 | + $key($joinBuilder); |
|
1260 | + $table = $this->addTablePrefix($table, false); |
|
1261 | + // Get the criteria only query from the joinBuilder object |
|
1262 | + $this->statements['joins'][] = compact('type', 'table', 'joinBuilder'); |
|
1263 | + return $this; |
|
1264 | + } |
|
1265 | + |
|
1266 | + /** |
|
1267 | + * @param string|Raw $table |
|
1268 | + * @param string|Raw|Closure $key |
|
1269 | + * @param string|null $operator |
|
1270 | + * @param mixed $value |
|
1271 | + * |
|
1272 | + * @return static |
|
1273 | + */ |
|
1274 | + public function leftJoin($table, $key, $operator = null, $value = null) |
|
1275 | + { |
|
1276 | + return $this->join($table, $key, $operator, $value, 'left'); |
|
1277 | + } |
|
1278 | + |
|
1279 | + /** |
|
1280 | + * @param string|Raw $table |
|
1281 | + * @param string|Raw|Closure $key |
|
1282 | + * @param string|null $operator |
|
1283 | + * @param mixed $value |
|
1284 | + * |
|
1285 | + * @return static |
|
1286 | + */ |
|
1287 | + public function rightJoin($table, $key, $operator = null, $value = null) |
|
1288 | + { |
|
1289 | + return $this->join($table, $key, $operator, $value, 'right'); |
|
1290 | + } |
|
1291 | + |
|
1292 | + /** |
|
1293 | + * @param string|Raw $table |
|
1294 | + * @param string|Raw|Closure $key |
|
1295 | + * @param string|null $operator |
|
1296 | + * @param mixed $value |
|
1297 | + * |
|
1298 | + * @return static |
|
1299 | + */ |
|
1300 | + public function innerJoin($table, $key, $operator = null, $value = null) |
|
1301 | + { |
|
1302 | + return $this->join($table, $key, $operator, $value, 'inner'); |
|
1303 | + } |
|
1304 | + |
|
1305 | + /** |
|
1306 | + * @param string|Raw $table |
|
1307 | + * @param string|Raw|Closure $key |
|
1308 | + * @param string|null $operator |
|
1309 | + * @param mixed $value |
|
1310 | + * |
|
1311 | + * @return static |
|
1312 | + */ |
|
1313 | + public function crossJoin($table, $key, $operator = null, $value = null) |
|
1314 | + { |
|
1315 | + return $this->join($table, $key, $operator, $value, 'cross'); |
|
1316 | + } |
|
1317 | + |
|
1318 | + /** |
|
1319 | + * @param string|Raw $table |
|
1320 | + * @param string|Raw|Closure $key |
|
1321 | + * @param string|null $operator |
|
1322 | + * @param mixed $value |
|
1323 | + * |
|
1324 | + * @return static |
|
1325 | + */ |
|
1326 | + public function outerJoin($table, $key, $operator = null, $value = null) |
|
1327 | + { |
|
1328 | + return $this->join($table, $key, $operator, $value, 'full outer'); |
|
1329 | + } |
|
1330 | + |
|
1331 | + /** |
|
1332 | + * Shortcut to join 2 tables on the same key name with equals |
|
1333 | + * |
|
1334 | + * @param string $table |
|
1335 | + * @param string $key |
|
1336 | + * @param string $type |
|
1337 | + * @return self |
|
1338 | + * @throws Exception If base table is set as more than 1 or 0 |
|
1339 | + */ |
|
1340 | + public function joinUsing(string $table, string $key, string $type = 'INNER'): self |
|
1341 | + { |
|
1342 | + if (!array_key_exists('tables', $this->statements) || count($this->statements['tables']) !== 1) { |
|
1343 | + throw new Exception("JoinUsing can only be used with a single table set as the base of the query", 1); |
|
1344 | + } |
|
1345 | + $baseTable = end($this->statements['tables']); |
|
1346 | + |
|
1347 | + // Potentialy cast key from JSON |
|
1348 | + if ($this->jsonHandler->isJsonSelector($key)) { |
|
1349 | + $key = $this->jsonHandler->extractAndUnquoteFromJsonSelector($key); |
|
1350 | + } |
|
1351 | + |
|
1352 | + $remoteKey = $table = $this->addTablePrefix("{$table}.{$key}", true); |
|
1353 | + $localKey = $table = $this->addTablePrefix("{$baseTable}.{$key}", true); |
|
1354 | + return $this->join($table, $remoteKey, '=', $localKey, $type); |
|
1355 | + } |
|
1356 | + |
|
1357 | + /** |
|
1358 | + * Add a raw query |
|
1359 | + * |
|
1360 | + * @param string|Raw $value |
|
1361 | + * @param mixed|mixed[] $bindings |
|
1362 | + * |
|
1363 | + * @return Raw |
|
1364 | + */ |
|
1365 | + public function raw($value, $bindings = []): Raw |
|
1366 | + { |
|
1367 | + return new Raw($value, $bindings); |
|
1368 | + } |
|
1369 | + |
|
1370 | + /** |
|
1371 | + * Return wpdb instance |
|
1372 | + * |
|
1373 | + * @return wpdb |
|
1374 | + */ |
|
1375 | + public function dbInstance(): wpdb |
|
1376 | + { |
|
1377 | + return $this->dbInstance; |
|
1378 | + } |
|
1379 | + |
|
1380 | + /** |
|
1381 | + * @param Connection $connection |
|
1382 | + * |
|
1383 | + * @return static |
|
1384 | + */ |
|
1385 | + public function setConnection(Connection $connection): self |
|
1386 | + { |
|
1387 | + $this->connection = $connection; |
|
1388 | + |
|
1389 | + return $this; |
|
1390 | + } |
|
1391 | + |
|
1392 | + /** |
|
1393 | + * @return Connection |
|
1394 | + */ |
|
1395 | + public function getConnection(): Connection |
|
1396 | + { |
|
1397 | + return $this->connection; |
|
1398 | + } |
|
1399 | + |
|
1400 | + /** |
|
1401 | + * @param string|Raw|Closure $key |
|
1402 | + * @param string|null $operator |
|
1403 | + * @param mixed|null $value |
|
1404 | + * @param string $joiner |
|
1405 | + * |
|
1406 | + * @return static |
|
1407 | + */ |
|
1408 | + protected function whereHandler($key, $operator = null, $value = null, $joiner = 'AND') |
|
1409 | + { |
|
1410 | + $key = $this->addTablePrefix($key); |
|
1411 | + if ($key instanceof Raw) { |
|
1412 | + $key = $this->adapterInstance->parseRaw($key); |
|
1413 | + } |
|
1414 | + |
|
1415 | + if ($this->jsonHandler->isJsonSelector($key)) { |
|
1416 | + $key = $this->jsonHandler->extractAndUnquoteFromJsonSelector($key); |
|
1417 | + } |
|
1418 | + |
|
1419 | + $this->statements['wheres'][] = compact('key', 'operator', 'value', 'joiner'); |
|
1420 | + return $this; |
|
1421 | + } |
|
1422 | + |
|
1423 | + |
|
1424 | + |
|
1425 | + /** |
|
1426 | + * @param string $key |
|
1427 | + * @param mixed|mixed[]|bool $value |
|
1428 | + * |
|
1429 | + * @return void |
|
1430 | + */ |
|
1431 | + protected function addStatement($key, $value) |
|
1432 | + { |
|
1433 | + if (!is_array($value)) { |
|
1434 | + $value = [$value]; |
|
1435 | + } |
|
1436 | + |
|
1437 | + if (!array_key_exists($key, $this->statements)) { |
|
1438 | + $this->statements[$key] = $value; |
|
1439 | + } else { |
|
1440 | + $this->statements[$key] = array_merge($this->statements[$key], $value); |
|
1441 | + } |
|
1442 | + } |
|
1443 | + |
|
1444 | + /** |
|
1445 | + * @param string $event |
|
1446 | + * @param string|Raw $table |
|
1447 | + * |
|
1448 | + * @return callable|null |
|
1449 | + */ |
|
1450 | + public function getEvent(string $event, $table = ':any'): ?callable |
|
1451 | + { |
|
1452 | + return $this->connection->getEventHandler()->getEvent($event, $table); |
|
1453 | + } |
|
1454 | + |
|
1455 | + /** |
|
1456 | + * @param string $event |
|
1457 | + * @param string|Raw $table |
|
1458 | + * @param Closure $action |
|
1459 | + * |
|
1460 | + * @return void |
|
1461 | + */ |
|
1462 | + public function registerEvent($event, $table, Closure $action): void |
|
1463 | + { |
|
1464 | + $table = $table ?: ':any'; |
|
1465 | + |
|
1466 | + if (':any' != $table) { |
|
1467 | + $table = $this->addTablePrefix($table, false); |
|
1468 | + } |
|
1469 | + |
|
1470 | + $this->connection->getEventHandler()->registerEvent($event, $table, $action); |
|
1471 | + } |
|
1472 | + |
|
1473 | + /** |
|
1474 | + * @param string $event |
|
1475 | + * @param string|Raw $table |
|
1476 | + * |
|
1477 | + * @return void |
|
1478 | + */ |
|
1479 | + public function removeEvent(string $event, $table = ':any') |
|
1480 | + { |
|
1481 | + if (':any' != $table) { |
|
1482 | + $table = $this->addTablePrefix($table, false); |
|
1483 | + } |
|
1484 | + |
|
1485 | + $this->connection->getEventHandler()->removeEvent($event, $table); |
|
1486 | + } |
|
1487 | + |
|
1488 | + /** |
|
1489 | + * @param string $event |
|
1490 | + * |
|
1491 | + * @return mixed |
|
1492 | + */ |
|
1493 | + public function fireEvents(string $event) |
|
1494 | + { |
|
1495 | + $params = func_get_args(); // @todo Replace this with an easier to read alteratnive |
|
1496 | + array_unshift($params, $this); |
|
1497 | + |
|
1498 | + return call_user_func_array([$this->connection->getEventHandler(), 'fireEvents'], $params); |
|
1499 | + } |
|
1500 | + |
|
1501 | + /** |
|
1502 | + * @return array<string, mixed[]> |
|
1503 | + */ |
|
1504 | + public function getStatements() |
|
1505 | + { |
|
1506 | + return $this->statements; |
|
1507 | + } |
|
1508 | + |
|
1509 | + /** |
|
1510 | + * @return string will return WPDB Fetch mode |
|
1511 | + */ |
|
1512 | + public function getFetchMode() |
|
1513 | + { |
|
1514 | + return null !== $this->fetchMode |
|
1515 | + ? $this->fetchMode |
|
1516 | + : \OBJECT; |
|
1517 | + } |
|
1518 | + |
|
1519 | + /** |
|
1520 | + * Returns an NEW instance of the JSON builder populated with the same connection and hydrator details. |
|
1521 | + * |
|
1522 | + * @return JsonQueryBuilder |
|
1523 | + */ |
|
1524 | + public function jsonBuilder(): JsonQueryBuilder |
|
1525 | + { |
|
1526 | + return new JsonQueryBuilder($this->getConnection(), $this->getFetchMode(), $this->hydratorConstructorArgs); |
|
1527 | + } |
|
1528 | 1528 | } |
@@ -232,7 +232,7 @@ discard block |
||
232 | 232 | $start = microtime(true); |
233 | 233 | $sqlStatement = empty($bindings) ? $sql : $this->interpolateQuery($sql, $bindings); |
234 | 234 | |
235 | - if (!is_string($sqlStatement)) { |
|
235 | + if ( ! is_string($sqlStatement)) { |
|
236 | 236 | throw new Exception('Could not interpolate query', 1); |
237 | 237 | } |
238 | 238 | |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | public function get() |
250 | 250 | { |
251 | 251 | $eventResult = $this->fireEvents('before-select'); |
252 | - if (!is_null($eventResult)) { |
|
252 | + if ( ! is_null($eventResult)) { |
|
253 | 253 | return $eventResult; |
254 | 254 | } |
255 | 255 | $executionTime = 0; |
@@ -274,7 +274,7 @@ discard block |
||
274 | 274 | $this->sqlStatement = null; |
275 | 275 | |
276 | 276 | // Ensure we have an array of results. |
277 | - if (!is_array($result) && null !== $result) { |
|
277 | + if ( ! is_array($result) && null !== $result) { |
|
278 | 278 | $result = [$result]; |
279 | 279 | } |
280 | 280 | |
@@ -307,7 +307,7 @@ discard block |
||
307 | 307 | */ |
308 | 308 | protected function useHydrator(): bool |
309 | 309 | { |
310 | - return !in_array($this->getFetchMode(), [\ARRAY_A, \ARRAY_N, \OBJECT, \OBJECT_K]); |
|
310 | + return ! in_array($this->getFetchMode(), [\ARRAY_A, \ARRAY_N, \OBJECT, \OBJECT_K]); |
|
311 | 311 | } |
312 | 312 | |
313 | 313 | /** |
@@ -408,7 +408,7 @@ discard block |
||
408 | 408 | ->select([$this->raw(sprintf('%s(%s) AS field', strtoupper($type), $field))]) |
409 | 409 | ->first(); |
410 | 410 | |
411 | - return true === isset($count->field) ? (float)$count->field : 0; |
|
411 | + return true === isset($count->field) ? (float) $count->field : 0; |
|
412 | 412 | } |
413 | 413 | |
414 | 414 | /** |
@@ -424,7 +424,7 @@ discard block |
||
424 | 424 | */ |
425 | 425 | public function count($field = '*'): int |
426 | 426 | { |
427 | - return (int)$this->aggregate('count', $field); |
|
427 | + return (int) $this->aggregate('count', $field); |
|
428 | 428 | } |
429 | 429 | |
430 | 430 | /** |
@@ -502,8 +502,8 @@ discard block |
||
502 | 502 | public function getQuery(string $type = 'select', $dataToBePassed = []) |
503 | 503 | { |
504 | 504 | $allowedTypes = ['select', 'insert', 'insertignore', 'replace', 'delete', 'update', 'criteriaonly']; |
505 | - if (!in_array(strtolower($type), $allowedTypes)) { |
|
506 | - throw new Exception($type . ' is not a known type.', 2); |
|
505 | + if ( ! in_array(strtolower($type), $allowedTypes)) { |
|
506 | + throw new Exception($type.' is not a known type.', 2); |
|
507 | 507 | } |
508 | 508 | |
509 | 509 | $queryArr = $this->adapterInstance->$type($this->statements, $dataToBePassed); |
@@ -522,9 +522,9 @@ discard block |
||
522 | 522 | */ |
523 | 523 | public function subQuery(QueryBuilderHandler $queryBuilder, ?string $alias = null) |
524 | 524 | { |
525 | - $sql = '(' . $queryBuilder->getQuery()->getRawSql() . ')'; |
|
525 | + $sql = '('.$queryBuilder->getQuery()->getRawSql().')'; |
|
526 | 526 | if (is_string($alias) && 0 !== mb_strlen($alias)) { |
527 | - $sql = $sql . ' as ' . $alias; |
|
527 | + $sql = $sql.' as '.$alias; |
|
528 | 528 | } |
529 | 529 | |
530 | 530 | return $queryBuilder->raw($sql); |
@@ -541,12 +541,12 @@ discard block |
||
541 | 541 | private function doInsert(array $data, string $type) |
542 | 542 | { |
543 | 543 | $eventResult = $this->fireEvents('before-insert'); |
544 | - if (!is_null($eventResult)) { |
|
544 | + if ( ! is_null($eventResult)) { |
|
545 | 545 | return $eventResult; |
546 | 546 | } |
547 | 547 | |
548 | 548 | // If first value is not an array () not a batch insert) |
549 | - if (!is_array(current($data))) { |
|
549 | + if ( ! is_array(current($data))) { |
|
550 | 550 | $queryObject = $this->getQuery($type, $data); |
551 | 551 | |
552 | 552 | list($preparedQuery, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); |
@@ -616,7 +616,7 @@ discard block |
||
616 | 616 | public function update(array $data): ?int |
617 | 617 | { |
618 | 618 | $eventResult = $this->fireEvents('before-update'); |
619 | - if (!is_null($eventResult)) { |
|
619 | + if ( ! is_null($eventResult)) { |
|
620 | 620 | return $eventResult; |
621 | 621 | } |
622 | 622 | $queryObject = $this->getQuery('update', $data); |
@@ -669,7 +669,7 @@ discard block |
||
669 | 669 | public function delete() |
670 | 670 | { |
671 | 671 | $eventResult = $this->fireEvents('before-delete'); |
672 | - if (!is_null($eventResult)) { |
|
672 | + if ( ! is_null($eventResult)) { |
|
673 | 673 | return $eventResult; |
674 | 674 | } |
675 | 675 | |
@@ -691,7 +691,7 @@ discard block |
||
691 | 691 | */ |
692 | 692 | public function table(...$tables) |
693 | 693 | { |
694 | - $instance = $this->constructCurrentBuilderClass($this->connection); |
|
694 | + $instance = $this->constructCurrentBuilderClass($this->connection); |
|
695 | 695 | $this->setFetchMode($this->getFetchMode(), $this->hydratorConstructorArgs); |
696 | 696 | $tables = $this->addTablePrefix($tables, false); |
697 | 697 | $instance->addStatement('tables', $tables); |
@@ -719,7 +719,7 @@ discard block |
||
719 | 719 | */ |
720 | 720 | public function select($fields): self |
721 | 721 | { |
722 | - if (!is_array($fields)) { |
|
722 | + if ( ! is_array($fields)) { |
|
723 | 723 | $fields = func_get_args(); |
724 | 724 | } |
725 | 725 | |
@@ -780,7 +780,7 @@ discard block |
||
780 | 780 | */ |
781 | 781 | public function orderBy($fields, string $defaultDirection = 'ASC'): self |
782 | 782 | { |
783 | - if (!is_array($fields)) { |
|
783 | + if ( ! is_array($fields)) { |
|
784 | 784 | $fields = [$fields]; |
785 | 785 | } |
786 | 786 | |
@@ -796,7 +796,7 @@ discard block |
||
796 | 796 | $field = $this->jsonHandler->extractAndUnquoteFromJsonSelector($field); |
797 | 797 | } |
798 | 798 | |
799 | - if (!$field instanceof Raw) { |
|
799 | + if ( ! $field instanceof Raw) { |
|
800 | 800 | $field = $this->addTablePrefix($field); |
801 | 801 | } |
802 | 802 | $this->statements['orderBys'][] = compact('field', 'type'); |
@@ -1148,7 +1148,7 @@ discard block |
||
1148 | 1148 | throw new Exception('Key used for whereNull condition must be a string or raw exrpession.', 1); |
1149 | 1149 | } |
1150 | 1150 | |
1151 | - return $this->{$operator . 'Where'}($this->raw("{$key} IS{$prefix} NULL")); |
|
1151 | + return $this->{$operator.'Where'}($this->raw("{$key} IS{$prefix} NULL")); |
|
1152 | 1152 | } |
1153 | 1153 | |
1154 | 1154 | |
@@ -1245,8 +1245,8 @@ discard block |
||
1245 | 1245 | $value = $this->jsonHandler->extractAndUnquoteFromJsonSelector($value); |
1246 | 1246 | } |
1247 | 1247 | |
1248 | - if (!$key instanceof Closure) { |
|
1249 | - $key = function ($joinBuilder) use ($key, $operator, $value) { |
|
1248 | + if ( ! $key instanceof Closure) { |
|
1249 | + $key = function($joinBuilder) use ($key, $operator, $value) { |
|
1250 | 1250 | $joinBuilder->on($key, $operator, $value); |
1251 | 1251 | }; |
1252 | 1252 | } |
@@ -1339,7 +1339,7 @@ discard block |
||
1339 | 1339 | */ |
1340 | 1340 | public function joinUsing(string $table, string $key, string $type = 'INNER'): self |
1341 | 1341 | { |
1342 | - if (!array_key_exists('tables', $this->statements) || count($this->statements['tables']) !== 1) { |
|
1342 | + if ( ! array_key_exists('tables', $this->statements) || count($this->statements['tables']) !== 1) { |
|
1343 | 1343 | throw new Exception("JoinUsing can only be used with a single table set as the base of the query", 1); |
1344 | 1344 | } |
1345 | 1345 | $baseTable = end($this->statements['tables']); |
@@ -1430,11 +1430,11 @@ discard block |
||
1430 | 1430 | */ |
1431 | 1431 | protected function addStatement($key, $value) |
1432 | 1432 | { |
1433 | - if (!is_array($value)) { |
|
1433 | + if ( ! is_array($value)) { |
|
1434 | 1434 | $value = [$value]; |
1435 | 1435 | } |
1436 | 1436 | |
1437 | - if (!array_key_exists($key, $this->statements)) { |
|
1437 | + if ( ! array_key_exists($key, $this->statements)) { |
|
1438 | 1438 | $this->statements[$key] = $value; |
1439 | 1439 | } else { |
1440 | 1440 | $this->statements[$key] = array_merge($this->statements[$key], $value); |