Completed
Push — master ( e5c5d6...6b5dbf )
by Ivan
15:12
created

TableQuery::collection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 2
cp 0.5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1.125
1
<?php
2
namespace vakata\database\schema;
3
4
use vakata\collection\Collection;
5
use vakata\database\DBInterface;
6
use vakata\database\DBException;
7
use vakata\database\ResultInterface;
8
9
/**
10
 * A database query class
11
 */
12
class TableQuery implements \IteratorAggregate, \ArrayAccess, \Countable
13
{
14
    const SEP = '___';
15
    /**
16
     * @var DBInterface
17
     */
18
    protected $db;
19
    /**
20
     * @var Table
21
     */
22
    protected $definition;
23
    /**
24
     * @var TableQueryIterator|null
25
     */
26
    protected $qiterator;
27
28
    /**
29
     * @var array
30
     */
31
    protected $where = [];
32
    /**
33
     * @var array
34
     */
35
    protected $order = [];
36
    /**
37
     * @var array
38
     */
39
    protected $group = [];
40
    /**
41
     * @var array
42
     */
43
    protected $having = [];
44
    /**
45
     * @var int[]
46
     */
47
    protected $li_of = [0,0,0];
48
    /**
49
     * @var array
50
     */
51
    protected $fields = [];
52
    /**
53
     * @var array
54
     */
55
    protected $withr = [];
56
    /**
57
     * @var array
58
     */
59
    protected $joins = [];
60
    /**
61
     * @var array
62
     */
63
    protected $pkey = [];
64
    /**
65
     * @var array
66
     */
67
    protected $aliases = [];
68
69
    /**
70
     * Create an instance
71
     * @param  DBInterface    $db         the database connection
72
     * @param  Table|string   $table      the name or definition of the main table in the query
73
     */
74 156
    public function __construct(DBInterface $db, $table)
75
    {
76 156
        $this->db = $db;
77 156
        $this->definition = $table instanceof Table ? $table : $this->db->definition((string)$table);
78 156
        $primary = $this->definition->getPrimaryKey();
79 156
        $columns = $this->definition->getColumns();
80 156
        $this->pkey = count($primary) ? $primary : $columns;
81 156
        $this->columns($columns);
82 156
    }
83
    public function __clone()
84
    {
85
        $this->reset();
86
    }
87
    /**
88
     * Get the table definition of the queried table
89
     * @return Table        the definition
90
     */
91
    public function getDefinition() : Table
92
    {
93
        return $this->definition;
94
    }
95
96 156
    protected function getColumn($column)
97
    {
98 156
        $column = explode('.', $column);
99 156
        if (count($column) === 1) {
100 156
            $column = [ $this->definition->getFullName(), $column[0] ];
101 156
            $col = $this->definition->getColumn($column[1]);
102 156
            if (!$col) {
103 156
                throw new DBException('Invalid column name in own table');
104
            }
105 28
        } elseif (count($column) === 2) {
106 28
            if ($column[0] === $this->definition->getName()) {
107
                $col = $this->definition->getColumn($column[1]);
108
                if (!$col) {
109
                    throw new DBException('Invalid column name in own table');
110
                }
111
            } else {
112 28
                if ($this->definition->hasRelation($column[0])) {
113 24
                    $col = $this->definition->getRelation($column[0])->table->getColumn($column[1]);
114 24
                    if (!$col) {
115 24
                        throw new DBException('Invalid column name in related table');
116
                    }
117 4
                } elseif (isset($this->joins[$column[0]])) {
118 4
                    $col = $this->joins[$column[0]]->table->getColumn($column[1]);
119 4
                    if (!$col) {
120 4
                        throw new DBException('Invalid column name in related table');
121
                    }
122
                } else {
123 28
                    throw new DBException('Invalid foreign table name: ' . implode(',', $column));
124
                }
125
            }
126
        } else {
127
            $name = array_pop($column);
128
            if ($this->definition->hasRelation(implode('.', $column))) {
129
                $this->with(implode('.', $column));
130
                $col = $this->definition->getRelation(implode('.', $column))->table->getColumn($name);
131
                $column = [ implode('.', $column), $name ];
132
            } else {
133
                $this->with(implode('.', $column));
134
                $table = $this->definition;
135
                $table = array_reduce(
136
                    $column,
137
                    function ($carry, $item) use (&$table) {
138
                        $table = $table->getRelation($item)->table;
139
                        return $table;
140 156
                    }
141
                );
142 76
                $col = $table->getColumn($name);
143
                $column = [ implode(static::SEP, $column), $name ];
144 76
            }
145 76
        }
146
        return [ 'name' => implode('.', $column), 'data' => $col ];
147
    }
148 76
    protected function normalizeValue(TableColumn $col, $value)
149 76
    {
150
        $strict = (int)$this->db->driverOption('strict', 0) > 0;
151
        if ($value === null && $col->isNullable()) {
152
            return null;
153
        }
154
        switch ($col->getBasicType()) {
155
            case 'date':
156
                if (is_string($value)) {
157
                    $temp = strtotime($value);
158
                    if (!$temp) {
159
                        if ($strict) {
160
                            throw new DBException('Invalid value for date column ' . $col->getName());
161
                        }
162
                        return null;
163
                    }
164
                    return date('Y-m-d', $temp);
165
                }
166
                if (is_int($value)) {
167
                    return date('Y-m-d', $value);
168
                }
169
                if ($value instanceof \DateTime) {
170 76
                    return $value->format('Y-m-d');
171
                }
172
                if ($strict) {
173
                    throw new DBException('Invalid value (unknown data type) for date column ' . $col->getName());
174
                }
175
                return $value;
176
            case 'datetime':
177
                if (is_string($value)) {
178
                    $temp = strtotime($value);
179
                    if (!$temp) {
180
                        if ($strict) {
181
                            throw new DBException('Invalid value for datetime column ' . $col->getName());
182
                        }
183
                        return null;
184
                    }
185
                    return date('Y-m-d H:i:s', $temp);
186
                }
187
                if (is_int($value)) {
188
                    return date('Y-m-d H:i:s', $value);
189
                }
190
                if ($value instanceof \DateTime) {
191 76
                    return $value->format('Y-m-d H:i:s');
192
                }
193
                if ($strict) {
194
                    throw new DBException('Invalid value (unknown data type) for datetime column ' . $col->getName());
195
                }
196
                return $value;
197
            case 'enum':
198
                $values = $col->getValues();
199
                if (is_int($value)) {
200
                    if (!isset($values[$value])) {
201
                        if ($strict) {
202
                            throw new DBException('Invalid value (using integer) for enum ' . $col->getName());
203
                        }
204
                        return $value;
205
                    }
206
                    return $values[$value];
207
                }
208
                if (!in_array($value, $col->getValues())) {
209 76
                    if ($strict) {
210 22
                        throw new DBException('Invalid value for enum ' . $col->getName());
211 22
                    }
212 62
                    return 0;
213
                }
214
                return $value;
215 62
            case 'int':
216
                $temp = preg_replace('([^+\-0-9]+)', '', $value);
217
                return is_string($temp) ? (int)$temp : 0;
218 62
            case 'float':
219 8
                $temp = preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', $value));
220 4
                return is_string($temp) ? (float)$temp : 0;
221
            case 'text':
222 4
                // check using strlen first, in order to avoid hitting mb_ functions which might be polyfilled
223
                // because the polyfill is quite slow
224 56
                if ($col->hasLength() && strlen($value) > $col->getLength() && mb_strlen($value) > $col->getLength()) {
225
                    if ($strict) {
226
                        throw new DBException('Invalid value for text column ' . $col->getName());
227
                    }
228
                    return mb_substr($value, 0, $col->getLength());
229
                }
230 60
                return $value;
231
            default: // time, blob, etc
232 60
                return $value;
233 60
        }
234 4
    }
235 4
236
    protected function filterSQL(string $column, $value, bool $negate = false) : array
237 60
    {
238 4
        list($name, $column) = array_values($this->getColumn($column));
239
        if (is_array($value) && count($value) === 1 && isset($value['not'])) {
240 4
            $negate = true;
241
            $value = $value['not'];
242
        }
243
        if (is_array($value) && count($value) === 1 && isset($value['like'])) {
244
            $value = $value['like'];
245
            // str_replace(['%', '_'], ['\\%','\\_'], $q)
246 4
            return $negate ?
247 4
                [
248
                    $name . ' NOT LIKE ?',
249
                    [ $this->normalizeValue($column, $value) ]
250 56
                ] :
251
                [
252
                    $name . ' LIKE ?',
253
                    [ $this->normalizeValue($column, $value) ]
254
                ];
255 56
        }
256 56
        if (is_null($value)) {
257
            return $negate ?
258 4
                [ $name . ' IS NOT NULL', [] ]:
259 4
                [ $name . ' IS NULL', [] ];
260
        }
261
        if (!is_array($value)) {
262 56
            return $negate ?
263 56
                [
264
                    $name . ' <> ?',
265
                    [ $this->normalizeValue($column, $value) ]
266 12
                ] :
267
                [
268
                    $name . ' = ?',
269 12
                    [ $this->normalizeValue($column, $value) ]
270
                ];
271
        }
272 12
        if (isset($value['beg']) && strlen($value['beg']) && (!isset($value['end']) || !strlen($value['end']))) {
273
            $value = [ 'gte' => $value['beg'] ];
274
        }
275
        if (isset($value['end']) && strlen($value['end']) && (!isset($value['beg']) || !strlen($value['beg']))) {
276
            $value = [ 'lte' => $value['end'] ];
277
        }
278
        if (isset($value['beg']) && isset($value['end'])) {
279
            return $negate ?
280
                [
281
                    $name.' NOT BETWEEN ? AND ?',
282
                    [
283
                        $this->normalizeValue($column, $value['beg']),
284
                        $this->normalizeValue($column, $value['end'])
285
                    ]
286
                ] :
287
                [
288
                    $name.' BETWEEN ? AND ?',
289 12
                    [
290 8
                        $this->normalizeValue($column, $value['beg']),
291 8
                        $this->normalizeValue($column, $value['end'])
292 8
                    ]
293 4
                ];
294 4
        }
295
        if (isset($value['gt']) || isset($value['lt']) || isset($value['gte']) || isset($value['lte'])) {
296 8
            $sql = [];
297 4
            $par = [];
298 4
            if (isset($value['gt'])) {
299
                $sql[] = $name. ' ' . ($negate ? '<=' : '>') . ' ?';
300 8
                $par[] = $this->normalizeValue($column, $value['gt']);
301 8
            }
302 8
            if (isset($value['gte'])) {
303
                $sql[] = $name. ' ' . ($negate ? '<' : '>=') . ' ?';
304 8
                $par[] = $this->normalizeValue($column, $value['gte']);
305 4
            }
306 4
            if (isset($value['lt'])) {
307
                $sql[] = $name. ' ' . ($negate ? '>=' : '<') . ' ?';
308
                $par[] = $this->normalizeValue($column, $value['lt']);
309 8
            }
310 8
            if (isset($value['lte'])) {
311
                $sql[] = $name. ' ' . ($negate ? '>' : '<=') . ' ?';
312
                $par[] = $this->normalizeValue($column, $value['lte']);
313 8
            }
314
            return [
315
                '(' . implode(' AND ', $sql) . ')',
316
                $par
317
            ];
318
        }
319
        return $negate ?
320
            [
321 8
                $name . ' NOT IN (??)',
322 8
                [ array_map(function ($v) use ($column) {
323 8
                    return $this->normalizeValue($column, $v);
324 8
                }, $value) ]
325
            ] :
326
            [
327
                $name . ' IN (??)',
328
                [ array_map(function ($v) use ($column) {
329
                    return $this->normalizeValue($column, $v);
330
                }, $value) ]
331
            ];
332
    }
333
    /**
334 52
     * Filter the results by a column and a value
335
     * @param  string $column  the column name to filter by (related columns can be used - for example: author.name)
336 52
     * @param  mixed  $value   a required value, array of values or range of values (range example: ['beg'=>1,'end'=>3])
337 52
     * @param  bool   $negate  optional boolean indicating that the filter should be negated
338
     * @return $this
339
     */
340
    public function filter(string $column, $value, bool $negate = false) : self
341
    {
342
        $sql = $this->filterSQL($column, $value, $negate);
343
        return strlen($sql[0]) ? $this->where($sql[0], $sql[1]) : $this;
344 16
    }
345
    /**
346 16
     * Filter the results matching any of the criteria
347 16
     * @param  array $criteria  each row is a column, value and optional negate flag (same as filter method)
348 16
     * @return $this
349 16
     */
350 16
    public function any(array $criteria) : self
351 16
    {
352 16
        $sql = [];
353
        $par = [];
354
        foreach ($criteria as $row) {
355 16
            if (isset($row[1])) {
356
                $temp = $this->filterSQL($row[0], $row[1] ?? null, $row[2] ?? false);
357
                $sql[] = $temp[0];
358
                $par = array_merge($par, $temp[1]);
359
            }
360
        }
361
        return $this->where('(' . implode(' OR ', $sql) . ')', $par);
362 12
    }
363
    /**
364 12
     * Filter the results matching all of the criteria
365 12
     * @param  array $criteria  each row is a column, value and optional negate flag (same as filter method)
366 12
     * @return $this
367 12
     */
368 12
    public function all(array $criteria) : self
369 12
    {
370 12
        $sql = [];
371
        $par = [];
372
        foreach ($criteria as $row) {
373 12
            if (isset($row[1])) {
374
                $temp = $this->filterSQL($row[0], $row[1] ?? null, $row[2] ?? false);
375
                $sql[] = $temp[0];
376
                $par = array_merge($par, $temp[1]);
377
            }
378
        }
379
        return $this->where('(' . implode(' AND ', $sql) . ')', $par);
380
    }
381
    /**
382
     * Sort by a column
383
     * @param  string       $column the column name to sort by (related columns can be used - for example: author.name)
384
     * @param  bool|boolean $desc   should the sorting be in descending order, defaults to `false`
385
     * @return $this
386
     */
387
    public function sort(string $column, bool $desc = false) : self
388
    {
389
        return $this->order($this->getColumn($column)['name'] . ' ' . ($desc ? 'DESC' : 'ASC'));
390 4
    }
391
    /**
392 4
     * Group by a column (or columns)
393 4
     * @param  string|array        $column the column name (or names) to group by
394
     * @return $this
395 4
     */
396 4
    public function group($column) : self
397
    {
398 4
        if (!is_array($column)) {
399
            $column = [ $column ];
400
        }
401
        foreach ($column as $k => $v) {
402
            $column[$k] = $this->getColumn($v)['name'];
403
        }
404
        return $this->groupBy(implode(', ', $column), []);
405
    }
406
    /**
407
     * Get a part of the data
408
     * @param  int|integer $page    the page number to get (1-based), defaults to 1
409
     * @param  int|integer $perPage the number of records per page - defaults to 25
410 4
     * @return $this
411
     */
412 4
    public function paginate(int $page = 1, int $perPage = 25) : self
413
    {
414
        return $this->limit($perPage, ($page - 1) * $perPage);
415 4
    }
416
    public function __call($name, $data)
417
    {
418 4
        if (strpos($name, 'filterBy') === 0) {
419 4
            return $this->filter(strtolower(substr($name, 8)), $data[0]);
420
        }
421
        if (strpos($name, 'sortBy') === 0) {
422
            return $this->sort(strtolower(substr($name, 6)), $data[0]);
423
        }
424
        if (strpos($name, 'groupBy') === 0) {
425
            return $this->group(strtolower(substr($name, 7)));
426 12
        }
427
    }
428 12
    /**
429 12
     * Remove all filters, sorting, etc
430 12
     * @return $this
431 12
     */
432 12
    public function reset() : self
433 12
    {
434 12
        $this->where = [];
435 12
        $this->joins = [];
436 12
        $this->group = [];
437 12
        $this->withr = [];
438
        $this->order = [];
439
        $this->having = [];
440
        $this->aliases = [];
441
        $this->li_of = [0,0,0];
442
        $this->qiterator = null;
443
        return $this;
444
    }
445 4
    /**
446
     * Apply advanced grouping
447 4
     * @param  string $sql    SQL statement to use in the GROUP BY clause
448 4
     * @param  array  $params optional params for the statement (defaults to an empty array)
449 4
     * @return $this
450
     */
451
    public function groupBy(string $sql, array $params = []) : self
452
    {
453
        $this->qiterator = null;
454
        $this->group = [ $sql, $params ];
455
        return $this;
456
    }
457
    /**
458
     * Join a table to the query (no need to do this for relations defined with foreign keys)
459 4
     * @param  Table|string $table     the table to join
460
     * @param  array        $fields    what to join on (joined_table_field => other_field)
461 4
     * @param  string|null  $name      alias for the join, defaults to the table name
462 4
     * @param  bool         $multiple  are multiple rows joined (results in a LEFT JOIN), default to true
463 4
     * @return $this
464
     */
465
    public function join($table, array $fields, string $name = null, bool $multiple = true)
466 4
    {
467 4
        $table = $table instanceof Table ? $table : $this->db->definition((string)$table);
468 4
        $name = $name ?? $table->getName();
469 4
        if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) {
470 4
            throw new DBException('Alias / table name already in use');
471
        }
472 4
        $this->joins[$name] = new TableRelation($name, $table, [], $multiple);
473
        foreach ($fields as $k => $v) {
474
            $k = explode('.', $k, 2);
475
            $k = count($k) == 2 ? $k[1] : $k[0];
476
            $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name'];
477
        }
478
        return $this;
479
    }
