Completed
Push — master ( 726c6a...0c04f9 )
by Ivan
10:21
created
src/schema/TableQuery.php 1 patch
Spacing   +201 added lines, -205 removed lines patch added patch discarded remove patch
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
     /**
46 46
      * @var int[]
47 47
      */
48
-    protected $li_of = [0,0,0];
48
+    protected $li_of = [0, 0, 0];
49 49
     /**
50 50
      * @var array
51 51
      */
@@ -75,7 +75,7 @@  discard block
 block discarded – undo
75 75
     public function __construct(DBInterface $db, $table)
76 76
     {
77 77
         $this->db = $db;
78
-        $this->definition = $table instanceof Table ? $table : $this->db->definition((string)$table);
78
+        $this->definition = $table instanceof Table ? $table : $this->db->definition((string) $table);
79 79
         $primary = $this->definition->getPrimaryKey();
80 80
         $columns = $this->definition->getColumns();
81 81
         $this->pkey = count($primary) ? $primary : $columns;
@@ -98,7 +98,7 @@  discard block
 block discarded – undo
98 98
     {
99 99
         $column = explode('.', $column);
100 100
         if (count($column) === 1) {
101
-            $column = [ $this->definition->getFullName(), $column[0] ];
101
+            $column = [$this->definition->getFullName(), $column[0]];
102 102
             $col = $this->definition->getColumn($column[1]);
103 103
             if (!$col) {
104 104
                 throw new DBException('Invalid column name in own table');
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
                         throw new DBException('Invalid column name in related table');
122 122
                     }
123 123
                 } else {
124
-                    throw new DBException('Invalid foreign table name: ' . implode(',', $column));
124
+                    throw new DBException('Invalid foreign table name: '.implode(',', $column));
125 125
                 }
126 126
             }
127 127
         } else {
@@ -129,26 +129,26 @@  discard block
 block discarded – undo
129 129
             if ($this->definition->hasRelation(implode('.', $column))) {
130 130
                 $this->with(implode('.', $column), false);
131 131
                 $col = $this->definition->getRelation(implode('.', $column))->table->getColumn($name);
132
-                $column = [ implode('.', $column), $name ];
132
+                $column = [implode('.', $column), $name];
133 133
             } else {
134 134
                 $this->with(implode('.', $column), false);
135 135
                 $table = $this->definition;
136 136
                 $table = array_reduce(
137 137
                     $column,
138
-                    function ($carry, $item) use (&$table) {
138
+                    function($carry, $item) use (&$table) {
139 139
                         $table = $table->getRelation($item)->table;
140 140
                         return $table;
141 141
                     }
142 142
                 );
143 143
                 $col = $table->getColumn($name);
144
-                $column = [ implode(static::SEP, $column), $name ];
144
+                $column = [implode(static::SEP, $column), $name];
145 145
             }
146 146
         }
147
-        return [ 'name' => implode('.', $column), 'data' => $col ];
147
+        return ['name' => implode('.', $column), 'data' => $col];
148 148
     }
149 149
     protected function normalizeValue(TableColumn $col, $value)
150 150
     {
151
-        $strict = (int)$this->db->driverOption('strict', 0) > 0;
151
+        $strict = (int) $this->db->driverOption('strict', 0) > 0;
152 152
         if ($value === null && $col->isNullable()) {
153 153
             return null;
154 154
         }
@@ -158,7 +158,7 @@  discard block
 block discarded – undo
158 158
                     $temp = strtotime($value);
159 159
                     if (!$temp) {
160 160
                         if ($strict) {
161
-                            throw new DBException('Invalid value for date column ' . $col->getName());
161
+                            throw new DBException('Invalid value for date column '.$col->getName());
162 162
                         }
163 163
                         return null;
164 164
                     }
@@ -171,7 +171,7 @@  discard block
 block discarded – undo
171 171
                     return $value->format('Y-m-d');
172 172
                 }
173 173
                 if ($strict) {
174
-                    throw new DBException('Invalid value (unknown data type) for date column ' . $col->getName());
174
+                    throw new DBException('Invalid value (unknown data type) for date column '.$col->getName());
175 175
                 }
176 176
                 return $value;
177 177
             case 'datetime':
@@ -179,7 +179,7 @@  discard block
 block discarded – undo
179 179
                     $temp = strtotime($value);
180 180
                     if (!$temp) {
181 181
                         if ($strict) {
182
-                            throw new DBException('Invalid value for datetime column ' . $col->getName());
182
+                            throw new DBException('Invalid value for datetime column '.$col->getName());
183 183
                         }
184 184
                         return null;
185 185
                     }
@@ -192,7 +192,7 @@  discard block
 block discarded – undo
192 192
                     return $value->format('Y-m-d H:i:s');
193 193
                 }
194 194
                 if ($strict) {
195
-                    throw new DBException('Invalid value (unknown data type) for datetime column ' . $col->getName());
195
+                    throw new DBException('Invalid value (unknown data type) for datetime column '.$col->getName());
196 196
                 }
197 197
                 return $value;
198 198
             case 'enum':
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
                 if (is_int($value)) {
201 201
                     if (!isset($values[$value])) {
202 202
                         if ($strict) {
203
-                            throw new DBException('Invalid value (using integer) for enum ' . $col->getName());
203
+                            throw new DBException('Invalid value (using integer) for enum '.$col->getName());
204 204
                         }
205 205
                         return $value;
206 206
                     }
@@ -208,23 +208,23 @@  discard block
 block discarded – undo
208 208
                 }
209 209
                 if (!in_array($value, $col->getValues())) {
210 210
                     if ($strict) {
211
-                        throw new DBException('Invalid value for enum ' . $col->getName());
211
+                        throw new DBException('Invalid value for enum '.$col->getName());
212 212
                     }
213 213
                     return 0;
214 214
                 }
215 215
                 return $value;
216 216
             case 'int':
217 217
                 $temp = preg_replace('([^+\-0-9]+)', '', $value);
