1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
// ------------------------------------------------------------------------
|
12
|
|
|
|
13
|
|
|
namespace O2System\Database\NoSql\Abstracts;
|
14
|
|
|
|
15
|
|
|
// ------------------------------------------------------------------------
|
16
|
|
|
|
17
|
|
|
use O2System\Database\NoSql\DataStructures\Query\BuilderCache;
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* Class AbstractQueryBuilder
|
21
|
|
|
*
|
22
|
|
|
* @package O2System\Database\Abstracts
|
23
|
|
|
*/
|
24
|
|
|
abstract class AbstractQueryBuilder
|
25
|
|
|
{
|
26
|
|
|
/**
|
27
|
|
|
* AbstractQueryBuilder::testMode
|
28
|
|
|
*
|
29
|
|
|
* If true, no queries will actually be
|
30
|
|
|
* ran against the database.
|
31
|
|
|
*
|
32
|
|
|
* @var bool
|
33
|
|
|
*/
|
34
|
|
|
public $testMode = false;
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* AbstractQueryBuilder::$conn
|
38
|
|
|
*
|
39
|
|
|
* Query database connection instance.
|
40
|
|
|
*
|
41
|
|
|
* @var AbstractConnection
|
42
|
|
|
*/
|
43
|
|
|
protected $conn;
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* AbstractQueryBuilder::$builderCache
|
47
|
|
|
*
|
48
|
|
|
* Query builder cache instance.
|
49
|
|
|
*
|
50
|
|
|
* @var \O2System\Database\NoSql\DataStructures\Query\BuilderCache
|
51
|
|
|
*/
|
52
|
|
|
protected $builderCache;
|
53
|
|
|
|
54
|
|
|
// ------------------------------------------------------------------------
|
55
|
|
|
|
56
|
|
|
/**
|
57
|
|
|
* AbstractQueryBuilder::__construct.
|
58
|
|
|
*
|
59
|
|
|
* @param AbstractConnection $conn
|
60
|
|
|
*/
|
61
|
|
|
public function __construct(AbstractConnection $conn)
|
62
|
|
|
{
|
63
|
|
|
$this->conn = $conn;
|
64
|
|
|
$this->builderCache = new BuilderCache();
|
65
|
|
|
}
|
66
|
|
|
|
67
|
|
|
// ------------------------------------------------------------------------
|
68
|
|
|
|
69
|
|
|
/**
|
70
|
|
|
* AbstractQueryBuilder::select
|
71
|
|
|
*
|
72
|
|
|
* Add SELECT Sql statement portions into Query Builder.
|
73
|
|
|
*
|
74
|
|
|
* @param string|array $field String of field name
|
75
|
|
|
* Array list of string field names
|
76
|
|
|
* Array list of static
|
77
|
|
|
*
|
78
|
|
|
* @return static
|
79
|
|
|
*/
|
80
|
|
|
public function select($field)
|
81
|
|
|
{
|
82
|
|
|
if (strpos($field, ',') !== false) {
|
|
|
|
|
83
|
|
|
$field = explode(', ', $field);
|
|
|
|
|
84
|
|
|
} else {
|
85
|
|
|
$field = [$field];
|
86
|
|
|
}
|
87
|
|
|
|
88
|
|
|
$field = array_map('trim', $field);
|
89
|
|
|
|
90
|
|
|
foreach ($field as $key) {
|
91
|
|
|
$this->builderCache->store('select', $key);
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
return $this;
|
95
|
|
|
}
|
96
|
|
|
|
97
|
|
|
// ------------------------------------------------------------------------
|
98
|
|
|
|
99
|
|
|
/**
|
100
|
|
|
* AbstractQueryBuilder::collection
|
101
|
|
|
*
|
102
|
|
|
* @param string $collection Collection name.
|
103
|
|
|
*
|
104
|
|
|
* @return static
|
105
|
|
|
*/
|
106
|
|
|
public function collection($collection)
|
107
|
|
|
{
|
108
|
|
|
$this->from($collection);
|
109
|
|
|
|
110
|
|
|
return $this;
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
// ------------------------------------------------------------------------
|
114
|
|
|
|
115
|
|
|
/**
|
116
|
|
|
* AbstractQueryBuilder::from
|
117
|
|
|
*
|
118
|
|
|
* Generates FROM Sql statement portions into Query Builder.
|
119
|
|
|
*
|
120
|
|
|
* @param string $collection Collection name
|
121
|
|
|
*
|
122
|
|
|
* @return static
|
123
|
|
|
*/
|
124
|
|
|
public function from($collection)
|
125
|
|
|
{
|
126
|
|
|
$this->builderCache->store('from', trim($collection));
|
|
|
|
|
127
|
|
|
|
128
|
|
|
return $this;
|
129
|
|
|
}
|
130
|
|
|
|
131
|
|
|
//--------------------------------------------------------------------
|
132
|
|
|
|
133
|
|
|
/**
|
134
|
|
|
* AbstractQueryBuilder::join
|
135
|
|
|
*
|
136
|
|
|
* Add JOIN Sql statement portions into Query Builder.
|
137
|
|
|
*
|
138
|
|
|
* @param string $collection Collection name
|
139
|
|
|
* @param null $condition Join conditions: table.column = other_table.column
|
|
|
|
|
140
|
|
|
*
|
141
|
|
|
* @return static
|
142
|
|
|
*/
|
143
|
|
|
public function join($collection, $condition = null)
|
|
|
|
|
144
|
|
|
{
|
145
|
|
|
|
146
|
|
|
return $this;
|
147
|
|
|
}
|
148
|
|
|
|
149
|
|
|
//--------------------------------------------------------------------
|
150
|
|
|
|
151
|
|
|
/**
|
152
|
|
|
* AbstractQueryBuilder::orWhere
|
153
|
|
|
*
|
154
|
|
|
* Add OR WHERE Sql statement portions into Query Builder
|
155
|
|
|
*
|
156
|
|
|
* @param string|array $field Input name, array of [field => value] (grouped where)
|
157
|
|
|
* @param null|string $value Input criteria or UPPERCASE grouped type AND|OR
|
158
|
|
|
*
|
159
|
|
|
* @return static
|
160
|
|
|
*/
|
161
|
|
|
public function orWhere($field, $value = null)
|
162
|
|
|
{
|
163
|
|
|
$this->prepareWhere($field, $value, 'orWhere');
|
164
|
|
|
|
165
|
|
|
return $this;
|
166
|
|
|
}
|
167
|
|
|
|
168
|
|
|
//--------------------------------------------------------------------
|
169
|
|
|
|
170
|
|
|
/**
|
171
|
|
|
* AbstractQueryBuilder::prepareWhereIn
|
172
|
|
|
*
|
173
|
|
|
* @param string|array $field
|
174
|
|
|
* @param null|mixed $value
|
175
|
|
|
* @param string $cacheKey
|
176
|
|
|
*/
|
177
|
|
|
protected function prepareWhere($field, $value = null, $cacheKey)
|
178
|
|
|
{
|
179
|
|
|
if (is_array($field)) {
|
180
|
|
|
foreach ($field as $name => $value) {
|
181
|
|
|
$this->prepareWhere($name, $value, $cacheKey);
|
182
|
|
|
}
|
183
|
|
|
} elseif (isset($value)) {
|
184
|
|
|
$this->builderCache->store($cacheKey, [$field => $value]);
|
185
|
|
|
}
|
186
|
|
|
}
|
187
|
|
|
|
188
|
|
|
//--------------------------------------------------------------------
|
189
|
|
|
|
190
|
|
|
/**
|
191
|
|
|
* AbstractQueryBuilder::whereIn
|
192
|
|
|
*
|
193
|
|
|
* Add WHERE IN Sql statement portions into Query Builder
|
194
|
|
|
*
|
195
|
|
|
* @param string $field Input name
|
196
|
|
|
* @param array $values Array of values criteria
|
197
|
|
|
*
|
198
|
|
|
* @return static
|
199
|
|
|
*/
|
200
|
|
|
public function whereIn($field, array $values = [])
|
201
|
|
|
{
|
202
|
|
|
$this->prepareWhereIn($field, $values, 'whereIn');
|
203
|
|
|
|
204
|
|
|
return $this;
|
205
|
|
|
}
|
206
|
|
|
|
207
|
|
|
//--------------------------------------------------------------------
|
208
|
|
|
|
209
|
|
|
/**
|
210
|
|
|
* AbstractQueryBuilder::prepareWhereIn
|
211
|
|
|
*
|
212
|
|
|
* @param string $field
|
213
|
|
|
* @param array $values
|
214
|
|
|
* @param string $cacheKey
|
215
|
|
|
*/
|
216
|
|
|
protected function prepareWhereIn($field, array $values = [], $cacheKey)
|
217
|
|
|
{
|
218
|
|
|
if (is_array($field)) {
|
|
|
|
|
219
|
|
|
foreach ($field as $name => $values) {
|
220
|
|
|
$this->prepareWhereIn($name, $values, $cacheKey);
|
221
|
|
|
}
|
222
|
|
|
} elseif (count($values)) {
|
223
|
|
|
$this->builderCache->store($cacheKey, [$field => $values]);
|
224
|
|
|
}
|
225
|
|
|
}
|
226
|
|
|
|
227
|
|
|
//--------------------------------------------------------------------
|
228
|
|
|
|
229
|
|
|
/**
|
230
|
|
|
* AbstractQueryBuilder::orWhereIn
|
231
|
|
|
*
|
232
|
|
|
* Add OR WHERE IN Sql statement portions into Query Builder
|
233
|
|
|
*
|
234
|
|
|
* @param string $field Input name
|
235
|
|
|
* @param array $values Array of values criteria
|
236
|
|
|
*
|
237
|
|
|
* @return static
|
238
|
|
|
*/
|
239
|
|
|
public function orWhereIn($field, array $values = [])
|
240
|
|
|
{
|
241
|
|
|
$this->prepareWhereIn($field, $values, 'orWhereIn');
|
242
|
|
|
|
243
|
|
|
return $this;
|
244
|
|
|
}
|
245
|
|
|
|
246
|
|
|
//--------------------------------------------------------------------
|
247
|
|
|
|
248
|
|
|
/**
|
249
|
|
|
* AbstractQueryBuilder::whereNotIn
|
250
|
|
|
*
|
251
|
|
|
* Add WHERE NOT IN Sql statement portions into Query Builder
|
252
|
|
|
*
|
253
|
|
|
* @param string $field Input name
|
254
|
|
|
* @param array $values Array of values criteria
|
255
|
|
|
*
|
256
|
|
|
* @return static
|
257
|
|
|
*/
|
258
|
|
|
public function whereNotIn($field, array $values = [])
|
259
|
|
|
{
|
260
|
|
|
$this->prepareWhereIn($field, $values, 'whereNotIn');
|
261
|
|
|
|
262
|
|
|
return $this;
|
263
|
|
|
}
|
264
|
|
|
|
265
|
|
|
//--------------------------------------------------------------------
|
266
|
|
|
|
267
|
|
|
/**
|
268
|
|
|
* AbstractQueryBuilder::orWhereNotIn
|
269
|
|
|
*
|
270
|
|
|
* Add OR WHERE NOT IN Sql statement portions into Query Builder
|
271
|
|
|
*
|
272
|
|
|
* @param string $field Input name
|
273
|
|
|
* @param array $values Array of values criteria
|
274
|
|
|
*
|
275
|
|
|
* @return static
|
276
|
|
|
*/
|
277
|
|
|
public function orWhereNotIn($field, array $values = [])
|
278
|
|
|
{
|
279
|
|
|
$this->prepareWhereIn($field, $values, 'orWhereNotIn');
|
280
|
|
|
|
281
|
|
|
return $this;
|
282
|
|
|
}
|
283
|
|
|
|
284
|
|
|
//--------------------------------------------------------------------
|
285
|
|
|
|
286
|
|
|
/**
|
287
|
|
|
* AbstractQueryBuilder::whereBetween
|
288
|
|
|
*
|
289
|
|
|
* Add WHERE BETWEEN Sql statement portions into Query Builder
|
290
|
|
|
*
|
291
|
|
|
* @param string $field
|
292
|
|
|
* @param int $start
|
293
|
|
|
* @param int $end
|
294
|
|
|
*
|
295
|
|
|
* @return static
|
296
|
|
|
*/
|
297
|
|
|
public function whereBetween($field, $start, $end)
|
298
|
|
|
{
|
299
|
|
|
$this->prepareWhereBetween($field, $start, $end, 'between');
|
300
|
|
|
|
301
|
|
|
return $this;
|
302
|
|
|
}
|
303
|
|
|
|
304
|
|
|
//--------------------------------------------------------------------
|
305
|
|
|
|
306
|
|
|
/**
|
307
|
|
|
* AbstractQueryBuilder::prepareWhereBetween
|
308
|
|
|
*
|
309
|
|
|
* @param string $field
|
310
|
|
|
* @param int $start
|
311
|
|
|
* @param int $end
|
312
|
|
|
* @param string $cacheKey
|
313
|
|
|
*/
|
314
|
|
|
protected function prepareWhereBetween($field, $start, $end, $cacheKey)
|
315
|
|
|
{
|
316
|
|
|
$this->builderCache->store($cacheKey, [
|
317
|
|
|
$field => [
|
318
|
|
|
'start' => $start,
|
319
|
|
|
'end' => $end,
|
320
|
|
|
],
|
321
|
|
|
]);
|
322
|
|
|
}
|
323
|
|
|
|
324
|
|
|
//--------------------------------------------------------------------
|
325
|
|
|
|
326
|
|
|
/**
|
327
|
|
|
* AbstractQueryBuilder::orWhereBetween
|
328
|
|
|
*
|
329
|
|
|
* Add OR WHERE BETWEEN Sql statement portions into Query Builder
|
330
|
|
|
*
|
331
|
|
|
* @param string $field
|
332
|
|
|
* @param int $start
|
333
|
|
|
* @param int $end
|
334
|
|
|
*
|
335
|
|
|
* @return static
|
336
|
|
|
*/
|
337
|
|
|
public function orWhereBetween($field, $start, $end)
|
338
|
|
|
{
|
339
|
|
|
$this->prepareWhereBetween($field, $start, $end, 'orBetween');
|
340
|
|
|
|
341
|
|
|
return $this;
|
342
|
|
|
}
|
343
|
|
|
|
344
|
|
|
//--------------------------------------------------------------------
|
345
|
|
|
|
346
|
|
|
/**
|
347
|
|
|
* AbstractQueryBuilder::whereNotBetween
|
348
|
|
|
*
|
349
|
|
|
* Add WHERE NOT BETWEEN Sql statement portions into Query Builder
|
350
|
|
|
*
|
351
|
|
|
* @param string $field
|
352
|
|
|
* @param int $start
|
353
|
|
|
* @param int $end
|
354
|
|
|
*
|
355
|
|
|
* @return static
|
356
|
|
|
*/
|
357
|
|
|
public function whereNotBetween($field, $start, $end)
|
358
|
|
|
{
|
359
|
|
|
$this->prepareWhereBetween($field, $start, $end, 'notBetween');
|
360
|
|
|
|
361
|
|
|
return $this;
|
362
|
|
|
}
|
363
|
|
|
|
364
|
|
|
//--------------------------------------------------------------------
|
365
|
|
|
|
366
|
|
|
/**
|
367
|
|
|
* AbstractQueryBuilder::orWhereNotBetween
|
368
|
|
|
*
|
369
|
|
|
* Add OR WHERE NOT BETWEEN Sql statement portions into Query Builder
|
370
|
|
|
*
|
371
|
|
|
* @param string $field
|
372
|
|
|
* @param int $start
|
373
|
|
|
* @param int $end
|
374
|
|
|
*
|
375
|
|
|
* @return static
|
376
|
|
|
*/
|
377
|
|
|
public function orWhereNotBetween($field, $start, $end)
|
378
|
|
|
{
|
379
|
|
|
$this->prepareWhereBetween($field, $start, $end, 'orNotBetween');
|
380
|
|
|
|
381
|
|
|
return $this;
|
382
|
|
|
}
|
383
|
|
|
|
384
|
|
|
//--------------------------------------------------------------------
|
385
|
|
|
|
386
|
|
|
/**
|
387
|
|
|
* AbstractQueryBuilder::like
|
388
|
|
|
*
|
389
|
|
|
* Generates a %LIKE% Sql statement portions of the query.
|
390
|
|
|
* Separates multiple calls with 'AND'.
|
391
|
|
|
*
|
392
|
|
|
* @param string $field Input name
|
393
|
|
|
* @param string $match Input criteria match
|
394
|
|
|
* @param string $wildcard UPPERCASE positions of wildcard character BOTH|BEFORE|AFTER
|
395
|
|
|
* @param bool $caseSensitive Whether perform case sensitive LIKE or not
|
396
|
|
|
*
|
397
|
|
|
* @return static
|
398
|
|
|
*/
|
399
|
|
|
public function like($field, $match = '', $wildcard = 'BOTH', $caseSensitive = true)
|
400
|
|
|
{
|
401
|
|
|
$this->prepareLike($field, $match, $wildcard, $caseSensitive, 'like');
|
402
|
|
|
|
403
|
|
|
return $this;
|
404
|
|
|
}
|
405
|
|
|
|
406
|
|
|
//--------------------------------------------------------------------
|
407
|
|
|
|
408
|
|
|
/**
|
409
|
|
|
* AbstractQueryBuilder::prepareLike
|
410
|
|
|
*
|
411
|
|
|
* @param string $field
|
412
|
|
|
* @param string $match
|
413
|
|
|
* @param string $wildcard
|
414
|
|
|
* @param bool $caseSensitive
|
415
|
|
|
* @param string $cacheKey
|
416
|
|
|
*/
|
417
|
|
|
protected function prepareLike($field, $match, $wildcard, $caseSensitive, $cacheKey)
|
418
|
|
|
{
|
419
|
|
|
$match = quotemeta(trim($match));
|
420
|
|
|
|
421
|
|
|
switch ($wildcard) {
|
422
|
|
|
default:
|
423
|
|
|
case 'BOTH':
|
424
|
|
|
$match = '^' . $match . '$';
|
425
|
|
|
break;
|
426
|
|
|
case 'BEFORE':
|
427
|
|
|
$match = '^' . $match;
|
428
|
|
|
break;
|
429
|
|
|
case 'AFTER':
|
430
|
|
|
$match = '^' . $match;
|
431
|
|
|
break;
|
432
|
|
|
}
|
433
|
|
|
|
434
|
|
|
$flags = 'm|x|s';
|
435
|
|
|
if ($caseSensitive === false) {
|
436
|
|
|
$flags .= '|i';
|
437
|
|
|
}
|
438
|
|
|
|
439
|
|
|
$this->builderCache->store($cacheKey, [$field => new \MongoDB\BSON\Regex($match, $flags)]);
|
440
|
|
|
}
|
441
|
|
|
|
442
|
|
|
//--------------------------------------------------------------------
|
443
|
|
|
|
444
|
|
|
/**
|
445
|
|
|
* AbstractQueryBuilder::orLike
|
446
|
|
|
*
|
447
|
|
|
* Add OR LIKE Sql statement portions into Query Builder
|
448
|
|
|
*
|
449
|
|
|
* @param string $field Input name
|
450
|
|
|
* @param string $match Input criteria match
|
451
|
|
|
* @param string $wildcard UPPERCASE positions of wildcard character BOTH|BEFORE|AFTER
|
452
|
|
|
* @param bool $caseSensitive Whether perform case sensitive LIKE or not
|
453
|
|
|
*
|
454
|
|
|
* @return static
|
455
|
|
|
*/
|
456
|
|
|
public function orLike($field, $match = '', $wildcard = 'BOTH', $caseSensitive = true)
|
457
|
|
|
{
|
458
|
|
|
$this->prepareLike($field, $match, $wildcard, $caseSensitive, 'orLike');
|
459
|
|
|
|
460
|
|
|
return $this;
|
461
|
|
|
}
|
462
|
|
|
|
463
|
|
|
//--------------------------------------------------------------------
|
464
|
|
|
|
465
|
|
|
/**
|
466
|
|
|
* AbstractQueryBuilder::notLike
|
467
|
|
|
*
|
468
|
|
|
* Add NOT LIKE Sql statement portions into Query Builder
|
469
|
|
|
*
|
470
|
|
|
* @param string $field Input name
|
471
|
|
|
* @param string $match Input criteria match
|
472
|
|
|
* @param string $wildcard UPPERCASE positions of wildcard character BOTH|BEFORE|AFTER
|
473
|
|
|
* @param bool $caseSensitive Whether perform case sensitive LIKE or not
|
474
|
|
|
*
|
475
|
|
|
* @return static
|
476
|
|
|
*/
|
477
|
|
|
public function notLike($field, $match = '', $wildcard = 'BOTH', $caseSensitive = true)
|
478
|
|
|
{
|
479
|
|
|
$this->prepareLike($field, $match, $wildcard, $caseSensitive, 'notLike');
|
480
|
|
|
|
481
|
|
|
return $this;
|
482
|
|
|
}
|
483
|
|
|
|
484
|
|
|
//--------------------------------------------------------------------
|
485
|
|
|
|
486
|
|
|
/**
|
487
|
|
|
* AbstractQueryBuilder::orNotLike
|
488
|
|
|
*
|
489
|
|
|
* Add OR NOT LIKE Sql statement portions into Query Builder
|
490
|
|
|
*
|
491
|
|
|
* @param string $field Input name
|
492
|
|
|
* @param string $match Input criteria match
|
493
|
|
|
* @param string $wildcard UPPERCASE positions of wildcard character BOTH|BEFORE|AFTER
|
494
|
|
|
* @param bool $caseSensitive Whether perform case sensitive LIKE or not
|
495
|
|
|
*
|
496
|
|
|
* @return static
|
497
|
|
|
*/
|
498
|
|
|
public function orNotLike($field, $match = '', $wildcard = 'BOTH', $caseSensitive = true)
|
499
|
|
|
{
|
500
|
|
|
$this->prepareLike($field, $match, $wildcard, $caseSensitive, 'orNotLike');
|
501
|
|
|
|
502
|
|
|
return $this;
|
503
|
|
|
}
|
504
|
|
|
|
505
|
|
|
//--------------------------------------------------------------------
|
506
|
|
|
|
507
|
|
|
/**
|
508
|
|
|
* AbstractQueryBuilder::groupBy
|
509
|
|
|
*
|
510
|
|
|
* Add GROUP BY Sql statement into Query Builder.
|
511
|
|
|
*
|
512
|
|
|
* @param string $field
|
513
|
|
|
*
|
514
|
|
|
* @return static
|
515
|
|
|
*/
|
516
|
|
|
public function groupBy($field)
|
|
|
|
|
517
|
|
|
{
|
518
|
|
|
return $this;
|
519
|
|
|
}
|
520
|
|
|
|
521
|
|
|
//--------------------------------------------------------------------
|
522
|
|
|
|
523
|
|
|
/**
|
524
|
|
|
* AbstractQueryBuilder::orderBy
|
525
|
|
|
*
|
526
|
|
|
* Add ORDER BY Sql statement portions into Query Builder.
|
527
|
|
|
*
|
528
|
|
|
* @param string $field
|
529
|
|
|
* @param string $direction
|
530
|
|
|
*
|
531
|
|
|
* @return static
|
532
|
|
|
*/
|
533
|
|
|
public function orderBy($field, $direction = 'ASC')
|
534
|
|
|
{
|
535
|
|
|
$this->builderCache->store('orderBy', [$field => strtoupper($direction)]);
|
536
|
|
|
|
537
|
|
|
return $this;
|
538
|
|
|
}
|
539
|
|
|
|
540
|
|
|
//--------------------------------------------------------------------
|
541
|
|
|
|
542
|
|
|
/**
|
543
|
|
|
* AbstractQueryBuilder::page
|
544
|
|
|
*
|
545
|
|
|
* Add Set LIMIT, OFFSET Sql statement by page number and entries.
|
546
|
|
|
*
|
547
|
|
|
* @param int $page Page number
|
548
|
|
|
* @param null $entries Num entries of each page
|
|
|
|
|
549
|
|
|
*
|
550
|
|
|
* @return static
|
551
|
|
|
*/
|
552
|
|
|
public function page($page = 1, $entries = null)
|
553
|
|
|
{
|
554
|
|
|
$page = (int)intval($page);
|
555
|
|
|
|
556
|
|
|
$entries = (int)(isset($entries)
|
557
|
|
|
? $entries
|
558
|
|
|
: ($this->builderCache->limit === false
|
|
|
|
|
559
|
|
|
? 5
|
560
|
|
|
: $this->builderCache->limit
|
561
|
|
|
)
|
562
|
|
|
);
|
563
|
|
|
|
564
|
|
|
$offset = ($page - 1) * $entries;
|
565
|
|
|
|
566
|
|
|
$this->limit($entries, $offset);
|
567
|
|
|
|
568
|
|
|
return $this;
|
569
|
|
|
}
|
570
|
|
|
|
571
|
|
|
//--------------------------------------------------------------------
|
572
|
|
|
|
573
|
|
|
/**
|
574
|
|
|
* AbstractQueryBuilder::limit
|
575
|
|
|
*
|
576
|
|
|
* Add LIMIT,OFFSET Sql statement into Query Builder.
|
577
|
|
|
*
|
578
|
|
|
* @param int $limit LIMIT value
|
579
|
|
|
* @param int $offset OFFSET value
|
580
|
|
|
*
|
581
|
|
|
* @return static
|
582
|
|
|
*/
|
583
|
|
|
public function limit($limit, $offset = 0)
|
584
|
|
|
{
|
585
|
|
|
$this->builderCache->store('limit', $limit);
|
|
|
|
|
586
|
|
|
$this->offset($offset);
|
587
|
|
|
|
588
|
|
|
return $this;
|
589
|
|
|
}
|
590
|
|
|
|
591
|
|
|
// --------------------------------------------------------------------
|
592
|
|
|
|
593
|
|
|
/**
|
594
|
|
|
* AbstractQueryBuilder::offset
|
595
|
|
|
*
|
596
|
|
|
* Add OFFSET Sql statement into Query Builder.
|
597
|
|
|
*
|
598
|
|
|
* @param int $offset OFFSET value
|
599
|
|
|
*
|
600
|
|
|
* @return static
|
601
|
|
|
*/
|
602
|
|
|
public function offset($offset)
|
603
|
|
|
{
|
604
|
|
|
$this->builderCache->store('offset', $offset);
|
|
|
|
|
605
|
|
|
|
606
|
|
|
return $this;
|
607
|
|
|
}
|
608
|
|
|
|
609
|
|
|
//--------------------------------------------------------------------
|
610
|
|
|
|
611
|
|
|
/**
|
612
|
|
|
* AbstractQueryBuilder::get
|
613
|
|
|
*
|
614
|
|
|
* Perform execution of Sql Query Builder and run ConnectionInterface::query()
|
615
|
|
|
*
|
616
|
|
|
* @param null|int $limit
|
617
|
|
|
* @param null|int $offset
|
618
|
|
|
*
|
619
|
|
|
* @return bool|\O2System\Database\DataObjects\Result
|
620
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
621
|
|
|
*/
|
622
|
|
|
public function get($limit = null, $offset = null)
|
623
|
|
|
{
|
624
|
|
|
if (isset($limit)) {
|
625
|
|
|
$this->limit($limit, $offset);
|
626
|
|
|
}
|
627
|
|
|
|
628
|
|
|
if (false !== ($result = $this->conn->query($this->builderCache))) {
|
629
|
|
|
$result->setNumTotal($this->countAllResults(true));
|
630
|
|
|
}
|
631
|
|
|
|
632
|
|
|
$this->builderCache->reset();
|
633
|
|
|
|
634
|
|
|
return $result;
|
635
|
|
|
}
|
636
|
|
|
|
637
|
|
|
//--------------------------------------------------------------------
|
638
|
|
|
|
639
|
|
|
/**
|
640
|
|
|
* AbstractQueryBuilder::countAllResult
|
641
|
|
|
*
|
642
|
|
|
* Returns numbers of total documents.
|
643
|
|
|
*
|
644
|
|
|
* @param bool $reset Whether perform reset Query Builder or not
|
645
|
|
|
*
|
646
|
|
|
* @return int
|
647
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
648
|
|
|
* @access public
|
649
|
|
|
*/
|
650
|
|
|
abstract public function countAllResults($reset = true);
|
651
|
|
|
|
652
|
|
|
//--------------------------------------------------------------------
|
653
|
|
|
|
654
|
|
|
/**
|
655
|
|
|
* AbstractQueryBuilder::getWhere
|
656
|
|
|
*
|
657
|
|
|
* Perform execution of Sql Query Builder and run ConnectionInterface::query()
|
658
|
|
|
*
|
659
|
|
|
* @param array $where
|
660
|
|
|
* @param null|int $limit
|
661
|
|
|
* @param null|int $offset
|
662
|
|
|
*
|
663
|
|
|
* @return bool|\O2System\Database\DataObjects\Result
|
664
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
665
|
|
|
*/
|
666
|
|
|
public function getWhere(array $where = [], $limit = null, $offset = null)
|
667
|
|
|
{
|
668
|
|
|
$this->where($where);
|
669
|
|
|
|
670
|
|
|
if (isset($limit)) {
|
671
|
|
|
$this->limit($limit, $offset);
|
672
|
|
|
}
|
673
|
|
|
|
674
|
|
|
if (false !== ($result = $this->conn->query($this->builderCache))) {
|
675
|
|
|
$result->setNumTotal($this->countAllResults(true));
|
676
|
|
|
}
|
677
|
|
|
|
678
|
|
|
$this->builderCache->reset();
|
679
|
|
|
|
680
|
|
|
return $result;
|
681
|
|
|
}
|
682
|
|
|
|
683
|
|
|
//--------------------------------------------------------------------
|
684
|
|
|
|
685
|
|
|
/**
|
686
|
|
|
* AbstractQueryBuilder::where
|
687
|
|
|
*
|
688
|
|
|
* Add WHERE Sql statement portions into Query Builder
|
689
|
|
|
*
|
690
|
|
|
* @param string|array $field Input name, array of [field => value] (grouped where)
|
691
|
|
|
* @param null|string $value Input criteria or UPPERCASE grouped type AND|OR
|
692
|
|
|
*
|
693
|
|
|
* @return static
|
694
|
|
|
*/
|
695
|
|
|
public function where($field, $value = null)
|
696
|
|
|
{
|
697
|
|
|
$this->prepareWhere($field, $value, 'where');
|
698
|
|
|
|
699
|
|
|
return $this;
|
700
|
|
|
}
|
701
|
|
|
|
702
|
|
|
//--------------------------------------------------------------------
|
703
|
|
|
|
704
|
|
|
/**
|
705
|
|
|
* AbstractQueryBuilder::countAll
|
706
|
|
|
*
|
707
|
|
|
* Returns numbers of query result.
|
708
|
|
|
*
|
709
|
|
|
* @access public
|
710
|
|
|
* @return int|string
|
711
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException
|
712
|
|
|
*/
|
713
|
|
|
abstract public function countAll();
|
714
|
|
|
|
715
|
|
|
//--------------------------------------------------------------------
|
716
|
|
|
|
717
|
|
|
/**
|
718
|
|
|
* AbstractQueryBuilder::insert
|
719
|
|
|
*
|
720
|
|
|
* @param array $sets
|
721
|
|
|
*
|
722
|
|
|
* @return bool
|
723
|
|
|
*/
|
724
|
|
|
public function insert(array $sets)
|
725
|
|
|
{
|
726
|
|
|
$this->builderCache->store('sets', $sets);
|
727
|
|
|
|
728
|
|
|
return $this->platformInsertHandler($this->builderCache);
|
729
|
|
|
}
|
730
|
|
|
|
731
|
|
|
//--------------------------------------------------------------------
|
732
|
|
|
|
733
|
|
|
/**
|
734
|
|
|
* AbstractQueryBuilder::platformInsertHandler
|
735
|
|
|
*
|
736
|
|
|
* @param \O2System\Database\NoSql\DataStructures\Query\BuilderCache $builderCache
|
737
|
|
|
*
|
738
|
|
|
* @return bool
|
739
|
|
|
*/
|
740
|
|
|
abstract protected function platformInsertHandler(BuilderCache $builderCache);
|
741
|
|
|
|
742
|
|
|
//--------------------------------------------------------------------
|
743
|
|
|
|
744
|
|
|
/**
|
745
|
|
|
* AbstractQueryBuilder::insertBatch
|
746
|
|
|
*
|
747
|
|
|
* @param array $sets
|
748
|
|
|
*
|
749
|
|
|
* @return bool
|
750
|
|
|
*/
|
751
|
|
|
public function insertBatch(array $sets)
|
752
|
|
|
{
|
753
|
|
|
$this->builderCache->store('sets', $sets);
|
754
|
|
|
|
755
|
|
|
return $this->platformInsertBatchHandler($this->builderCache);
|
756
|
|
|
}
|
757
|
|
|
|
758
|
|
|
//--------------------------------------------------------------------
|
759
|
|
|
|
760
|
|
|
/**
|
761
|
|
|
* AbstractQueryBuilder::platformInsertBatchHandler
|
762
|
|
|
*
|
763
|
|
|
* @param BuilderCache $builderCache
|
764
|
|
|
*
|
765
|
|
|
* @return bool
|
766
|
|
|
*/
|
767
|
|
|
abstract protected function platformInsertBatchHandler(BuilderCache $builderCache);
|
768
|
|
|
|
769
|
|
|
//--------------------------------------------------------------------
|
770
|
|
|
|
771
|
|
|
/**
|
772
|
|
|
* AbstractQueryBuilder::update
|
773
|
|
|
*
|
774
|
|
|
* @param array $sets
|
775
|
|
|
* @param array $where
|
776
|
|
|
*
|
777
|
|
|
* @return bool
|
778
|
|
|
*/
|
779
|
|
|
public function update(array $sets, array $where = [])
|
780
|
|
|
{
|
781
|
|
|
$this->where($where);
|
782
|
|
|
$this->builderCache->store('sets', $sets);
|
783
|
|
|
|
784
|
|
|
return $this->platformUpdateHandler($this->builderCache);
|
785
|
|
|
}
|
786
|
|
|
|
787
|
|
|
//--------------------------------------------------------------------
|
788
|
|
|
|
789
|
|
|
/**
|
790
|
|
|
* AbstractQueryBuilder::platformUpdateHandler
|
791
|
|
|
*
|
792
|
|
|
* @param BuilderCache $builderCache
|
793
|
|
|
*
|
794
|
|
|
* @return bool
|
795
|
|
|
*/
|
796
|
|
|
abstract protected function platformUpdateHandler(BuilderCache $builderCache);
|
797
|
|
|
|
798
|
|
|
//--------------------------------------------------------------------
|
799
|
|
|
|
800
|
|
|
/**
|
801
|
|
|
* AbstractQueryBuilder::updateBatch
|
802
|
|
|
*
|
803
|
|
|
* @param array $sets
|
804
|
|
|
* @param array $where
|
805
|
|
|
*
|
806
|
|
|
* @return bool
|
807
|
|
|
*/
|
808
|
|
|
public function updateBatch(array $sets, array $where = [])
|
809
|
|
|
{
|
810
|
|
|
$this->where($where);
|
811
|
|
|
$this->builderCache->store('sets', $sets);
|
812
|
|
|
|
813
|
|
|
return $this->platformUpdateBatchHandler($this->builderCache);
|
814
|
|
|
}
|
815
|
|
|
|
816
|
|
|
//--------------------------------------------------------------------
|
817
|
|
|
|
818
|
|
|
/**
|
819
|
|
|
* AbstractQueryBuilder::platformUpdateBatchHandler
|
820
|
|
|
*
|
821
|
|
|
* @param BuilderCache $builderCache
|
822
|
|
|
*
|
823
|
|
|
* @return bool
|
824
|
|
|
*/
|
825
|
|
|
abstract protected function platformUpdateBatchHandler(BuilderCache $builderCache);
|
826
|
|
|
|
827
|
|
|
//--------------------------------------------------------------------
|
828
|
|
|
|
829
|
|
|
/**
|
830
|
|
|
* AbstractQueryBuilder::replace
|
831
|
|
|
*
|
832
|
|
|
* @param array $sets
|
833
|
|
|
* @param array $where
|
834
|
|
|
*
|
835
|
|
|
* @return bool
|
836
|
|
|
*/
|
837
|
|
|
public function replace(array $sets, array $where = [])
|
838
|
|
|
{
|
839
|
|
|
$this->where($where);
|
840
|
|
|
$this->builderCache->store('sets', $sets);
|
841
|
|
|
|
842
|
|
|
return $this->platformReplaceHandler($this->builderCache);
|
843
|
|
|
}
|
844
|
|
|
|
845
|
|
|
//--------------------------------------------------------------------
|
846
|
|
|
|
847
|
|
|
/**
|
848
|
|
|
* AbstractQueryBuilder::platformReplaceHandler
|
849
|
|
|
*
|
850
|
|
|
* @param BuilderCache $builderCache
|
851
|
|
|
*
|
852
|
|
|
* @return bool
|
853
|
|
|
*/
|
854
|
|
|
abstract protected function platformReplaceHandler(BuilderCache $builderCache);
|
855
|
|
|
|
856
|
|
|
//--------------------------------------------------------------------
|
857
|
|
|
|
858
|
|
|
/**
|
859
|
|
|
* AbstractQueryBuilder::replaceBatch
|
860
|
|
|
*
|
861
|
|
|
* @param array $sets
|
862
|
|
|
* @param array $where
|
863
|
|
|
*
|
864
|
|
|
* @return bool
|
865
|
|
|
*/
|
866
|
|
|
public function replaceBatch(array $sets, array $where = [])
|
867
|
|
|
{
|
868
|
|
|
$this->where($where);
|
869
|
|
|
$this->builderCache->store('sets', $sets);
|
870
|
|
|
|
871
|
|
|
return $this->platformReplaceBatchHandler($this->builderCache);
|
872
|
|
|
}
|
873
|
|
|
|
874
|
|
|
//--------------------------------------------------------------------
|
875
|
|
|
|
876
|
|
|
/**
|
877
|
|
|
* AbstractQueryBuilder::platformReplaceBatchHandler
|
878
|
|
|
*
|
879
|
|
|
* @param BuilderCache $builderCache
|
880
|
|
|
*
|
881
|
|
|
* @return bool
|
882
|
|
|
*/
|
883
|
|
|
abstract protected function platformReplaceBatchHandler(BuilderCache $builderCache);
|
884
|
|
|
|
885
|
|
|
//--------------------------------------------------------------------
|
886
|
|
|
|
887
|
|
|
/**
|
888
|
|
|
* AbstractQueryBuilder::delete
|
889
|
|
|
*
|
890
|
|
|
* @param array $where Where clause.
|
891
|
|
|
* @param int $limit Limit clause.
|
892
|
|
|
*
|
893
|
|
|
* @return bool
|
894
|
|
|
*/
|
895
|
|
|
public function delete($where = [], $limit = null)
|
896
|
|
|
{
|
897
|
|
|
$this->where($where);
|
898
|
|
|
|
899
|
|
|
if (isset($limit)) {
|
900
|
|
|
$this->limit($limit);
|
901
|
|
|
}
|
902
|
|
|
|
903
|
|
|
return $this->platformDeleteHandler($this->builderCache);
|
904
|
|
|
}
|
905
|
|
|
|
906
|
|
|
//--------------------------------------------------------------------
|
907
|
|
|
|
908
|
|
|
/**
|
909
|
|
|
* AbstractQueryBuilder::platformDeleteHandler
|
910
|
|
|
*
|
911
|
|
|
* @param BuilderCache $builderCache
|
912
|
|
|
*
|
913
|
|
|
* @return bool
|
914
|
|
|
*/
|
915
|
|
|
abstract protected function platformDeleteHandler(BuilderCache $builderCache);
|
916
|
|
|
|
917
|
|
|
//--------------------------------------------------------------------
|
918
|
|
|
|
919
|
|
|
/**
|
920
|
|
|
* AbstractQueryBuilder::deleteBatch
|
921
|
|
|
*
|
922
|
|
|
* @param array $where Where clause.
|
923
|
|
|
* @param int $limit Limit clause.
|
924
|
|
|
*
|
925
|
|
|
* @return bool
|
926
|
|
|
*/
|
927
|
|
|
public function deleteBatch($where = [], $limit = null)
|
928
|
|
|
{
|
929
|
|
|
$this->where($where);
|
930
|
|
|
|
931
|
|
|
if (isset($limit)) {
|
932
|
|
|
$this->limit($limit);
|
933
|
|
|
}
|
934
|
|
|
|
935
|
|
|
return $this->platformDeleteBatchHandler($this->builderCache);
|
936
|
|
|
}
|
937
|
|
|
|
938
|
|
|
//--------------------------------------------------------------------
|
939
|
|
|
|
940
|
|
|
/**
|
941
|
|
|
* AbstractQueryBuilder::platformDeleteBatchHandler
|
942
|
|
|
*
|
943
|
|
|
* @param BuilderCache $builderCache
|
944
|
|
|
*
|
945
|
|
|
* @return bool
|
946
|
|
|
*/
|
947
|
|
|
abstract protected function platformDeleteBatchHandler(BuilderCache $builderCache);
|
948
|
|
|
}
|
949
|
|
|
|