Completed
Push — master ( a6d599...c90248 )
by Ivan
04:03
created
src/schema/TableQuery.php 1 patch
Spacing   +67 added lines, -70 removed lines patch added patch discarded remove patch
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
     /**
43 43
      * @var int[]
44 44
      */
45
-    protected $li_of = [0,0];
45
+    protected $li_of = [0, 0];
46 46
     protected $fields = [];
47 47
     /**
48 48
      * @var array
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
     public function __construct(DBInterface $db, $table)
62 62
     {
63 63
         $this->db = $db;
64
-        $this->definition = $table instanceof Table ? $table : $this->db->definition((string)$table);
64
+        $this->definition = $table instanceof Table ? $table : $this->db->definition((string) $table);
65 65
         $this->columns($this->definition->getColumns());
66 66
     }
67 67
     public function __clone()
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
     {
82 82
         $column = explode('.', $column, 2);
83 83
         if (count($column) === 1) {
84
-            $column = [ $this->definition->getName(), $column[0] ];
84
+            $column = [$this->definition->getName(), $column[0]];
85 85
         }
86 86
         if ($column[0] === $this->definition->getName()) {
87 87
             $col = $this->definition->getColumn($column[1]);
@@ -100,10 +100,10 @@  discard block
 block discarded – undo
100 100
                     throw new DBException('Invalid column name in related table');
101 101
                 }
102 102
             } else {
103
-                throw new DBException('Invalid foreign table name: ' . implode(',', $column));
103
+                throw new DBException('Invalid foreign table name: '.implode(',', $column));
104 104
             }
105 105
         }
106
-        return [ 'name' => implode('.', $column), 'data' => $col ];
106
+        return ['name' => implode('.', $column), 'data' => $col];
107 107
     }
108 108
     protected function normalizeValue(TableColumn $col, $value)
109 109
     {
@@ -142,7 +142,7 @@  discard block
 block discarded – undo
142 142
                 }
143 143
                 return $value;
144 144
             case 'int':
145
-                return (int)$value;
145
+                return (int) $value;
146 146
             default:
147 147
                 return $value;
148 148
         }
@@ -159,18 +159,16 @@  discard block
 block discarded – undo
159 159
         list($name, $column) = array_values($this->getColumn($column));
160 160
         if (is_null($value)) {
161 161
             return $negate ?
162
-                $this->where($name . ' IS NOT NULL') :
163
-                $this->where($name . ' IS NULL');
162
+                $this->where($name.' IS NOT NULL') : $this->where($name.' IS NULL');
164 163
         }
165 164
         if (!is_array($value)) {
166 165
             return $negate ?
167 166
                 $this->where(
168
-                    $name . ' <> ?',
169
-                    [ $this->normalizeValue($column, $value) ]
170
-                ) :
171
-                $this->where(
172
-                    $name . ' = ?',
173
-                    [ $this->normalizeValue($column, $value) ]
167
+                    $name.' <> ?',
168
+                    [$this->normalizeValue($column, $value)]
169
+                ) : $this->where(
170
+                    $name.' = ?',
171
+                    [$this->normalizeValue($column, $value)]
174 172
                 );
175 173
         }
176 174
         if (isset($value['beg']) && isset($value['end'])) {
@@ -181,8 +179,7 @@  discard block
 block discarded – undo
181 179
                         $this->normalizeValue($column, $value['beg']),
182 180
                         $this->normalizeValue($column, $value['end'])
183 181
                     ]
184
-                ) :
185
-                $this->where(
182
+                ) : $this->where(
186 183
                     $name.' BETWEEN ? AND ?',
187 184
                     [
188 185
                         $this->normalizeValue($column, $value['beg']),
@@ -192,12 +189,12 @@  discard block
 block discarded – undo
192 189
         }
193 190
         return $negate ?
194 191
             $this->where(
195
-                $name . ' NOT IN (??)',
196
-                [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ]
192
+                $name.' NOT IN (??)',
193
+                [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)]
197 194
             ) :
198 195
             $this->where(
199
-                $name . ' IN (??)',
200
-                [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ]
196
+                $name.' IN (??)',
197
+                [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)]
201 198
             );
202 199
     }
203 200
     /**
@@ -208,7 +205,7 @@  discard block
 block discarded – undo
208 205
      */
209 206
     public function sort(string $column, bool $desc = false) : TableQuery