480 64
    /**
481
     * Apply an advanced filter (can be called multiple times)
482 64
     * @param  string $sql    SQL statement to be used in the where clause
483 64
     * @param  array  $params parameters for the SQL statement (defaults to an empty array)
484 64
     * @return $this
485
     */
486
    public function where(string $sql, array $params = []) : self
487
    {
488
        $this->qiterator = null;
489
        $this->where[] = [ $sql, $params ];
490
        return $this;
491
    }
492 4
    /**
493
     * Apply an advanced HAVING filter (can be called multiple times)
494 4
     * @param  string $sql    SQL statement to be used in the HAING clause
495 4
     * @param  array  $params parameters for the SQL statement (defaults to an empty array)
496 4
     * @return $this
497
     */
498
    public function having(string $sql, array $params = []) : self
499
    {
500
        $this->qiterator = null;
501
        $this->having[] = [ $sql, $params ];
502
        return $this;
503
    }
504 8
    /**
505
     * Apply advanced sorting
506 8
     * @param  string $sql    SQL statement to use in the ORDER clause
507 8
     * @param  array  $params optional params for the statement (defaults to an empty array)
508 8
     * @return $this
509 8
     */
510
    public function order(string $sql, array $params = []) : self
511 8
    {
512
        $this->qiterator = null;
513
        $name = null;
514 8
        if (!count($params)) {
515 4
            $name = preg_replace('(\s+(ASC|DESC)\s*$)i', '', $sql);
516 4
            try {
517
                if ($name === null) {
518
                    throw new \Exception();
519 8
                }
520 8
                $name = $this->getColumn(trim($name))['name'];
521
            } catch (\Exception $e) {
522
                $name = null;
523
            }
524
        }
525
        $this->order = [ $sql, $params, $name ];
526
        return $this;
527
    }
