1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soupmix; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class SQLQueryBuilder extends AbstractQueryBuilder |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
private $queryBuilder = null; |
10
|
|
|
|
11
|
2 |
|
public function run() { |
12
|
2 |
|
$this->queryBuilder = $this->getQueryBuilder(); |
13
|
2 |
|
$this->setJoins(); |
14
|
2 |
|
$count = $this->getCount(); |
15
|
2 |
|
if (!isset($count[0]['total']) || ($count[0]['total']==0)) { |
16
|
|
|
return ['total' => 0, 'data' => null]; |
17
|
|
|
} |
18
|
2 |
|
$numberOfRows = $count[0]['total']; |
19
|
2 |
|
$this->setSortOrders(); |
20
|
2 |
|
$this->setOffsetAndLimit(); |
21
|
2 |
|
$this->setReturnFields(); |
22
|
2 |
|
$stmt = $this->conn->executeQuery( |
23
|
2 |
|
$this->queryBuilder->getSql(), |
24
|
2 |
|
$this->queryBuilder->getParameters() |
25
|
2 |
|
); |
26
|
2 |
|
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
27
|
2 |
|
if ($this->distinctFieldName !== null) { |
28
|
1 |
|
$numberOfRows = count($result); |
29
|
1 |
|
} |
30
|
2 |
|
return ['total' => $numberOfRows, 'data' => $result]; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
private function getQueryBuilder() |
34
|
|
|
{ |
35
|
2 |
|
if ($this->orFilters !== null) { |
36
|
2 |
|
$this->andFilters[] = $this->orFilters; |
37
|
2 |
|
} |
38
|
2 |
|
$this->filters = $this->andFilters; |
39
|
2 |
|
return $this->buildQuery($this->collection, $this->filters); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
private function getCount() |
43
|
|
|
{ |
44
|
2 |
|
$queryBuilderCount = clone $this->queryBuilder; |
45
|
2 |
|
$queryBuilderCount->select(" COUNT(*) AS total "); |
46
|
2 |
|
$stmt = $this->conn->executeQuery($queryBuilderCount->getSql(), $queryBuilderCount->getParameters()); |
47
|
2 |
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
private function setSortOrders() |
51
|
|
|
{ |
52
|
2 |
|
if ($this->sortFields !== null) { |
53
|
|
|
foreach ($this->addAlias($this->sortFields) as $sortKey => $sortDir) { |
|
|
|
|
54
|
|
|
$this->queryBuilder->addOrderBy($sortKey, $sortDir); |
55
|
|
|
} |
56
|
|
|
} |
57
|
2 |
|
} |
58
|
|
|
|
59
|
2 |
|
private function setJoins() |
60
|
|
|
{ |
61
|
2 |
|
$this->setJoinsForType('innerJoin'); |
62
|
2 |
|
$this->setJoinsForType('leftJoin'); |
63
|
2 |
|
$this->setJoinsForType('rightJoin'); |
64
|
2 |
|
$this->setJoinsForType('outerJoin'); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
2 |
|
private function setJoinsForType($joinType) |
68
|
|
|
{ |
69
|
2 |
|
if (is_null($this->{$joinType})) { |
70
|
2 |
|
return; |
71
|
|
|
} |
72
|
1 |
|
foreach ($this->{$joinType} as $collectionName => $collection) { |
73
|
1 |
|
$fieldNames = $this->addAlias($collection['returnFields'], $collectionName); |
74
|
1 |
|
$this->returnFieldsForJoin($fieldNames); |
|
|
|
|
75
|
1 |
|
$joinCondition = ''; |
76
|
1 |
|
foreach ($collection['relations'] as $relation) { |
77
|
1 |
|
$joinCondition .= ($joinCondition=='') ? '':' AND '; |
78
|
1 |
|
$relationType = array_keys($relation)[0]; |
79
|
1 |
|
$source = array_keys($relation[$relationType])[0]; |
80
|
1 |
|
$condition = $collectionName . "." . $source |
81
|
1 |
|
. " = " . $this->collection . "." . $relation[$relationType][$source]; |
82
|
1 |
|
if($relationType != 'field') { |
83
|
1 |
|
$condition = $collectionName . "." . $source . " " |
84
|
1 |
|
. $relationType . " " . $relation[$relationType][$source]; |
85
|
1 |
|
} |
86
|
1 |
|
$joinCondition .= $condition; |
87
|
1 |
|
} |
88
|
1 |
|
$this->queryBuilder->$joinType($this->collection, $collectionName, $collectionName, $joinCondition); |
89
|
|
|
|
90
|
1 |
|
} |
91
|
1 |
|
return $this; |
92
|
|
|
} |
93
|
1 |
|
public function returnFieldsForJoin(array $fieldNames=null) |
94
|
|
|
{ |
95
|
1 |
|
if($fieldNames !== null ) { |
96
|
1 |
|
foreach ($fieldNames as $fieldName) { |
97
|
1 |
|
$this->fieldNames[] = $fieldName; |
98
|
1 |
|
} |
99
|
1 |
|
} |
100
|
1 |
|
} |
101
|
|
|
|
102
|
2 |
|
private function setReturnFields() |
103
|
|
|
{ |
104
|
2 |
|
if ($this->distinctFieldName === null) { |
105
|
2 |
|
$fieldNames = ($this->fieldNames === null) ? $this->addAlias("*") : $this->addAlias($this->fieldNames); |
106
|
2 |
|
$this->queryBuilder->select($fieldNames); |
107
|
2 |
|
return; |
108
|
|
|
} |
109
|
1 |
|
$this->queryBuilder->select('DISTINCT (`' . $this->collection . "`.`" . $this->distinctFieldName . '`)'); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
2 |
|
private function setOffsetAndLimit() |
113
|
|
|
{ |
114
|
2 |
|
$this->queryBuilder->setFirstResult($this->offset) |
115
|
2 |
|
->setMaxResults($this->limit); |
116
|
2 |
|
} |
117
|
|
|
|
118
|
2 |
|
private function addAlias($fields, $collection=null) |
119
|
|
|
{ |
120
|
2 |
|
$collection = (!is_null($collection)) ? $collection : $this->collection; |
|
|
|
|
121
|
2 |
|
if (!is_array($fields)) { |
122
|
1 |
|
return $collection . "." . $fields; |
123
|
|
|
} |
124
|
1 |
|
if (!is_array($fields)) { |
125
|
|
|
return $collection . "." . $fields; |
126
|
|
|
} |
127
|
1 |
|
$newFields = []; |
128
|
1 |
|
foreach ($fields as $field => $value){ |
129
|
1 |
|
if(is_int($field)){ |
130
|
1 |
|
if (!is_array($fields)) { |
131
|
|
|
$newFields[] = $collection . "." . $fields; |
132
|
|
|
continue; |
133
|
|
|
} |
134
|
1 |
|
$newFields[] = $collection . "." . $value; |
135
|
1 |
|
continue; |
136
|
|
|
} |
137
|
|
|
$newFields[$collection.".".$field] = $value; |
138
|
1 |
|
} |
139
|
1 |
|
return $newFields; |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
protected function buildQuery($collection, $filters) |
143
|
|
|
{ |
144
|
2 |
|
$queryBuilder = $this->conn->createQueryBuilder(); |
145
|
2 |
|
$queryBuilder->from($collection, $collection); |
146
|
2 |
|
if ($filters === null) { |
147
|
|
|
return $queryBuilder; |
148
|
|
|
} |
149
|
2 |
|
return $this->buildQueryFilters($queryBuilder, $filters); |
150
|
|
|
} |
151
|
|
|
|
152
|
2 |
|
protected function buildQueryFilters($queryBuilder, $filters) |
153
|
|
|
{ |
154
|
2 |
|
foreach ($filters as $key => $value) { |
155
|
2 |
|
if (strpos($key, '__') === false && is_array($value)) { |
156
|
2 |
|
$queryBuilder = $this->buildQueryForOr($queryBuilder, $value); |
|
|
|
|
157
|
2 |
|
continue; |
158
|
|
|
} |
159
|
2 |
|
$queryBuilder = $this->buildQueryForAnd($queryBuilder, $key, $value); |
|
|
|
|
160
|
2 |
|
} |
161
|
2 |
|
return $queryBuilder; |
162
|
|
|
} |
163
|
|
|
|
164
|
2 |
|
protected function buildQueryForAnd($queryBuilder, $key, $value) |
165
|
|
|
{ |
166
|
2 |
|
$sqlOptions = self::buildFilter([$key => $value]); |
167
|
2 |
View Code Duplication |
if (in_array($sqlOptions['method'], ['in', 'notIn'])) { |
|
|
|
|
168
|
|
|
$queryBuilder->andWhere( |
169
|
|
|
$queryBuilder->expr()->{$sqlOptions['method']}($this->collection . "." . $sqlOptions['key'], $sqlOptions['value']) |
170
|
|
|
); |
171
|
|
|
return $queryBuilder; |
172
|
|
|
} |
173
|
2 |
|
$queryBuilder->andWhere( |
174
|
2 |
|
'`' . $this->collection . "`.`" . $sqlOptions['key'].'`' |
175
|
2 |
|
. ' ' . $sqlOptions['operand'] |
176
|
2 |
|
. ' ' . $queryBuilder->createNamedParameter($sqlOptions['value']) |
177
|
2 |
|
); |
178
|
2 |
|
return $queryBuilder; |
179
|
|
|
} |
180
|
2 |
|
protected function buildQueryForOr($queryBuilder, $value) |
181
|
|
|
{ |
182
|
2 |
|
$orQuery =[]; |
183
|
2 |
|
foreach ($value as $orValue) { |
184
|
2 |
|
$subKey = array_keys($orValue)[0]; |
185
|
2 |
|
$subValue = $orValue[$subKey]; |
186
|
2 |
|
$sqlOptions = self::buildFilter([$subKey => $subValue]); |
187
|
2 |
View Code Duplication |
if (in_array($sqlOptions['method'], ['in', 'notIn'])) { |
|
|
|
|
188
|
1 |
|
$orQuery[] = $queryBuilder->expr()->{$sqlOptions['method']}($this->collection . "." . $sqlOptions['key'], $sqlOptions['value']); |
189
|
1 |
|
continue; |
190
|
|
|
} |
191
|
2 |
|
$orQuery[] = |
192
|
2 |
|
'`' . $this->collection . "`.`" . $sqlOptions['key'].'`' |
193
|
2 |
|
. ' ' . $sqlOptions['operand'] |
194
|
2 |
|
. ' ' . $queryBuilder->createNamedParameter($sqlOptions['value']); |
195
|
2 |
|
} |
196
|
2 |
|
$queryBuilder->andWhere( |
197
|
2 |
|
'(' . implode(' OR ', $orQuery) . ')' |
198
|
2 |
|
); |
199
|
2 |
|
return $queryBuilder; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
2 |
|
public static function buildFilter($filter) |
204
|
|
|
{ |
205
|
2 |
|
$key = array_keys($filter)[0]; |
206
|
2 |
|
$value = $filter[$key]; |
207
|
2 |
|
$operator = ' = '; |
208
|
2 |
|
$method = 'eq'; |
209
|
|
|
$options =[ |
210
|
2 |
|
'gte' => ['method' => 'gte', 'operand' => ' >= '], |
211
|
2 |
|
'gt' => ['method' => 'gt', 'operand' => ' > '], |
212
|
2 |
|
'lte' => ['method' => 'lte', 'operand' => ' <= '], |
213
|
2 |
|
'lt' => ['method' => 'lt', 'operand' => ' < '], |
214
|
2 |
|
'in' => ['method' => 'in', 'operand' => ' IN '], |
215
|
2 |
|
'!in' => ['method' => 'notIn', 'operand' => ' NOT IN '], |
216
|
2 |
|
'not' => ['method' => 'not', 'operand' => ' NOT '], |
217
|
2 |
|
'wildcard' => ['method' => 'like', 'operand' => ' LIKE '], |
218
|
2 |
|
'prefix' => ['method' => 'like', 'operand' => ' LIKE '], |
219
|
2 |
|
]; |
220
|
2 |
|
if (strpos($key, '__') !== false) { |
221
|
2 |
|
preg_match('/__(.*?)$/i', $key, $matches); |
222
|
2 |
|
$key = str_replace($matches[0], '', $key); |
223
|
2 |
|
$queryOperator = $matches[1]; |
224
|
2 |
|
$method = $options[$queryOperator]['method']; |
225
|
2 |
|
$operator = $options[$queryOperator]['operand']; |
226
|
|
|
switch ($queryOperator) { |
227
|
2 |
|
case 'wildcard': |
228
|
1 |
|
$value = '%'.str_replace(array('?', '*'), array('_', '%'), $value).'%'; |
229
|
1 |
|
break; |
230
|
2 |
|
case 'prefix': |
231
|
1 |
|
$value = $value.'%'; |
232
|
1 |
|
break; |
233
|
|
|
} |
234
|
2 |
|
} |
235
|
|
|
return [ |
236
|
2 |
|
'key' => $key, |
237
|
2 |
|
'operand' => $operator, |
238
|
2 |
|
'method' => $method, |
239
|
|
|
'value' => $value |
240
|
2 |
|
]; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.