Completed
Push — master ( 0ff8e8...37635b )
by Ivan
03:55
created
src/schema/TableQuery.php 1 patch
Spacing   +168 added lines, -172 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
     /**
45 45
      * @var int[]
46 46
      */
47
-    protected $li_of = [0,0,0];
47
+    protected $li_of = [0, 0, 0];
48 48
     /**
49 49
      * @var array
50 50
      */
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
     public function __construct(DBInterface $db, $table)
75 75
     {
76 76
         $this->db = $db;
77
-        $this->definition = $table instanceof Table ? $table : $this->db->definition((string)$table);
77
+        $this->definition = $table instanceof Table ? $table : $this->db->definition((string) $table);
78 78
         $primary = $this->definition->getPrimaryKey();
79 79
         $columns = $this->definition->getColumns();
80 80
         $this->pkey = count($primary) ? $primary : $columns;
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
     {
98 98
         $column = explode('.', $column);
99 99
         if (count($column) === 1) {
100
-            $column = [ $this->definition->getName(), $column[0] ];
100
+            $column = [$this->definition->getName(), $column[0]];
101 101
             $col = $this->definition->getColumn($column[1]);
102 102
             if (!$col) {
103 103
                 throw new DBException('Invalid column name in own table');
@@ -120,7 +120,7 @@  discard block
 block discarded – undo
120 120
                         throw new DBException('Invalid column name in related table');
121 121
                     }
122 122
                 } else {
123
-                    throw new DBException('Invalid foreign table name: ' . implode(',', $column));
123
+                    throw new DBException('Invalid foreign table name: '.implode(',', $column));
124 124
                 }
125 125
             }
126 126
         } else {
@@ -129,19 +129,19 @@  discard block
 block discarded – undo
129 129
             $table = $this->definition;
130 130
             $table = array_reduce(
131 131
                 $column,
132
-                function ($carry, $item) use (&$table) {
132
+                function($carry, $item) use (&$table) {
133 133
                     $table = $table->getRelation($item)->table;
134 134
                     return $table;
135 135
                 }
136 136
             );
137 137
             $col = $table->getColumn($name);
138
-            $column = [ implode(static::SEP, $column), $name ];
138
+            $column = [implode(static::SEP, $column), $name];
139 139
         }
140
-        return [ 'name' => implode('.', $column), 'data' => $col ];
140
+        return ['name' => implode('.', $column), 'data' => $col];
141 141
     }
142 142
     protected function normalizeValue(TableColumn $col, $value)
143 143
     {
144
-        $strict = (int)$this->db->driverOption('strict', 0) > 0;
144
+        $strict = (int) $this->db->driverOption('strict', 0) > 0;
145 145
         if ($value === null && $col->isNullable()) {
146 146
             return null;
147 147
         }
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
                     $temp = strtotime($value);
152 152
                     if (!$temp) {
153 153
                         if ($strict) {
154
-                            throw new DBException('Invalid value for date column ' . $col->getName());
154
+                            throw new DBException('Invalid value for date column '.$col->getName());
155 155
                         }
156 156
                         return null;
157 157
                     }
@@ -164,7 +164,7 @@  discard block
 block discarded – undo
164 164
                     return $value->format('Y-m-d');
165 165
                 }
166 166
                 if ($strict) {
167
-                    throw new DBException('Invalid value (unknown data type) for date column ' . $col->getName());
167
+                    throw new DBException('Invalid value (unknown data type) for date column '.$col->getName());
168 168
                 }
169 169
                 return $value;
170 170
             case 'datetime':
@@ -172,7 +172,7 @@  discard block
 block discarded – undo
172 172
                     $temp = strtotime($value);
173 173
                     if (!$temp) {
174 174
                         if ($strict) {
175
-                            throw new DBException('Invalid value for datetime column ' . $col->getName());
175
+                            throw new DBException('Invalid value for datetime column '.$col->getName());
176 176
                         }
177 177
                         return null;
178 178
                     }
@@ -185,7 +185,7 @@  discard block
 block discarded – undo
185 185
                     return $value->format('Y-m-d H:i:s');
186 186
                 }
187 187
                 if ($strict) {
188
-                    throw new DBException('Invalid value (unknown data type) for datetime column ' . $col->getName());
188
+                    throw new DBException('Invalid value (unknown data type) for datetime column '.$col->getName());
189 189
                 }
190 190
                 return $value;
191 191
             case 'enum':
@@ -193,7 +193,7 @@  discard block
 block discarded – undo
193 193
                 if (is_int($value)) {
194 194
                     if (!isset($values[$value])) {
195 195
                         if ($strict) {
196
-                            throw new DBException('Invalid value (using integer) for enum ' . $col->getName());
196
+                            throw new DBException('Invalid value (using integer) for enum '.$col->getName());
197 197
                         }
198 198
                         return $value;
199 199
                     }
@@ -201,19 +201,19 @@  discard block
 block discarded – undo
201 201
                 }
202 202
                 if (!in_array($value, $col->getValues())) {
203 203
                     if ($strict) {
204
-                        throw new DBException('Invalid value for enum ' . $col->getName());
204
+                        throw new DBException('Invalid value for enum '.$col->getName());
205 205
                     }
206 206
                     return 0;
207 207
                 }