528 4
    /**
529
     * Apply an advanced limit
530 4
     * @param  int         $limit  number of rows to return
531 4
     * @param  int         $offset number of rows to skip from the beginning (defaults to 0)
532 4
     * @return $this
533
     */
534
    public function limit(int $limit, int $offset = 0, bool $limitOnMainTable = false) : self
535
    {
536
        $this->qiterator = null;
537
        $this->li_of = [ $limit, $offset, $limitOnMainTable ? 1 : 0 ];
538 44
        return $this;
539
    }
540 44
    /**
541 44
     * Get the number of records
542
     * @return int the total number of records (does not respect pagination)
543 12
     */
544 44
    public function count() : int
545 44
    {
546 44
        $aliases = [];
547 44
        $getAlias = function ($name) use (&$aliases) {
548
            // to bypass use: return $name;
549 44
            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
550 44
        };
551
        $table = $this->definition->getFullName();
552
        $sql = 'SELECT COUNT(DISTINCT '.$table.'.'.implode(', '.$table.'.', $this->pkey).') FROM '.$table.' ';
553 44
        $par = [];
554 44
        
555 44
        $relations = $this->withr;
556 44
        foreach ($relations as $k => $v) {
557 44
            $getAlias($k);
558
        }
559 44
        $w = $this->where;
560 44
        $h = $this->having;
561 44
        $o = $this->order;
562 24
        $g = $this->group;
563 12
        $j = array_map(function ($v) {
564 12
            return clone $v;
565
        }, $this->joins);
566
        foreach ($this->definition->getRelations() as $k => $v) {
567 44
            foreach ($w as $kk => $vv) {
568
                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
569
                    $relations[$k] = [ $v, $table ];
570 44
                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
571
                }
572
            }
573
            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
574
                $relations[$k] = [ $v, $table ];
575
            }
