tsukasa-mixer /
QueryBuilder
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Tsukasa\QueryBuilder; |
||||||
| 4 | |||||||
| 5 | use Tsukasa\QueryBuilder\Expression\Expression; |
||||||
| 6 | use Tsukasa\QueryBuilder\Interfaces\IAdapter; |
||||||
| 7 | use Tsukasa\QueryBuilder\Interfaces\ILookupCollection; |
||||||
| 8 | use Tsukasa\QueryBuilder\Interfaces\ISQLGenerator; |
||||||
| 9 | |||||||
| 10 | class BaseLookupCollection implements ILookupCollection |
||||||
| 11 | { |
||||||
| 12 | /** |
||||||
| 13 | * @param $lookup |
||||||
| 14 | * @return bool |
||||||
| 15 | */ |
||||||
| 16 | public function has($lookup) |
||||||
| 17 | { |
||||||
| 18 | return in_array(strtolower($lookup), [ |
||||||
| 19 | 'exact', 'gte', 'gt', 'lte', 'lt', |
||||||
| 20 | 'range', 'isnt', 'isnull', 'contains', |
||||||
| 21 | 'icontains', 'startswith', 'istartswith', |
||||||
| 22 | 'endswith', 'iendswith', 'in', 'raw' |
||||||
| 23 | ]); |
||||||
| 24 | } |
||||||
| 25 | |||||||
| 26 | /** |
||||||
| 27 | * @param IAdapter|ISQLGenerator $adapter |
||||||
| 28 | * @param $lookup |
||||||
| 29 | * @param $column |
||||||
| 30 | * @param $value |
||||||
| 31 | * @return string |
||||||
| 32 | */ |
||||||
| 33 | public function process(IAdapter $adapter, $lookup, $column, $value) |
||||||
| 34 | { |
||||||
| 35 | switch (strtolower($lookup)) { |
||||||
| 36 | case 'exact': |
||||||
| 37 | if ($value instanceof \DateTime) { |
||||||
| 38 | $value = $adapter->getDateTime($value); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 39 | } |
||||||
| 40 | |||||||
| 41 | if ($value instanceof Expression) { |
||||||
| 42 | $sqlValue = $value->toSQL(); |
||||||
| 43 | } |
||||||
| 44 | else if ($value instanceof QueryBuilder) { |
||||||
| 45 | $sqlValue = '(' . $value->toSQL() . ')'; |
||||||
| 46 | } |
||||||
| 47 | else if (strpos($value, 'SELECT') !== false) { |
||||||
| 48 | $sqlValue = '(' . $value . ')'; |
||||||
| 49 | } |
||||||
| 50 | else { |
||||||
| 51 | $sqlValue = $adapter->quoteValue($value); |
||||||
| 52 | } |
||||||
| 53 | return $adapter->quoteColumn($column) . '=' . $sqlValue; |
||||||
| 54 | |||||||
| 55 | case 'gte': |
||||||
| 56 | if ($value instanceof \DateTime) { |
||||||
| 57 | $value = $adapter->getDateTime($value); |
||||||
| 58 | } |
||||||
| 59 | return $adapter->quoteColumn($column) . '>=' . $adapter->quoteValue($value); |
||||||
| 60 | |||||||
| 61 | case 'gt': |
||||||
| 62 | if ($value instanceof \DateTime) { |
||||||
| 63 | $value = $adapter->getDateTime($value); |
||||||
| 64 | } |
||||||
| 65 | return $adapter->quoteColumn($column) . '>' . $adapter->quoteValue($value); |
||||||
| 66 | |||||||
| 67 | case 'lte': |
||||||
| 68 | if ($value instanceof \DateTime) { |
||||||
| 69 | $value = $adapter->getDateTime($value); |
||||||
| 70 | } |
||||||
| 71 | return $adapter->quoteColumn($column) . '<=' . $adapter->quoteValue($value); |
||||||
| 72 | |||||||
| 73 | case 'lt': |
||||||
| 74 | if ($value instanceof \DateTime) { |
||||||
| 75 | $value = $adapter->getDateTime($value); |
||||||
| 76 | } |
||||||
| 77 | return $adapter->quoteColumn($column) . '<' . $adapter->quoteValue($value); |
||||||
| 78 | |||||||
| 79 | case 'range': |
||||||
| 80 | list($min, $max) = $value; |
||||||
| 81 | return $adapter->quoteColumn($column) . ' BETWEEN ' . $adapter->quoteValue($min) . ' AND ' . $adapter->quoteValue($max); |
||||||
| 82 | |||||||
| 83 | case 'isnt': |
||||||
| 84 | if (in_array($adapter->getSqlType($value), ['TRUE', 'FALSE', 'NULL'])) { |
||||||
|
0 ignored issues
–
show
The method
getSqlType() does not exist on Tsukasa\QueryBuilder\Interfaces\IAdapter. Since it exists in all sub-types, consider adding an abstract or default implementation to Tsukasa\QueryBuilder\Interfaces\IAdapter.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 85 | return $adapter->quoteColumn($column) . ' IS NOT ' . $adapter->getSqlType($value); |
||||||
| 86 | } |
||||||
| 87 | |||||||
| 88 | return $adapter->quoteColumn($column) . '!=' . $adapter->quoteValue($value); |
||||||
| 89 | |||||||
| 90 | case 'isnull': |
||||||
| 91 | return $adapter->quoteColumn($column) . ' ' . ((bool)$value ? 'IS NULL' : 'IS NOT NULL'); |
||||||
| 92 | |||||||
| 93 | case 'contains': |
||||||
| 94 | return $adapter->quoteColumn($column) . ' LIKE ' . $adapter->quoteValue('%' . $value . '%'); |
||||||
| 95 | |||||||
| 96 | case 'icontains': |
||||||
| 97 | return 'LOWER(' . $adapter->quoteColumn($column) . ') LIKE ' . $adapter->quoteValue('%' . mb_strtolower($value, 'UTF-8') . '%'); |
||||||
| 98 | |||||||
| 99 | case 'startswith': |
||||||
| 100 | return $adapter->quoteColumn($column) . ' LIKE ' . $adapter->quoteValue($value . '%'); |
||||||
| 101 | |||||||
| 102 | case 'istartswith': |
||||||
| 103 | return 'LOWER(' . $adapter->quoteColumn($column) . ') LIKE ' . $adapter->quoteValue(mb_strtolower($value, 'UTF-8') . '%'); |
||||||
| 104 | |||||||
| 105 | case 'endswith': |
||||||
| 106 | return $adapter->quoteColumn($column) . ' LIKE ' . $adapter->quoteValue('%' . $value); |
||||||
| 107 | |||||||
| 108 | case 'iendswith': |
||||||
| 109 | return 'LOWER(' . $adapter->quoteColumn($column) . ') LIKE ' . $adapter->quoteValue('%' . mb_strtolower($value, 'UTF-8')); |
||||||
| 110 | |||||||
| 111 | case 'in': |
||||||
| 112 | if (is_array($value)) { |
||||||
| 113 | $quotedValues = array_map(function($item) use ($adapter) { |
||||||
| 114 | return $adapter->quoteValue($item); |
||||||
| 115 | }, $value); |
||||||
| 116 | $sqlValue = implode(', ', $quotedValues); |
||||||
| 117 | } |
||||||
| 118 | else if ($value instanceof QueryBuilder) { |
||||||
| 119 | $sqlValue = $value->toSQL(); |
||||||
| 120 | } |
||||||
| 121 | else { |
||||||
| 122 | $sqlValue = $adapter->quoteSql($value); |
||||||
|
0 ignored issues
–
show
The method
quoteSql() does not exist on Tsukasa\QueryBuilder\Interfaces\IAdapter. Since it exists in all sub-types, consider adding an abstract or default implementation to Tsukasa\QueryBuilder\Interfaces\IAdapter.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 123 | } |
||||||
| 124 | return $adapter->quoteColumn($column) . ' IN (' . $sqlValue . ')'; |
||||||
| 125 | |||||||
| 126 | case 'raw': |
||||||
| 127 | return $adapter->quoteColumn($column) . ' ' . $adapter->quoteSql($value); |
||||||
| 128 | |||||||
| 129 | default: |
||||||
| 130 | return null; |
||||||
| 131 | } |
||||||
| 132 | } |
||||||
| 133 | } |