Completed
Push — 6.0 ( 638630...1aff42 )
by liu
04:11
created
src/think/db/Builder.php 1 patch
Spacing   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
 // +----------------------------------------------------------------------
9 9
 // | Author: liu21st <[email protected]>
10 10
 // +----------------------------------------------------------------------
11
-declare (strict_types = 1);
11
+declare(strict_types=1);
12 12
 
13 13
 namespace think\db;
14 14
 
@@ -158,20 +158,20 @@  discard block
 block discarded – undo
158 158
             if (false !== strpos($key, '->')) {
159 159
                 list($key, $name) = explode('->', $key, 2);
160 160
                 $item             = $this->parseKey($query, $key);
161
-                $result[$item]    = 'json_set(' . $item . ', \'$.' . $name . '\', ' . $this->parseDataBind($query, $key . '->' . $name, $val, $bind) . ')';
161
+                $result[$item]    = 'json_set('.$item.', \'$.'.$name.'\', '.$this->parseDataBind($query, $key.'->'.$name, $val, $bind).')';
162 162
             } elseif (false === strpos($key, '.') && !in_array($key, $fields, true)) {
163 163
                 if ($options['strict']) {
164
-                    throw new Exception('fields not exists:[' . $key . ']');
164
+                    throw new Exception('fields not exists:['.$key.']');
165 165
                 }
166 166
             } elseif (is_null($val)) {
167 167
                 $result[$item] = 'NULL';
168 168
             } elseif (is_array($val) && !empty($val)) {
169 169
                 switch (strtoupper($val[0])) {
170 170
                     case 'INC':
171
-                        $result[$item] = $item . ' + ' . floatval($val[1]);
171
+                        $result[$item] = $item.' + '.floatval($val[1]);
172 172
                         break;
173 173
                     case 'DEC':
174
-                        $result[$item] = $item . ' - ' . floatval($val[1]);
174
+                        $result[$item] = $item.' - '.floatval($val[1]);
175 175
                         break;
176 176
                 }
177 177
             } elseif (is_scalar($val)) {
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
 
201 201
         $name = $query->bindValue($data, $bind[$key] ?? PDO::PARAM_STR);
202 202
 
203
-        return ':' . $name;
203
+        return ':'.$name;
204 204
     }
205 205
 
206 206
     /**
@@ -225,7 +225,7 @@  discard block
 block discarded – undo
225 225
      */
226 226
     protected function parseExtra(Query $query, string $extra): string
227 227
     {
228
-        return preg_match('/^[\w]+$/i', $extra) ? ' ' . strtoupper($extra) : '';
228
+        return preg_match('/^[\w]+$/i', $extra) ? ' '.strtoupper($extra) : '';
229 229
     }
230 230
 
