Completed
Push — master ( 72179e...eb4f06 )
by Ivan
02:58
created
src/schema/TableQuery.php 1 patch
Spacing   +115 added lines, -118 removed lines patch added patch discarded remove patch
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
     /**
44 44
      * @var int[]
45 45
      */
46
-    protected $li_of = [0,0];
46
+    protected $li_of = [0, 0];
47 47
     /**
48 48
      * @var array
49 49
      */
@@ -73,7 +73,7 @@  discard block
 block discarded – undo
73 73
     public function __construct(DBInterface $db, $table)
74 74
     {
75 75
         $this->db = $db;
76
-        $this->definition = $table instanceof Table ? $table : $this->db->definition((string)$table);
76
+        $this->definition = $table instanceof Table ? $table : $this->db->definition((string) $table);
77 77
         $primary = $this->definition->getPrimaryKey();
78 78
         $columns = $this->definition->getColumns();
79 79
         $this->pkey = count($primary) ? $primary : $columns;
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
     {
97 97
         $column = explode('.', $column);
98 98
         if (count($column) === 1) {
99
-            $column = [ $this->definition->getName(), $column[0] ];
99
+            $column = [$this->definition->getName(), $column[0]];
100 100
             $col = $this->definition->getColumn($column[1]);
101 101
             if (!$col) {
102 102
                 throw new DBException('Invalid column name in own table');
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
                         throw new DBException('Invalid column name in related table');
120 120
                     }
121 121
                 } else {
122
-                    throw new DBException('Invalid foreign table name: ' . implode(',', $column));
122
+                    throw new DBException('Invalid foreign table name: '.implode(',', $column));
123 123
                 }
124 124
             }
125 125
         } else {
@@ -128,15 +128,15 @@  discard block
 block discarded – undo
128 128
             $table = $this->definition;
129 129
             $table = array_reduce(
130 130
                 $column,
131
-                function ($carry, $item) use (&$table) {
131
+                function($carry, $item) use (&$table) {
132 132
                     $table = $table->getRelation($item)->table;
133 133
                     return $table;
134 134
                 }
135 135
             );
136 136
             $col = $table->getColumn($name);
137
-            $column = [ implode(static::SEP, $column), $name ];
137
+            $column = [implode(static::SEP, $column), $name];
138 138
         }
139
-        return [ 'name' => implode('.', $column), 'data' => $col ];
139
+        return ['name' => implode('.', $column), 'data' => $col];
140 140
     }
141 141
     protected function normalizeValue(TableColumn $col, $value)
142 142
     {
@@ -183,7 +183,7 @@  discard block
 block discarded – undo
183 183
                 }
184 184
                 return $value;
185 185
             case 'int':
186
-                return (int)$value;
186
+                return (int) $value;
187 187
             default:
188 188
                 return $value;
189 189
         }
@@ -201,25 +201,23 @@  discard block
 block discarded – undo
201 201
         list($name, $column) = array_values($this->getColumn($column));
202 202
         if (is_null($value)) {
203 203
             return $negate ?
204
-                $this->where($name . ' IS NOT NULL') :
205
-                $this->where($name . ' IS NULL');
204
+                $this->where($name.' IS NOT NULL') : $this->where($name.' IS NULL');
206 205
         }
207 206
         if (!is_array($value)) {
208 207
             return $negate ?
209 208
                 $this->where(
210
-                    $name . ' <> ?',
211
-                    [ $this->normalizeValue($column, $value) ]
212
-                ) :
213
-                $this->where(
214
-                    $name . ' = ?',
215
-                    [ $this->normalizeValue($column, $value) ]
209
+                    $name.' <> ?',
210
+                    [$this->normalizeValue($column, $value)]
211
+                ) : $this->where(
212
+                    $name.' = ?',
213
+                    [$this->normalizeValue($column, $value)]
216 214
                 );
217 215
         }