208 208
                 return $value;
209 209
             case 'int':
210
-                return (int)preg_replace('([^+\-0-9]+)', '', $value);
210
+                return (int) preg_replace('([^+\-0-9]+)', '', $value);
211 211
             case 'float':
212
-                return (float)preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', $value));
212
+                return (float) preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', $value));
213 213
             case 'text':
214 214
                 if ($col->hasLength() && mb_strlen($value) > $col->getLength()) {
215 215
                     if ($strict) {
216
-                        throw new DBException('Invalid value for text column ' . $col->getName());
216
+                        throw new DBException('Invalid value for text column '.$col->getName());
217 217
                     }
218 218
                     return mb_substr($value, 0, $col->getLength());
219 219
                 }
@@ -235,35 +235,32 @@  discard block
 block discarded – undo
235 235
             // str_replace(['%', '_'], ['\\%','\\_'], $q)
236 236
             return $negate ?
237 237
                 [
238
-                    $name . ' NOT LIKE ?',
239
-                    [ $this->normalizeValue($column, $value) ]
240
-                ] :
241
-                [
242
-                    $name . ' LIKE ?',
243
-                    [ $this->normalizeValue($column, $value) ]
238
+                    $name.' NOT LIKE ?',
239
+                    [$this->normalizeValue($column, $value)]
240
+                ] : [
241
+                    $name.' LIKE ?',
242
+                    [$this->normalizeValue($column, $value)]
244 243
                 ];
245 244
         }
246 245
         if (is_null($value)) {
247 246
             return $negate ?
248
-                [ $name . ' IS NOT NULL', [] ]:
249
-                [ $name . ' IS NULL', [] ];
247
+                [$name.' IS NOT NULL', []] : [$name.' IS NULL', []];
250 248
         }
251 249
         if (!is_array($value)) {
252 250
             return $negate ?
253 251
                 [
254
-                    $name . ' <> ?',
255
-                    [ $this->normalizeValue($column, $value) ]
256
-                ] :
257
-                [
258
-                    $name . ' = ?',
259
-                    [ $this->normalizeValue($column, $value) ]
252
+                    $name.' <> ?',
253
+                    [$this->normalizeValue($column, $value)]
254
+                ] : [
255
+                    $name.' = ?',
256
+                    [$this->normalizeValue($column, $value)]
260 257
                 ];
261 258
         }
262 259
         if (isset($value['beg']) && strlen($value['beg']) && (!isset($value['end']) || !strlen($value['end']))) {
263
-            $value = [ 'gte' => $value['beg'] ];
260
+            $value = ['gte' => $value['beg']];
264 261
         }
265 262
         if (isset($value['end']) && strlen($value['end']) && (!isset($value['beg']) || !strlen($value['beg']))) {
266
-            $value = [ 'lte' => $value['end'] ];
263
+            $value = ['lte' => $value['end']];
267 264
         }
268 265
         if (isset($value['beg']) && isset($value['end'])) {
269 266
             return $negate ?
@@ -273,8 +270,7 @@  discard block
 block discarded – undo
273 270
                         $this->normalizeValue($column, $value['beg']),
274 271
                         $this->normalizeValue($column, $value['end'])
275 272
                     ]
276
-                ] :
277
-                [
273
+                ] : [
278 274
                     $name.' BETWEEN ? AND ?',
279 275
                     [
280 276
                         $this->normalizeValue($column, $value['beg']),
@@ -286,34 +282,34 @@  discard block
 block discarded – undo
286 282
             $sql = [];
287 283
             $par = [];
288 284
             if (isset($value['gt'])) {
289
-                $sql[] = $name. ' ' . ($negate ? '<=' : '>') . ' ?';
285
+                $sql[] = $name.' '.($negate ? '<=' : '>').' ?';
290 286
                 $par[] = $this->normalizeValue($column, $value['gt']);
291 287
             }
292 288
             if (isset($value['gte'])) {
293
-                $sql[] = $name. ' ' . ($negate ? '<' : '>=') . ' ?';
289
+                $sql[] = $name.' '.($negate ? '<' : '>=').' ?';
294 290
                 $par[] = $this->normalizeValue($column, $value['gte']);
295 291
             }
296 292
             if (isset($value['lt'])) {
297
-                $sql[] = $name. ' ' . ($negate ? '>=' : '<') . ' ?';
293
+                $sql[] = $name.' '.($negate ? '>=' : '<').' ?';
298 294
                 $par[] = $this->normalizeValue($column, $value['lt']);
299 295
             }
300 296
             if (isset($value['lte'])) {
301
-                $sql[] = $name. ' ' . ($negate ? '>' : '<=') . ' ?';
297
+                $sql[] = $name.' '.($negate ? '>' : '<=').' ?';
302 298
                 $par[] = $this->normalizeValue($column, $value['lte']);
303 299
             }
304 300
             return [
305
-                '(' . implode(' AND ', $sql) . ')',
301
+                '('.implode(' AND ', $sql).')',
306 302
                 $par
307 303
             ];
308 304
         }
309 305
         return $negate ?
310 306
             [
311
-                $name . ' NOT IN (??)',
312
-                [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ]
307
+                $name.' NOT IN (??)',
308
+                [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)]
313 309
             ] :