576 44
            foreach ($h as $kk => $vv) {
577
                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
578
                    $relations[$k] = [ $v, $table ];
579
                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
580 44
                }
581
            }
582
            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
583
                $relations[$k] = [ $v, $table ];
584
                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
585
            }
586
            foreach ($j as $kk => $vv) {
587
                foreach ($vv->keymap as $kkk => $vvv) {
588
                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vvv)) {
589
                        $relations[$k] = [ $v, $table ];
590
                        $j[$kk]->keymap[$kkk] =
591 44
                            preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vvv);
592 12
                    }
593 12
                }
594 12
            }
595 12
        }
596 12
597 12
        foreach ($relations as $k => $v) {
598 12
            $table = $v[1] !== $this->definition->getName() && $v[1] !== $this->definition->getFullName() ? $getAlias($v[1]) : $v[1];
599 12
            $v = $v[0];
600
            if ($v->pivot) {
601 12
                $alias = $getAlias($k.'_pivot');
602 12
                $sql .= 'LEFT JOIN '.$v->pivot->getFullName().' '.$alias.' ON ';
603 12
                $tmp = [];
604 12
                foreach ($v->keymap as $kk => $vv) {
605 12
                    $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
606
                }
607 12
                $sql .= implode(' AND ', $tmp) . ' ';
608
                $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$getAlias($k).' ON ';
609 12
                $tmp = [];
610 12
                foreach ($v->pivot_keymap as $kk => $vv) {
611 12
                    $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
612 12
                }
613 12
                $sql .= implode(' AND ', $tmp) . ' ';
614
            } else {
615 12
                $alias = $getAlias($k);
616
                $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$alias.' ON ';
617
                $tmp = [];
618
                foreach ($v->keymap as $kk => $vv) {
619 12
                    $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
620
                }
621
                if ($v->sql) {
622 44
                    $tmp[] = $v->sql . ' ';
623
                    $par = array_merge($par, $v->par ?? []);
624
                }
625
                $sql .= implode(' AND ', $tmp) . ' ';
626
            }
627
        }
628
        foreach ($j as $k => $v) {
629
            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getFullName().' '.$k.' ON ';
630 44
            $tmp = [];
631 24
            foreach ($v->keymap as $kk => $vv) {
632 24
                $tmp[] = $kk.' = '.$vv;
633 24
            }
634 24
            $sql .= implode(' AND ', $tmp) . ' ';
635 24
        }
636
        if (count($w)) {
637 24
            $sql .= 'WHERE ';
638
            $tmp = [];
639 44
            foreach ($w as $v) {
640
                $tmp[] = '(' . $v[0] . ')';
641
                $par = array_merge($par, $v[1]);
642
            }
643 44
            $sql .= implode(' AND ', $tmp).' ';
644
        }
645
        if (count($g)) {
646
            $sql .= 'GROUP BY ' . $g[0] . ' ';
647
            $par = array_merge($par, $g[1]);
648
        }
649
        if (count($h)) {
650
            $sql .= 'HAVING ';
651
            $tmp = [];
652 44
            foreach ($h as $v) {
653
                $tmp[] = '(' . $v[0] . ')';
654
                $par = array_merge($par, $v[1]);
655
            }
656
            $sql .= implode(' AND ', $tmp).' ';
657
        }
658
        return $this->db->one($sql, $par);
659 156
    }
660
    /**
661 156
     * Specify which columns to fetch (be default all table columns are fetched)
662 156
     * @param  array $fields optional array of columns to select (related columns can be used too)
663
     * @return $this
664
     */
665
    public function columns(array $fields, bool $addPrimary = true) : self