218
-                return is_string($temp) ? (int)$temp : 0;
218
+                return is_string($temp) ? (int) $temp : 0;
219 219
             case 'float':
220 220
                 $temp = preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', $value));
221
-                return is_string($temp) ? (float)$temp : 0;
221
+                return is_string($temp) ? (float) $temp : 0;
222 222
             case 'text':
223 223
                 // check using strlen first, in order to avoid hitting mb_ functions which might be polyfilled
224 224
                 // because the polyfill is quite slow
225 225
                 if ($col->hasLength() && strlen($value) > $col->getLength() && mb_strlen($value) > $col->getLength()) {
226 226
                     if ($strict) {
227
-                        throw new DBException('Invalid value for text column ' . $col->getName());
227
+                        throw new DBException('Invalid value for text column '.$col->getName());
228 228
                     }
229 229
                     return mb_substr($value, 0, $col->getLength());
230 230
                 }
@@ -246,35 +246,32 @@  discard block
 block discarded – undo
246 246
             // str_replace(['%', '_'], ['\\%','\\_'], $q)
247 247
             return $negate ?
248 248
                 [
249
-                    $name . ' NOT LIKE ?',
250
-                    [ $this->normalizeValue($column, $value) ]
251
-                ] :
252
-                [
253
-                    $name . ' LIKE ?',
254
-                    [ $this->normalizeValue($column, $value) ]
249
+                    $name.' NOT LIKE ?',
250
+                    [$this->normalizeValue($column, $value)]
251
+                ] : [
252
+                    $name.' LIKE ?',
253
+                    [$this->normalizeValue($column, $value)]
255 254
                 ];
256 255
         }
257 256
         if (is_null($value)) {
258 257
             return $negate ?
259
-                [ $name . ' IS NOT NULL', [] ]:
260
-                [ $name . ' IS NULL', [] ];
258
+                [$name.' IS NOT NULL', []] : [$name.' IS NULL', []];
261 259
         }
262 260
         if (!is_array($value)) {
263 261
             return $negate ?
264 262
                 [
265
-                    $name . ' <> ?',
266
-                    [ $this->normalizeValue($column, $value) ]
267
-                ] :
268
-                [
269
-                    $name . ' = ?',
270
-                    [ $this->normalizeValue($column, $value) ]
263
+                    $name.' <> ?',
264
+                    [$this->normalizeValue($column, $value)]
265
+                ] : [
266
+                    $name.' = ?',
267
+                    [$this->normalizeValue($column, $value)]
271 268
                 ];
272 269
         }
273 270
         if (isset($value['beg']) && strlen($value['beg']) && (!isset($value['end']) || !strlen($value['end']))) {
274
-            $value = [ 'gte' => $value['beg'] ];
271
+            $value = ['gte' => $value['beg']];
275 272
         }
276 273
         if (isset($value['end']) && strlen($value['end']) && (!isset($value['beg']) || !strlen($value['beg']))) {
277
-            $value = [ 'lte' => $value['end'] ];
274
+            $value = ['lte' => $value['end']];
278 275
         }
279 276
         if (isset($value['beg']) && isset($value['end'])) {
280 277
             return $negate ?
@@ -284,8 +281,7 @@  discard block
 block discarded – undo
284 281
                         $this->normalizeValue($column, $value['beg']),
285 282
                         $this->normalizeValue($column, $value['end'])
286 283
                     ]
287
-                ] :
288
-                [
284
+                ] : [
289 285
                     $name.' BETWEEN ? AND ?',
290 286
                     [
291 287
                         $this->normalizeValue($column, $value['beg']),
@@ -297,42 +293,42 @@  discard block
 block discarded – undo
297 293
             $sql = [];
298 294
             $par = [];
299 295
             if (isset($value['gt'])) {
300
-                $sql[] = $name. ' ' . ($negate ? '<=' : '>') . ' ?';
296
+                $sql[] = $name.' '.($negate ? '<=' : '>').' ?';
301 297
                 $par[] = $this->normalizeValue($column, $value['gt']);
302 298
             }
303 299
             if (isset($value['gte'])) {
304
-                $sql[] = $name. ' ' . ($negate ? '<' : '>=') . ' ?';
300
+                $sql[] = $name.' '.($negate ? '<' : '>=').' ?';
305 301
                 $par[] = $this->normalizeValue($column, $value['gte']);
306 302
             }
307 303
             if (isset($value['lt'])) {
308
-                $sql[] = $name. ' ' . ($negate ? '>=' : '<') . ' ?';
304
+                $sql[] = $name.' '.($negate ? '>=' : '<').' ?';
309 305
                 $par[] = $this->normalizeValue($column, $value['lt']);
310 306
             }
311 307
             if (isset($value['lte'])) {
312
-                $sql[] = $name. ' ' . ($negate ? '>' : '<=') . ' ?';
308
+                $sql[] = $name.' '.($negate ? '>' : '<=').' ?';
313 309
                 $par[] = $this->normalizeValue($column, $value['lte']);
314 310
             }
315 311
             return [
316
-                '(' . implode(' AND ', $sql) . ')',
312
+                '('.implode(' AND ', $sql).')',
317 313
                 $par
318 314
             ];
319 315
         }
320 316
 
321
-        $value = array_values(array_map(function ($v) use ($column) {
317
+        $value = array_values(array_map(function($v) use ($column) {
322 318
             return $this->normalizeValue($column, $v);
323 319
         }, $value));
324 320
         if ($this->db->driverName() === 'oracle') {
325 321
             $sql = [];
326 322
             $par = [];
327 323
             for ($i = 0; $i < count($value); $i += 500) {
328
-                $sql[] = $negate ? $name . ' NOT IN (??)' : $name . ' IN (??)';
324
+                $sql[] = $negate ? $name.' NOT IN (??)' : $name.' IN (??)';
329 325
                 $par[] = array_slice($value, $i, 500);
330 326
             }
331
-            $sql = '(' . implode($negate ? ' AND ' : ' OR ', $sql) . ')';
332
-            return [ $sql, [$par] ];
327
+            $sql = '('.implode($negate ? ' AND ' : ' OR ', $sql).')';
328
+            return [$sql, [$par]];
333 329
         }
334 330
         return [
335
-            $negate ? $name . ' NOT IN (??)' : $name . ' IN (??)',
331
+            $negate ? $name.' NOT IN (??)' : $name.' IN (??)',
336 332
             [$value]
337 333
         ];
338 334
     }
@@ -364,7 +360,7 @@  discard block
 block discarded – undo
364 360
                 $par = array_merge($par, $temp[1]);
365 361
             }
366 362
         }
367
-        return $this->where('(' . implode(' OR ', $sql) . ')', $par);
363
+        return $this->where('('.implode(' OR ', $sql).')', $par);
368 364
     }