210 207
     {
211
-        return $this->order($this->getColumn($column)['name'] . ' ' . ($desc ? 'DESC' : 'ASC'));
208
+        return $this->order($this->getColumn($column)['name'].' '.($desc ? 'DESC' : 'ASC'));
212 209
     }
213 210
     /**
214 211
      * Group by a column (or columns)
@@ -218,7 +215,7 @@  discard block
 block discarded – undo
218 215
     public function group($column) : TableQuery
219 216
     {
220 217
         if (!is_array($column)) {
221
-            $column = [ $column ];
218
+            $column = [$column];
222 219
         }
223 220
         foreach ($column as $k => $v) {
224 221
             $column[$k] = $this->getColumn($v)['name'];
@@ -259,7 +256,7 @@  discard block
 block discarded – undo
259 256
         $this->withr = [];
260 257
         $this->order = [];
261 258
         $this->having = [];
262
-        $this->li_of = [0,0];
259
+        $this->li_of = [0, 0];
263 260
         $this->qiterator = null;
264 261
         return $this;
265 262
     }
@@ -272,7 +269,7 @@  discard block
 block discarded – undo
272 269
     public function groupBy(string $sql, array $params = []) : TableQuery
273 270
     {
274 271
         $this->qiterator = null;
275
-        $this->group = [ $sql, $params ];
272
+        $this->group = [$sql, $params];
276 273
         return $this;
277 274
     }
278 275
     /**
@@ -285,7 +282,7 @@  discard block
 block discarded – undo
285 282
      */
286 283
     public function join($table, array $fields, string $name = null, bool $multiple = true)
287 284
     {
288
-        $table = $table instanceof Table ? $table : $this->db->definition((string)$table);
285
+        $table = $table instanceof Table ? $table : $this->db->definition((string) $table);
289 286
         $name = $name ?? $table->getName();
290 287
         if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) {
291 288
             throw new DBException('Alias / table name already in use');
@@ -294,7 +291,7 @@  discard block
 block discarded – undo
294 291
         foreach ($fields as $k => $v) {
295 292
             $k = explode('.', $k, 2);
296 293
             $k = count($k) == 2 ? $k[1] : $k[0];
297
-            $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name'];
294
+            $this->joins[$name]->keymap[$this->getColumn($name.'.'.$k)['name']] = $this->getColumn($v)['name'];
298 295
         }
299 296
         return $this;
300 297
     }
@@ -307,7 +304,7 @@  discard block
 block discarded – undo
307 304
     public function where(string $sql, array $params = []) : TableQuery
308 305
     {
309 306
         $this->qiterator = null;
310
-        $this->where[] = [ $sql, $params ];
307
+        $this->where[] = [$sql, $params];
311 308
         return $this;
312 309
     }
