Completed
Push — master ( 8a5ed3...6caf88 )
by Ivan
02:58
created

TableQuery::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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