314 310
             [
315
-                $name . ' IN (??)',
316
-                [ array_map(function ($v) use ($column) { return $this->normalizeValue($column, $v); }, $value) ]
311
+                $name.' IN (??)',
312
+                [array_map(function($v) use ($column) { return $this->normalizeValue($column, $v); }, $value)]
317 313
             ];
318 314
     }
319 315
     /**
@@ -344,7 +340,7 @@  discard block
 block discarded – undo
344 340
                 $par = array_merge($par, $temp[1]);
345 341
             }
346 342
         }
347
-        return $this->where('(' . implode(' OR ', $sql) . ')', $par);
343
+        return $this->where('('.implode(' OR ', $sql).')', $par);
348 344
     }
349 345
     /**
350 346
      * Filter the results matching all of the criteria
@@ -362,7 +358,7 @@  discard block
 block discarded – undo
362 358
                 $par = array_merge($par, $temp[1]);
363 359
             }
364 360
         }
365
-        return $this->where('(' . implode(' AND ', $sql) . ')', $par);
361
+        return $this->where('('.implode(' AND ', $sql).')', $par);
366 362
     }
367 363
     /**
368 364
      * Sort by a column
@@ -372,7 +368,7 @@  discard block
 block discarded – undo
372 368
      */
373 369
     public function sort(string $column, bool $desc = false) : TableQuery
374 370
     {
375
-        return $this->order($this->getColumn($column)['name'] . ' ' . ($desc ? 'DESC' : 'ASC'));
371
+        return $this->order($this->getColumn($column)['name'].' '.($desc ? 'DESC' : 'ASC'));
376 372
     }
377 373
     /**
378 374
      * Group by a column (or columns)
@@ -382,7 +378,7 @@  discard block
 block discarded – undo
382 378
     public function group($column) : TableQuery
383 379
     {
384 380
         if (!is_array($column)) {
385
-            $column = [ $column ];
381
+            $column = [$column];
386 382
         }
387 383
         foreach ($column as $k => $v) {
388 384
             $column[$k] = $this->getColumn($v)['name'];
@@ -424,7 +420,7 @@  discard block
 block discarded – undo
424 420
         $this->order = [];
425 421
         $this->having = [];
426 422
         $this->aliases = [];
427
-        $this->li_of = [0,0,0];
423
+        $this->li_of = [0, 0, 0];
428 424
         $this->qiterator = null;
429 425
         return $this;
430 426
     }
@@ -437,7 +433,7 @@  discard block
 block discarded – undo
437 433
     public function groupBy(string $sql, array $params = []) : TableQuery
438 434
     {
439 435
         $this->qiterator = null;
440
-        $this->group = [ $sql, $params ];
436
+        $this->group = [$sql, $params];
441 437
         return $this;
442 438
     }
443 439
     /**
@@ -450,7 +446,7 @@  discard block
 block discarded – undo
450 446
      */
451 447
     public function join($table, array $fields, string $name = null, bool $multiple = true)
452 448
     {
453
-        $table = $table instanceof Table ? $table : $this->db->definition((string)$table);
449
+        $table = $table instanceof Table ? $table : $this->db->definition((string) $table);
454 450
         $name = $name ?? $table->getName();
455 451
         if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) {
456 452
             throw new DBException('Alias / table name already in use');
@@ -459,7 +455,7 @@  discard block
 block discarded – undo
459 455
         foreach ($fields as $k => $v) {
460 456
             $k = explode('.', $k, 2);
461 457
             $k = count($k) == 2 ? $k[1] : $k[0];
462
-            $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name'];
458
+            $this->joins[$name]->keymap[$this->getColumn($name.'.'.$k)['name']] = $this->getColumn($v)['name'];
463 459
         }
464 460
         return $this;
465 461
     }
@@ -472,7 +468,7 @@  discard block
 block discarded – undo
472 468
     public function where(string $sql, array $params = []) : TableQuery
473 469
     {
474 470
         $this->qiterator = null;
475
-        $this->where[] = [ $sql, $params ];
471
+        $this->where[] = [$sql, $params];
476 472
         return $this;
477 473
     }