231 231
     /**
@@ -245,7 +245,7 @@  discard block
 block discarded – undo
245 245
                 if ($field instanceof Raw) {
246 246
                     $array[] = $field->getValue();
247 247
                 } elseif (!is_numeric($key)) {
248
-                    $array[] = $this->parseKey($query, $key) . ' AS ' . $this->parseKey($query, $field, true);
248
+                    $array[] = $this->parseKey($query, $key).' AS '.$this->parseKey($query, $field, true);
249 249
                 } else {
250 250
                     $array[] = $this->parseKey($query, $field);
251 251
                 }
@@ -275,9 +275,9 @@  discard block
 block discarded – undo
275 275
             if ($table instanceof Raw) {
276 276
                 $item[] = $table->getValue();
277 277
             } elseif (!is_numeric($key)) {
278
-                $item[] = $this->parseKey($query, $key) . ' ' . $this->parseKey($query, $table);
278
+                $item[] = $this->parseKey($query, $key).' '.$this->parseKey($query, $table);
279 279
             } elseif (isset($options['alias'][$table])) {
280
-                $item[] = $this->parseKey($query, $table) . ' ' . $this->parseKey($query, $options['alias'][$table]);
280
+                $item[] = $this->parseKey($query, $table).' '.$this->parseKey($query, $options['alias'][$table]);
281 281
             } else {
282 282
                 $item[] = $this->parseKey($query, $table);
283 283
             }
@@ -303,11 +303,11 @@  discard block
 block discarded – undo
303 303
             list($field, $condition) = $options['soft_delete'];
304 304
 
305 305
             $binds    = $query->getFieldsBindType();
306
-            $whereStr = $whereStr ? '( ' . $whereStr . ' ) AND ' : '';
307
-            $whereStr = $whereStr . $this->parseWhereItem($query, $field, $condition, $binds);
306
+            $whereStr = $whereStr ? '( '.$whereStr.' ) AND ' : '';
307
+            $whereStr = $whereStr.$this->parseWhereItem($query, $field, $condition, $binds);
308 308
         }
309 309
 
310
-        return empty($whereStr) ? '' : ' WHERE ' . $whereStr;
310
+        return empty($whereStr) ? '' : ' WHERE '.$whereStr;
311 311
     }
312 312
 
313 313
     /**
@@ -350,20 +350,20 @@  discard block
 block discarded – undo
350 350
         $where = [];
351 351
         foreach ($val as $value) {
352 352
             if ($value instanceof Raw) {
353
-                $where[] = ' ' . $logic . ' ( ' . $value->getValue() . ' )';
353
+                $where[] = ' '.$logic.' ( '.$value->getValue().' )';
354 354
                 continue;
355 355
             }
356 356
 
357 357
             if (is_array($value)) {
358 358
                 if (key($value) !== 0) {
359
-                    throw new Exception('where express error:' . var_export($value, true));
359
+                    throw new Exception('where express error:'.var_export($value, true));
360 360
                 }
361 361
                 $field = array_shift($value);
362 362
             } elseif (true === $value) {
363
-                $where[] = ' ' . $logic . ' 1 ';
363
+                $where[] = ' '.$logic.' 1 ';
364 364
                 continue;
365 365
             } elseif (!($value instanceof Closure)) {
366
-                throw new Exception('where express error:' . var_export($value, true));
366
+                throw new Exception('where express error:'.var_export($value, true));
367 367
             }
368 368
 
369 369
             if ($value instanceof Closure) {
@@ -372,7 +372,7 @@  discard block
 block discarded – undo
372 372
             } elseif (is_array($field)) {
373 373
                 $where[] = $this->parseMultiWhereField($query, $value, $field, $logic, $binds);
374 374
             } elseif ($field instanceof Raw) {
375
-                $where[] = ' ' . $logic . ' ' . $this->parseWhereItem($query, $field, $value, $binds);
375
+                $where[] = ' '.$logic.' '.$this->parseWhereItem($query, $field, $value, $binds);
376 376
             } elseif (strpos($field, '|')) {
377 377
                 $where[] = $this->parseFieldsOr($query, $value, $field, $logic, $binds);
378 378
             } elseif (strpos($field, '&')) {
@@ -380,7 +380,7 @@  discard block
 block discarded – undo
380 380
             } else {
381 381
                 // 对字段使用表达式查询
382 382
                 $field   = is_string($field) ? $field : '';
383
-                $where[] = ' ' . $logic . ' ' . $this->parseWhereItem($query, $field, $value, $binds);
383
+                $where[] = ' '.$logic.' '.$this->parseWhereItem($query, $field, $value, $binds);
384 384
             }
385 385
         }
386 386
 
@@ -405,7 +405,7 @@  discard block
 block discarded – undo
405 405
             $item[] = $this->parseWhereItem($query, $k, $value, $binds);
406 406
         }
407 407
 
408
-        return ' ' . $logic . ' ( ' . implode(' AND ', $item) . ' )';
408
+        return ' '.$logic.' ( '.implode(' AND ', $item).' )';
409 409
     }
410 410
 
411 411
     /**
@@ -426,7 +426,7 @@  discard block
 block discarded – undo
426 426
             $item[] = $this->parseWhereItem($query, $k, $value, $binds);
427 427
         }
428 428
 
429
-        return ' ' . $logic . ' ( ' . implode(' OR ', $item) . ' )';
429
+        return ' '.$logic.' ( '.implode(' OR ', $item).' )';
430 430
     }
431 431
 
432 432
     /**
@@ -444,7 +444,7 @@  discard block
 block discarded – undo
444 444
         $whereClause = $this->buildWhere($query, $newQuery->getOptions('where') ?: []);
445 445
 
446 446
         if (!empty($whereClause)) {
447
-            $where = ' ' . $logic . ' ( ' . $whereClause . ' )';
447
+            $where = ' '.$logic.' ( '.$whereClause.' )';
448 448
         }
449 449
 
450 450
         return $where ?? '';
@@ -469,7 +469,7 @@  discard block
 block discarded – undo
469 469
             $where[] = $this->parseWhereItem($query, array_shift($item), $item, $binds);
470 470
         }
471 471
 
472
-        return ' ' . $logic . ' ( ' . implode(' AND ', $where) . ' )';
472
+        return ' '.$logic.' ( '.implode(' AND ', $where).' )';
473 473
     }
474 474
 
475 475
     /**
@@ -490,7 +490,7 @@  discard block
 block discarded – undo
490 490
 
491 491
         // 检测操作符
492 492
         if (!is_string($exp)) {
493
-            throw new Exception('where express error:' . var_export($exp, true));
493
+            throw new Exception('where express error:'.var_export($exp, true));
494 494
         }
495 495
 
496 496
         $exp = strtoupper($exp);
@@ -515,7 +515,7 @@  discard block
 block discarded – undo
515 515
             if (is_string($value) && 0 === strpos($value, ':') && $query->isBind(substr($value, 1))) {
516 516
             } else {
517 517
                 $name  = $query->bindValue($value, $bindType);
518
-                $value = ':' . $name;
518
+                $value = ':'.$name;
519 519
             }
520 520
         }
521 521
 
@@ -526,7 +526,7 @@  discard block
 block discarded – undo
526 526
             }
527 527
         }
528 528
 
529
-        throw new Exception('where express error:' . $exp);
529
+        throw new Exception('where express error:'.$exp);
530 530
     }
531 531
 
532 532
     /**
@@ -548,12 +548,12 @@  discard block
 block discarded – undo
548 548
             $array = [];
549 549
             foreach ($value as $item) {
550 550
                 $name    = $query->bindValue($item, PDO::PARAM_STR);
551
-                $array[] = $key . ' ' . $exp . ' :' . $name;
551
+                $array[] = $key.' '.$exp.' :'.$name;
552 552
             }
553 553
 
554
-            $whereStr = '(' . implode(' ' . strtoupper($logic) . ' ', $array) . ')';
554
+            $whereStr = '('.implode(' '.strtoupper($logic).' ', $array).')';
555 555
         } else {
556
-            $whereStr = $key . ' ' . $exp . ' ' . $value;
556
+            $whereStr = $key.' '.$exp.' '.$value;
557 557
         }
558 558
 
559 559
         return $whereStr;
@@ -573,7 +573,7 @@  discard block
 block discarded – undo
573 573
     protected function parseExp(Query $query, string $key, string $exp, Raw $value, string $field, int $bindType): string
574 574
     {
575 575
         // 表达式查询
576
-        return '( ' . $key . ' ' . $value->getValue() . ' )';
576
+        return '( '.$key.' '.$value->getValue().' )';
577 577
     }
578 578
 
579 579
     /**
@@ -593,10 +593,10 @@  discard block
 block discarded – undo
593 593
         list($op, $field) = $value;
594 594
 
595 595
         if (!in_array(trim($op), ['=', '<>', '>', '>=', '<', '<='])) {
596
-            throw new Exception('where express error:' . var_export($value, true));
596
+            throw new Exception('where express error:'.var_export($value, true));
597 597
         }
598 598
 
599
-        return '( ' . $key . ' ' . $op . ' ' . $this->parseKey($query, $field, true) . ' )';
599
+        return '( '.$key.' '.$op.' '.$this->parseKey($query, $field, true).' )';
600 600
     }
601 601
 
602 602
     /**
@@ -613,7 +613,7 @@  discard block
 block discarded – undo
613 613
     protected function parseNull(Query $query, string $key, string $exp, $value, $field, int $bindType): string
614 614
     {
615 615
         // NULL 查询
616
-        return $key . ' IS ' . $exp;
616
+        return $key.' IS '.$exp;
617 617
     }
618 618
 
619 619
     /**
@@ -635,7 +635,7 @@  discard block
 block discarded – undo
635 635
         $min = $query->bindValue($data[0], $bindType);
636 636
         $max = $query->bindValue($data[1], $bindType);
637 637
 
638
-        return $key . ' ' . $exp . ' :' . $min . ' AND :' . $max . ' ';
638
+        return $key.' '.$exp.' :'.$min.' AND :'.$max.' ';
639 639
     }
640 640
 
641 641
     /**
@@ -657,10 +657,10 @@  discard block
 block discarded – undo
657 657
         } elseif ($value instanceof Raw) {
658 658
             $value = $value->getValue();
659 659
         } else {
660
-            throw new Exception('where express error:' . $value);
660
+            throw new Exception('where express error:'.$value);
661 661
         }
662 662
 
663
-        return $exp . ' ( ' . $value . ' )';
663
+        return $exp.' ( '.$value.' )';
664 664
     }
665 665
 
666 666
     /**
@@ -676,7 +676,7 @@  discard block
 block discarded – undo
676 676
      */