218 216
         if (isset($value['beg']) && strlen($value['beg']) && (!isset($value['end']) || !strlen($value['end']))) {
219
-            $value = [ 'gte' => $value['beg'] ];
217
+            $value = ['gte' => $value['beg']];
220 218
         }
221 219
         if (isset($value['end']) && strlen($value['end']) && (!isset($value['beg']) || !strlen($value['beg']))) {
222
-            $value = [ 'lte' => $value['end'] ];
220
+            $value = ['lte' => $value['end']];
223 221
         }
224 222
         if (isset($value['beg']) && isset($value['end'])) {
225 223
             return $negate ?
@@ -229,8 +227,7 @@  discard block
 block discarded – undo
229 227
                         $this->normalizeValue($column, $value['beg']),
230 228
                         $this->normalizeValue($column, $value['end'])
231 229
                     ]
232
-                ) :
233
-                $this->where(
230
+                ) : $this->where(
234 231
                     $name.' BETWEEN ? AND ?',
235 232
                     [
236 233
                         $this->normalizeValue($column, $value['beg']),
@@ -241,38 +238,38 @@  discard block
 block discarded – undo
241 238
         if (isset($value['gt']) || isset($value['lt']) || isset($value['gte']) || isset($value['lte'])) {
242 239
             if (isset($value['gt'])) {
243 240
                 $this->where(
244
-                    $name. ' ' . ($negate ? '<=' : '>') . ' ?',
245
-                    [ $this->normalizeValue($column, $value['gt']) ]
241
+                    $name.' '.($negate ? '<=' : '>').' ?',
242
+                    [$this->normalizeValue($column, $value['gt'])]
246 243
                 );
247 244
             }
248 245
             if (isset($value['gte'])) {
249 246
                 $this->where(
250
-                    $name. ' ' . ($negate ? '<' : '>=') . ' ?',
251
-                    [ $this->normalizeValue($column, $value['gte']) ]
247
+                    $name.' '.($negate ? '<' : '>=').' ?',
248
+                    [$this->normalizeValue($column, $value['gte'])]
252 249
                 );
253 250
             }
254 251
             if (isset($value['lt'])) {
255 252
                 $this->where(
256
-                    $name. ' ' . ($negate ? '>=' : '<') . ' ?',
257
-                    [ $this->normalizeValue($column, $value['lt']) ]
253
+                    $name.' '.($negate ? '>=' : '<').' ?',
254
+                    [$this->normalizeValue($column, $value['lt'])]
258 255
                 );
259 256
             }
260 257
             if (isset($value['lte'])) {
261 258
                 $this->where(
262
-                    $name. ' ' . ($negate ? '>' : '<=') . ' ?',
263
-                    [ $this->normalizeValue($column, $value['lte']) ]
259
+                    $name.' '.($negate ? '>' : '<=').' ?',
260
+                    [$this->normalizeValue($column, $value['lte'])]
264 261
                 );
265 262
             }
266 263
             return $this;
267 264
         }
268 265
         return $negate ?
269 266
             $this->where(
270
-                $name . ' NOT IN (??)',
271
-                [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ]
267
+                $name.' NOT IN (??)',
268
+                [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)]
272 269
             ) :
273 270
             $this->where(
274
-                $name . ' IN (??)',
275
-                [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ]
271
+                $name.' IN (??)',
272
+                [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)]
276 273
             );
277 274
     }
278 275
     /**
@@ -283,7 +280,7 @@  discard block
 block discarded – undo
283 280
      */
284 281
     public function sort(string $column, bool $desc = false) : TableQuery
285 282
     {
286
-        return $this->order($this->getColumn($column)['name'] . ' ' . ($desc ? 'DESC' : 'ASC'));
283
+        return $this->order($this->getColumn($column)['name'].' '.($desc ? 'DESC' : 'ASC'));
287 284
     }