666
    {
667
        foreach ($fields as $k => $v) {
668
            if (strpos($v, '*') !== false) {
669
                $temp = explode('.', $v);
670
                if (count($temp) === 1) {
671
                    $table = $this->definition->getName();
672
                    $cols = $this->definition->getColumns();
673
                } elseif (count($temp) === 2) {
674
                    $table = $temp[0];
675
                    if ($this->definition->hasRelation($table)) {
676
                        $cols = $this->definition->getRelation($table)->table->getColumns();
677
                    } elseif (isset($this->joins[$table])) {
678
                        $cols = $this->joins[$table]->table->getColumns();
679
                    } else {
680
                        throw new DBException('Invalid foreign table name');
681
                    }
682
                } else {
683
                    array_pop($temp);
684
                    $this->with(implode('.', $temp));
685
                    $table = array_reduce(
686
                        $temp,
687
                        function ($carry, $item) use (&$table) {
688
                            return $table->getRelation($item)->table;
689
                        }
690
                    );
691
                    $cols = $table->getColumns();
692
                    $table = implode(static::SEP, $temp);
693
                }
694 156
                unset($fields[$k]);
695 156
                foreach ($cols as $col) {
696
                    $fields[] = $table . '.' . $col;
697 156
                }
698 4
            }
699 4
        }
700
        $primary = $this->definition->getPrimaryKey();
701
        foreach ($fields as $k => $v) {
702 156
            try {
703 156
                $fields[$k] = $this->getColumn($v)['name'];
704 156
            } catch (DBException $e) {
705 156
                $fields[$k] = $v;
706
            }
707
        }
708
        if ($addPrimary) {
709
            foreach ($primary as $field) {
710 156
                $field = $this->getColumn($field)['name'];
711 156
                if (!in_array($field, $fields)) {
712
                    $fields[] = $field;
713
                }
714
            }
715
        }
716
        $this->fields = $fields;
717
        return $this;
718 120
    }
719
    /**
720 120
     * Perform the actual fetch
721 80
     * @param  array|null $fields optional array of columns to select (related columns can be used too)
722
     * @return mixed               the query result as an iterator (with array access)
723 120
     */
724 120
    public function iterator(array $fields = null, array $collectionKey = null)
725
    {
726 32
        if ($this->qiterator) {
727 120
            return $this->qiterator;
728 120
        }
729 120
        $aliases = [];
730 4
        $getAlias = function ($name) use (&$aliases) {
731
            // to bypass use: return $name;
732 120
            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
733 120
        };
734 24
        $table = $this->definition->getName();
735
        if ($fields !== null) {
736
            $this->columns($fields);
737 120
        }
738 120
        $relations = $this->withr;
739 120
        foreach ($relations as $k => $v) {
740 120
            $getAlias($k);
741 120
        }
742 120
743 4
        $f = $this->fields;
744 120
        $w = $this->where;
745
        $h = $this->having;
746 120
        $o = $this->order;
747 120
        $g = $this->group;
748 120
        $j = array_map(function ($v) {
749
            return clone $v;
750
        }, $this->joins);
751 120
752 120
        $porder = [];
753 120
        foreach ($this->definition->getPrimaryKey() as $field) {
754
            $porder[] = $this->getColumn($field)['name'];
755
        }
756
757
        foreach ($this->definition->getRelations() as $k => $relation) {
758 120
            foreach ($f as $kk => $field) {
759 16
                if (strpos($field, $k . '.') === 0) {
760 8
                    $relations[$k] = [ $relation, $table ];
761 8
                    $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field);
762
                }
763
            }
764 120
            foreach ($w as $kk => $v) {
765 4
                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
766
                    $relations[$k] = [ $relation, $table ];
767
                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
768
                }
769
            }
770 120
            foreach ($h as $kk => $v) {
771
                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
772
                    $relations[$k] = [ $relation, $table ];
773
                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
774 120
                }
775
            }
776
            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
777
                $relations[$k] = [ $relation, $table ];
778 120
                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
779 4
            }
780 4
            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
781
                $relations[$k] = [ $relation, $table ];
782
                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
783
            }
784
            foreach ($j as $kk => $v) {
785
                foreach ($v->keymap as $kkk => $vv) {
786
                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
787
                        $relations[$k] = [ $relation, $table ];
788
                        $j[$kk]->keymap[$kkk] = preg_replace(
789
                            '(\b'.preg_quote($k . '.'). ')i',
790
                            $getAlias($k) . '.',
791 120
                            $vv
792 120
                        );
793 120
                    }
794
                }
795 120
            }
796 24
        }
797 24
        $select = [];
798
        foreach ($f as $k => $field) {
799
            $select[] = $field . (!is_numeric($k) ? ' ' . $k : '');
800 120
        }
801 120
        foreach ($this->withr as $name => $relation) {
802 120
            foreach ($relation[0]->table->getColumns() as $column) {
803 120
                $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column);
804 32
            }
805 32
        }
806 32
        $sql = 'SELECT '.implode(', ', $select).' FROM '.$this->definition->getFullName().' ';
807 24
        $par = [];
808
        $many = false;
809 32
        foreach ($relations as $relation => $v) {
810 20
            $table = $v[1] !== $this->definition->getName() && $v[1] !== $this->definition->getFullName() ? $getAlias($v[1]) : $v[1];
811 20
            $v = $v[0];
812 20
            if ($v->many || $v->pivot) {
813 20
                $many = true;
814 20
            }
815
            if ($v->pivot) {
816 20
                $alias = $getAlias($relation.'_pivot');
817 20
                $sql .= 'LEFT JOIN '.$v->pivot->getFullName().' '.$alias.' ON ';
818 20
                $tmp = [];
819 20
                foreach ($v->keymap as $kk => $vv) {
820 20
                    $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
821
                }
822 20
                $sql .= implode(' AND ', $tmp) . ' ';
823
                $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$getAlias($relation).' ON ';
824 24
                $tmp = [];
825
                foreach ($v->pivot_keymap as $kk => $vv) {
826 24
                    $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' ';
827 24
                }
828 24
                $sql .= implode(' AND ', $tmp) . ' ';
829 24
            } else {
830
                $alias = $getAlias($relation);
831 24
832
                $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$alias.' ON ';
833
                $tmp = [];
834
                foreach ($v->keymap as $kk => $vv) {
835 24
                    $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
836
                }
837
                if ($v->sql) {
838 120
                    $tmp[] = $v->sql . ' ';
839 4
                    $par = array_merge($par, $v->par ?? []);
840 4
                }
841
                $sql .= implode(' AND ', $tmp) . ' ';
842 4
            }
843 4
        }