677 677
     protected function parseTime(Query $query, string $key, string $exp, $value, $field, int $bindType): string
678 678
     {
679
-        return $key . ' ' . substr($exp, 0, 2) . ' ' . $this->parseDateTime($query, $value, $field, $bindType);
679
+        return $key.' '.substr($exp, 0, 2).' '.$this->parseDateTime($query, $value, $field, $bindType);
680 680
     }
681 681
 
682 682
     /**
@@ -693,7 +693,7 @@  discard block
 block discarded – undo
693 693
     protected function parseCompare(Query $query, string $key, string $exp, $value, $field, int $bindType): string
694 694
     {
695 695
         if (is_array($value)) {
696
-            throw new Exception('where express error:' . $exp . var_export($value, true));
696
+            throw new Exception('where express error:'.$exp.var_export($value, true));
697 697
         }
698 698
 
699 699
         // 比较运算
@@ -702,10 +702,10 @@  discard block
 block discarded – undo
702 702
         }
703 703
 
704 704
         if ('=' == $exp && is_null($value)) {
705
-            return $key . ' IS NULL';
705
+            return $key.' IS NULL';
706 706
         }
707 707
 
708
-        return $key . ' ' . $exp . ' ' . $value;
708
+        return $key.' '.$exp.' '.$value;
709 709
     }
710 710
 
711 711
     /**
@@ -725,7 +725,7 @@  discard block
 block discarded – undo
725 725
             $value = explode(',', $value);
726 726
         }
727 727
 
728
-        return $key . ' ' . substr($exp, 0, -4)
728
+        return $key.' '.substr($exp, 0, -4)
729 729
         . $this->parseDateTime($query, $value[0], $field, $bindType)
730 730
         . ' AND '
731 731
         . $this->parseDateTime($query, $value[1], $field, $bindType);
@@ -756,18 +756,18 @@  discard block
 block discarded – undo
756 756
 
757 757
             foreach ($value as $v) {
758 758
                 $name    = $query->bindValue($v, $bindType);
759
-                $array[] = ':' . $name;
759
+                $array[] = ':'.$name;
760 760
             }
761 761
 
762 762
             if (count($array) == 1) {
763
-                return $key . ('IN' == $exp ? ' = ' : ' <> ') . $array[0];
763
+                return $key.('IN' == $exp ? ' = ' : ' <> ').$array[0];
764 764
             } else {
765 765
                 $zone  = implode(',', $array);
766 766
                 $value = empty($zone) ? "''" : $zone;
767 767
             }
768 768
         }
769 769
 
770
-        return $key . ' ' . $exp . ' (' . $value . ')';
770
+        return $key.' '.$exp.' ('.$value.')';
771 771
     }
772 772
 
773 773
     /**
@@ -830,7 +830,7 @@  discard block
 block discarded – undo
830 830
 
831 831
         $name = $query->bindValue($value, $bindType);
832 832
 
833
-        return ':' . $name;
833
+        return ':'.$name;
834 834
     }
835 835
 
836 836
     /**
@@ -842,7 +842,7 @@  discard block
 block discarded – undo
842 842
      */