369 365
     /**
370 366
      * Filter the results matching all of the criteria
@@ -382,7 +378,7 @@  discard block
 block discarded – undo
382 378
                 $par = array_merge($par, $temp[1]);
383 379
             }
384 380
         }
385
-        return $this->where('(' . implode(' AND ', $sql) . ')', $par);
381
+        return $this->where('('.implode(' AND ', $sql).')', $par);
386 382
     }
387 383
     /**
388 384
      * Sort by a column
@@ -397,7 +393,7 @@  discard block
 block discarded – undo
397 393
         } catch (DBException $e) {
398 394
             throw new DBException('Invalid sort column');
399 395
         }
400
-        return $this->order($column . ' ' . ($desc ? 'DESC' : 'ASC'));
396
+        return $this->order($column.' '.($desc ? 'DESC' : 'ASC'));
401 397
     }
402 398
     /**
403 399
      * Group by a column (or columns)
@@ -407,7 +403,7 @@  discard block
 block discarded – undo
407 403
     public function group($column) : self
408 404
     {
409 405
         if (!is_array($column)) {
410
-            $column = [ $column ];
406
+            $column = [$column];
411 407
         }
412 408
         foreach ($column as $k => $v) {
413 409
             $column[$k] = $this->getColumn($v)['name'];
@@ -449,7 +445,7 @@  discard block
 block discarded – undo
449 445
         $this->order = [];
450 446
         $this->having = [];
451 447
         $this->aliases = [];
452
-        $this->li_of = [0,0,0];
448
+        $this->li_of = [0, 0, 0];
453 449
         $this->qiterator = null;
454 450
         return $this;
455 451
     }
@@ -462,7 +458,7 @@  discard block
 block discarded – undo
462 458
     public function groupBy(string $sql, array $params = []) : self
463 459
     {
464 460
         $this->qiterator = null;
465
-        $this->group = [ $sql, $params ];
461
+        $this->group = [$sql, $params];
466 462
         return $this;
467 463
     }
468 464
     /**
@@ -475,7 +471,7 @@  discard block
 block discarded – undo
475 471
      */
476 472
     public function join($table, array $fields, string $name = null, bool $multiple = true)
477 473
     {
478
-        $table = $table instanceof Table ? $table : $this->db->definition((string)$table);
474
+        $table = $table instanceof Table ? $table : $this->db->definition((string) $table);
479 475
         $name = $name ?? $table->getName();
480 476
         if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) {
481 477
             throw new DBException('Alias / table name already in use');
@@ -484,7 +480,7 @@  discard block
 block discarded – undo
484 480
         foreach ($fields as $k => $v) {
485 481
             $k = explode('.', $k, 2);
486 482
             $k = count($k) == 2 ? $k[1] : $k[0];
487
-            $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name'];
483
+            $this->joins[$name]->keymap[$this->getColumn($name.'.'.$k)['name']] = $this->getColumn($v)['name'];
488 484
         }
489 485
         return $this;
490 486
     }
@@ -497,7 +493,7 @@  discard block
 block discarded – undo
497 493
     public function where(string $sql, array $params = []) : self
498 494
     {
499 495
         $this->qiterator = null;
500
-        $this->where[] = [ $sql, $params ];
496
+        $this->where[] = [$sql, $params];
501 497
         return $this;
502 498
     }