844 4
        foreach ($j as $k => $v) {
845 4
            if ($v->many) {
846
                $many = true;
847 4
            }
848
            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
849 120
            $tmp = [];
850
            foreach ($v->keymap as $kk => $vv) {
851
                $tmp[] = $kk.' = '.$vv;
852
            }
853
            $sql .= implode(' AND ', $tmp) . ' ';
854
        }
855
        if ($many && count($porder) && $this->li_of[2] === 1) {
856
            $ids = $this->ids();
857
            if (count($ids)) {
858
                if (count($porder) > 1) {
859
                    $pkw = [];
860
                    foreach ($porder as $name) {
861
                        $pkw[] = $name . ' = ?';
862
                    }
863
                    $pkw = '(' . implode(' AND ', $pkw) . ')';
864
                    $pkp = [];
865
                    foreach ($ids as $id) {
866
                        foreach ($id as $p) {
867
                            $pkp[] = $p;
868
                        }
869
                    }
870
                    $w[] = [
871
                        implode(' OR ', array_fill(0, count($ids), $pkw)),
872
                        $pkp
873
                    ];
874
                } else {
875 120
                    $w[] = [ $porder[0] . ' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids ];
876 16
                }
877 16
            } else {
878 16
                $w[] = [ '1=0', [] ];
879 16
            }
880 16
        }
881
        if (count($w)) {
882 16
            $sql .= 'WHERE ';
883
            $tmp = [];
884 120
            foreach ($w as $v) {
885 4
                $tmp[] = '(' . $v[0] . ')';
886 4
                $par = array_merge($par, $v[1]);
887
            }
888 120
            $sql .= implode(' AND ', $tmp).' ';
889 4
        }
890 4
        if (count($g)) {
891 4
            $sql .= 'GROUP BY ' . $g[0] . ' ';
892 4
            $par = array_merge($par, $g[1]);
893 4
        }
894
        if (count($h)) {
895 4
            $sql .= 'HAVING ';
896
            $tmp = [];
897 120
            foreach ($h as $v) {
898 4
                $tmp[] = '(' . $v[0] . ')';
899 4
                $par = array_merge($par, $v[1]);
900
            }
901 120
            $sql .= implode(' AND ', $tmp).' ';
902 116
        }
903 116
        if (count($o)) {
904 116
            $sql .= 'ORDER BY ' . $o[0] . ' ';
905 116
            $par = array_merge($par, $o[1]);
906 116
        }
907
        if (!count($g) && count($porder)) {
908 120
            $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC';
909 4
            $porder = array_map(function ($v) use ($pdir) {
910
                return $v . ' ' . $pdir;
911
            }, $porder);
912
            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
913
        }
914
        if ((!$many || $this->li_of[2] === 0 || !count($porder)) && $this->li_of[0]) {
915
            if ($this->db->driverName() === 'oracle') {
916
                if ((int)$this->db->driverOption('version', 0) >= 12) {
917
                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
918
                } else {
919
                    $f = array_map(function ($v) {
920
                        $v = explode(' ', trim($v), 2);
921
                        if (count($v) === 2) {
922
                            return $v[1];
923
                        }
924
                        $v = explode('.', $v[0], 2);
925
                        return count($v) === 2 ? $v[1] : $v[0];
926
                    }, $select);
927
                    $sql = "SELECT " . implode(', ', $f) . " 
928
                            FROM (
929
                                SELECT tbl__.*, rownum rnum__ FROM (
930 4
                                    " . $sql . "
931
                                ) tbl__ 
932
                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
933 120
                            ) WHERE rnum__ > " . $this->li_of[1];
934 120
                }
935 120
            } else {
936 120
                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
937 120
            }
938
        }
939
        return $this->qiterator = new TableQueryIterator(
940
            $this->db->get($sql, $par, null, false, false, true),
941
            $collectionKey ?? $this->pkey,
942
            $this->withr,
943
            $aliases
944
        );
945 4
    }
946
    /**
947 4
     * Perform the actual fetch
948
     * @param  array|null $fields optional array of columns to select (related columns can be used too)
949
     * @return array               the query result as an array
950
     */
951
    public function select(array $fields = null, array $collectionKey = null) : array
952
    {
953
        return iterator_to_array($this->iterator($fields, $collectionKey));
954 16
    }
955
    /**
956 16
     * Insert a new row in the table
957 16
     * @param  array   $data   key value pairs, where each key is the column name and the value is the value to insert
958 16
     * @return array           the inserted ID where keys are column names and values are column values
959 16
     */
960 16
    public function insert(array $data) : array
961 16
    {
962
        $table = $this->definition->getFullName();
963
        $columns = $this->definition->getFullColumns();
964 12
        $insert = [];
965
        foreach ($data as $column => $value) {
966
            if (isset($columns[$column])) {
967 12
                $insert[$column] = $this->normalizeValue($columns[$column], $value);
968 12
            }
969 12
        }
970 12
        if (!count($insert)) {
971
            throw new DBException('No valid columns to insert');
972
        }
973
        $sql = 'INSERT INTO '.$table.' ('.implode(', ', array_keys($insert)).') VALUES (??)';
974 12
        $par = [$insert];
975
        $primary = $this->definition->getPrimaryKey();
976
        if (!count($primary)) {
977
            $this->db->query($sql, $par);
978
            return [];
979
        }
980
        if ($this->db->driverName() === 'oracle') {
981
            $ret = [];
982
            foreach ($primary as $k) {
983
                $ret[$k] = str_repeat(' ', 255);
984
                $par[] = &$ret[$k];
985 12
            }
986 12
            $sql .= ' RETURNING ' . implode(',', $primary) .
987 12
                ' INTO ' . implode(',', array_fill(0, count($primary), '?'));
988 12
            $this->db->query($sql, $par);
989
            return $ret;
990 12
        } else {
991
            $ret = [];
992
            $ins = $this->db->query($sql, $par)->insertID();
993
            foreach ($primary as $k) {
994
                $ret[$k] = $data[$k] ?? $ins;
995
            }
996
            return $ret;
997
        }
998 12
    }
