|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace duxet\Rethinkdb\Query; |
|
4
|
|
|
|
|
5
|
|
|
use duxet\Rethinkdb\Connection; |
|
6
|
|
|
use duxet\Rethinkdb\Query; |
|
7
|
|
|
use duxet\Rethinkdb\RQL\FilterBuilder; |
|
8
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder; |
|
9
|
|
|
use r; |
|
10
|
|
|
|
|
11
|
|
|
class Builder extends QueryBuilder |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The query instance. |
|
15
|
|
|
* |
|
16
|
|
|
* @var Query |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $query; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The r\Table instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @var r\Table |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $table; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* All of the available clause operators. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
public $operators = [ |
|
33
|
|
|
'=', '<', '>', '<=', '>=', '<>', '!=', |
|
34
|
|
|
'like', 'not like', 'between', 'ilike', |
|
35
|
|
|
'&', '|', '^', '<<', '>>', |
|
36
|
|
|
'rlike', 'regexp', 'not regexp', |
|
37
|
|
|
'~', '~*', '!~', '!~*', |
|
38
|
|
|
'contains', 'exists', 'type', 'mod', 'size', |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Create a new query builder instance. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Connection $connection |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(Connection $connection) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->connection = $connection; |
|
49
|
|
|
$this->grammar = $connection->getQueryGrammar(); |
|
50
|
|
|
$this->processor = $connection->getPostProcessor(); |
|
51
|
|
|
$this->query = new Query($this->connection); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set the collection which the query is targeting. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $table |
|
58
|
|
|
* |
|
59
|
|
|
* @return Builder |
|
60
|
|
|
*/ |
|
61
|
|
|
public function from($table) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($table) { |
|
64
|
|
|
$this->table = r\table($table); |
|
|
|
|
|
|
65
|
|
|
$this->query->table($table); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return parent::from($table); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Execute the query as a fresh "select" statement. |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $columns |
|
75
|
|
|
* |
|
76
|
|
|
* @return array|static[] |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getFresh($columns = []) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->compileOrders(); |
|
81
|
|
|
$this->compileWheres(); |
|
82
|
|
|
|
|
83
|
|
|
if ($this->offset) { |
|
84
|
|
|
$this->query->skip($this->offset); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
if ($this->limit) { |
|
87
|
|
|
$this->query->limit($this->limit); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
if ($this->columns) { |
|
|
|
|
|
|
90
|
|
|
$columns = $this->columns; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (!empty($columns) && $columns[0] != '*') { |
|
94
|
|
|
$this->query->pluck($columns); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$results = $this->query->run(); |
|
98
|
|
|
if (is_object($results)) { |
|
99
|
|
|
$results = $results->toArray(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (isset($results['$reql_type$']) |
|
103
|
|
|
&& $results['$reql_type$'] === 'GROUPED_DATA') { |
|
104
|
|
|
return $results['data']; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $results; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Run the query as a "select" statement against the connection. |
|
112
|
|
|
* |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function runSelect() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->getFresh(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Compile orders into query. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function compileOrders() |
|
124
|
|
|
{ |
|
125
|
|
|
if (!$this->orders) { |
|
|
|
|
|
|
126
|
|
|
return; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
foreach ($this->orders as $order) { |
|
130
|
|
|
$column = $order['column']; |
|
131
|
|
|
$direction = $order['direction']; |
|
132
|
|
|
|
|
133
|
|
|
$compiled = strtolower($direction) == 'asc' |
|
134
|
|
|
? r\asc($column) : r\desc($column); |
|
135
|
|
|
|
|
136
|
|
|
// Use index as field if needed |
|
137
|
|
|
if ($order['index']) { |
|
138
|
|
|
$compiled = ['index' => $compiled]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$this->query->orderBy($compiled); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Insert a new record into the database. |
|
147
|
|
|
* |
|
148
|
|
|
* @param array $values |
|
149
|
|
|
* |
|
150
|
|
|
* @return bool |
|
151
|
|
|
*/ |
|
152
|
|
|
public function insert(array $values) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->compileWheres(); |
|
155
|
|
|
$result = $this->query->insert($values); |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
return 0 == (int) $result['errors']; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Insert a new record and get the value of the primary key. |
|
162
|
|
|
* |
|
163
|
|
|
* @param array $values |
|
164
|
|
|
* @param string $sequence |
|
165
|
|
|
* |
|
166
|
|
|
* @return int |
|
167
|
|
|
*/ |
|
168
|
|
|
public function insertGetId(array $values, $sequence = null) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->compileWheres(); |
|
171
|
|
|
$result = $this->query->insert($values); |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
if (0 == (int) $result['errors']) { |
|
174
|
|
|
if (isset($values['id'])) { |
|
175
|
|
|
return $values['id']; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
// Return id |
|
179
|
|
|
return current($result['generated_keys']); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Update a record in the database. |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $values |
|
187
|
|
|
* @param array $options |
|
188
|
|
|
* |
|
189
|
|
|
* @return int |
|
190
|
|
|
*/ |
|
191
|
|
|
public function update(array $values, array $options = []) |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->performUpdate($values, $options); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Perform an update query. |
|
198
|
|
|
* |
|
199
|
|
|
* @param array $query |
|
200
|
|
|
* @param array $options |
|
201
|
|
|
* |
|
202
|
|
|
* @return int |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function performUpdate($query, array $options = []) |
|
|
|
|
|
|
205
|
|
|
{ |
|
206
|
|
|
$this->compileWheres(); |
|
207
|
|
|
$result = $this->query->update($query)->run(); |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
if (0 == (int) $result['errors']) { |
|
210
|
|
|
return $result['replaced']; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return 0; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Delete a record from the database. |
|
218
|
|
|
* |
|
219
|
|
|
* @param mixed $id |
|
220
|
|
|
* |
|
221
|
|
|
* @return int |
|
222
|
|
|
*/ |
|
223
|
|
|
public function delete($id = null) |
|
224
|
|
|
{ |
|
225
|
|
|
// If an ID is passed to the method, we will set the where clause to check |
|
226
|
|
|
// the ID to allow developers to simply and quickly remove a single row |
|
227
|
|
|
// from their database without manually specifying the where clauses. |
|
228
|
|
|
if (!is_null($id)) { |
|
229
|
|
|
$this->where('id', '=', $id); |
|
230
|
|
|
} |
|
231
|
|
|
$this->compileWheres(); |
|
232
|
|
|
|
|
233
|
|
|
return $this->query->delete()->run(); |
|
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Compile the where array to filter chain. |
|
238
|
|
|
* |
|
239
|
|
|
* @return array |
|
240
|
|
|
*/ |
|
241
|
|
|
protected function compileWheres() |
|
242
|
|
|
{ |
|
243
|
|
|
// Wheres to compile |
|
244
|
|
|
$wheres = $this->wheres; |
|
245
|
|
|
|
|
246
|
|
|
// If there is nothing to do, then return |
|
247
|
|
|
if (!$wheres) { |
|
|
|
|
|
|
248
|
|
|
return; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
$this->query->filter(function ($document) use ($wheres) { |
|
|
|
|
|
|
252
|
|
|
$builder = new FilterBuilder($document); |
|
253
|
|
|
|
|
254
|
|
|
return $builder->compileWheres($wheres); |
|
255
|
|
|
}); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
public function buildFilter($document) |
|
259
|
|
|
{ |
|
260
|
|
|
$builder = new FilterBuilder($document); |
|
261
|
|
|
|
|
262
|
|
|
return $builder->compileWheres($this->wheres); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Run a truncate statement on the table. |
|
267
|
|
|
* |
|
268
|
|
|
* @return void |
|
269
|
|
|
*/ |
|
270
|
|
|
public function truncate() |
|
271
|
|
|
{ |
|
272
|
|
|
$result = $this->query->delete()->run(); |
|
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
return 0 == (int) $result['errors']; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Append one or more values to an array. |
|
279
|
|
|
* |
|
280
|
|
|
* @param mixed $column |
|
281
|
|
|
* @param mixed $value |
|
282
|
|
|
* @param bool $unique |
|
283
|
|
|
* |
|
284
|
|
|
* @return bool |
|
285
|
|
|
*/ |
|
286
|
|
|
public function push($column, $value = null, $unique = false) |
|
287
|
|
|
{ |
|
288
|
|
|
$operation = $unique ? 'merge' : 'append'; |
|
289
|
|
|
|
|
290
|
|
|
$this->compileWheres(); |
|
291
|
|
|
$result = $this->query->update([ |
|
|
|
|
|
|
292
|
|
|
$column => r\row($column)->{$operation}($value), |
|
293
|
|
|
])->run(); |
|
294
|
|
|
|
|
295
|
|
|
return 0 == (int) $result['errors']; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Remove one or more values from an array. |
|
300
|
|
|
* |
|
301
|
|
|
* @param mixed $column |
|
302
|
|
|
* @param mixed $value |
|
303
|
|
|
* |
|
304
|
|
|
* @return bool |
|
305
|
|
|
*/ |
|
306
|
|
|
public function pull($column, $value = null) |
|
307
|
|
|
{ |
|
308
|
|
|
$this->compileWheres(); |
|
309
|
|
|
$result = $this->query->update([ |
|
|
|
|
|
|
310
|
|
|
$column => r\row($column)->difference([$value]), |
|
311
|
|
|
])->run(); |
|
312
|
|
|
|
|
313
|
|
|
return 0 == (int) $result['errors']; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* Force the query to only return distinct results. |
|
318
|
|
|
* |
|
319
|
|
|
* @var string|null |
|
320
|
|
|
* |
|
321
|
|
|
* @return Builder |
|
322
|
|
|
*/ |
|
323
|
|
|
public function distinct($column = null) |
|
324
|
|
|
{ |
|
325
|
|
|
if ($column) { |
|
326
|
|
|
$column = ['index' => $column]; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
$this->query = $this->query->distinct($column); |
|
|
|
|
|
|
330
|
|
|
|
|
331
|
|
|
return $this; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* Retrieve the "count" result of the query. |
|
336
|
|
|
* |
|
337
|
|
|
* @param string $columns |
|
338
|
|
|
* |
|
339
|
|
|
* @return int |
|
340
|
|
|
*/ |
|
341
|
|
|
public function count($columns = null) |
|
342
|
|
|
{ |
|
343
|
|
|
$this->compileWheres(); |
|
344
|
|
|
$result = $this->query->count(); |
|
|
|
|
|
|
345
|
|
|
|
|
346
|
|
|
return (int) $result; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* Retrieve the sum of the values of a given column. |
|
351
|
|
|
* |
|
352
|
|
|
* @param string $column |
|
353
|
|
|
* |
|
354
|
|
|
* @return mixed |
|
355
|
|
|
*/ |
|
356
|
|
|
public function sum($column) |
|
357
|
|
|
{ |
|
358
|
|
|
$this->compileWheres(); |
|
359
|
|
|
$result = $this->query->sum($column); |
|
|
|
|
|
|
360
|
|
|
|
|
361
|
|
|
return $result; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* Retrieve the minimum value of a given column. |
|
366
|
|
|
* |
|
367
|
|
|
* @param string $column |
|
368
|
|
|
* |
|
369
|
|
|
* @return mixed |
|
370
|
|
|
*/ |
|
371
|
|
View Code Duplication |
public function min($column) |
|
|
|
|
|
|
372
|
|
|
{ |
|
373
|
|
|
$this->compileWheres(); |
|
374
|
|
|
$result = $this->query->min($column) |
|
|
|
|
|
|
375
|
|
|
->getField($column)->rDefault(null) |
|
376
|
|
|
->run(); |
|
377
|
|
|
|
|
378
|
|
|
return $result; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* Retrieve the maximum value of a given column. |
|
383
|
|
|
* |
|
384
|
|
|
* @param string $column |
|
385
|
|
|
* |
|
386
|
|
|
* @return mixed |
|
387
|
|
|
*/ |
|
388
|
|
View Code Duplication |
public function max($column) |
|
|
|
|
|
|
389
|
|
|
{ |
|
390
|
|
|
$this->compileWheres(); |
|
391
|
|
|
$result = $this->query->max($column) |
|
|
|
|
|
|
392
|
|
|
->getField($column)->rDefault(null) |
|
393
|
|
|
->run(); |
|
394
|
|
|
|
|
395
|
|
|
return $result; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* Retrieve the average of the values of a given column. |
|
400
|
|
|
* |
|
401
|
|
|
* @param string $column |
|
402
|
|
|
* |
|
403
|
|
|
* @return mixed |
|
404
|
|
|
*/ |
|
405
|
|
|
public function avg($column) |
|
406
|
|
|
{ |
|
407
|
|
|
$this->compileWheres(); |
|
408
|
|
|
$result = $this->query->avg($column) |
|
|
|
|
|
|
409
|
|
|
->rDefault(null)->run(); |
|
410
|
|
|
|
|
411
|
|
|
return $result; |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
/** |
|
415
|
|
|
* Remove one or more fields. |
|
416
|
|
|
* |
|
417
|
|
|
* @param mixed $columns |
|
418
|
|
|
* |
|
419
|
|
|
* @return int |
|
420
|
|
|
*/ |
|
421
|
|
|
public function drop($columns) |
|
422
|
|
|
{ |
|
423
|
|
|
if (!is_array($columns)) { |
|
424
|
|
|
$columns = [$columns]; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
$this->compileWheres(); |
|
428
|
|
|
$result = $this->query->replace(function ($doc) use ($columns) { |
|
|
|
|
|
|
429
|
|
|
return $doc->without($columns); |
|
430
|
|
|
})->run(); |
|
431
|
|
|
|
|
432
|
|
|
return 0 == (int) $result['errors']; |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* Add a "group by" clause to the query. |
|
437
|
|
|
* |
|
438
|
|
|
* @param array|string $column,... |
|
|
|
|
|
|
439
|
|
|
* |
|
440
|
|
|
* @return $this |
|
441
|
|
|
*/ |
|
442
|
|
|
public function groupBy(...$groups) |
|
|
|
|
|
|
443
|
|
|
{ |
|
444
|
|
|
foreach (func_get_args() as $arg) { |
|
445
|
|
|
$this->query->group($arg)->ungroup()->map(function ($doc) { |
|
|
|
|
|
|
446
|
|
|
return $doc('reduction')->nth(0); |
|
447
|
|
|
}); |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
return $this; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
/** |
|
454
|
|
|
* Add an "order by" clause to the query. |
|
455
|
|
|
* |
|
456
|
|
|
* @param string $column |
|
457
|
|
|
* @param string $direction |
|
458
|
|
|
* @param bool $index |
|
459
|
|
|
* |
|
460
|
|
|
* @return $this |
|
461
|
|
|
*/ |
|
462
|
|
|
public function orderBy($column, $direction = 'asc', $index = false) |
|
463
|
|
|
{ |
|
464
|
|
|
$property = $this->unions ? 'unionOrders' : 'orders'; |
|
465
|
|
|
$direction = strtolower($direction) == 'asc' ? 'asc' : 'desc'; |
|
466
|
|
|
$this->{$property}[] = compact('column', 'direction', 'index'); |
|
467
|
|
|
|
|
468
|
|
|
return $this; |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Add a where between statement to the query. |
|
473
|
|
|
* |
|
474
|
|
|
* @param string $column |
|
475
|
|
|
* @param array $values |
|
476
|
|
|
* @param string $boolean |
|
477
|
|
|
* @param bool $not |
|
478
|
|
|
* |
|
479
|
|
|
* @return Builder |
|
480
|
|
|
*/ |
|
481
|
|
|
public function whereBetween($column, array $values, $boolean = 'and', $not = false) |
|
482
|
|
|
{ |
|
483
|
|
|
$type = 'between'; |
|
484
|
|
|
$this->wheres[] = compact('column', 'type', 'boolean', 'values', 'not'); |
|
485
|
|
|
|
|
486
|
|
|
return $this; |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* Handle dynamic method calls into the method. |
|
491
|
|
|
* |
|
492
|
|
|
* @param string $method |
|
493
|
|
|
* @param array $parameters |
|
494
|
|
|
* |
|
495
|
|
|
* @return mixed |
|
496
|
|
|
*/ |
|
497
|
|
|
public function __call($method, $parameters) |
|
498
|
|
|
{ |
|
499
|
|
|
if ($method == 'unset') { |
|
500
|
|
|
return call_user_func_array([$this, 'drop'], $parameters); |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
return parent::__call($method, $parameters); |
|
504
|
|
|
} |
|
505
|
|
|
} |
|
506
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..