1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Database\Query\Expression; |
9
|
|
|
|
10
|
|
|
trait HandlesAqlGrammar |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Available predicate operators. |
14
|
|
|
* |
15
|
|
|
* @var array<string, int> |
16
|
|
|
*/ |
17
|
|
|
protected array $comparisonOperators = [ |
18
|
|
|
'==' => 1, |
19
|
|
|
'!=' => 1, |
20
|
|
|
'<' => 1, |
21
|
|
|
'>' => 1, |
22
|
|
|
'<=' => 1, |
23
|
|
|
'>=' => 1, |
24
|
|
|
'IN' => 1, |
25
|
|
|
'NOT IN' => 1, |
26
|
|
|
'LIKE' => 1, |
27
|
|
|
'~' => 1, |
28
|
|
|
'!~' => 1, |
29
|
|
|
'ALL ==' => 1, |
30
|
|
|
'ALL !=' => 1, |
31
|
|
|
'ALL <' => 1, |
32
|
|
|
'ALL >' => 1, |
33
|
|
|
'ALL <=' => 1, |
34
|
|
|
'ALL >=' => 1, |
35
|
|
|
'ALL IN' => 1, |
36
|
|
|
'ANY ==' => 1, |
37
|
|
|
'ANY !=' => 1, |
38
|
|
|
'ANY <' => 1, |
39
|
|
|
'ANY >' => 1, |
40
|
|
|
'ANY <=' => 1, |
41
|
|
|
'ANY >=' => 1, |
42
|
|
|
'ANY IN' => 1, |
43
|
|
|
'NONE ==' => 1, |
44
|
|
|
'NONE !=' => 1, |
45
|
|
|
'NONE <' => 1, |
46
|
|
|
'NONE >' => 1, |
47
|
|
|
'NONE <=' => 1, |
48
|
|
|
'NONE >=' => 1, |
49
|
|
|
'NONE IN' => 1, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array|int[] |
54
|
|
|
*/ |
55
|
|
|
protected array $arithmeticOperators = [ |
56
|
|
|
'+' => 1, |
57
|
|
|
'-' => 1, |
58
|
|
|
'*' => 1, |
59
|
|
|
'/' => 1, |
60
|
|
|
'%' => 1, |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array|int[] |
65
|
|
|
*/ |
66
|
|
|
protected array $logicalOperators = [ |
67
|
|
|
'AND' => 1, |
68
|
|
|
'&&' => 1, |
69
|
|
|
'OR' => 1, |
70
|
|
|
'||' => 1, |
71
|
|
|
'NOT' => 1, |
72
|
|
|
'!' => 1, |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
protected string $rangeOperator = '..'; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the format for database stored dates. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getDateFormat(): string |
83
|
|
|
{ |
84
|
|
|
return 'Y-m-d\TH:i:s.vp'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function isBind($value): bool |
88
|
|
|
{ |
89
|
|
|
if (is_string($value) && preg_match('/^@?[0-9]{4}_' . json_encode($value) . '_[0-9_]+$/', $value)) { |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the appropriate query parameter place-holder for a value. |
98
|
|
|
* |
99
|
|
|
* @param mixed $value |
100
|
|
|
*/ |
101
|
|
|
public function parameter($value): string |
102
|
|
|
{ |
103
|
|
|
return $this->isExpression($value) ? $this->getValue($value) : (string) $value; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Quote the given string literal. |
109
|
|
|
* |
110
|
|
|
* @param string|array $value |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function quoteString($value) |
114
|
|
|
{ |
115
|
|
|
if (is_array($value)) { |
116
|
|
|
return implode(', ', array_map([$this, __FUNCTION__], $value)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return "`$value`"; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Wrap a value in keyword identifiers. |
125
|
|
|
* |
126
|
|
|
* @param Array<mixed>|Expression|string $value |
127
|
|
|
* @param bool $prefixAlias |
128
|
|
|
* @return string|array |
129
|
|
|
* |
130
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
131
|
|
|
*/ |
132
|
|
|
public function wrap($value, $prefixAlias = false) |
133
|
|
|
{ |
134
|
|
|
if ($this->isExpression($value)) { |
135
|
|
|
return $this->getValue($value); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (is_array($value)) { |
139
|
|
|
foreach($value as $key => $subvalue) { |
140
|
|
|
$value[$key] = $this->wrap($subvalue, $prefixAlias); |
141
|
|
|
} |
142
|
|
|
return $value; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// If the value being wrapped has a column alias we will need to separate out |
146
|
|
|
// the pieces so we can wrap each of the segments of the expression on its |
147
|
|
|
// own, and then join these both back together using the "as" connector. |
148
|
|
|
if (is_string($value) && stripos($value, ' as ') !== false) { |
149
|
|
|
return $this->wrapAliasedValue($value, $prefixAlias); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->wrapSegments(explode('.', $value)); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Wrap a table in keyword identifiers. |
158
|
|
|
* |
159
|
|
|
* @param \Illuminate\Database\Query\Expression|string $table |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
public function wrapTable($table) |
163
|
|
|
{ |
164
|
|
|
if (!$this->isExpression($table)) { |
165
|
|
|
return $this->wrap($this->tablePrefix . $table, true); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this->getValue($table); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Wrap a single string in keyword identifiers. |
173
|
|
|
* |
174
|
|
|
* @param string $value |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
protected function wrapValue($value) |
178
|
|
|
{ |
179
|
|
|
$postfix = ''; |
180
|
|
|
if ($value === 'groupsVariable') { |
181
|
|
|
$postfix = '[*]'; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if ($value === '*') { |
185
|
|
|
return $value; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return '`' . str_replace('`', '``', $value) . '`' . $postfix; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Wrap a subquery single string in braces. |
193
|
|
|
*/ |
194
|
|
|
public function wrapSubquery(string $subquery): string |
195
|
|
|
{ |
196
|
|
|
return '(' . $subquery . ')'; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function generateAqlObject(array $data): string |
200
|
|
|
{ |
201
|
|
|
$data = Arr::undot($data); |
202
|
|
|
|
203
|
|
|
return $this->generateAqlObjectString($data); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
protected function generateAqlObjectString(array $data): string |
207
|
|
|
{ |
208
|
|
|
foreach($data as $key => $value) { |
209
|
|
|
$prefix = $key . ': '; |
210
|
|
|
if (is_numeric($key)) { |
211
|
|
|
$prefix = ''; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if (is_array($value)) { |
215
|
|
|
$data[$key] = $prefix . $this->generateAqlObjectString($value); |
216
|
|
|
continue; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if ($value instanceof Expression) { |
220
|
|
|
$data[$key] = $prefix . $value->getValue($this); |
|
|
|
|
221
|
|
|
continue; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$data[$key] = $prefix . $value; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$returnString = implode(', ', $data); |
228
|
|
|
|
229
|
|
|
if (array_is_list($data)) { |
230
|
|
|
return '[' . $returnString . ']'; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return '{' . $returnString . '}'; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Substitute the given bindings into the given raw AQL query. |
238
|
|
|
* |
239
|
|
|
* @param string $sql |
240
|
|
|
* @param array $bindings |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
public function substituteBindingsIntoRawSql($sql, $bindings) |
244
|
|
|
{ |
245
|
|
|
$bindings = array_map(fn($value) => $this->escape($value), $bindings); |
|
|
|
|
246
|
|
|
|
247
|
|
|
$bindings = array_reverse($bindings); |
248
|
|
|
|
249
|
|
|
foreach($bindings as $key => $value) { |
250
|
|
|
$pattern = '/(@' . $key . ')(?![^a-zA-Z_ ,\}\]])/'; |
251
|
|
|
$sql = preg_replace( |
252
|
|
|
$pattern, |
253
|
|
|
$value, |
254
|
|
|
$sql |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $sql; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Determine if the given string is a JSON selector. |
263
|
|
|
* |
264
|
|
|
* @param string $value |
265
|
|
|
* @return bool |
266
|
|
|
*/ |
267
|
|
|
public function isJsonSelector($value) |
268
|
|
|
{ |
269
|
|
|
if(!is_string($value)) { |
|
|
|
|
270
|
|
|
return false; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return str_contains($value, '->'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function convertJsonFields($data): mixed |
277
|
|
|
{ |
278
|
|
|
if (!is_array($data) && !is_string($data)) { |
279
|
|
|
return $data; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if (is_string($data)) { |
283
|
|
|
return str_replace('->', '.', $data); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
if (array_is_list($data)) { |
287
|
|
|
return $this->convertJsonValuesToDotNotation($data); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
return $this->convertJsonKeysToDotNotation($data); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function convertJsonValuesToDotNotation(array $fields): array |
294
|
|
|
{ |
295
|
|
|
foreach($fields as $key => $value) { |
296
|
|
|
if ($this->isJsonSelector($value)) { |
297
|
|
|
$fields[$key] = str_replace('->', '.', $value); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
return $fields; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function convertJsonKeysToDotNotation(array $fields): array |
304
|
|
|
{ |
305
|
|
|
foreach($fields as $key => $value) { |
306
|
|
|
if ($this->isJsonSelector($key)) { |
307
|
|
|
$fields[str_replace('->', '.', $key)] = $value; |
308
|
|
|
unset($fields[$key]); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
return $fields; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|