843 843
     protected function parseLimit(Query $query, string $limit): string
844 844
     {
845
-        return (!empty($limit) && false === strpos($limit, '(')) ? ' LIMIT ' . $limit . ' ' : '';
845
+        return (!empty($limit) && false === strpos($limit, '(')) ? ' LIMIT '.$limit.' ' : '';
846 846
     }
847 847
 
848 848
     /**
@@ -862,14 +862,14 @@  discard block
 block discarded – undo
862 862
             if (strpos($on, '=')) {
863 863
                 list($val1, $val2) = explode('=', $on, 2);
864 864
 
865
-                $condition = $this->parseKey($query, $val1) . '=' . $this->parseKey($query, $val2);
865
+                $condition = $this->parseKey($query, $val1).'='.$this->parseKey($query, $val2);
866 866
             } else {
867 867
                 $condition = $on;
868 868
             }
869 869
 
870 870
             $table = $this->parseTable($query, $table);
871 871
 
872
-            $joinStr .= ' ' . $type . ' JOIN ' . $table . ' ON ' . $condition;
872
+            $joinStr .= ' '.$type.' JOIN '.$table.' ON '.$condition;
873 873
         }
874 874
 
875 875
         return $joinStr;
@@ -894,22 +894,22 @@  discard block
 block discarded – undo
894 894
                 $array[] = $this->parseRand($query);
895 895
             } elseif (is_string($val)) {
896 896
                 if (is_numeric($key)) {
897
-                    list($key, $sort) = explode(' ', strpos($val, ' ') ? $val : $val . ' ');
897
+                    list($key, $sort) = explode(' ', strpos($val, ' ') ? $val : $val.' ');
898 898
                 } else {
899 899
                     $sort = $val;
900 900
                 }
901 901
 
902 902
                 if (preg_match('/^[\w\.]+$/', $key)) {
903 903
                     $sort    = strtoupper($sort);
904
-                    $sort    = in_array($sort, ['ASC', 'DESC'], true) ? ' ' . $sort : '';
905
-                    $array[] = $this->parseKey($query, $key, true) . $sort;
904
+                    $sort    = in_array($sort, ['ASC', 'DESC'], true) ? ' '.$sort : '';
905
+                    $array[] = $this->parseKey($query, $key, true).$sort;
906 906
                 } else {
907
-                    throw new Exception('order express error:' . $key);
907
+                    throw new Exception('order express error:'.$key);
908 908
                 }
909 909
             }
910 910
         }
911 911
 
912
-        return empty($array) ? '' : ' ORDER BY ' . implode(',', $array);
912
+        return empty($array) ? '' : ' ORDER BY '.implode(',', $array);
913 913
     }
914 914
 
915 915
     /**
@@ -941,14 +941,14 @@  discard block
 block discarded – undo
941 941
         }
942 942
 
943 943
         $sort = strtoupper($sort);
944
-        $sort = in_array($sort, ['ASC', 'DESC'], true) ? ' ' . $sort : '';
944
+        $sort = in_array($sort, ['ASC', 'DESC'], true) ? ' '.$sort : '';
945 945
         $bind = $query->getFieldsBindType();
946 946
 
947 947
         foreach ($val as $item) {
948 948
             $val[] = $this->parseDataBind($query, $key, $item, $bind);
949 949
         }
950 950
 
951
-        return 'field(' . $this->parseKey($query, $key, true) . ',' . implode(',', $val) . ')' . $sort;
951
+        return 'field('.$this->parseKey($query, $key, true).','.implode(',', $val).')'.$sort;
952 952
     }
953 953
 
954 954
     /**
@@ -973,7 +973,7 @@  discard block
 block discarded – undo
973 973
             $val[] = $this->parseKey($query, $key);
974 974
         }
975 975
 
976
-        return ' GROUP BY ' . implode(',', $val);
976
+        return ' GROUP BY '.implode(',', $val);
977 977
     }
978 978
 
979 979
     /**
@@ -985,7 +985,7 @@  discard block
 block discarded – undo
985 985
      */