503 499
     /**
@@ -509,7 +505,7 @@  discard block
 block discarded – undo
509 505
     public function having(string $sql, array $params = []) : self
510 506
     {
511 507
         $this->qiterator = null;
512
-        $this->having[] = [ $sql, $params ];
508
+        $this->having[] = [$sql, $params];
513 509
         return $this;
514 510
     }
515 511
     /**
@@ -529,12 +525,12 @@  discard block
 block discarded – undo
529 525
                     throw new \Exception();
530 526
                 }
531 527
                 $name = $this->getColumn(trim($name))['name'];
532
-                $sql = $name . ' ' . (strpos(strtolower($sql), ' desc') ? 'DESC' : 'ASC');
528
+                $sql = $name.' '.(strpos(strtolower($sql), ' desc') ? 'DESC' : 'ASC');
533 529
             } catch (\Exception $e) {
534 530
                 $name = null;
535 531
             }
536 532
         }
537
-        $this->order = [ $sql, $params, $name ];
533
+        $this->order = [$sql, $params, $name];
538 534
         return $this;
539 535
     }
540 536
     /**
@@ -546,7 +542,7 @@  discard block
 block discarded – undo
546 542
     public function limit(int $limit, int $offset = 0, bool $limitOnMainTable = false) : self
547 543
     {
548 544
         $this->qiterator = null;
549
-        $this->li_of = [ $limit, $offset, $limitOnMainTable ? 1 : 0 ];
545
+        $this->li_of = [$limit, $offset, $limitOnMainTable ? 1 : 0];
550 546
         return $this;
551 547
     }
552 548
     /**
@@ -556,9 +552,9 @@  discard block
 block discarded – undo
556 552
     public function count() : int
557 553
     {
558 554
         $aliases = [];
559
-        $getAlias = function ($name) use (&$aliases) {
555
+        $getAlias = function($name) use (&$aliases) {
560 556
             // to bypass use: return $name;
561
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
557
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
562 558
         };
563 559
         $table = $this->definition->getFullName();
564 560
         $sql = 'SELECT COUNT(DISTINCT ('.$table.'.'.implode(', '.$table.'.', $this->pkey).')) FROM '.$table.' ';
@@ -572,35 +568,35 @@  discard block
 block discarded – undo
572 568
         $h = $this->having;
573 569
         $o = $this->order;
574 570
         $g = $this->group;
575
-        $j = array_map(function ($v) {
571
+        $j = array_map(function($v) {
576 572
             return clone $v;
577 573
         }, $this->joins);
578 574
         foreach ($this->definition->getRelations() as $k => $v) {
579 575
             foreach ($w as $kk => $vv) {
580
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
581
-                    $relations[$k] = [ $v, $table ];
582
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
576
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
577
+                    $relations[$k] = [$v, $table];
578
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
583 579
                 }
584 580
             }
585
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
586
-                $relations[$k] = [ $v, $table ];
581
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
582
+                $relations[$k] = [$v, $table];
587 583
             }
588 584
             foreach ($h as $kk => $vv) {
589
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
590
-                    $relations[$k] = [ $v, $table ];
591
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
585
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
586
+                    $relations[$k] = [$v, $table];
587
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
592 588
                 }
593 589
             }
594
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
595
-                $relations[$k] = [ $v, $table ];
596
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
590
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
591
+                $relations[$k] = [$v, $table];
592
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
597 593
             }
598 594
             foreach ($j as $kk => $vv) {
599 595
                 foreach ($vv->keymap as $kkk => $vvv) {
600
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vvv)) {
601
-                        $relations[$k] = [ $v, $table ];
596
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vvv)) {
597
+                        $relations[$k] = [$v, $table];
602 598
                         $j[$kk]->keymap[$kkk] =
603
-                            preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vvv);
599
+                            preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vvv);
604 600
                     }
605 601
                 }
606 602
             }
@@ -616,13 +612,13 @@  discard block
 block discarded – undo
616 612
                 foreach ($v->keymap as $kk => $vv) {
617 613
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
618 614
                 }
619
-                $sql .= implode(' AND ', $tmp) . ' ';
615
+                $sql .= implode(' AND ', $tmp).' ';
620 616
                 $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$getAlias($k).' ON ';
621 617
                 $tmp = [];
622 618
                 foreach ($v->pivot_keymap as $kk => $vv) {
623 619
                     $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
624 620
                 }
625
-                $sql .= implode(' AND ', $tmp) . ' ';
621
+                $sql .= implode(' AND ', $tmp).' ';
626 622
             } else {
627 623
                 $alias = $getAlias($k);
628 624
                 $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$alias.' ON ';
@@ -631,38 +627,38 @@  discard block
 block discarded – undo
631 627
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
632 628
                 }
633 629
                 if ($v->sql) {
634
-                    $tmp[] = $v->sql . ' ';
630
+                    $tmp[] = $v->sql.' ';
635 631
                     $par = array_merge($par, $v->par ?? []);
636 632
                 }
637
-                $sql .= implode(' AND ', $tmp) . ' ';
633
+                $sql .= implode(' AND ', $tmp).' ';
638 634
             }
639 635
         }
640 636
         foreach ($j as $k => $v) {
641
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getFullName().' '.$k.' ON ';
637
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getFullName().' '.$k.' ON ';
642 638
             $tmp = [];
643 639
             foreach ($v->keymap as $kk => $vv) {
644 640
                 $tmp[] = $kk.' = '.$vv;
645 641
             }
646
-            $sql .= implode(' AND ', $tmp) . ' ';
642
+            $sql .= implode(' AND ', $tmp).' ';
647 643
         }
648 644
         if (count($w)) {
649 645
             $sql .= 'WHERE ';
650 646
             $tmp = [];
651 647
             foreach ($w as $v) {
652
-                $tmp[] = '(' . $v[0] . ')';
648
+                $tmp[] = '('.$v[0].')';
653 649
                 $par = array_merge($par, $v[1]);
654 650
             }
655 651
             $sql .= implode(' AND ', $tmp).' ';
656 652
         }
657 653
         if (count($g)) {
658
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
654
+            $sql .= 'GROUP BY '.$g[0].' ';
659 655
             $par = array_merge($par, $g[1]);
660 656
         }
661 657
         if (count($h)) {
662 658
             $sql .= 'HAVING ';
663 659
             $tmp = [];
664 660
             foreach ($h as $v) {
665
-                $tmp[] = '(' . $v[0] . ')';
661
+                $tmp[] = '('.$v[0].')';
666 662
                 $par = array_merge($par, $v[1]);
667 663
             }
668 664
             $sql .= implode(' AND ', $tmp).' ';
@@ -696,7 +692,7 @@  discard block
 block discarded – undo
696 692
                     $this->with(implode('.', $temp));
697 693
                     $table = array_reduce(
698 694
                         $temp,
699
-                        function ($carry, $item) use (&$table) {
695
+                        function($carry, $item) use (&$table) {
700 696
                             return $table->getRelation($item)->table;
701 697
                         }
702 698
                     );
@@ -705,7 +701,7 @@  discard block
 block discarded – undo
705 701
                 }
706 702
                 unset($fields[$k]);
707 703
                 foreach ($cols as $col) {
708
-                    $fields[] = $table . '.' . $col;
704
+                    $fields[] = $table.'.'.$col;
709 705
                 }
710 706
             }
711 707
         }
@@ -739,9 +735,9 @@  discard block
 block discarded – undo
739 735
             return $this->qiterator;
740 736
         }
741 737
         $aliases = [];
742
-        $getAlias = function ($name) use (&$aliases) {
738
+        $getAlias = function($name) use (&$aliases) {
743 739
             // to bypass use: return $name;
744
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
740
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
745 741
         };
746 742
         $table = $this->definition->getName();
747 743
         if ($fields !== null) {
@@ -757,7 +753,7 @@  discard block
 block discarded – undo
757 753
         $h = $this->having;
758 754
         $o = $this->order;
759 755
         $g = $this->group;
760
-        $j = array_map(function ($v) {
756
+        $j = array_map(function($v) {
761 757
             return clone $v;
762 758
         }, $this->joins);
763 759
 
@@ -771,32 +767,32 @@  discard block
 block discarded – undo
771 767
                 continue;
772 768
             }
773 769
             foreach ($f as $kk => $field) {
774
-                if (strpos($field, $k . '.') === 0) {
775
-                    $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field);
770
+                if (strpos($field, $k.'.') === 0) {
771
+                    $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field);
776 772
                 }
777 773
             }
778 774
             foreach ($w as $kk => $v) {
779
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
780
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
775
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
776
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
781 777
                 }
782 778
             }
783 779
             foreach ($h as $kk => $v) {
784
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
785
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
780
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
781
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
786 782
                 }
787 783
             }
788
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
789
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
784
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
785
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
790 786
             }
791
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
792
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
787
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
788
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
793 789
             }
794 790
             foreach ($j as $kk => $v) {
795 791
                 foreach ($v->keymap as $kkk => $vv) {
796
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
792
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
797 793
                         $j[$kk]->keymap[$kkk] = preg_replace(
798
-                            '(\b'.preg_quote($k . '.'). ')i',
799
-                            $getAlias($k) . '.',
794
+                            '(\b'.preg_quote($k.'.').')i',
795
+                            $getAlias($k).'.',
800 796
                             $vv
801 797
                         );
802 798
                     }
@@ -805,38 +801,38 @@  discard block
 block discarded – undo
805 801
         }
806 802
         foreach ($this->definition->getRelations() as $k => $relation) {
807 803
             foreach ($f as $kk => $field) {
808
-                if (strpos($field, $k . '.') === 0) {
809
-                    $relations[$k] = [ $relation, $table ];
810
-                    $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field);
804
+                if (strpos($field, $k.'.') === 0) {
805
+                    $relations[$k] = [$relation, $table];
806
+                    $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field);
811 807
                 }
812 808
             }
813 809
             foreach ($w as $kk => $v) {
814
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
815
-                    $relations[$k] = [ $relation, $table ];
816
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
810
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
811
+                    $relations[$k] = [$relation, $table];
812
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
817 813
                 }
818 814
             }
819 815
             foreach ($h as $kk => $v) {
820
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
821
-                    $relations[$k] = [ $relation, $table ];
822
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
816
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) {
817
+                    $relations[$k] = [$relation, $table];
818
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]);
823 819
                 }
824 820
             }
825
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
826
-                $relations[$k] = [ $relation, $table ];
827
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
821
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
822
+                $relations[$k] = [$relation, $table];
823
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
828 824
             }
829
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
830
-                $relations[$k] = [ $relation, $table ];
831
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
825
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
826
+                $relations[$k] = [$relation, $table];
827
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
832 828
             }
833 829
             foreach ($j as $kk => $v) {
834 830
                 foreach ($v->keymap as $kkk => $vv) {
835
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
836
-                        $relations[$k] = [ $relation, $table ];
831
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) {
832
+                        $relations[$k] = [$relation, $table];
837 833
                         $j[$kk]->keymap[$kkk] = preg_replace(
838
-                            '(\b'.preg_quote($k . '.'). ')i',
839
-                            $getAlias($k) . '.',
834
+                            '(\b'.preg_quote($k.'.').')i',
835
+                            $getAlias($k).'.',
840 836
                             $vv
841 837
                         );
842 838
                     }
@@ -845,12 +841,12 @@  discard block
 block discarded – undo
845 841
         }
846 842
         $select = [];
847 843
         foreach ($f as $k => $field) {
848
-            $select[] = $field . (!is_numeric($k) ? ' ' . $k : '');
844
+            $select[] = $field.(!is_numeric($k) ? ' '.$k : '');
849 845
         }
850 846
         foreach ($this->withr as $name => $relation) {
851 847
             if ($relation[2]) {
852 848
                 foreach ($relation[0]->table->getColumns() as $column) {
853
-                    $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column);
849
+                    $select[] = $getAlias($name).'.'.$column.' '.$getAlias($name.static::SEP.$column);
854 850
                 }
855 851
             }
856 852
         }
@@ -870,13 +866,13 @@  discard block
 block discarded – undo
870 866
                 foreach ($v->keymap as $kk => $vv) {
871 867
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
872 868
                 }
873
-                $sql .= implode(' AND ', $tmp) . ' ';
869
+                $sql .= implode(' AND ', $tmp).' ';
874 870
                 $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$getAlias($relation).' ON ';
875 871
                 $tmp = [];
876 872
                 foreach ($v->pivot_keymap as $kk => $vv) {
877 873
                     $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' ';
878 874
                 }
879
-                $sql .= implode(' AND ', $tmp) . ' ';
875
+                $sql .= implode(' AND ', $tmp).' ';
880 876
             } else {
881 877
                 $alias = $getAlias($relation);
882 878
 
@@ -886,22 +882,22 @@  discard block
 block discarded – undo
886 882
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
887 883
                 }
888 884
                 if ($v->sql) {
889
-                    $tmp[] = $v->sql . ' ';
885
+                    $tmp[] = $v->sql.' ';
890 886
                     $par = array_merge($par, $v->par ?? []);
891 887
                 }
892
-                $sql .= implode(' AND ', $tmp) . ' ';
888
+                $sql .= implode(' AND ', $tmp).' ';
893 889
             }
894 890
         }
895 891
         foreach ($j as $k => $v) {
896 892
             if ($v->many) {
897 893
                 $many = true;
898 894
             }
899
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
895
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
900 896
             $tmp = [];
901 897
             foreach ($v->keymap as $kk => $vv) {
902 898
                 $tmp[] = $kk.' = '.$vv;
903 899
             }
904
-            $sql .= implode(' AND ', $tmp) . ' ';
900
+            $sql .= implode(' AND ', $tmp).' ';
905 901
         }
906 902
         if ($many && count($porder) && $this->li_of[2] === 1) {
907 903
             $ids = $this->ids();
@@ -909,9 +905,9 @@  discard block
 block discarded – undo
909 905
                 if (count($porder) > 1) {
910 906
                     $pkw = [];
911 907
                     foreach ($porder as $name) {
912
-                        $pkw[] = $name . ' = ?';
908
+                        $pkw[] = $name.' = ?';
913 909
                     }
914
-                    $pkw = '(' . implode(' AND ', $pkw) . ')';
910
+                    $pkw = '('.implode(' AND ', $pkw).')';
915 911
                     $pkp = [];
916 912
                     foreach ($ids as $id) {
917 913
                         foreach ($id as $p) {
@@ -923,51 +919,51 @@  discard block
 block discarded – undo
923 919
                         $pkp
924 920
                     ];
925 921
                 } else {
926
-                    $w[] = [ $porder[0] . ' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids ];
922
+                    $w[] = [$porder[0].' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids];
927 923
                 }
928 924
             } else {
929
-                $w[] = [ '1=0', [] ];
925
+                $w[] = ['1=0', []];
930 926
             }
931 927
         }
932 928
         if (count($w)) {
933 929
             $sql .= 'WHERE ';
934 930
             $tmp = [];
935 931
             foreach ($w as $v) {
936
-                $tmp[] = '(' . $v[0] . ')';
932
+                $tmp[] = '('.$v[0].')';
937 933
                 $par = array_merge($par, $v[1]);
938 934
             }
939 935
             $sql .= implode(' AND ', $tmp).' ';
940 936
         }
941 937
         if (count($g)) {
942
-            $sql .= 'GROUP BY ' . $g[0] . ' ';
938
+            $sql .= 'GROUP BY '.$g[0].' ';
943 939
             $par = array_merge($par, $g[1]);
944 940
         }
945 941
         if (count($h)) {
946 942
             $sql .= 'HAVING ';
947 943
             $tmp = [];
948 944
             foreach ($h as $v) {
949
-                $tmp[] = '(' . $v[0] . ')';
945
+                $tmp[] = '('.$v[0].')';
950 946
                 $par = array_merge($par, $v[1]);
951 947
             }
952 948
             $sql .= implode(' AND ', $tmp).' ';
953 949
         }
954 950
         if (count($o)) {
955
-            $sql .= 'ORDER BY ' . $o[0] . ' ';
951
+            $sql .= 'ORDER BY '.$o[0].' ';
956 952
             $par = array_merge($par, $o[1]);
957 953
         }
958 954
         if (!count($g) && count($porder)) {
959 955
             $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC';
960
-            $porder = array_map(function ($v) use ($pdir) {
961
-                return $v . ' ' . $pdir;
956
+            $porder = array_map(function($v) use ($pdir) {
957
+                return $v.' '.$pdir;
962 958
             }, $porder);
963
-            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
959
+            $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
964 960
         }
965 961
         if ((!$many || $this->li_of[2] === 0 || !count($porder)) && $this->li_of[0]) {
966 962
             if ($this->db->driverName() === 'oracle') {
967
-                if ((int)$this->db->driverOption('version', 0) >= 12) {
968
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
963
+                if ((int) $this->db->driverOption('version', 0) >= 12) {
964
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
969 965
                 } else {
970
-                    $f = array_map(function ($v) {
966
+                    $f = array_map(function($v) {
971 967
                         $v = explode(' ', trim($v), 2);
972 968
                         if (count($v) === 2) {
973 969
                             return $v[1];
@@ -975,16 +971,16 @@  discard block
 block discarded – undo
975 971
                         $v = explode('.', $v[0], 2);
976 972
                         return count($v) === 2 ? $v[1] : $v[0];
977 973
                     }, $select);
978
-                    $sql = "SELECT " . implode(', ', $f) . " 
974
+                    $sql = "SELECT ".implode(', ', $f)." 
979 975
                             FROM (
980 976
                                 SELECT tbl__.*, rownum rnum__ FROM (
981
-                                    " . $sql . "
977
+                                    " . $sql."
982 978
                                 ) tbl__ 
983
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
979
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
984 980
                             ) WHERE rnum__ > " . $this->li_of[1];
985 981
                 }
986 982
             } else {
987
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
983
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
988 984
             }
989 985
         }
990 986
         return $this->qiterator = new TableQueryIterator(
@@ -1034,12 +1030,12 @@  discard block
 block discarded – undo
1034 1030
                 $ret[$k] = str_repeat(' ', 255);
1035 1031
                 $par[] = &$ret[$k];
1036 1032
             }
1037
-            $sql .= ' RETURNING ' . implode(',', $primary) .
1038
-                ' INTO ' . implode(',', array_fill(0, count($primary), '?'));
1033
+            $sql .= ' RETURNING '.implode(',', $primary).
1034
+                ' INTO '.implode(',', array_fill(0, count($primary), '?'));
1039 1035
             $this->db->query($sql, $par);
1040 1036
             return $ret;
1041 1037
         } elseif ($this->db->driverName() === 'postgre') {
1042
-            $sql .= ' RETURNING ' . implode(',', $primary);
1038
+            $sql .= ' RETURNING '.implode(',', $primary);
1043 1039
             return $this->db->one($sql, $par, false);
1044 1040
         } else {
1045 1041
             $ret = [];
@@ -1070,9 +1066,9 @@  discard block
 block discarded – undo
1070 1066
         }
1071 1067
         $sql = 'UPDATE '.$table.' SET ';
1072 1068
         $par = [];
1073
-        $sql .= implode(', ', array_map(function ($v) {
1074
-            return $v . ' = ?';
1075
-        }, array_keys($update))) . ' ';
1069
+        $sql .= implode(', ', array_map(function($v) {
1070
+            return $v.' = ?';
1071
+        }, array_keys($update))).' ';
1076 1072
         $par = array_merge($par, array_values($update));
1077 1073
         if (count($this->where)) {
1078 1074
             $sql .= 'WHERE ';
@@ -1081,7 +1077,7 @@  discard block
 block discarded – undo
1081 1077
                 $tmp[] = $v[0];
1082 1078
                 $par = array_merge($par, $v[1]);
1083 1079
             }
1084
-            $sql .= implode(' AND ', $tmp) . ' ';
1080
+            $sql .= implode(' AND ', $tmp).' ';
1085 1081
         }
1086 1082
         if (count($this->order)) {
1087 1083
             $sql .= $this->order[0];
@@ -1105,7 +1101,7 @@  discard block
 block discarded – undo
1105 1101
                 $tmp[] = $v[0];
1106 1102
                 $par = array_merge($par, $v[1]);
1107 1103
             }
1108
-            $sql .= implode(' AND ', $tmp) . ' ';
1104
+            $sql .= implode(' AND ', $tmp).' ';
1109 1105
         }
1110 1106
         if (count($this->order)) {
1111 1107
             $sql .= $this->order[0];
@@ -1124,17 +1120,17 @@  discard block
 block discarded – undo
1124 1120
         $table = $this->definition;
1125 1121
         if ($table->hasRelation($relation)) {
1126 1122
             $temp = $table->getRelation($relation);
1127
-            $this->withr[$relation] = [ $temp, $table->getName(), $select || ($this->withr[$relation][2] ?? false) ];
1123
+            $this->withr[$relation] = [$temp, $table->getName(), $select || ($this->withr[$relation][2] ?? false)];
1128 1124
         } else {
1129 1125
             $parts = explode('.', $relation);
1130 1126
             array_reduce(
1131 1127
                 $parts,
1132
-                function ($carry, $item) use (&$table, $select) {
1128
+                function($carry, $item) use (&$table, $select) {
1133 1129
                     if (!$table->hasRelation($item)) {
1134
-                        throw new DBException('Invalid relation name: '.$table->getName().' -> ' . $item);
1130
+                        throw new DBException('Invalid relation name: '.$table->getName().' -> '.$item);
1135 1131
                     }
1136 1132
                     $relation = $table->getRelation($item);
1137
-                    $name = $carry ? $carry . static::SEP . $item : $item;
1133
+                    $name = $carry ? $carry.static::SEP.$item : $item;
1138 1134
                     $this->withr[$name] = [
1139 1135
                         $relation,
1140 1136
                         $carry ?? $table->getName(),
@@ -1185,9 +1181,9 @@  discard block
 block discarded – undo
1185 1181
         }
1186 1182
 
1187 1183
         $aliases = [];
1188
-        $getAlias = function ($name) use (&$aliases) {
1184
+        $getAlias = function($name) use (&$aliases) {
1189 1185
             // to bypass use: return $name;
1190
-            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
1186
+            return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases);
1191 1187
         };
1192 1188
         
1193 1189
         $table = $this->definition->getName();
@@ -1199,65 +1195,65 @@  discard block
 block discarded – undo
1199 1195
         $h = $this->having;
1200 1196
         $o = $this->order;
1201 1197
         $g = $this->group;
1202
-        $j = array_map(function ($v) {
1198
+        $j = array_map(function($v) {
1203 1199
             return clone $v;
1204 1200
         }, $this->joins);
1205 1201
         foreach ($this->definition->getRelations() as $k => $v) {
1206 1202
             foreach ($w as $kk => $vv) {
1207
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1208
-                    $relations[$k] = [ $v, $table ];
1209
-                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
1203
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
1204
+                    $relations[$k] = [$v, $table];
1205
+                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
1210 1206
                 }
1211 1207
             }
1212
-            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
1213
-                $relations[$k] = [ $v, $table ];
1214
-                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
1215
-                $o[2] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[2]);
1208
+            if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) {
1209
+                $relations[$k] = [$v, $table];
1210
+                $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]);
1211
+                $o[2] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[2]);
1216 1212
             }
1217 1213
             foreach ($h as $kk => $vv) {
1218
-                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) {
1219
-                    $relations[$k] = [ $v, $table ];
1220
-                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]);
1214
+                if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) {
1215
+                    $relations[$k] = [$v, $table];
1216
+                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]);
1221 1217
                 }
1222 1218
             }
1223
-            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
1224
-                $relations[$k] = [ $v, $table ];
1225
-                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
1219
+            if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) {
1220
+                $relations[$k] = [$v, $table];
1221
+                $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]);
1226 1222
             }
1227 1223
             foreach ($j as $kk => $vv) {
1228 1224
                 foreach ($vv->keymap as $kkk => $vvv) {
1229
-                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vvv)) {
1230
-                        $relations[$k] = [ $v, $table ];
1225
+                    if (preg_match('(\b'.preg_quote($k.'.').')i', $vvv)) {
1226
+                        $relations[$k] = [$v, $table];
1231 1227
                         $j[$kk]->keymap[$kkk] =
1232
-                            preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vvv);
1228
+                            preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vvv);
1233 1229
                     }
1234 1230
                 }
1235 1231
             }
1236 1232
         }
1237 1233
 
1238
-        $key = array_map(function ($v) use ($table) {
1239
-            return $table . '.' . $v;
1234
+        $key = array_map(function($v) use ($table) {
1235
+            return $table.'.'.$v;
1240 1236
         }, $this->pkey);
1241 1237
         $own = false;
1242 1238
         $dir = 'ASC';
1243 1239
         if (count($o)) {
1244 1240
             $dir = strpos($o[0], ' DESC') ? 'DESC' : 'ASC';
1245
-            $own = strpos($o[2], $table . '.') === 0;
1241
+            $own = strpos($o[2], $table.'.') === 0;
1246 1242
         }
1247 1243
 
1248 1244
         $dst = $key;
1249 1245
         if (count($o)) {
1250 1246
             if ($own) {
1251 1247
                 // if using own table - do not use max/min in order - that will prevent index usage
1252
-                $dst[] = $o[2] . ' orderbyfix___';
1248
+                $dst[] = $o[2].' orderbyfix___';
1253 1249
             } else {
1254
-                $dst[] = 'MAX(' . $o[2] . ') orderbyfix___';
1250
+                $dst[] = 'MAX('.$o[2].') orderbyfix___';
1255 1251
             }
1256 1252
         }
1257 1253
         $dst = array_unique($dst);
1258 1254
 
1259 1255
         $par = [];
1260
-        $sql  = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$this->definition->getFullName().' ';
1256
+        $sql = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$this->definition->getFullName().' ';
1261 1257
         foreach ($relations as $k => $v) {
1262 1258
             $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
1263 1259
             $v = $v[0];
@@ -1268,13 +1264,13 @@  discard block
 block discarded – undo
1268 1264
                 foreach ($v->keymap as $kk => $vv) {
1269 1265
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1270 1266
                 }
1271
-                $sql .= implode(' AND ', $tmp) . ' ';
1267
+                $sql .= implode(' AND ', $tmp).' ';
1272 1268
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON ';
1273 1269
                 $tmp = [];
1274 1270
                 foreach ($v->pivot_keymap as $kk => $vv) {
1275 1271
                     $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' ';
1276 1272
                 }
1277
-                $sql .= implode(' AND ', $tmp) . ' ';
1273
+                $sql .= implode(' AND ', $tmp).' ';
1278 1274
             } else {
1279 1275
                 $alias = $getAlias($k);
1280 1276
                 $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON ';
@@ -1283,37 +1279,37 @@  discard block
 block discarded – undo
1283 1279
                     $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
1284 1280
                 }
1285 1281
                 if ($v->sql) {
1286
-                    $tmp[] = $v->sql . ' ';
1282
+                    $tmp[] = $v->sql.' ';
1287 1283
                     $par = array_merge($par, $v->par ?? []);
1288 1284
                 }
1289
-                $sql .= implode(' AND ', $tmp) . ' ';
1285
+                $sql .= implode(' AND ', $tmp).' ';
1290 1286
             }
1291 1287
         }
1292 1288
         foreach ($j as $k => $v) {
1293
-            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
1289
+            $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON ';
1294 1290
             $tmp = [];
1295 1291
             foreach ($v->keymap as $kk => $vv) {
1296 1292
                 $tmp[] = $kk.' = '.$vv;
1297 1293
             }
1298
-            $sql .= implode(' AND ', $tmp) . ' ';
1294
+            $sql .= implode(' AND ', $tmp).' ';
1299 1295
         }
1300 1296
         if (count($w)) {
1301 1297
             $sql .= 'WHERE ';
1302 1298
             $tmp = [];
1303 1299
             foreach ($w as $v) {
1304
-                $tmp[] = '(' . $v[0] . ')';
1300
+                $tmp[] = '('.$v[0].')';
1305 1301
                 $par = array_merge($par, $v[1]);
1306 1302
             }
1307 1303
             $sql .= implode(' AND ', $tmp).' ';
1308 1304
         }
1309 1305
         if (!$own) {
1310
-            $sql .= 'GROUP BY ' . implode(', ', $key) . ' ';
1306
+            $sql .= 'GROUP BY '.implode(', ', $key).' ';
1311 1307
         }
1312 1308
         if (count($h)) {
1313 1309
             $sql .= 'HAVING ';
1314 1310
             $tmp = [];
1315 1311
             foreach ($h as $v) {
1316
-                $tmp[] = '(' . $v[0] . ')';
1312
+                $tmp[] = '('.$v[0].')';
1317 1313
                 $par = array_merge($par, $v[1]);
1318 1314
             }
1319 1315
             $sql .= implode(' AND ', $tmp).' ';
@@ -1321,38 +1317,38 @@  discard block
 block discarded – undo
1321 1317
         if (count($o)) {
1322 1318
             $sql .= 'ORDER BY ';
1323 1319
             if ($own) {
1324
-                $sql .= $o[2] . ' ' . $dir;
1320
+                $sql .= $o[2].' '.$dir;
1325 1321
             } else {
1326
-                $sql .= 'MAX('.$o[2].') ' . $dir;
1322
+                $sql .= 'MAX('.$o[2].') '.$dir;
1327 1323
             }
1328 1324
         }
1329 1325
         $porder = [];
1330 1326
         $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC';
1331 1327
         foreach ($this->definition->getPrimaryKey() as $field) {
1332
-            $porder[] = $this->getColumn($field)['name'] . ' ' . $pdir;
1328
+            $porder[] = $this->getColumn($field)['name'].' '.$pdir;
1333 1329
         }
1334 1330
         if (count($porder)) {
1335
-            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
1331
+            $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' ';
1336 1332
         }
1337 1333
 
1338 1334
         if ($this->li_of[0]) {
1339 1335
             if ($this->db->driverName() === 'oracle') {
1340
-                if ((int)$this->db->driverOption('version', 0) >= 12) {
1341
-                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
1336
+                if ((int) $this->db->driverOption('version', 0) >= 12) {
1337
+                    $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY';
1342 1338
                 } else {
1343
-                    $sql = "SELECT " . implode(', ', $dst) . " 
1339
+                    $sql = "SELECT ".implode(', ', $dst)." 
1344 1340
                             FROM (
1345 1341
                                 SELECT tbl__.*, rownum rnum__ FROM (
1346
-                                    " . $sql . "
1342
+                                    " . $sql."
1347 1343
                                 ) tbl__ 
1348
-                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
1344
+                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])."
1349 1345
                             ) WHERE rnum__ > " . $this->li_of[1];
1350 1346
                 }
1351 1347
             } else {
1352
-                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
1348
+                $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1];
1353 1349
             }
1354 1350
         }
1355
-        return array_map(function ($v) {
1351
+        return array_map(function($v) {
1356 1352
             if (array_key_exists('orderbyfix___', $v)) {
1357 1353
                 unset($v['orderbyfix___']);
1358 1354
             }
Please login to merge, or discard this patch.