|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace System\Database\MyQuery\Traits; |
| 6 | + |
| 7 | +use System\Database\MyQuery\Select; |
| 8 | + |
| 9 | +/** |
| 10 | + * Sub where query trait. |
| 11 | + */ |
| 12 | +trait SubQueryTrait |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Add sub query to where statement. |
| 16 | + */ |
| 17 | + public function whereClause(string $clause, Select $select): self |
| 18 | + { |
| 19 | + $binds = (fn () => $this->{'_binds'})->call($select); |
| 20 | + $this->_where[] = implode(' ', [$clause, '(', (string) $select, ')']); |
| 21 | + foreach ($binds as $bind) { |
| 22 | + $this->_binds[] = $bind; |
| 23 | + } |
| 24 | + |
| 25 | + return $this; |
| 26 | + } |
| 27 | + |
| 28 | + public function whereCompare(string $column_name, string $operator, Select $select): self |
| 29 | + { |
| 30 | + return $this->whereClause($column_name . ' ' . $operator, $select); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Added 'where exists' condition (query bulider). |
| 35 | + * |
| 36 | + * @param Select $select Select class |
| 37 | + * |
| 38 | + * @return self |
| 39 | + */ |
| 40 | + public function whereExist(Select $select) |
| 41 | + { |
| 42 | + return $this->whereClause('EXISTS', $select); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Added 'where not exists' condition (query bulider). |
| 47 | + * |
| 48 | + * @param Select $select Select class |
| 49 | + * |
| 50 | + * @return self |
| 51 | + */ |
| 52 | + public function whereNotExist(Select $select) |
| 53 | + { |
| 54 | + return $this->whereClause('NOT EXISTS', $select); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Added 'where equal' condition (query bulider). |
| 59 | + */ |
| 60 | + public function whereEqual(string $column_name, Select $select): self |
| 61 | + { |
| 62 | + return $this->whereClause($column_name . ' =', $select); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Added 'where like' condition (query bulider). |
| 67 | + */ |
| 68 | + public function whereLike(string $column_name, Select $select): self |
| 69 | + { |
| 70 | + return $this->whereClause($column_name . ' LIKE', $select); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Added 'where in' condition (query bulider). |
| 75 | + */ |
| 76 | + public function whereIn(string $column_name, Select $select): self |
| 77 | + { |
| 78 | + return $this->whereClause($column_name . ' IN', $select); |
| 79 | + } |
| 80 | +} |
0 commit comments