478 474
     /**
@@ -484,7 +480,7 @@  discard block
 block discarded – undo
484 480
     public function having(string $sql, array $params = []) : TableQuery
485 481
     {
486 482
         $this->qiterator = null;
487
-        $this->having[] = [ $sql, $params ];
483
+        $this->having[] = [$sql, $params];
488 484
         return $this;
489 485
     }
490 486
     /**
@@ -496,7 +492,7 @@  discard block
 block discarded – undo
496 492
     public function order(string $sql, array $params = []) : TableQuery
497 493
     {
498 494
         $this->qiterator = null;
499
-        $this->order = [ $sql, $params ];
495
+        $this->order = [$sql, $params];
500 496
         return $this;
501 497
     }
502 498
     /**
@@ -508,7 +504,7 @@  discard block
 block discarded – undo
508 504
     public function limit(int $limit, int $offset = 0, bool $limitOnMainTable = false) : TableQuery
509 505
     {
510 506
         $this->qiterator = null;
511
-        $this->li_of = [ $limit, $offset, $limitOnMainTable ? 1 : 0 ];
507
+        $this->li_of = [$limit, $offset, $limitOnMainTable ? 1 : 0];
512 508
         return $this;
513 509
     }
514 510
     /**
@@ -518,9 +514,9 @@  discard block
 block discarded – undo
518 514
     public function count() : int
519 515
     {
520 516
         $aliases = [];
521
-        $getAlias = function ($name) use (&$aliases) {
517
+        $getAlias = function($name) use (&$aliases) {
522 518
             // to bypass use: return $name;
523
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
519
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
524 520
         };
525 521
         $table = $this->definition->getName();
526 522
         $sql = 'SELECT COUNT(DISTINCT '.$table.'.'.implode(', '.$table.'.', $this->pkey).') FROM '.$table.' ';
@@ -535,44 +531,44 @@  discard block
 block discarded – undo
535 531
         $h = $this->having;
536 532
         $o = $this->order;
537 533
         $g = $this->group;
538
-        $j = array_map(function ($v) { return clone $v; }, $this->joins);
534
+        $j = array_map(function($v) { return clone $v; }, $this->joins);
539 535
         foreach ($this->definition->getRelations() as $k => $v) {
540 536
             foreach ($w as $kk => $vv) {
541
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
542
-                    $relations[$k] = [ $v, $table ];
543
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
537
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
538
+                    $relations[$k] = [$v, $table];
539
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
544 540
                 }
545 541
             }
546
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
547
-                $relations[$k] = [ $v, $table ];
542
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
543
+                $relations[$k] = [$v, $table];
548 544
             }
549 545
             foreach ($h as $kk => $vv) {
550
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
551
-                    $relations[$k] = [ $relation, $table ];
552
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
546
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
547
+                    $relations[$k] = [$relation, $table];
548
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
553 549
                 }
554 550
             }
555
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
556
-                $relations[$k] = [ $relation, $table ];
557
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
551
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
552
+                $relations[$k] = [$relation, $table];
553
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
558 554
             }
559 555
             foreach ($j as $kk => $v) {
560 556
                 foreach ($v->keymap as $kkk => $vv) {
561
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
562
-                        $relations[$k] = [ $relation, $table ];
563
-                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv);
557
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
558
+                        $relations[$k] = [$relation, $table];
559
+                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv);
564 560
                     }
565 561
                 }
566 562
             }
567 563
         }
568 564
 
569 565
         foreach ($j as $k => $v) {
570
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
566
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
571 567
             $tmp = [];
572 568
             foreach ($v->keymap as $kk => $vv) {
573 569
                 $tmp[] = $kk.' = '.$vv;
574 570
             }
575
-            $sql .= implode(' AND ', $tmp) . ' ';
571
+            $sql .= implode(' AND ', $tmp).' ';
576 572
         }
577 573
         foreach ($relations as $k => $v) {
578 574
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
@@ -584,13 +580,13 @@  discard block
 block discarded – undo
584 580
                 foreach ($v->keymap as $kk => $vv) {
585 581
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
586 582
                 }
587
-                $sql .= implode(' AND ', $tmp) . ' ';
583
+                $sql .= implode(' AND ', $tmp).' ';
588 584
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON ';
589 585
                 $tmp = [];
590 586
                 foreach ($v->pivot_keymap as $kk => $vv) {
591 587
                     $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
592 588
                 }
593
-                $sql .= implode(' AND ', $tmp) . ' ';
589
+                $sql .= implode(' AND ', $tmp).' ';
594 590
             } else {
595 591
                 $alias = $getAlias($k);
596 592
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON ';
@@ -599,30 +595,30 @@  discard block
 block discarded – undo
599 595
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
600 596
                 }
601 597
                 if ($v->sql) {
602
-                    $tmp[] = $v->sql . ' ';
598
+                    $tmp[] = $v->sql.' ';
603 599
                     $par = array_merge($par, $v->par ?? []);
604 600
                 }
605
-                $sql .= implode(' AND ', $tmp) . ' ';
601
+                $sql .= implode(' AND ', $tmp).' ';
606 602
             }
607 603
         }
608 604
         if (count($w)) {
609 605
             $sql .= 'WHERE ';
610 606
             $tmp = [];
611 607
             foreach ($w as $v) {
612
-                $tmp[] = '(' . $v[0] . ')';
608
+                $tmp[] = '('.$v[0].')';
613 609
                 $par = array_merge($par, $v[1]);
614 610
             }
615 611
             $sql .= implode(' AND ', $tmp).' ';
616 612
         }
617 613
         if (count($g)) {
618
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
614
+            $sql .= 'GROUP BY '.$g[0].' ';
619 615
             $par = array_merge($par, $g[1]);
620 616
         }
621 617
         if (count($h)) {
622 618
             $sql .= 'HAVING ';
623 619
             $tmp = [];
624 620
             foreach ($h as $v) {
625
-                $tmp[] = '(' . $v[0] . ')';
621
+                $tmp[] = '('.$v[0].')';
626 622
                 $par = array_merge($par, $v[1]);
627 623
             }
628 624
             $sql .= implode(' AND ', $tmp).' ';
@@ -656,7 +652,7 @@  discard block
 block discarded – undo
656 652
                     $this->with(implode('.', $temp));
657 653
                     $table = array_reduce(
658 654
                         $temp,
659
-                        function ($carry, $item) use (&$table) {
655
+                        function($carry, $item) use (&$table) {
660 656
                             return $table->getRelation($item)->table;
661 657
                         }
662 658
                     );
@@ -665,7 +661,7 @@  discard block
 block discarded – undo
665 661
                 }
666 662
                 unset($fields[$k]);
667 663
                 foreach ($cols as $col) {
668
-                    $fields[] = $table . '.' . $col;
664
+                    $fields[] = $table.'.'.$col;
669 665
                 }
670 666
             }
671 667
         }
@@ -697,9 +693,9 @@  discard block
 block discarded – undo
697 693
             return $this->qiterator;
698 694
         }
699 695
         $aliases = [];
700
-        $getAlias = function ($name) use (&$aliases) {
696
+        $getAlias = function($name) use (&$aliases) {
701 697
             // to bypass use: return $name;
702
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
698
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
703 699
         };
704 700
         $table = $this->definition->getName();
705 701
         if ($fields !== null) {
@@ -715,7 +711,7 @@  discard block
 block discarded – undo
715 711
         $h = $this->having;
716 712
         $o = $this->order;
717 713
         $g = $this->group;
718
-        $j = array_map(function ($v) { return clone $v; }, $this->joins);
714
+        $j = array_map(function($v) { return clone $v; }, $this->joins);
719 715
 
720 716
         $porder = [];
721 717
         foreach ($this->definition->getPrimaryKey() as $field) {
@@ -728,9 +724,9 @@  discard block
 block discarded – undo
728 724
                 if (count($porder) > 1) {
729 725
                     $pkw = [];
730 726
                     foreach ($porder as $name) {
731
-                        $pkw[] = $name . ' = ?';
727
+                        $pkw[] = $name.' = ?';
732 728
                     }
733
-                    $pkw = '(' . implode(' AND ', $pkw) . ')';
729
+                    $pkw = '('.implode(' AND ', $pkw).')';
734 730
                     $pkp = [];
735 731
                     foreach ($ids as $id) {
736 732
                         foreach ($id as $p) {
@@ -742,67 +738,67 @@  discard block
 block discarded – undo
742 738
                         $pkp
743 739
                     ];
744 740
                 } else {
745
-                    $w[] = [ $porder[0] . ' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids ];
741
+                    $w[] = [$porder[0].' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids];
746 742
                 }
747 743
             } else {
748
-                $w[] = [ '1=0', [] ];
744
+                $w[] = ['1=0', []];
749 745
             }
750 746
         }
751 747
 
752 748
         foreach ($this->definition->getRelations() as $k => $relation) {
753 749
             foreach ($f as $kk => $field) {
754
-                if (strpos($field, $k . '.') === 0) {
755
-                    $relations[$k] = [ $relation, $table ];
756
-                    $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field);
750
+                if (strpos($field, $k.'.') === 0) {
751
+                    $relations[$k] = [$relation, $table];
752
+                    $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field);
757 753
                 }
758 754
             }
759 755
             foreach ($w as $kk => $v) {
760
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
761
-                    $relations[$k] = [ $relation, $table ];
762
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
756
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
757
+                    $relations[$k] = [$relation, $table];
758
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
763 759
                 }
764 760
             }
765 761
             foreach ($h as $kk => $v) {
766
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
767
-                    $relations[$k] = [ $relation, $table ];
768
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
762
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
763
+                    $relations[$k] = [$relation, $table];
764
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
769 765
                 }
770 766
             }
771
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
772
-                $relations[$k] = [ $relation, $table ];
773
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
767
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
768
+                $relations[$k] = [$relation, $table];
769
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
774 770
             }
775
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
776
-                $relations[$k] = [ $relation, $table ];
777
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
771
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
772
+                $relations[$k] = [$relation, $table];
773
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
778 774
             }
779 775
             foreach ($j as $kk => $v) {
780 776
                 foreach ($v->keymap as $kkk => $vv) {
781
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
782
-                        $relations[$k] = [ $relation, $table ];
783
-                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv);
777
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
778
+                        $relations[$k] = [$relation, $table];
779
+                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv);
784 780
                     }
785 781
                 }
786 782
             }
787 783
         }
788 784
         $select = [];
789 785
         foreach ($f as $k => $field) {
790
-            $select[] = $field . (!is_numeric($k) ? ' ' . $k : '');
786
+            $select[] = $field.(!is_numeric($k) ? ' '.$k : '');
791 787
         }
792 788
         foreach ($this->withr as $name => $relation) {
793 789
             foreach ($relation[0]->table->getColumns() as $column) {
794
-                $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column);
790
+                $select[] = $getAlias($name).'.'.$column.' '.$getAlias($name.static::SEP.$column);
795 791
             }
796 792
         }
797 793
         $sql = 'SELECT '.implode(', ', $select).' FROM '.$table.' ';
798 794
         $par = [];
799 795
         foreach ($j as $k => $v) {
800
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
796
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
801 797
             $tmp = [];
802 798
             foreach ($v->keymap as $kk => $vv) {
803 799
                 $tmp[] = $kk.' = '.$vv;
804 800
             }
805
-            $sql .= implode(' AND ', $tmp) . ' ';
801
+            $sql .= implode(' AND ', $tmp).' ';
806 802
         }
807 803
         foreach ($relations as $relation => $v) {
808 804
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
@@ -814,13 +810,13 @@  discard block
 block discarded – undo
814 810
                 foreach ($v->keymap as $kk => $vv) {
815 811
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
816 812
                 }
817
-                $sql .= implode(' AND ', $tmp) . ' ';
813
+                $sql .= implode(' AND ', $tmp).' ';
818 814
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($relation).' ON ';
819 815
                 $tmp = [];
820 816
                 foreach ($v->pivot_keymap as $kk => $vv) {
821 817
                     $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' ';
822 818
                 }
823
-                $sql .= implode(' AND ', $tmp) . ' ';
819
+                $sql .= implode(' AND ', $tmp).' ';
824 820
             } else {
825 821
                 $alias = $getAlias($relation);
826 822
 
@@ -830,62 +826,62 @@  discard block
 block discarded – undo
830 826
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
831 827
                 }
832 828
                 if ($v->sql) {
833
-                    $tmp[] = $v->sql . ' ';
829
+                    $tmp[] = $v->sql.' ';
834 830
                     $par = array_merge($par, $v->par ?? []);
835 831
                 }
836
-                $sql .= implode(' AND ', $tmp) . ' ';
832
+                $sql .= implode(' AND ', $tmp).' ';
837 833
             }
838 834
         }
839 835
         if (count($w)) {
840 836
             $sql .= 'WHERE ';
841 837
             $tmp = [];
842 838
             foreach ($w as $v) {
843
-                $tmp[] = '(' . $v[0] . ')';
839
+                $tmp[] = '('.$v[0].')';
844 840
                 $par = array_merge($par, $v[1]);
845 841
             }
846 842
             $sql .= implode(' AND ', $tmp).' ';
847 843
         }
848 844
         if (count($g)) {
849
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
845
+            $sql .= 'GROUP BY '.$g[0].' ';
850 846
             $par = array_merge($par, $g[1]);
851 847
         }
852 848
         if (count($h)) {
853 849
             $sql .= 'HAVING ';
854 850
             $tmp = [];
855 851
             foreach ($h as $v) {
856
-                $tmp[] = '(' . $v[0] . ')';
852
+                $tmp[] = '('.$v[0].')';
857 853
                 $par = array_merge($par, $v[1]);
858 854
             }
859 855
             $sql .= implode(' AND ', $tmp).' ';
860 856
         }
861 857
         if (count($o)) {
862
-            $sql .= 'ORDER BY ' . $o[0] . ' ';
858
+            $sql .= 'ORDER BY '.$o[0].' ';
863 859
             $par = array_merge($par, $o[1]);
864 860
         }
865 861
         if (count($porder)) {
866
-            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
862
+            $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
867 863
         }
868 864
         if (($this->li_of[2] === 0 || !count($porder)) && $this->li_of[0]) {
869 865
             if ($this->db->driverName() === 'oracle') {
870
-                if ((int)$this->db->driverOption('version', 0) >= 12) {
871
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
866
+                if ((int) $this->db->driverOption('version', 0) >= 12) {
867
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
872 868
                 } else {
873
-                    $f = array_map(function ($v) {
869
+                    $f = array_map(function($v) {
874 870
                         $v = explode(' ', trim($v), 2);
875 871
                         if (count($v) === 2) { return $v[1]; }
876 872
                         $v = explode('.', $v[0], 2);
877 873
                         return count($v) === 2 ? $v[1] : $v[0];
878 874
                     }, $select);
879
-                    $sql = "SELECT " . implode(', ', $f) . " 
875
+                    $sql = "SELECT ".implode(', ', $f)." 
880 876
                             FROM (
881 877
                                 SELECT tbl__.*, rownum rnum__ FROM (
882
-                                    " . $sql . "
878
+                                    " . $sql."
883 879
                                 ) tbl__ 
884
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
880
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
885 881
                             ) WHERE rnum__ > " . $this->li_of[1];
886 882
                 }
887 883
             } else {
888
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
884
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
889 885
             }
890 886
         }
891 887
         return $this->qiterator = new TableQueryIterator(
@@ -935,7 +931,7 @@  discard block
 block discarded – undo
935 931
                 $ret[$k] = str_repeat(' ', 255);
936 932
                 $par[] = &$ret[$k];
937 933
             }
938
-            $sql .= ' RETURNING ' . implode(',', $primary) . ' INTO ' . implode(',', array_fill(0, count($primary), '?'));
934
+            $sql .= ' RETURNING '.implode(',', $primary).' INTO '.implode(',', array_fill(0, count($primary), '?'));
939 935
             $this->db->query($sql, $par);
940 936
             return $ret;
941 937
         } else {
@@ -967,7 +963,7 @@  discard block
 block discarded – undo
967 963
         }
968 964
         $sql = 'UPDATE '.$table.' SET ';
969 965
         $par = [];
970
-        $sql .= implode(', ', array_map(function ($v) { return $v . ' = ?'; }, array_keys($update))) . ' ';
966
+        $sql .= implode(', ', array_map(function($v) { return $v.' = ?'; }, array_keys($update))).' ';
971 967
         $par = array_merge($par, array_values($update));
972 968
         if (count($this->where)) {
973 969
             $sql .= 'WHERE ';
@@ -976,7 +972,7 @@  discard block
 block discarded – undo
976 972
                 $tmp[] = $v[0];
977 973
                 $par = array_merge($par, $v[1]);
978 974
             }
979
-            $sql .= implode(' AND ', $tmp) . ' ';
975
+            $sql .= implode(' AND ', $tmp).' ';
980 976
         }
981 977
         if (count($this->order)) {
982 978
             $sql .= $this->order[0];
@@ -1000,7 +996,7 @@  discard block
 block discarded – undo
1000 996
                 $tmp[] = $v[0];
1001 997
                 $par = array_merge($par, $v[1]);
1002 998
             }
1003
-            $sql .= implode(' AND ', $tmp) . ' ';
999
+            $sql .= implode(' AND ', $tmp).' ';
1004 1000
         }
1005 1001
         if (count($this->order)) {
1006 1002
             $sql .= $this->order[0];
@@ -1020,13 +1016,13 @@  discard block
 block discarded – undo
1020 1016
         $table = $this->definition;
1021 1017
         array_reduce(
1022 1018
             $parts,
1023
-            function ($carry, $item) use (&$table) {
1019
+            function($carry, $item) use (&$table) {
1024 1020
                 $relation = $table->getRelation($item);
1025 1021
                 if (!$relation) {
1026 1022
                     throw new DBException('Invalid relation name');
1027 1023
                 }
1028
-                $name = $carry ? $carry . static::SEP . $item : $item;
1029
-                $this->withr[$name] = [ $relation, $carry ?? $table->getName() ];
1024
+                $name = $carry ? $carry.static::SEP.$item : $item;
1025
+                $this->withr[$name] = [$relation, $carry ?? $table->getName()];
1030 1026
                 $table = $relation->table;
1031 1027
                 return $name;
1032 1028
             }
@@ -1064,9 +1060,9 @@  discard block
 block discarded – undo
1064 1060
     public function ids()
1065 1061
     {
1066 1062
         $aliases = [];
1067
-        $getAlias = function ($name) use (&$aliases) {
1063
+        $getAlias = function($name) use (&$aliases) {
1068 1064
             // to bypass use: return $name;
1069
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
1065
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
1070 1066
         };
1071 1067
         $table = $this->definition->getName();
1072 1068
         $sql = 'SELECT DISTINCT '.$table.'.'.implode(', '.$table.'.', $this->pkey).' FROM '.$table.' ';
@@ -1081,45 +1077,45 @@  discard block
 block discarded – undo
1081 1077
         $h = $this->having;
1082 1078
         $o = $this->order;
1083 1079
         $g = $this->group;
1084
-        $j = array_map(function ($v) { return clone $v; }, $this->joins);
1080
+        $j = array_map(function($v) { return clone $v; }, $this->joins);
1085 1081
         foreach ($this->definition->getRelations() as $k => $v) {
1086 1082
             foreach ($w as $kk => $vv) {
1087
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1088
-                    $relations[$k] = [ $v, $table ];
1089
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
1083
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
1084
+                    $relations[$k] = [$v, $table];
1085
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
1090 1086
                 }
1091 1087
             }
1092
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
1093
-                $relations[$k] = [ $v, $table ];
1094
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
1088
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
1089
+                $relations[$k] = [$v, $table];
1090
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
1095 1091
             }
1096 1092
             foreach ($h as $kk => $vv) {
1097
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1098
-                    $relations[$k] = [ $relation, $table ];
1099
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
1093
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
1094
+                    $relations[$k] = [$relation, $table];
1095
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
1100 1096
                 }
1101 1097
             }
1102
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
1103
-                $relations[$k] = [ $relation, $table ];
1104
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
1098
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
1099
+                $relations[$k] = [$relation, $table];
1100
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
1105 1101
             }
1106 1102
             foreach ($j as $kk => $v) {
1107 1103
                 foreach ($v->keymap as $kkk => $vv) {
1108
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
1109
-                        $relations[$k] = [ $relation, $table ];
1110
-                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv);
1104
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
1105
+                        $relations[$k] = [$relation, $table];
1106
+                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv);
1111 1107
                     }
1112 1108
                 }
1113 1109
             }
1114 1110
         }
1115 1111
 
1116 1112
         foreach ($j as $k => $v) {
1117
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
1113
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
1118 1114
             $tmp = [];
1119 1115
             foreach ($v->keymap as $kk => $vv) {
1120 1116
                 $tmp[] = $kk.' = '.$vv;
1121 1117
             }
1122
-            $sql .= implode(' AND ', $tmp) . ' ';
1118
+            $sql .= implode(' AND ', $tmp).' ';
1123 1119
         }
1124 1120
         foreach ($relations as $k => $v) {
1125 1121
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
@@ -1131,13 +1127,13 @@  discard block
 block discarded – undo
1131 1127
                 foreach ($v->keymap as $kk => $vv) {
1132 1128
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1133 1129
                 }
1134
-                $sql .= implode(' AND ', $tmp) . ' ';
1130
+                $sql .= implode(' AND ', $tmp).' ';
1135 1131
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON ';
1136 1132
                 $tmp = [];
1137 1133
                 foreach ($v->pivot_keymap as $kk => $vv) {
1138 1134
                     $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
1139 1135
                 }
1140
-                $sql .= implode(' AND ', $tmp) . ' ';
1136
+                $sql .= implode(' AND ', $tmp).' ';
1141 1137
             } else {
1142 1138
                 $alias = $getAlias($k);
1143 1139
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON ';
@@ -1146,36 +1142,36 @@  discard block
 block discarded – undo
1146 1142
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1147 1143
                 }
1148 1144
                 if ($v->sql) {
1149
-                    $tmp[] = $v->sql . ' ';
1145
+                    $tmp[] = $v->sql.' ';
1150 1146
                     $par = array_merge($par, $v->par ?? []);
1151 1147
                 }
1152
-                $sql .= implode(' AND ', $tmp) . ' ';
1148
+                $sql .= implode(' AND ', $tmp).' ';
1153 1149
             }
1154 1150
         }
1155 1151
         if (count($w)) {
1156 1152
             $sql .= 'WHERE ';
1157 1153
             $tmp = [];
1158 1154
             foreach ($w as $v) {
1159
-                $tmp[] = '(' . $v[0] . ')';
1155
+                $tmp[] = '('.$v[0].')';
1160 1156
                 $par = array_merge($par, $v[1]);
1161 1157
             }
1162 1158
             $sql .= implode(' AND ', $tmp).' ';
1163 1159
         }
1164 1160
         if (count($g)) {
1165
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
1161
+            $sql .= 'GROUP BY '.$g[0].' ';
1166 1162
             $par = array_merge($par, $g[1]);
1167 1163
         }
1168 1164
         if (count($h)) {
1169 1165
             $sql .= 'HAVING ';
1170 1166
             $tmp = [];
1171 1167
             foreach ($h as $v) {
1172
-                $tmp[] = '(' . $v[0] . ')';
1168
+                $tmp[] = '('.$v[0].')';
1173 1169
                 $par = array_merge($par, $v[1]);
1174 1170
             }
1175 1171
             $sql .= implode(' AND ', $tmp).' ';
1176 1172
         }
1177 1173
         if (count($o)) {
1178
-            $sql .= 'ORDER BY ' . $o[0] . ' ';
1174
+            $sql .= 'ORDER BY '.$o[0].' ';
1179 1175
             $par = array_merge($par, $o[1]);
1180 1176
         }
1181 1177
         $porder = [];
@@ -1183,30 +1179,30 @@  discard block
 block discarded – undo
1183 1179
             $porder[] = $this->getColumn($field)['name'];
1184 1180
         }
1185 1181
         if (count($porder)) {
1186
-            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
1182
+            $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
1187 1183
         }
1188 1184
 
1189 1185
         if ($this->li_of[0]) {
1190 1186
             if ($this->db->driverName() === 'oracle') {
1191
-                if ((int)$this->db->driverOption('version', 0) >= 12) {
1192
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
1187
+                if ((int) $this->db->driverOption('version', 0) >= 12) {
1188
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
1193 1189
                 } else {
1194
-                    $f = array_map(function ($v) {
1190
+                    $f = array_map(function($v) {
1195 1191
                         $v = explode(' ', trim($v), 2);
1196 1192
                         if (count($v) === 2) { return $v[1]; }
1197 1193
                         $v = explode('.', $v[0], 2);
1198 1194
                         return count($v) === 2 ? $v[1] : $v[0];
1199 1195
                     }, $select);
1200
-                    $sql = "SELECT " . implode(', ', $f) . " 
1196
+                    $sql = "SELECT ".implode(', ', $f)." 
1201 1197
                             FROM (
1202 1198
                                 SELECT tbl__.*, rownum rnum__ FROM (
1203
-                                    " . $sql . "
1199
+                                    " . $sql."
1204 1200
                                 ) tbl__ 
1205
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
1201
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
1206 1202
                             ) WHERE rnum__ > " . $this->li_of[1];
1207 1203
                 }
1208 1204
             } else {
1209
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
1205
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
1210 1206
             }
1211 1207
         }
1212 1208
         return $this->db->all($sql, $par);
Please login to merge, or discard this patch.