288 285
     /**
289 286
      * Group by a column (or columns)
@@ -293,7 +290,7 @@  discard block
 block discarded – undo
293 290
     public function group($column) : TableQuery
294 291
     {
295 292
         if (!is_array($column)) {
296
-            $column = [ $column ];
293
+            $column = [$column];
297 294
         }
298 295
         foreach ($column as $k => $v) {
299 296
             $column[$k] = $this->getColumn($v)['name'];
@@ -335,7 +332,7 @@  discard block
 block discarded – undo
335 332
         $this->order = [];
336 333
         $this->having = [];
337 334
         $this->aliases = [];
338
-        $this->li_of = [0,0];
335
+        $this->li_of = [0, 0];
339 336
         $this->qiterator = null;
340 337
         return $this;
341 338
     }
@@ -348,7 +345,7 @@  discard block
 block discarded – undo
348 345
     public function groupBy(string $sql, array $params = []) : TableQuery
349 346
     {
350 347
         $this->qiterator = null;
351
-        $this->group = [ $sql, $params ];
348
+        $this->group = [$sql, $params];
352 349
         return $this;
353 350
     }
354 351
     /**
@@ -361,7 +358,7 @@  discard block
 block discarded – undo
361 358
      */
362 359
     public function join($table, array $fields, string $name = null, bool $multiple = true)
363 360
     {
364
-        $table = $table instanceof Table ? $table : $this->db->definition((string)$table);
361
+        $table = $table instanceof Table ? $table : $this->db->definition((string) $table);
365 362
         $name = $name ?? $table->getName();
366 363
         if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) {
367 364
             throw new DBException('Alias / table name already in use');
@@ -370,7 +367,7 @@  discard block
 block discarded – undo
370 367
         foreach ($fields as $k => $v) {
371 368
             $k = explode('.', $k, 2);
372 369
             $k = count($k) == 2 ? $k[1] : $k[0];
373
-            $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name'];
370
+            $this->joins[$name]->keymap[$this->getColumn($name.'.'.$k)['name']] = $this->getColumn($v)['name'];
374 371
         }
375 372
         return $this;
376 373
     }
@@ -383,7 +380,7 @@  discard block
 block discarded – undo
383 380
     public function where(string $sql, array $params = []) : TableQuery
384 381
     {
385 382
         $this->qiterator = null;
386
-        $this->where[] = [ $sql, $params ];
383
+        $this->where[] = [$sql, $params];
387 384
         return $this;
388 385
     }