999
    /**
1000 12
     * Update the filtered rows with new data
1001 12
     * @param  array  $data key value pairs, where each key is the column name and the value is the value to insert
1002 12
     * @return int          the number of affected rows
1003 12
     */
1004 12
    public function update(array $data) : int
1005 12
    {
1006
        $table = $this->definition->getName();
1007
        $columns = $this->definition->getFullColumns();
1008 12
        $update = [];
1009
        foreach ($data as $column => $value) {
1010
            if (isset($columns[$column])) {
1011 12
                $update[$column] = $this->normalizeValue($columns[$column], $value);
1012 12
            }
1013 12
        }
1014 12
        if (!count($update)) {
1015 12
            throw new DBException('No valid columns to update');
1016 12
        }
1017 12
        $sql = 'UPDATE '.$table.' SET ';
1018 12
        $par = [];
1019 12
        $sql .= implode(', ', array_map(function ($v) {
1020 12
            return $v . ' = ?';
1021 12
        }, array_keys($update))) . ' ';
1022 12
        $par = array_merge($par, array_values($update));
1023
        if (count($this->where)) {
1024 12
            $sql .= 'WHERE ';
1025
            $tmp = [];
1026 12
            foreach ($this->where as $v) {
1027
                $tmp[] = $v[0];
1028
                $par = array_merge($par, $v[1]);
1029
            }
1030 12
            $sql .= implode(' AND ', $tmp) . ' ';
1031
        }
1032
        if (count($this->order)) {
1033
            $sql .= $this->order[0];
1034
            $par = array_merge($par, $this->order[1]);
1035
        }
1036 8
        return $this->db->query($sql, $par)->affected();
1037
    }
1038 8
    /**
1039 8
     * Delete the filtered rows from the DB
1040 8
     * @return int the number of deleted rows
1041 8
     */
1042 8
    public function delete() : int
1043 8
    {
1044 8
        $table = $this->definition->getFullName();
1045 8
        $sql = 'DELETE FROM '.$table.' ';
1046 8
        $par = [];
1047
        if (count($this->where)) {
1048 8
            $sql .= 'WHERE ';
1049
            $tmp = [];
1050 8
            foreach ($this->where as $v) {
1051
                $tmp[] = $v[0];
1052
                $par = array_merge($par, $v[1]);
1053
            }
1054 8
            $sql .= implode(' AND ', $tmp) . ' ';
1055
        }
1056
        if (count($this->order)) {
1057
            $sql .= $this->order[0];
1058
            $par = array_merge($par, $this->order[1]);
1059
        }
1060
        return $this->db->query($sql, $par)->affected();
1061 28
    }
1062
    /**
1063 28
     * Solve the n+1 queries problem by prefetching a relation by name
1064 28
     * @param  string $relation the relation name to fetch along with the data
1065 28
     * @return $this
1066 28
     */
1067 28
    public function with(string $relation) : self
1068 28
    {
1069 28
        $this->qiterator = null;
1070
        $table = $this->definition;
1071
        if ($table->hasRelation($relation)) {
1072 28
            $temp = $table->getRelation($relation);
1073 28
            $this->withr[$relation] = [ $temp, $table->getName() ];
1074 28
        } else {
1075 28
            $parts = explode('.', $relation);
1076 28
            array_reduce(
1077 28
                $parts,
1078
                function ($carry, $item) use (&$table) {
1079 28
                    if (!$table->hasRelation($item)) {
1080
                        throw new DBException('Invalid relation name: '.$table->getName().' -> ' . $item);
1081
                    }
1082 20
                    $relation = $table->getRelation($item);
1083
                    $name = $carry ? $carry . static::SEP . $item : $item;
1084 20
                    $this->withr[$name] = [ $relation, $carry ?? $table->getName() ];
1085
                    $table = $relation->table;
1086
                    return $name;
1087 104
                }
1088
            );
1089 104
        }
1090
        return $this;
1091
    }
1092
1093
    public function getIterator()
1094
    {
1095
        return $this->iterator();
1096
    }
1097
1098
    public function offsetGet($offset)
1099
    {
1100
        return $this->iterator()->offsetGet($offset);
1101
    }
1102
    public function offsetExists($offset)
1103
    {
1104
        return $this->iterator()->offsetExists($offset);
1105
    }
1106
    public function offsetUnset($offset)
1107
    {
1108
        $this->iterator()->offsetUnset($offset);
1109 4
    }
1110
    public function offsetSet($offset, $value)
1111 4
    {
1112
        $this->iterator()->offsetSet($offset, $value);
1113
    }
1114 4
1115
    public function collection(array $fields = null) : Collection
1116
    {
1117
        return new Collection($this->iterator($fields));
1118 4
    }
1119 4
1120
    public function ids()
1121 4
    {
1122 4
        if (count($this->group)) {
1123
            throw new DBException('Can not LIMIT result set by master table when GROUP BY is used');
1124 4
        }
1125 4
        if (count($this->order) && !isset($this->order[2])) {
1126 4
            throw new DBException('Can not LIMIT result set by master table with a complex ORDER BY query');
1127 4
        }
1128
1129 4
        $aliases = [];
1130 4
        $getAlias = function ($name) use (&$aliases) {
1131 4
            // to bypass use: return $name;
1132 4
            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
1133 4
        };
1134
        
1135 4
        $table = $this->definition->getName();
1136 4
        $relations = $this->withr;
1137 4
        foreach ($relations as $k => $v) {
1138 4
            $getAlias($k);
1139 4
        }
1140 4
        $w = $this->where;
1141
        $h = $this->having;
1142
        $o = $this->order;
1143 4
        $g = $this->group;
1144 4
        $j = array_map(function ($v) {
1145 4
            return clone $v;
1146 4
        }, $this->joins);
1147
        foreach ($this->definition->getRelations() as $k => $v) {
1148 4
            foreach ($w as $kk => $vv) {
1149
                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1150
                    $relations[$k] = [ $v, $table ];
1151
                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
1152
                }
1153
            }
1154 4
            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
1155
                $relations[$k] = [ $v, $table ];
1156
                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
1157
                $o[2] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[2]);
1158 4
            }
1159
            foreach ($h as $kk => $vv) {
1160
                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1161
                    $relations[$k] = [ $v, $table ];
1162
                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
1163
                }