313 310
     /**
@@ -319,7 +316,7 @@  discard block
 block discarded – undo
319 316
     public function having(string $sql, array $params = []) : TableQuery
320 317
     {
321 318
         $this->qiterator = null;
322
-        $this->having[] = [ $sql, $params ];
319
+        $this->having[] = [$sql, $params];
323 320
         return $this;
324 321
     }
325 322
     /**
@@ -331,7 +328,7 @@  discard block
 block discarded – undo
331 328
     public function order(string $sql, array $params = []) : TableQuery
332 329
     {
333 330
         $this->qiterator = null;
334
-        $this->order = [ $sql, $params ];
331
+        $this->order = [$sql, $params];
335 332
         return $this;
336 333
     }
337 334
     /**
@@ -343,7 +340,7 @@  discard block
 block discarded – undo
343 340
     public function limit(int $limit, int $offset = 0) : TableQuery
344 341
     {
345 342
         $this->qiterator = null;
346
-        $this->li_of = [ $limit, $offset ];
343
+        $this->li_of = [$limit, $offset];
347 344
         return $this;
348 345
     }
349 346
     /**
@@ -360,22 +357,22 @@  discard block
 block discarded – undo
360 357
         $relations = $this->withr;
361 358
         foreach ($this->definition->getRelations() as $k => $v) {
362 359
             foreach ($this->where as $vv) {
363
-                if (strpos($vv[0], $k . '.') !== false) {
360
+                if (strpos($vv[0], $k.'.') !== false) {
364 361
                     $relations[] = $k;
365 362
                 }
366 363
             }
367
-            if (isset($this->order[0]) && strpos($this->order[0], $k . '.') !== false) {
364
+            if (isset($this->order[0]) && strpos($this->order[0], $k.'.') !== false) {
368 365
                 $relations[] = $k;
369 366
             }
370 367
         }
371 368
 
372 369
         foreach ($this->joins as $k => $v) {
373
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
370
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
374 371
             $tmp = [];
375 372
             foreach ($v->keymap as $kk => $vv) {
376 373
                 $tmp[] = $kk.' = '.$vv;
377 374
             }
378
-            $sql .= implode(' AND ', $tmp) . ' ';
375
+            $sql .= implode(' AND ', $tmp).' ';
379 376
         }
380 377
         foreach (array_unique($relations) as $k) {
381 378
             $v = $this->definition->getRelation($k);
@@ -385,13 +382,13 @@  discard block
 block discarded – undo
385 382
                 foreach ($v->keymap as $kk => $vv) {
386 383
                     $tmp[] = $table.'.'.$kk.' = '.$k.'_pivot.'.$vv.' ';
387 384
                 }
388
-                $sql .= implode(' AND ', $tmp) . ' ';
385
+                $sql .= implode(' AND ', $tmp).' ';
389 386
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$k.' ON ';
390 387
                 $tmp = [];
391 388
                 foreach ($v->pivot_keymap as $kk => $vv) {
392 389
                     $tmp[] = $k.'.'.$vv.' = '.$k.'_pivot.'.$kk.' ';
393 390
                 }
394
-                $sql .= implode(' AND ', $tmp) . ' ';
391
+                $sql .= implode(' AND ', $tmp).' ';
395 392
             } else {
396 393
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$k.' ON ';
397 394
                 $tmp = [];
@@ -399,30 +396,30 @@  discard block
 block discarded – undo
399 396
                     $tmp[] = $table.'.'.$kk.' = '.$k.'.'.$vv.' ';
400 397
                 }
401 398
                 if ($v->sql) {
402
-                    $tmp[] = $v->sql . ' ';
399
+                    $tmp[] = $v->sql.' ';
403 400
                     $par = array_merge($par, $v->par ?? []);
404 401
                 }
405
-                $sql .= implode(' AND ', $tmp) . ' ';
402
+                $sql .= implode(' AND ', $tmp).' ';
406 403
             }
407 404
         }
408 405
         if (count($this->where)) {
409 406
             $sql .= 'WHERE ';
410 407
             $tmp = [];
411 408
             foreach ($this->where as $v) {
412
-                $tmp[] = '(' . $v[0] . ')';
409
+                $tmp[] = '('.$v[0].')';
413 410
                 $par = array_merge($par, $v[1]);
414 411
             }
415 412
             $sql .= implode(' AND ', $tmp).' ';
416 413
         }
417 414
         if (count($this->group)) {
418
-            $sql .= 'GROUP BY ' . $this->group[0] . ' ';
415
+            $sql .= 'GROUP BY '.$this->group[0].' ';
419 416
             $par = array_merge($par, $this->group[1]);
420 417
         }
421 418
         if (count($this->having)) {
422 419
             $sql .= 'HAVING ';
423 420
             $tmp = [];
424 421
             foreach ($this->having as $v) {
425
-                $tmp[] = '(' . $v[0] . ')';
422
+                $tmp[] = '('.$v[0].')';
426 423
                 $par = array_merge($par, $v[1]);
427 424
             }
428 425
             $sql .= implode(' AND ', $tmp).' ';
@@ -453,7 +450,7 @@  discard block
 block discarded – undo
453 450
                     throw new DBException('Invalid foreign table name');
454 451
                 }
455 452
                 foreach ($cols as $col) {
456
-                    $fields[] = $table . '.' . $col;
453
+                    $fields[] = $table.'.'.$col;
457 454
                 }
458 455
                 unset($fields[$k]);
459 456
             }
@@ -493,37 +490,37 @@  discard block
 block discarded – undo
493 490
         $relations = $this->withr;
494 491
         foreach ($this->definition->getRelations() as $k => $v) {
495 492
             foreach ($this->fields as $field) {
496
-                if (strpos($field, $k . '.') === 0) {
493
+                if (strpos($field, $k.'.') === 0) {
497 494
                     $relations[] = $k;
498 495
                 }
499 496
             }
500 497
             foreach ($this->where as $v) {
501
-                if (strpos($v[0], $k . '.') !== false) {
498
+                if (strpos($v[0], $k.'.') !== false) {
502 499
                     $relations[] = $k;
503 500
                 }
504 501
             }
505
-            if (isset($this->order[0]) && strpos($this->order[0], $k . '.') !== false) {
502
+            if (isset($this->order[0]) && strpos($this->order[0], $k.'.') !== false) {
506 503
                 $relations[] = $k;
507 504
             }
508 505
         }
509 506
         $select = [];
510 507
         foreach ($this->fields as $k => $field) {
511
-            $select[] = $field . (!is_numeric($k) ? ' ' . $k : '');
508
+            $select[] = $field.(!is_numeric($k) ? ' '.$k : '');
512 509
         }
513 510
         foreach ($this->withr as $relation) {
514 511
             foreach ($this->definition->getRelation($relation)->table->getColumns() as $column) {
515
-                $select[] = $relation . '.' . $column . ' ' . $relation . '___' . $column;
512
+                $select[] = $relation.'.'.$column.' '.$relation.'___'.$column;
516 513
             }
517 514
         }
518 515
         $sql = 'SELECT '.implode(', ', $select).' FROM '.$table.' ';
519 516
         $par = [];
520 517
         foreach ($this->joins as $k => $v) {
521
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
518
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
522 519
             $tmp = [];
523 520
             foreach ($v->keymap as $kk => $vv) {
524 521
                 $tmp[] = $kk.' = '.$vv;
525 522
             }
526
-            $sql .= implode(' AND ', $tmp) . ' ';
523
+            $sql .= implode(' AND ', $tmp).' ';
527 524
         }
528 525
         foreach (array_unique($relations) as $relation) {
529 526
             $v = $this->definition->getRelation($relation);
@@ -533,13 +530,13 @@  discard block
 block discarded – undo
533 530
                 foreach ($v->keymap as $kk => $vv) {
534 531
                     $tmp[] = $table.'.'.$kk.' = '.$relation.'_pivot.'.$vv.' ';
535 532
                 }
536
-                $sql .= implode(' AND ', $tmp) . ' ';
533
+                $sql .= implode(' AND ', $tmp).' ';
537 534
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$relation.' ON ';
538 535
                 $tmp = [];
539 536
                 foreach ($v->pivot_keymap as $kk => $vv) {
540 537
                     $tmp[] = $relation.'.'.$vv.' = '.$relation.'_pivot.'.$kk.' ';
541 538
                 }
542
-                $sql .= implode(' AND ', $tmp) . ' ';
539
+                $sql .= implode(' AND ', $tmp).' ';
543 540
             } else {
544 541
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$relation.' ON ';
545 542
                 $tmp = [];
@@ -547,30 +544,30 @@  discard block
 block discarded – undo
547 544
                     $tmp[] = $table.'.'.$kk.' = '.$relation.'.'.$vv.' ';
548 545
                 }
549 546
                 if ($v->sql) {
550
-                    $tmp[] = $v->sql . ' ';
547
+                    $tmp[] = $v->sql.' ';
551 548
                     $par = array_merge($par, $v->par ?? []);
552 549
                 }
553
-                $sql .= implode(' AND ', $tmp) . ' ';
550
+                $sql .= implode(' AND ', $tmp).' ';
554 551
             }
555 552
         }
556 553
         if (count($this->where)) {
557 554
             $sql .= 'WHERE ';
558 555
             $tmp = [];
559 556
             foreach ($this->where as $v) {
560
-                $tmp[] = '(' . $v[0] . ')';
557
+                $tmp[] = '('.$v[0].')';
561 558
                 $par = array_merge($par, $v[1]);
562 559
             }
563 560
             $sql .= implode(' AND ', $tmp).' ';
564 561
         }
565 562
         if (count($this->group)) {
566
-            $sql .= 'GROUP BY ' . $this->group[0] . ' ';
563
+            $sql .= 'GROUP BY '.$this->group[0].' ';
567 564
             $par = array_merge($par, $this->group[1]);
568 565
         }
569 566
         if (count($this->having)) {
570 567
             $sql .= 'HAVING ';
571 568
             $tmp = [];
572 569
             foreach ($this->having as $v) {
573
-                $tmp[] = '(' . $v[0] . ')';
570
+                $tmp[] = '('.$v[0].')';
574 571
                 $par = array_merge($par, $v[1]);
575 572
             }
576 573
             $sql .= implode(' AND ', $tmp).' ';
@@ -579,36 +576,36 @@  discard block
 block discarded – undo
579 576
         //    $sql .= 'GROUP BY '.$table.'.'.implode(', '.$table.'.', $primary).' ';
580 577
         //}
581 578
         if (count($this->order)) {
582
-            $sql .= 'ORDER BY ' . $this->order[0] . ' ';
579
+            $sql .= 'ORDER BY '.$this->order[0].' ';
583 580
             $par = array_merge($par, $this->order[1]);
584 581
         }
585 582
         $porder = [];
586 583
         foreach ($primary as $field) {
587 584
             $porder[] = $this->getColumn($field)['name'];
588 585
         }
589
-        $sql .= (count($this->order) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
586
+        $sql .= (count($this->order) ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
590 587
 
591 588
         if ($this->li_of[0]) {
592 589
             if ($this->db->driver() === 'oracle') {
593
-                if ((int)($this->db->settings()->options['version'] ?? 0) >= 12) {
594
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
590
+                if ((int) ($this->db->settings()->options['version'] ?? 0) >= 12) {
591
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
595 592
                 } else {
596
-                    $f = array_map(function ($v) {
593
+                    $f = array_map(function($v) {
597 594
                         $v = explode(' ', trim($v), 2);
598 595
                         if (count($v) === 2) { return $v[1]; }
599 596
                         $v = explode('.', $v[0], 2);
600 597
                         return count($v) === 2 ? $v[1] : $v[0];
601 598
                     }, $select);
602
-                    $sql = "SELECT " . implode(', ', $f) . " 
599
+                    $sql = "SELECT ".implode(', ', $f)." 
603 600
                             FROM (
604 601
                                 SELECT tbl__.*, rownum rnum__ FROM (
605
-                                    " . $sql . "
602
+                                    " . $sql."
606 603
                                 ) tbl__ 
607
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
604
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
608 605
                             ) WHERE rnum__ > " . $this->li_of[1];
609 606
                 }
610 607
             } else {
611
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
608
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
612 609
             }
613 610
         }
614 611
         return $this->qiterator = new TableQueryIterator(
@@ -616,7 +613,7 @@  discard block
 block discarded – undo
616 613
             $this->definition->getPrimaryKey(),
617 614
             array_combine(
618 615
                 $this->withr,
619
-                array_map(function ($relation) {
616
+                array_map(function($relation) {
620 617
                     return $this->definition->getRelation($relation);
621 618
                 }, $this->withr)
622 619
             )
@@ -663,7 +660,7 @@  discard block
 block discarded – undo
663 660
                 $ret[$k] = str_repeat(' ', 255);
664 661
                 $par[] = &$ret[$k];
665 662
             }
666
-            $sql .= ' RETURNING ' . implode(',', $primary) . ' INTO ' . implode(',', array_fill(0, count($primary), '?'));
663
+            $sql .= ' RETURNING '.implode(',', $primary).' INTO '.implode(',', array_fill(0, count($primary), '?'));
667 664
             $this->db->query($sql, $par);
668 665
             return $ret;
669 666
         } else {
@@ -695,7 +692,7 @@  discard block
 block discarded – undo
695 692
         }
696 693
         $sql = 'UPDATE '.$table.' SET ';
697 694
         $par = [];
698
-        $sql .= implode(', ', array_map(function ($v) { return $v . ' = ?'; }, array_keys($update))) . ' ';
695
+        $sql .= implode(', ', array_map(function($v) { return $v.' = ?'; }, array_keys($update))).' ';
699 696
         $par = array_merge($par, array_values($update));
700 697
         if (count($this->where)) {
701 698
             $sql .= 'WHERE ';
@@ -704,7 +701,7 @@  discard block
 block discarded – undo
704 701
                 $tmp[] = $v[0];
705 702
                 $par = array_merge($par, $v[1]);
706 703
             }
707
-            $sql .= implode(' AND ', $tmp) . ' ';
704
+            $sql .= implode(' AND ', $tmp).' ';
708 705
         }
709 706
         if (count($this->order)) {
710 707
             $sql .= $this->order[0];
@@ -728,7 +725,7 @@  discard block
 block discarded – undo
728 725
                 $tmp[] = $v[0];
729 726
                 $par = array_merge($par, $v[1]);
730 727
             }
731
-            $sql .= implode(' AND ', $tmp) . ' ';
728
+            $sql .= implode(' AND ', $tmp).' ';
732 729
         }
733 730
         if (count($this->order)) {
734 731
             $sql .= $this->order[0];
Please login to merge, or discard this patch.