389 386
     /**
@@ -395,7 +392,7 @@  discard block
 block discarded – undo
395 392
     public function having(string $sql, array $params = []) : TableQuery
396 393
     {
397 394
         $this->qiterator = null;
398
-        $this->having[] = [ $sql, $params ];
395
+        $this->having[] = [$sql, $params];
399 396
         return $this;
400 397
     }
401 398
     /**
@@ -407,7 +404,7 @@  discard block
 block discarded – undo
407 404
     public function order(string $sql, array $params = []) : TableQuery
408 405
     {
409 406
         $this->qiterator = null;
410
-        $this->order = [ $sql, $params ];
407
+        $this->order = [$sql, $params];
411 408
         return $this;
412 409
     }
413 410
     /**
@@ -419,7 +416,7 @@  discard block
 block discarded – undo
419 416
     public function limit(int $limit, int $offset = 0) : TableQuery
420 417
     {
421 418
         $this->qiterator = null;
422
-        $this->li_of = [ $limit, $offset ];
419
+        $this->li_of = [$limit, $offset];
423 420
         return $this;
424 421
     }
425 422
     /**
@@ -429,9 +426,9 @@  discard block
 block discarded – undo
429 426
     public function count() : int
430 427
     {
431 428
         $aliases = [];
432
-        $getAlias = function ($name) use (&$aliases) {
429
+        $getAlias = function($name) use (&$aliases) {
433 430
             // to bypass use: return $name;
434
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
431
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
435 432
         };
436 433
         $table = $this->definition->getName();
437 434
         $sql = 'SELECT COUNT(DISTINCT '.$table.'.'.implode(', '.$table.'.', $this->pkey).') FROM '.$table.' ';
@@ -446,44 +443,44 @@  discard block
 block discarded – undo
446 443
         $h = $this->having;
447 444
         $o = $this->order;
448 445
         $g = $this->group;
449
-        $j = array_map(function ($v) { return clone $v; }, $this->joins);
446
+        $j = array_map(function($v) { return clone $v; }, $this->joins);
450 447
         foreach ($this->definition->getRelations() as $k => $v) {
451 448
             foreach ($w as $kk => $vv) {
452
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
453
-                    $relations[$k] = [ $v, $table ];
454
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
449
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
450
+                    $relations[$k] = [$v, $table];
451
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
455 452
                 }
456 453
             }
457
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
458
-                $relations[$k] = [ $v, $table ];
454
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
455
+                $relations[$k] = [$v, $table];
459 456
             }
460 457
             foreach ($h as $kk => $vv) {
461
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
462
-                    $relations[$k] = [ $relation, $table ];
463
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
458
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
459
+                    $relations[$k] = [$relation, $table];
460
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
464 461
                 }
465 462
             }
466
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
467
-                $relations[$k] = [ $relation, $table ];
468
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
463
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
464
+                $relations[$k] = [$relation, $table];
465
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
469 466
             }
470 467
             foreach ($j as $kk => $v) {
471 468
                 foreach ($v->keymap as $kkk => $vv) {
472
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
473
-                        $relations[$k] = [ $relation, $table ];
474
-                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv);
469
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
470
+                        $relations[$k] = [$relation, $table];
471
+                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv);
475 472
                     }
476 473
                 }
477 474
             }
478 475
         }
479 476
 
480 477
         foreach ($j as $k => $v) {
481
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
478
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
482 479
             $tmp = [];
483 480
             foreach ($v->keymap as $kk => $vv) {
484 481
                 $tmp[] = $kk.' = '.$vv;
485 482
             }
486
-            $sql .= implode(' AND ', $tmp) . ' ';
483
+            $sql .= implode(' AND ', $tmp).' ';
487 484
         }
488 485
         foreach ($relations as $k => $v) {
489 486
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
@@ -495,13 +492,13 @@  discard block
 block discarded – undo
495 492
                 foreach ($v->keymap as $kk => $vv) {
496 493
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
497 494
                 }
498
-                $sql .= implode(' AND ', $tmp) . ' ';
495
+                $sql .= implode(' AND ', $tmp).' ';
499 496
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON ';
500 497
                 $tmp = [];
501 498
                 foreach ($v->pivot_keymap as $kk => $vv) {
502 499
                     $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
503 500
                 }
504
-                $sql .= implode(' AND ', $tmp) . ' ';
501
+                $sql .= implode(' AND ', $tmp).' ';
505 502
             } else {
506 503
                 $alias = $getAlias($k);
507 504
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON ';
@@ -510,30 +507,30 @@  discard block
 block discarded – undo
510 507
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
511 508
                 }
512 509
                 if ($v->sql) {
513
-                    $tmp[] = $v->sql . ' ';
510
+                    $tmp[] = $v->sql.' ';
514 511
                     $par = array_merge($par, $v->par ?? []);
515 512
                 }
516
-                $sql .= implode(' AND ', $tmp) . ' ';
513
+                $sql .= implode(' AND ', $tmp).' ';
517 514
             }
518 515
         }
519 516
         if (count($w)) {
520 517
             $sql .= 'WHERE ';
521 518
             $tmp = [];
522 519
             foreach ($w as $v) {
523
-                $tmp[] = '(' . $v[0] . ')';
520
+                $tmp[] = '('.$v[0].')';
524 521
                 $par = array_merge($par, $v[1]);
525 522
             }
526 523
             $sql .= implode(' AND ', $tmp).' ';
527 524
         }
528 525
         if (count($g)) {
529
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
526
+            $sql .= 'GROUP BY '.$g[0].' ';
530 527
             $par = array_merge($par, $g[1]);
531 528
         }
532 529
         if (count($h)) {
533 530
             $sql .= 'HAVING ';
534 531
             $tmp = [];
535 532
             foreach ($h as $v) {
536
-                $tmp[] = '(' . $v[0] . ')';
533
+                $tmp[] = '('.$v[0].')';
537 534
                 $par = array_merge($par, $v[1]);
538 535
             }
539 536
             $sql .= implode(' AND ', $tmp).' ';
@@ -567,7 +564,7 @@  discard block
 block discarded – undo
567 564
                     $this->with(implode('.', $temp));
568 565
                     $table = array_reduce(
569 566
                         $temp,
570
-                        function ($carry, $item) use (&$table) {
567
+                        function($carry, $item) use (&$table) {
571 568
                             return $table->getRelation($item)->table;
572 569
                         }
573 570
                     );
@@ -576,7 +573,7 @@  discard block
 block discarded – undo
576 573
                 }
577 574
                 unset($fields[$k]);
578 575
                 foreach ($cols as $col) {
579
-                    $fields[] = $table . '.' . $col;
576
+                    $fields[] = $table.'.'.$col;
580 577
                 }
581 578
             }
582 579
         }
@@ -608,9 +605,9 @@  discard block
 block discarded – undo
608 605
             return $this->qiterator;
609 606
         }
610 607
         $aliases = [];
611
-        $getAlias = function ($name) use (&$aliases) {
608
+        $getAlias = function($name) use (&$aliases) {
612 609
             // to bypass use: return $name;
613
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
610
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
614 611
         };
615 612
         $table = $this->definition->getName();
616 613
         if ($fields !== null) {
@@ -626,61 +623,61 @@  discard block
 block discarded – undo
626 623
         $h = $this->having;
627 624
         $o = $this->order;
628 625
         $g = $this->group;
629
-        $j = array_map(function ($v) { return clone $v; }, $this->joins);
626
+        $j = array_map(function($v) { return clone $v; }, $this->joins);
630 627
         foreach ($this->definition->getRelations() as $k => $relation) {
631 628
             foreach ($f as $kk => $field) {
632
-                if (strpos($field, $k . '.') === 0) {
633
-                    $relations[$k] = [ $relation, $table ];
634
-                    $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field);
629
+                if (strpos($field, $k.'.') === 0) {
630
+                    $relations[$k] = [$relation, $table];
631
+                    $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field);
635 632
                 }
636 633
             }
637 634
             foreach ($w as $kk => $v) {
638
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
639
-                    $relations[$k] = [ $relation, $table ];
640
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
635
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
636
+                    $relations[$k] = [$relation, $table];
637
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
641 638
                 }
642 639
             }
643 640
             foreach ($h as $kk => $v) {
644
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
645
-                    $relations[$k] = [ $relation, $table ];
646
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
641
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
642
+                    $relations[$k] = [$relation, $table];
643
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
647 644
                 }
648 645
             }
649
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
650
-                $relations[$k] = [ $relation, $table ];
651
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
646
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
647
+                $relations[$k] = [$relation, $table];
648
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
652 649
             }
653
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
654
-                $relations[$k] = [ $relation, $table ];
655
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
650
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
651
+                $relations[$k] = [$relation, $table];
652
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
656 653
             }
657 654
             foreach ($j as $kk => $v) {
658 655
                 foreach ($v->keymap as $kkk => $vv) {
659
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
660
-                        $relations[$k] = [ $relation, $table ];
661
-                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv);
656
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
657
+                        $relations[$k] = [$relation, $table];
658
+                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv);
662 659
                     }
663 660
                 }
664 661
             }
665 662
         }
666 663
         $select = [];
667 664
         foreach ($f as $k => $field) {
668
-            $select[] = $field . (!is_numeric($k) ? ' ' . $k : '');
665
+            $select[] = $field.(!is_numeric($k) ? ' '.$k : '');
669 666
         }
670 667
         foreach ($this->withr as $name => $relation) {
671 668
             foreach ($relation[0]->table->getColumns() as $column) {
672
-                $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column);
669
+                $select[] = $getAlias($name).'.'.$column.' '.$getAlias($name.static::SEP.$column);
673 670
             }
674 671
         }
675 672
         $sql = 'SELECT '.implode(', ', $select).' FROM '.$table.' ';
676 673
         $par = [];
677 674
         foreach ($j as $k => $v) {
678
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
675
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
679 676
             $tmp = [];
680 677
             foreach ($v->keymap as $kk => $vv) {
681 678
                 $tmp[] = $kk.' = '.$vv;
682 679
             }
683
-            $sql .= implode(' AND ', $tmp) . ' ';
680
+            $sql .= implode(' AND ', $tmp).' ';
684 681
         }
685 682
         foreach ($relations as $relation => $v) {
686 683
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
@@ -692,13 +689,13 @@  discard block
 block discarded – undo
692 689
                 foreach ($v->keymap as $kk => $vv) {
693 690
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
694 691
                 }
695
-                $sql .= implode(' AND ', $tmp) . ' ';
692
+                $sql .= implode(' AND ', $tmp).' ';
696 693
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($relation).' ON ';
697 694
                 $tmp = [];
698 695
                 foreach ($v->pivot_keymap as $kk => $vv) {
699 696
                     $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' ';
700 697
                 }
701
-                $sql .= implode(' AND ', $tmp) . ' ';
698
+                $sql .= implode(' AND ', $tmp).' ';
702 699
             } else {
703 700
                 $alias = $getAlias($relation);
704 701
 
@@ -708,36 +705,36 @@  discard block
 block discarded – undo
708 705
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
709 706
                 }
710 707
                 if ($v->sql) {
711
-                    $tmp[] = $v->sql . ' ';
708
+                    $tmp[] = $v->sql.' ';
712 709
                     $par = array_merge($par, $v->par ?? []);
713 710
                 }
714
-                $sql .= implode(' AND ', $tmp) . ' ';
711
+                $sql .= implode(' AND ', $tmp).' ';
715 712
             }
716 713
         }
717 714
         if (count($w)) {
718 715
             $sql .= 'WHERE ';
719 716
             $tmp = [];
720 717
             foreach ($w as $v) {
721
-                $tmp[] = '(' . $v[0] . ')';
718
+                $tmp[] = '('.$v[0].')';
722 719
                 $par = array_merge($par, $v[1]);
723 720
             }
724 721
             $sql .= implode(' AND ', $tmp).' ';
725 722
         }
726 723
         if (count($g)) {
727
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
724
+            $sql .= 'GROUP BY '.$g[0].' ';
728 725
             $par = array_merge($par, $g[1]);
729 726
         }
730 727
         if (count($h)) {
731 728
             $sql .= 'HAVING ';
732 729
             $tmp = [];
733 730
             foreach ($h as $v) {
734
-                $tmp[] = '(' . $v[0] . ')';
731
+                $tmp[] = '('.$v[0].')';
735 732
                 $par = array_merge($par, $v[1]);
736 733
             }
737 734
             $sql .= implode(' AND ', $tmp).' ';
738 735
         }
739 736
         if (count($o)) {
740
-            $sql .= 'ORDER BY ' . $o[0] . ' ';
737
+            $sql .= 'ORDER BY '.$o[0].' ';
741 738
             $par = array_merge($par, $o[1]);
742 739
         }
743 740
         $porder = [];
@@ -745,30 +742,30 @@  discard block
 block discarded – undo
745 742
             $porder[] = $this->getColumn($field)['name'];
746 743
         }
747 744
         if (count($porder)) {
748
-            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
745
+            $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
749 746
         }
750 747
 
751 748
         if ($this->li_of[0]) {
752 749
             if ($this->db->driverName() === 'oracle') {
753
-                if ((int)$this->db->driverOption('version', 0) >= 12) {
754
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
750
+                if ((int) $this->db->driverOption('version', 0) >= 12) {
751
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
755 752
                 } else {
756
-                    $f = array_map(function ($v) {
753
+                    $f = array_map(function($v) {
757 754
                         $v = explode(' ', trim($v), 2);
758 755
                         if (count($v) === 2) { return $v[1]; }
759 756
                         $v = explode('.', $v[0], 2);
760 757
                         return count($v) === 2 ? $v[1] : $v[0];
761 758
                     }, $select);
762
-                    $sql = "SELECT " . implode(', ', $f) . " 
759
+                    $sql = "SELECT ".implode(', ', $f)." 
763 760
                             FROM (
764 761
                                 SELECT tbl__.*, rownum rnum__ FROM (
765
-                                    " . $sql . "
762
+                                    " . $sql."
766 763
                                 ) tbl__ 
767
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
764
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
768 765
                             ) WHERE rnum__ > " . $this->li_of[1];
769 766
                 }
770 767
             } else {
771
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
768
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
772 769
             }
773 770
         }
774 771
         return $this->qiterator = new TableQueryIterator(
@@ -818,7 +815,7 @@  discard block
 block discarded – undo
818 815
                 $ret[$k] = str_repeat(' ', 255);
819 816
                 $par[] = &$ret[$k];
820 817
             }
821
-            $sql .= ' RETURNING ' . implode(',', $primary) . ' INTO ' . implode(',', array_fill(0, count($primary), '?'));
818
+            $sql .= ' RETURNING '.implode(',', $primary).' INTO '.implode(',', array_fill(0, count($primary), '?'));
822 819
             $this->db->query($sql, $par);
823 820
             return $ret;
824 821
         } else {
@@ -850,7 +847,7 @@  discard block
 block discarded – undo
850 847
         }
851 848
         $sql = 'UPDATE '.$table.' SET ';
852 849
         $par = [];
853
-        $sql .= implode(', ', array_map(function ($v) { return $v . ' = ?'; }, array_keys($update))) . ' ';
850
+        $sql .= implode(', ', array_map(function($v) { return $v.' = ?'; }, array_keys($update))).' ';
854 851
         $par = array_merge($par, array_values($update));
855 852
         if (count($this->where)) {
856 853
             $sql .= 'WHERE ';
@@ -859,7 +856,7 @@  discard block
 block discarded – undo
859 856
                 $tmp[] = $v[0];
860 857
                 $par = array_merge($par, $v[1]);
861 858
             }
862
-            $sql .= implode(' AND ', $tmp) . ' ';
859
+            $sql .= implode(' AND ', $tmp).' ';
863 860
         }
864 861
         if (count($this->order)) {
865 862
             $sql .= $this->order[0];
@@ -883,7 +880,7 @@  discard block
 block discarded – undo
883 880
                 $tmp[] = $v[0];
884 881
                 $par = array_merge($par, $v[1]);
885 882
             }
886
-            $sql .= implode(' AND ', $tmp) . ' ';
883
+            $sql .= implode(' AND ', $tmp).' ';
887 884
         }
888 885
         if (count($this->order)) {
889 886
             $sql .= $this->order[0];
@@ -903,13 +900,13 @@  discard block
 block discarded – undo
903 900
         $table = $this->definition;
904 901
         array_reduce(
905 902
             $parts,
906
-            function ($carry, $item) use (&$table) {
903
+            function($carry, $item) use (&$table) {
907 904
                 $relation = $table->getRelation($item);
908 905
                 if (!$relation) {
909 906
                     throw new DBException('Invalid relation name');
910 907
                 }
911
-                $name = $carry ? $carry . static::SEP . $item : $item;
912
-                $this->withr[$name] = [ $relation, $carry ?? $table->getName() ];
908
+                $name = $carry ? $carry.static::SEP.$item : $item;
909
+                $this->withr[$name] = [$relation, $carry ?? $table->getName()];
913 910
                 $table = $relation->table;
914 911
                 return $name;
915 912
             }
Please login to merge, or discard this patch.