986 986
     protected function parseHaving(Query $query, string $having): string
987 987
     {
988
-        return !empty($having) ? ' HAVING ' . $having : '';
988
+        return !empty($having) ? ' HAVING '.$having : '';
989 989
     }
990 990
 
991 991
     /**
@@ -1001,7 +1001,7 @@  discard block
 block discarded – undo
1001 1001
             $comment = strstr($comment, '*/', true);
1002 1002
         }
1003 1003
 
1004
-        return !empty($comment) ? ' /* ' . $comment . ' */' : '';
1004
+        return !empty($comment) ? ' /* '.$comment.' */' : '';
1005 1005
     }
1006 1006
 
1007 1007
     /**
@@ -1034,13 +1034,13 @@  discard block
 block discarded – undo
1034 1034
 
1035 1035
         foreach ($union as $u) {
1036 1036
             if ($u instanceof Closure) {
1037
-                $sql[] = $type . ' ' . $this->parseClosure($query, $u);
1037
+                $sql[] = $type.' '.$this->parseClosure($query, $u);
1038 1038
             } elseif (is_string($u)) {
1039
-                $sql[] = $type . ' ( ' . $u . ' )';
1039
+                $sql[] = $type.' ( '.$u.' )';
1040 1040
             }
1041 1041
         }
1042 1042
 
1043
-        return ' ' . implode(' ', $sql);
1043
+        return ' '.implode(' ', $sql);
1044 1044
     }
1045 1045
 
1046 1046
     /**
@@ -1077,7 +1077,7 @@  discard block
 block discarded – undo
1077 1077
         }
1078 1078
 
1079 1079
         if (is_string($lock) && !empty($lock)) {
1080
-            return ' ' . trim($lock) . ' ';
1080
+            return ' '.trim($lock).' ';
1081 1081
         } else {
1082 1082
             return '';
1083 1083
         }
@@ -1174,7 +1174,7 @@  discard block
 block discarded – undo
1174 1174
         foreach ($dataSet as $k => $data) {
1175 1175
             $data = $this->parseData($query, $data, $allowFields, $bind);
1176 1176
 
1177
-            $values[] = 'SELECT ' . implode(',', array_values($data));
1177
+            $values[] = 'SELECT '.implode(',', array_values($data));
1178 1178
 
1179 1179
             if (!isset($insertFields)) {
1180 1180
                 $insertFields = array_keys($data);
@@ -1212,7 +1212,7 @@  discard block
 block discarded – undo
1212 1212
             $field = $this->parseKey($query, $field, true);
1213 1213
         }
1214 1214
 
1215
-        return 'INSERT INTO ' . $this->parseTable($query, $table) . ' (' . implode(',', $fields) . ') ' . $this->select($query);
1215
+        return 'INSERT INTO '.$this->parseTable($query, $table).' ('.implode(',', $fields).') '.$this->select($query);
1216 1216
     }
1217 1217
 
1218 1218
     /**
@@ -1233,7 +1233,7 @@  discard block
 block discarded – undo
1233 1233
 
1234 1234
         $set = [];
1235 1235
         foreach ($data as $key => $val) {
1236
-            $set[] = $key . ' = ' . $val;
1236
+            $set[] = $key.' = '.$val;
1237 1237
         }
1238 1238
 
1239 1239
         return str_replace(
@@ -1267,7 +1267,7 @@  discard block
 block discarded – undo
1267 1267
             [
1268 1268
                 $this->parseTable($query, $options['table']),
1269 1269
                 $this->parseExtra($query, $options['extra']),
1270
-                !empty($options['using']) ? ' USING ' . $this->parseTable($query, $options['using']) . ' ' : '',
1270
+                !empty($options['using']) ? ' USING '.$this->parseTable($query, $options['using']).' ' : '',
1271 1271
                 $this->parseJoin($query, $options['join']),
1272 1272
                 $this->parseWhere($query, $options['where']),
1273 1273
                 $this->parseOrder($query, $options['order']),
Please login to merge, or discard this patch.