1164
            }
1165
            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
1166
                $relations[$k] = [ $v, $table ];
1167
                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
1168
            }
1169 4
            foreach ($j as $kk => $vv) {
1170 4
                foreach ($vv->keymap as $kkk => $vvv) {
1171 4
                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vvv)) {
1172 4
                        $relations[$k] = [ $v, $table ];
1173 4
                        $j[$kk]->keymap[$kkk] =
1174 4
                            preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vvv);
1175 4
                    }
1176 4
                }
1177
            }
1178
        }
1179 4
1180 4
        $key = array_map(function ($v) use ($table) {
1181 4
            return $table . '.' . $v;
1182
        }, $this->pkey);
1183
        $own = false;
1184
        $dir = 'ASC';
1185 4
        if (count($o)) {
1186
            $dir = strpos($o[0], ' DESC') ? 'DESC' : 'ASC';
1187
            $own = strpos($o[2], $table . '.') === 0;
1188 4
        }
1189
1190 4
        $dst = $key;
1191 4
        if (count($o)) {
1192 4
            if ($own) {
1193 4
                // if using own table - do not use max/min in order - that will prevent index usage
1194 4
                $dst[] = $o[2] . ' orderbyfix___';
1195 4
            } else {
1196
                $dst[] = 'MAX(' . $o[2] . ') orderbyfix___';
1197
            }
1198
        }
1199
        $dst = array_unique($dst);
1200
1201
        $par = [];
1202
        $sql  = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$this->definition->getFullName().' ';
1203
        foreach ($relations as $k => $v) {
1204
            $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
1205
            $v = $v[0];
1206
            if ($v->pivot) {
1207
                $alias = $getAlias($k.'_pivot');
1208
                $sql .= 'LEFT JOIN '.$v->pivot->getName().' '.$alias.' ON ';
1209
                $tmp = [];
1210 4
                foreach ($v->keymap as $kk => $vv) {
1211 4
                    $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1212 4
                }
1213 4
                $sql .= implode(' AND ', $tmp) . ' ';
1214 4
                $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON ';
1215
                $tmp = [];
1216 4
                foreach ($v->pivot_keymap as $kk => $vv) {
1217
                    $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
1218
                }
1219
                $sql .= implode(' AND ', $tmp) . ' ';
1220 4
            } else {
1221
                $alias = $getAlias($k);
1222
                $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON ';
1223 4
                $tmp = [];
1224
                foreach ($v->keymap as $kk => $vv) {
1225
                    $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1226
                }
1227
                if ($v->sql) {
1228
                    $tmp[] = $v->sql . ' ';
1229
                    $par = array_merge($par, $v->par ?? []);
1230
                }
1231 4
                $sql .= implode(' AND ', $tmp) . ' ';
1232 4
            }
1233 4
        }
1234 4
        foreach ($j as $k => $v) {
1235 4
            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
1236 4
            $tmp = [];
1237
            foreach ($v->keymap as $kk => $vv) {
1238 4
                $tmp[] = $kk.' = '.$vv;
1239
            }
1240 4
            $sql .= implode(' AND ', $tmp) . ' ';
1241 4
        }
1242
        if (count($w)) {
1243 4
            $sql .= 'WHERE ';
1244
            $tmp = [];
1245
            foreach ($w as $v) {
1246
                $tmp[] = '(' . $v[0] . ')';
1247
                $par = array_merge($par, $v[1]);
1248
            }
1249
            $sql .= implode(' AND ', $tmp).' ';
1250
        }
1251
        if (!$own) {
1252 4
            $sql .= 'GROUP BY ' . implode(', ', $key) . ' ';
1253 4
        }
1254 4
        if (count($h)) {
1255
            $sql .= 'HAVING ';
1256
            $tmp = [];
1257 4
            foreach ($h as $v) {
1258
                $tmp[] = '(' . $v[0] . ')';
1259
                $par = array_merge($par, $v[1]);
1260 4
            }
1261 4
            $sql .= implode(' AND ', $tmp).' ';
1262 4
        }
1263 4
        if (count($o)) {
1264
            $sql .= 'ORDER BY ';
1265 4
            if ($own) {
1266 4
                $sql .= $o[2] . ' ' . $dir;
1267
            } else {
1268
                $sql .= 'MAX('.$o[2].') ' . $dir;
1269 4
            }
1270
        }
1271
        $porder = [];
1272
        $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC';
1273
        foreach ($this->definition->getPrimaryKey() as $field) {
1274
            $porder[] = $this->getColumn($field)['name'] . ' ' . $pdir;
1275
        }
1276
        if (count($porder)) {
1277
            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
1278
        }
1279
1280
        if ($this->li_of[0]) {
1281
            if ($this->db->driverName() === 'oracle') {
1282
                if ((int)$this->db->driverOption('version', 0) >= 12) {
1283
                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
1284
                } else {
1285
                    $sql = "SELECT " . implode(', ', $dst) . " 
1286 4
                            FROM (
1287 4
                                SELECT tbl__.*, rownum rnum__ FROM (
1288 4
                                    " . $sql . "
1289
                                ) tbl__ 
1290 4
                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
1291 4
                            ) WHERE rnum__ > " . $this->li_of[1];
1292
                }
1293
            } else {
1294
                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
1295
            }
1296
        }
1297
        return array_map(function ($v) {
1298
            if (array_key_exists('orderbyfix___', $v)) {
1299
                unset($v['orderbyfix___']);
1300
            }
1301
            return count($v) === 1 ? array_values($v)[0] : $v;
1302
        }, $this->db->all($sql, $par, null, false, false));
1303
    }
1304
    public function find($primary)
1305
    {
1306
        $columns = $this->definition->getPrimaryKey();
1307
        if (!count($columns)) {
1308
            throw new DBException('Missing primary key');
1309
        }
1310
        if (!is_array($primary)) {
1311
            $temp = [];
1312
            $temp[$columns[0]] = $primary;
1313
            $primary = $temp;
1314
        }
1315
        foreach ($columns as $k) {
1316
            if (!isset($primary[$k])) {
1317
                throw new DBException('Missing primary key component');
1318
            }
1319
            $this->filter($k, $primary[$k]);
1320
        }
1321
        return $this->iterator()[0] ?? null;
1322
    }
1323
}
1324