@@ -9,6 +9,12 @@ discard block |
||
| 9 | 9 | use PhpBoot\DB\Context; |
| 10 | 10 | |
| 11 | 11 | class ExecResult{ |
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * @param boolean $success |
|
| 15 | + * @param \PDO $pdo |
|
| 16 | + * @param \PDOStatement $st |
|
| 17 | + */ |
|
| 12 | 18 | public function __construct($success, $pdo, $st){ |
| 13 | 19 | $this->pdo = $pdo; |
| 14 | 20 | $this->st = $st; |
@@ -42,6 +48,10 @@ discard block |
||
| 42 | 48 | |
| 43 | 49 | class SelectImpl |
| 44 | 50 | { |
| 51 | + /** |
|
| 52 | + * @param Context $context |
|
| 53 | + * @param string $columns |
|
| 54 | + */ |
|
| 45 | 55 | static public function select($context, $columns){ |
| 46 | 56 | $context->appendSql("SELECT $columns"); |
| 47 | 57 | } |
@@ -49,6 +59,10 @@ discard block |
||
| 49 | 59 | |
| 50 | 60 | class FromImpl |
| 51 | 61 | { |
| 62 | + /** |
|
| 63 | + * @param Context $context |
|
| 64 | + * @param string $tables |
|
| 65 | + */ |
|
| 52 | 66 | static public function from($context, $tables,$as=null){ |
| 53 | 67 | if($tables instanceof BasicRule){ |
| 54 | 68 | $context->appendSql("FROM (".$tables->context->sql.')'); |
@@ -64,6 +78,10 @@ discard block |
||
| 64 | 78 | |
| 65 | 79 | class DeleteImpl |
| 66 | 80 | { |
| 81 | + /** |
|
| 82 | + * @param Context $context |
|
| 83 | + * @param string $from |
|
| 84 | + */ |
|
| 67 | 85 | static public function deleteFrom($context, $from) |
| 68 | 86 | { |
| 69 | 87 | $context->appendSql("DELETE FROM ".DB::wrap($from)); |
@@ -72,6 +90,11 @@ discard block |
||
| 72 | 90 | |
| 73 | 91 | class JoinImpl |
| 74 | 92 | { |
| 93 | + /** |
|
| 94 | + * @param Context $context |
|
| 95 | + * @param null|string $type |
|
| 96 | + * @param string $table |
|
| 97 | + */ |
|
| 75 | 98 | static public function join($context, $type, $table) { |
| 76 | 99 | $table = DB::wrap($table); |
| 77 | 100 | if($type){ |
@@ -84,6 +107,10 @@ discard block |
||
| 84 | 107 | |
| 85 | 108 | class JoinOnImpl |
| 86 | 109 | { |
| 110 | + /** |
|
| 111 | + * @param Context $context |
|
| 112 | + * @param string $condition |
|
| 113 | + */ |
|
| 87 | 114 | static public function on($context, $condition) { |
| 88 | 115 | $context->appendSql("ON $condition"); |
| 89 | 116 | } |
@@ -91,6 +118,9 @@ discard block |
||
| 91 | 118 | |
| 92 | 119 | class ForUpdateImpl |
| 93 | 120 | { |
| 121 | + /** |
|
| 122 | + * @param Context $context |
|
| 123 | + */ |
|
| 94 | 124 | static public function forUpdate($context){ |
| 95 | 125 | $context->appendSql("FOR UPDATE"); |
| 96 | 126 | } |
@@ -98,6 +128,10 @@ discard block |
||
| 98 | 128 | |
| 99 | 129 | class ForUpdateOfImpl |
| 100 | 130 | { |
| 131 | + /** |
|
| 132 | + * @param Context $context |
|
| 133 | + * @param string $column |
|
| 134 | + */ |
|
| 101 | 135 | static public function of($context, $column){ |
| 102 | 136 | $column = DB::wrap($column); |
| 103 | 137 | $context->appendSql("OF $column"); |
@@ -106,6 +140,10 @@ discard block |
||
| 106 | 140 | |
| 107 | 141 | class InsertImpl |
| 108 | 142 | { |
| 143 | + /** |
|
| 144 | + * @param Context $context |
|
| 145 | + * @param string $table |
|
| 146 | + */ |
|
| 109 | 147 | static public function insertInto($context, $table) { |
| 110 | 148 | $table = DB::wrap($table); |
| 111 | 149 | $context->appendSql("INSERT INTO $table"); |
@@ -113,6 +151,10 @@ discard block |
||
| 113 | 151 | } |
| 114 | 152 | class ReplaceImpl |
| 115 | 153 | { |
| 154 | + /** |
|
| 155 | + * @param Context $context |
|
| 156 | + * @param string $table |
|
| 157 | + */ |
|
| 116 | 158 | static public function replaceInto($context, $table) { |
| 117 | 159 | $table = DB::wrap($table); |
| 118 | 160 | $context->appendSql("REPLACE INTO $table"); |
@@ -196,6 +238,10 @@ discard block |
||
| 196 | 238 | |
| 197 | 239 | class UpdateImpl |
| 198 | 240 | { |
| 241 | + /** |
|
| 242 | + * @param Context $context |
|
| 243 | + * @param string $table |
|
| 244 | + */ |
|
| 199 | 245 | static public function update($context, $table){ |
| 200 | 246 | $table = DB::wrap($table); |
| 201 | 247 | $context->appendSql("UPDATE $table"); |
@@ -212,6 +258,9 @@ discard block |
||
| 212 | 258 | } |
| 213 | 259 | } |
| 214 | 260 | |
| 261 | + /** |
|
| 262 | + * @param string $expr |
|
| 263 | + */ |
|
| 215 | 264 | public function setExpr(Context $context, $expr, $args){ |
| 216 | 265 | if($this->first){ |
| 217 | 266 | $this->first = false; |
@@ -274,6 +323,11 @@ discard block |
||
| 274 | 323 | } |
| 275 | 324 | return $this; |
| 276 | 325 | } |
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * @param string $column |
|
| 329 | + * @param string $order |
|
| 330 | + */ |
|
| 277 | 331 | public function orderBy(Context $context, $column, $order=null){ |
| 278 | 332 | if(is_string($column)){ |
| 279 | 333 | if($order === null){ |
@@ -291,12 +345,20 @@ discard block |
||
| 291 | 345 | |
| 292 | 346 | class LimitImpl |
| 293 | 347 | { |
| 348 | + /** |
|
| 349 | + * @param integer $size |
|
| 350 | + */ |
|
| 294 | 351 | static public function limit(Context $context, $size){ |
| 295 | 352 | $intSize = intval($size); |
| 296 | 353 | strval($intSize) == $size or \PhpBoot\abort( |
| 297 | 354 | new \InvalidArgumentException("invalid params for limit($size)")); |
| 298 | 355 | $context->appendSql("LIMIT $size"); |
| 299 | 356 | } |
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * @param integer $start |
|
| 360 | + * @param integer $size |
|
| 361 | + */ |
|
| 300 | 362 | static public function limitWithOffset(Context $context, $start, $size){ |
| 301 | 363 | $intStart = intval($start); |
| 302 | 364 | $intSize = intval($size); |
@@ -308,6 +370,9 @@ discard block |
||
| 308 | 370 | |
| 309 | 371 | class WhereImpl{ |
| 310 | 372 | |
| 373 | + /** |
|
| 374 | + * @param string $str |
|
| 375 | + */ |
|
| 311 | 376 | static private function findQ($str,$offset = 0,$no=0){ |
| 312 | 377 | $found = strpos($str, '?', $offset); |
| 313 | 378 | if($no == 0 || $found === false){ |
@@ -316,6 +381,9 @@ discard block |
||
| 316 | 381 | return self::findQ($str, $found+1, $no-1); |
| 317 | 382 | } |
| 318 | 383 | |
| 384 | + /** |
|
| 385 | + * @param string $prefix |
|
| 386 | + */ |
|
| 319 | 387 | static public function where(Context $context, $prefix, $expr, $args){ |
| 320 | 388 | if(empty($expr)){ |
| 321 | 389 | return; |
@@ -434,6 +502,10 @@ discard block |
||
| 434 | 502 | |
| 435 | 503 | self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
| 436 | 504 | } |
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * @param string $expr |
|
| 508 | + */ |
|
| 437 | 509 | static public function condition(Context $context, $prefix, $expr, $args){ |
| 438 | 510 | if(!empty($expr)){ |
| 439 | 511 | $expr = "($expr)"; |
@@ -506,6 +578,10 @@ discard block |
||
| 506 | 578 | } |
| 507 | 579 | |
| 508 | 580 | class GroupByImpl{ |
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * @param string $column |
|
| 584 | + */ |
|
| 509 | 585 | static public function groupBy(Context $context, $column){ |
| 510 | 586 | $column = DB::wrap($column); |
| 511 | 587 | $context->appendSql("GROUP BY $column"); |
@@ -534,7 +610,6 @@ discard block |
||
| 534 | 610 | /** |
| 535 | 611 | * |
| 536 | 612 | * @param Context $context |
| 537 | - * @param string|false $asDict return as dict or array |
|
| 538 | 613 | * @return false|array |
| 539 | 614 | * @throws DBException|\Exception |
| 540 | 615 | */ |
@@ -605,6 +680,9 @@ discard block |
||
| 605 | 680 | } |
| 606 | 681 | class OnDuplicateKeyUpdateImpl |
| 607 | 682 | { |
| 683 | + /** |
|
| 684 | + * @param Context $context |
|
| 685 | + */ |
|
| 608 | 686 | public function set($context, $column, $value){ |
| 609 | 687 | if(is_string($column)){ |
| 610 | 688 | $this->setExpr($context, $column, $value); |
@@ -613,6 +691,9 @@ discard block |
||
| 613 | 691 | } |
| 614 | 692 | } |
| 615 | 693 | |
| 694 | + /** |
|
| 695 | + * @param string $expr |
|
| 696 | + */ |
|
| 616 | 697 | public function setExpr($context, $expr, $args){ |
| 617 | 698 | $prefix = ''; |
| 618 | 699 | if($this->first){ |
@@ -8,14 +8,14 @@ discard block |
||
| 8 | 8 | use PhpBoot\DB\rules\basic\BasicRule; |
| 9 | 9 | use PhpBoot\DB\Context; |
| 10 | 10 | |
| 11 | -class ExecResult{ |
|
| 12 | - public function __construct($success, $pdo, $st){ |
|
| 11 | +class ExecResult { |
|
| 12 | + public function __construct($success, $pdo, $st) { |
|
| 13 | 13 | $this->pdo = $pdo; |
| 14 | 14 | $this->st = $st; |
| 15 | 15 | $this->success = $success; |
| 16 | 16 | $this->rows = $this->st->rowCount(); |
| 17 | 17 | } |
| 18 | - public function lastInsertId($name=null){ |
|
| 18 | + public function lastInsertId($name = null) { |
|
| 19 | 19 | return $this->pdo->lastInsertId($name); |
| 20 | 20 | } |
| 21 | 21 | /** |
@@ -42,21 +42,21 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | class SelectImpl |
| 44 | 44 | { |
| 45 | - static public function select($context, $columns){ |
|
| 45 | + static public function select($context, $columns) { |
|
| 46 | 46 | $context->appendSql("SELECT $columns"); |
| 47 | 47 | } |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | class FromImpl |
| 51 | 51 | { |
| 52 | - static public function from($context, $tables,$as=null){ |
|
| 53 | - if($tables instanceof BasicRule){ |
|
| 52 | + static public function from($context, $tables, $as = null) { |
|
| 53 | + if ($tables instanceof BasicRule) { |
|
| 54 | 54 | $context->appendSql("FROM (".$tables->context->sql.')'); |
| 55 | - $context->params = array_merge($context->params,$tables->context->params); |
|
| 55 | + $context->params = array_merge($context->params, $tables->context->params); |
|
| 56 | 56 | }else { |
| 57 | 57 | $context->appendSql("FROM ".DB::wrap($tables)); |
| 58 | 58 | } |
| 59 | - if($as){ |
|
| 59 | + if ($as) { |
|
| 60 | 60 | $context->appendSql("AS ".DB::wrap($as)); |
| 61 | 61 | } |
| 62 | 62 | } |
@@ -74,9 +74,9 @@ discard block |
||
| 74 | 74 | { |
| 75 | 75 | static public function join($context, $type, $table) { |
| 76 | 76 | $table = DB::wrap($table); |
| 77 | - if($type){ |
|
| 77 | + if ($type) { |
|
| 78 | 78 | $context->appendSql("$type JOIN $table"); |
| 79 | - }else{ |
|
| 79 | + }else { |
|
| 80 | 80 | $context->appendSql("JOIN $table"); |
| 81 | 81 | } |
| 82 | 82 | } |
@@ -91,14 +91,14 @@ discard block |
||
| 91 | 91 | |
| 92 | 92 | class ForUpdateImpl |
| 93 | 93 | { |
| 94 | - static public function forUpdate($context){ |
|
| 94 | + static public function forUpdate($context) { |
|
| 95 | 95 | $context->appendSql("FOR UPDATE"); |
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | class ForUpdateOfImpl |
| 100 | 100 | { |
| 101 | - static public function of($context, $column){ |
|
| 101 | + static public function of($context, $column) { |
|
| 102 | 102 | $column = DB::wrap($column); |
| 103 | 103 | $context->appendSql("OF $column"); |
| 104 | 104 | } |
@@ -120,57 +120,57 @@ discard block |
||
| 120 | 120 | } |
| 121 | 121 | class ValuesImpl |
| 122 | 122 | { |
| 123 | - static public function values(Context $context, array $values){ |
|
| 123 | + static public function values(Context $context, array $values) { |
|
| 124 | 124 | $params = []; |
| 125 | 125 | $stubs = []; |
| 126 | - foreach ($values as $v){ |
|
| 127 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 128 | - $stubs[]=$v->get(); |
|
| 129 | - }else{ |
|
| 130 | - $stubs[]='?'; |
|
| 126 | + foreach ($values as $v) { |
|
| 127 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 128 | + $stubs[] = $v->get(); |
|
| 129 | + }else { |
|
| 130 | + $stubs[] = '?'; |
|
| 131 | 131 | $params[] = $v; |
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | 134 | $stubs = implode(',', $stubs); |
| 135 | 135 | |
| 136 | - if(array_keys($values) === range(0, count($values) - 1)){ |
|
| 136 | + if (array_keys($values) === range(0, count($values) - 1)) { |
|
| 137 | 137 | //VALUES(val0, val1, val2) |
| 138 | 138 | $context->appendSql("VALUES($stubs)"); |
| 139 | 139 | |
| 140 | - }else{ |
|
| 140 | + }else { |
|
| 141 | 141 | //(col0, col1, col2) VALUES(val0, val1, val2) |
| 142 | - $columns = implode(',', array_map(function($k){return DB::wrap($k);}, array_keys($values))); |
|
| 143 | - $context->appendSql("($columns) VALUES($stubs)",false); |
|
| 142 | + $columns = implode(',', array_map(function($k) {return DB::wrap($k); }, array_keys($values))); |
|
| 143 | + $context->appendSql("($columns) VALUES($stubs)", false); |
|
| 144 | 144 | } |
| 145 | 145 | $context->appendParams($params); |
| 146 | 146 | } |
| 147 | 147 | static public function batchValues(Context $context, array $values) |
| 148 | 148 | { |
| 149 | 149 | $count = count($values); |
| 150 | - if($count == 0){ |
|
| 150 | + if ($count == 0) { |
|
| 151 | 151 | return; |
| 152 | 152 | } |
| 153 | 153 | $keys = array_keys($values[0]); |
| 154 | 154 | $row = implode(',', self::toSql(array_values($values[0]))); |
| 155 | - if($keys === range(0, count($keys) - 1)){ |
|
| 155 | + if ($keys === range(0, count($keys) - 1)) { |
|
| 156 | 156 | //VALUES(val0, val1, val2) |
| 157 | 157 | $context->appendSql("VALUES($row)"); |
| 158 | - }else{ |
|
| 158 | + }else { |
|
| 159 | 159 | //(col0, col1, col2) VALUES(val0, val1, val2) |
| 160 | - $columns = implode(',', array_map(function($k){return DB::wrap($k);}, $keys)); |
|
| 161 | - $context->appendSql("($columns) VALUES($row)",false); |
|
| 160 | + $columns = implode(',', array_map(function($k) {return DB::wrap($k); }, $keys)); |
|
| 161 | + $context->appendSql("($columns) VALUES($row)", false); |
|
| 162 | 162 | } |
| 163 | - for($i=1; $i<$count; $i++){ |
|
| 163 | + for ($i = 1; $i<$count; $i++) { |
|
| 164 | 164 | $value = self::pick($keys, $values[$i]); |
| 165 | 165 | $row = implode(',', self::toSql($value)); |
| 166 | - $context->appendSql(", ($row)",false); |
|
| 166 | + $context->appendSql(", ($row)", false); |
|
| 167 | 167 | } |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | static protected function pick(array $keys, array $values) |
| 171 | 171 | { |
| 172 | 172 | $res = []; |
| 173 | - foreach ($keys as $key){ |
|
| 173 | + foreach ($keys as $key) { |
|
| 174 | 174 | array_key_exists($key, $values) or \PhpBoot\abort("key $key not exist from the given array"); |
| 175 | 175 | $res[$key] = $values[$key]; |
| 176 | 176 | } |
@@ -178,12 +178,12 @@ discard block |
||
| 178 | 178 | } |
| 179 | 179 | static protected function toSql(array $values) |
| 180 | 180 | { |
| 181 | - foreach ($values as &$v){ |
|
| 182 | - if($v instanceof Raw){ |
|
| 181 | + foreach ($values as &$v) { |
|
| 182 | + if ($v instanceof Raw) { |
|
| 183 | 183 | $v = $v->get(); |
| 184 | - }elseif(is_bool($v)){ |
|
| 185 | - $v = $v?'true':'false'; |
|
| 186 | - }elseif(!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])){ |
|
| 184 | + }elseif (is_bool($v)) { |
|
| 185 | + $v = $v ? 'true' : 'false'; |
|
| 186 | + }elseif (!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])) { |
|
| 187 | 187 | $v = (string)$v; |
| 188 | 188 | $v = str_replace("\\", "\\\\", $v); |
| 189 | 189 | $v = str_replace("'", "\\'", $v); |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | |
| 197 | 197 | class UpdateImpl |
| 198 | 198 | { |
| 199 | - static public function update($context, $table){ |
|
| 199 | + static public function update($context, $table) { |
|
| 200 | 200 | $table = DB::wrap($table); |
| 201 | 201 | $context->appendSql("UPDATE $table"); |
| 202 | 202 | } |
@@ -204,81 +204,81 @@ discard block |
||
| 204 | 204 | |
| 205 | 205 | class UpdateSetImpl |
| 206 | 206 | { |
| 207 | - public function set(Context $context, $expr, $args){ |
|
| 208 | - if(is_string($expr)){ |
|
| 207 | + public function set(Context $context, $expr, $args) { |
|
| 208 | + if (is_string($expr)) { |
|
| 209 | 209 | return $this->setExpr($context, $expr, $args); |
| 210 | - }else{ |
|
| 210 | + }else { |
|
| 211 | 211 | return $this->setArgs($context, $expr); |
| 212 | 212 | } |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | - public function setExpr(Context $context, $expr, $args){ |
|
| 216 | - if($this->first){ |
|
| 215 | + public function setExpr(Context $context, $expr, $args) { |
|
| 216 | + if ($this->first) { |
|
| 217 | 217 | $this->first = false; |
| 218 | 218 | $prefix = 'SET '; |
| 219 | - }else{ |
|
| 219 | + }else { |
|
| 220 | 220 | $prefix = ','; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - $context->appendSql("$prefix$expr",$prefix == 'SET '); |
|
| 223 | + $context->appendSql("$prefix$expr", $prefix == 'SET '); |
|
| 224 | 224 | $context->appendParams($args); |
| 225 | 225 | |
| 226 | 226 | } |
| 227 | - public function setArgs(Context $context, $values){ |
|
| 227 | + public function setArgs(Context $context, $values) { |
|
| 228 | 228 | $set = []; |
| 229 | 229 | $params = []; |
| 230 | - foreach ($values as $k=>$v){ |
|
| 230 | + foreach ($values as $k=>$v) { |
|
| 231 | 231 | $k = DB::wrap($k); |
| 232 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 233 | - $set[]= "$k=".$v->get(); |
|
| 234 | - }else{ |
|
| 235 | - $set[]= "$k=?"; |
|
| 236 | - $params[]=$v; |
|
| 232 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 233 | + $set[] = "$k=".$v->get(); |
|
| 234 | + }else { |
|
| 235 | + $set[] = "$k=?"; |
|
| 236 | + $params[] = $v; |
|
| 237 | 237 | } |
| 238 | 238 | } |
| 239 | - if($this->first){ |
|
| 239 | + if ($this->first) { |
|
| 240 | 240 | $this->first = false; |
| 241 | 241 | $context->appendSql('SET '.implode(',', $set)); |
| 242 | 242 | $context->appendParams($params); |
| 243 | - }else{ |
|
| 244 | - $context->appendSql(','.implode(',', $set),false); |
|
| 243 | + }else { |
|
| 244 | + $context->appendSql(','.implode(',', $set), false); |
|
| 245 | 245 | $context->appendParams($params); |
| 246 | 246 | } |
| 247 | 247 | } |
| 248 | - private $first=true; |
|
| 248 | + private $first = true; |
|
| 249 | 249 | } |
| 250 | 250 | class OrderByImpl |
| 251 | 251 | { |
| 252 | - public function orderByArgs(Context $context, $orders){ |
|
| 253 | - if(empty($orders)){ |
|
| 252 | + public function orderByArgs(Context $context, $orders) { |
|
| 253 | + if (empty($orders)) { |
|
| 254 | 254 | return $this; |
| 255 | 255 | } |
| 256 | 256 | $params = array(); |
| 257 | - foreach ($orders as $k=>$v){ |
|
| 258 | - if(is_integer($k)){ |
|
| 257 | + foreach ($orders as $k=>$v) { |
|
| 258 | + if (is_integer($k)) { |
|
| 259 | 259 | $params[] = DB::wrap($v); |
| 260 | - }else{ |
|
| 260 | + }else { |
|
| 261 | 261 | $k = DB::wrap($k); |
| 262 | 262 | |
| 263 | 263 | $v = strtoupper($v); |
| 264 | - ($v =='DESC' || $v =='ASC') or \PhpBoot\abort( new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
| 264 | + ($v == 'DESC' || $v == 'ASC') or \PhpBoot\abort(new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
| 265 | 265 | |
| 266 | 266 | $params[] = "$k $v"; |
| 267 | 267 | } |
| 268 | 268 | } |
| 269 | - if($this->first){ |
|
| 269 | + if ($this->first) { |
|
| 270 | 270 | $this->first = false; |
| 271 | 271 | $context->appendSql('ORDER BY '.implode(',', $params)); |
| 272 | - }else{ |
|
| 273 | - $context->appendSql(','.implode(',', $params),false); |
|
| 272 | + }else { |
|
| 273 | + $context->appendSql(','.implode(',', $params), false); |
|
| 274 | 274 | } |
| 275 | 275 | return $this; |
| 276 | 276 | } |
| 277 | - public function orderBy(Context $context, $column, $order=null){ |
|
| 278 | - if(is_string($column)){ |
|
| 279 | - if($order === null){ |
|
| 277 | + public function orderBy(Context $context, $column, $order = null) { |
|
| 278 | + if (is_string($column)) { |
|
| 279 | + if ($order === null) { |
|
| 280 | 280 | $column = [$column]; |
| 281 | - }else{ |
|
| 281 | + }else { |
|
| 282 | 282 | $column = [$column=>$order]; |
| 283 | 283 | } |
| 284 | 284 | } |
@@ -286,18 +286,18 @@ discard block |
||
| 286 | 286 | |
| 287 | 287 | |
| 288 | 288 | } |
| 289 | - private $first=true; |
|
| 289 | + private $first = true; |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | class LimitImpl |
| 293 | 293 | { |
| 294 | - static public function limit(Context $context, $size){ |
|
| 294 | + static public function limit(Context $context, $size) { |
|
| 295 | 295 | $intSize = intval($size); |
| 296 | 296 | strval($intSize) == $size or \PhpBoot\abort( |
| 297 | 297 | new \InvalidArgumentException("invalid params for limit($size)")); |
| 298 | 298 | $context->appendSql("LIMIT $size"); |
| 299 | 299 | } |
| 300 | - static public function limitWithOffset(Context $context, $start, $size){ |
|
| 300 | + static public function limitWithOffset(Context $context, $start, $size) { |
|
| 301 | 301 | $intStart = intval($start); |
| 302 | 302 | $intSize = intval($size); |
| 303 | 303 | strval($intStart) == $start && strval($intSize) == $size or \PhpBoot\abort( |
@@ -306,31 +306,31 @@ discard block |
||
| 306 | 306 | } |
| 307 | 307 | } |
| 308 | 308 | |
| 309 | -class WhereImpl{ |
|
| 309 | +class WhereImpl { |
|
| 310 | 310 | |
| 311 | - static private function findQ($str,$offset = 0,$no=0){ |
|
| 311 | + static private function findQ($str, $offset = 0, $no = 0) { |
|
| 312 | 312 | $found = strpos($str, '?', $offset); |
| 313 | - if($no == 0 || $found === false){ |
|
| 313 | + if ($no == 0 || $found === false) { |
|
| 314 | 314 | return $found; |
| 315 | 315 | } |
| 316 | - return self::findQ($str, $found+1, $no-1); |
|
| 316 | + return self::findQ($str, $found + 1, $no - 1); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | - static public function where(Context $context, $prefix, $expr, $args){ |
|
| 320 | - if(empty($expr)){ |
|
| 319 | + static public function where(Context $context, $prefix, $expr, $args) { |
|
| 320 | + if (empty($expr)) { |
|
| 321 | 321 | return; |
| 322 | 322 | } |
| 323 | - if(is_callable($expr)){ |
|
| 324 | - self::conditionClosure($context,$prefix, $expr); |
|
| 325 | - }elseif (is_string($expr)){ |
|
| 323 | + if (is_callable($expr)) { |
|
| 324 | + self::conditionClosure($context, $prefix, $expr); |
|
| 325 | + }elseif (is_string($expr)) { |
|
| 326 | 326 | self::condition($context, $prefix, $expr, $args); |
| 327 | - }else{ |
|
| 327 | + }else { |
|
| 328 | 328 | self::conditionArgs($context, $prefix, $expr); |
| 329 | 329 | } |
| 330 | 330 | |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | - static public function conditionClosure(Context $context, $prefix, callable $callback){ |
|
| 333 | + static public function conditionClosure(Context $context, $prefix, callable $callback) { |
|
| 334 | 334 | $context->appendSql($prefix.' ('); |
| 335 | 335 | $callback($context); |
| 336 | 336 | $context->appendSql(')'); |
@@ -356,15 +356,15 @@ discard block |
||
| 356 | 356 | * NOT IN 'id'=>['NOT IN' => [1,2,3]] |
| 357 | 357 | * @return void |
| 358 | 358 | */ |
| 359 | - static public function conditionArgs(Context $context, $prefix, $args=[]){ |
|
| 360 | - if($args ===null){ |
|
| 361 | - return ; |
|
| 359 | + static public function conditionArgs(Context $context, $prefix, $args = []) { |
|
| 360 | + if ($args === null) { |
|
| 361 | + return; |
|
| 362 | 362 | } |
| 363 | 363 | $exprs = array(); |
| 364 | 364 | $params = array(); |
| 365 | - foreach ($args as $k => $v){ |
|
| 365 | + foreach ($args as $k => $v) { |
|
| 366 | 366 | $k = DB::wrap($k); |
| 367 | - if(!is_array($v)){ |
|
| 367 | + if (!is_array($v)) { |
|
| 368 | 368 | $v = ['='=>$v]; |
| 369 | 369 | } |
| 370 | 370 | |
@@ -376,56 +376,56 @@ discard block |
||
| 376 | 376 | new \InvalidArgumentException("invalid param $op for whereArgs")); |
| 377 | 377 | |
| 378 | 378 | $var = array_values($v)[0]; |
| 379 | - if($op == 'IN' || $op == 'NOT IN'){ |
|
| 379 | + if ($op == 'IN' || $op == 'NOT IN') { |
|
| 380 | 380 | $stubs = []; |
| 381 | 381 | |
| 382 | - if($var instanceof BasicRule){ |
|
| 382 | + if ($var instanceof BasicRule) { |
|
| 383 | 383 | $stubs = "({$var->context->sql})"; |
| 384 | 384 | $params = array_merge($params, $var->context->params); |
| 385 | 385 | $exprs[] = "$k $op $stubs"; |
| 386 | - }else{ |
|
| 387 | - foreach ($var as $i){ |
|
| 388 | - if(is_a($i, Raw::class)){ |
|
| 389 | - $stubs[]=strval($i); |
|
| 390 | - }elseif($i instanceof BasicRule){ |
|
| 386 | + }else { |
|
| 387 | + foreach ($var as $i) { |
|
| 388 | + if (is_a($i, Raw::class)) { |
|
| 389 | + $stubs[] = strval($i); |
|
| 390 | + }elseif ($i instanceof BasicRule) { |
|
| 391 | 391 | $stubs = "({$i->context->sql})"; |
| 392 | 392 | $params = array_merge($params, $i->context->params); |
| 393 | - }else{ |
|
| 394 | - $stubs[]='?'; |
|
| 393 | + }else { |
|
| 394 | + $stubs[] = '?'; |
|
| 395 | 395 | $params[] = $i; |
| 396 | 396 | } |
| 397 | 397 | } |
| 398 | 398 | $stubs = implode(',', $stubs); |
| 399 | 399 | $exprs[] = "$k $op ($stubs)"; |
| 400 | 400 | } |
| 401 | - }else if($op == 'BETWEEN'){ |
|
| 401 | + }else if ($op == 'BETWEEN') { |
|
| 402 | 402 | $cond = "$k BETWEEN"; |
| 403 | - if(is_a($var[0], Raw::class)){ |
|
| 403 | + if (is_a($var[0], Raw::class)) { |
|
| 404 | 404 | $cond = "$cond ".strval($var[0]); |
| 405 | - }elseif($var[0] instanceof BasicRule){ |
|
| 405 | + }elseif ($var[0] instanceof BasicRule) { |
|
| 406 | 406 | $cond = "$cond ({$var[0]->context->sql})"; |
| 407 | 407 | $params = array_merge($params, $var[0]->context->params); |
| 408 | - }else{ |
|
| 408 | + }else { |
|
| 409 | 409 | $cond = "$cond ?"; |
| 410 | 410 | $params[] = $var[0]; |
| 411 | 411 | } |
| 412 | - if(is_a($var[1], Raw::class)){ |
|
| 412 | + if (is_a($var[1], Raw::class)) { |
|
| 413 | 413 | $cond = "$cond AND ".strval($var[1]); |
| 414 | - }elseif($var[1] instanceof BasicRule){ |
|
| 414 | + }elseif ($var[1] instanceof BasicRule) { |
|
| 415 | 415 | $cond = "$cond AND ({$var[1]->context->sql})"; |
| 416 | 416 | $params = array_merge($params, $var[1]->context->params); |
| 417 | - }else{ |
|
| 417 | + }else { |
|
| 418 | 418 | $cond = "$cond AND ?"; |
| 419 | 419 | $params[] = $var[1]; |
| 420 | 420 | } |
| 421 | 421 | $exprs[] = $cond; |
| 422 | - }else{ |
|
| 423 | - if(is_a($var, Raw::class)){ |
|
| 422 | + }else { |
|
| 423 | + if (is_a($var, Raw::class)) { |
|
| 424 | 424 | $exprs[] = "$k $op ".strval($var); |
| 425 | - }elseif($var instanceof BasicRule){ |
|
| 425 | + }elseif ($var instanceof BasicRule) { |
|
| 426 | 426 | $exprs[] = "$k $op {$var->context->sql}"; |
| 427 | 427 | $params = array_merge($params, $var->context->params); |
| 428 | - }else{ |
|
| 428 | + }else { |
|
| 429 | 429 | $exprs[] = "$k $op ?"; |
| 430 | 430 | $params[] = $var; |
| 431 | 431 | } |
@@ -434,22 +434,22 @@ discard block |
||
| 434 | 434 | |
| 435 | 435 | self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
| 436 | 436 | } |
| 437 | - static public function condition(Context $context, $prefix, $expr, $args){ |
|
| 438 | - if(!empty($expr)){ |
|
| 437 | + static public function condition(Context $context, $prefix, $expr, $args) { |
|
| 438 | + if (!empty($expr)) { |
|
| 439 | 439 | $expr = "($expr)"; |
| 440 | - if($args){ |
|
| 440 | + if ($args) { |
|
| 441 | 441 | //因为PDO不支持绑定数组变量, 这里需要手动展开数组 |
| 442 | 442 | //也就是说把 where("id IN(?)", [1,2]) 展开成 where("id IN(?,?)", 1,2) |
| 443 | 443 | $cutted = null; |
| 444 | 444 | $cut = null; |
| 445 | 445 | $toReplace = array(); |
| 446 | 446 | |
| 447 | - $newArgs=array(); |
|
| 447 | + $newArgs = array(); |
|
| 448 | 448 | //找到所有数组对应的?符位置 |
| 449 | - foreach ($args as $k =>$arg){ |
|
| 450 | - if(is_array($arg) || is_a($arg, Raw::class) || is_a($arg, BasicRule::class)){ |
|
| 449 | + foreach ($args as $k =>$arg) { |
|
| 450 | + if (is_array($arg) || is_a($arg, Raw::class) || is_a($arg, BasicRule::class)) { |
|
| 451 | 451 | |
| 452 | - if(!$cutted){ |
|
| 452 | + if (!$cutted) { |
|
| 453 | 453 | $cut = new NestedStringCut($expr); |
| 454 | 454 | $cutted = $cut->getText(); |
| 455 | 455 | } |
@@ -459,54 +459,54 @@ discard block |
||
| 459 | 459 | $pos !== false or \PhpBoot\abort( |
| 460 | 460 | new \InvalidArgumentException("unmatched params and ? @ $expr")); |
| 461 | 461 | |
| 462 | - if(is_array($arg)){ |
|
| 462 | + if (is_array($arg)) { |
|
| 463 | 463 | $stubs = []; |
| 464 | - foreach ($arg as $i){ |
|
| 465 | - if(is_a($i, Raw::class)){ |
|
| 464 | + foreach ($arg as $i) { |
|
| 465 | + if (is_a($i, Raw::class)) { |
|
| 466 | 466 | $stubs[] = strval($i); |
| 467 | - }else{ |
|
| 467 | + }else { |
|
| 468 | 468 | $stubs[] = '?'; |
| 469 | 469 | $newArgs[] = $i; |
| 470 | 470 | } |
| 471 | 471 | } |
| 472 | 472 | $stubs = implode(',', $stubs); |
| 473 | - }elseif($arg instanceof BasicRule){ |
|
| 473 | + }elseif ($arg instanceof BasicRule) { |
|
| 474 | 474 | $stubs = "({$arg->context->sql})"; |
| 475 | 475 | $newArgs = array_merge($newArgs, $arg->context->params); |
| 476 | - }else{ |
|
| 476 | + }else { |
|
| 477 | 477 | $stubs = strval($arg); |
| 478 | 478 | } |
| 479 | 479 | $toReplace[] = [$pos, $stubs]; |
| 480 | 480 | |
| 481 | - }else{ |
|
| 482 | - $newArgs[]=$arg; |
|
| 481 | + }else { |
|
| 482 | + $newArgs[] = $arg; |
|
| 483 | 483 | } |
| 484 | 484 | } |
| 485 | 485 | |
| 486 | - if(count($toReplace)){ |
|
| 486 | + if (count($toReplace)) { |
|
| 487 | 487 | $toReplace = array_reverse($toReplace); |
| 488 | - foreach ($toReplace as $i){ |
|
| 488 | + foreach ($toReplace as $i) { |
|
| 489 | 489 | list($pos, $v) = $i; |
| 490 | - $expr = substr($expr, 0, $pos).$v.substr($expr, $pos+1); |
|
| 490 | + $expr = substr($expr, 0, $pos).$v.substr($expr, $pos + 1); |
|
| 491 | 491 | } |
| 492 | 492 | $args = $newArgs; |
| 493 | 493 | } |
| 494 | 494 | } |
| 495 | - if($prefix){ |
|
| 495 | + if ($prefix) { |
|
| 496 | 496 | $context->appendSql($prefix.' '.$expr); |
| 497 | - }else{ |
|
| 497 | + }else { |
|
| 498 | 498 | $context->appendSql($expr); |
| 499 | 499 | } |
| 500 | 500 | |
| 501 | - if($args){ |
|
| 501 | + if ($args) { |
|
| 502 | 502 | $context->appendParams($args); |
| 503 | 503 | } |
| 504 | 504 | } |
| 505 | 505 | } |
| 506 | 506 | } |
| 507 | 507 | |
| 508 | -class GroupByImpl{ |
|
| 509 | - static public function groupBy(Context $context, $column){ |
|
| 508 | +class GroupByImpl { |
|
| 509 | + static public function groupBy(Context $context, $column) { |
|
| 510 | 510 | $column = DB::wrap($column); |
| 511 | 511 | $context->appendSql("GROUP BY $column"); |
| 512 | 512 | } |
@@ -522,12 +522,12 @@ discard block |
||
| 522 | 522 | * @throws DBException|\Exception |
| 523 | 523 | */ |
| 524 | 524 | static public function exec($context) { |
| 525 | - try{ |
|
| 525 | + try { |
|
| 526 | 526 | $st = $context->connection->prepare($context->sql); |
| 527 | 527 | $success = $st->execute($context->params); |
| 528 | 528 | return new ExecResult($success, $context->connection, $st); |
| 529 | - }catch (\Exception $e){ |
|
| 530 | - \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
|
| 529 | + }catch (\Exception $e) { |
|
| 530 | + \PhpBoot\abort(new DBException($context, $e->getMessage(), $e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params]); |
|
| 531 | 531 | return null; |
| 532 | 532 | } |
| 533 | 533 | } |
@@ -538,25 +538,25 @@ discard block |
||
| 538 | 538 | * @return false|array |
| 539 | 539 | * @throws DBException|\Exception |
| 540 | 540 | */ |
| 541 | - static public function get($context, $dictAs=false){ |
|
| 541 | + static public function get($context, $dictAs = false) { |
|
| 542 | 542 | |
| 543 | - try{ |
|
| 543 | + try { |
|
| 544 | 544 | $st = $context->connection->prepare($context->sql); |
| 545 | - if($st->execute($context->params)){ |
|
| 545 | + if ($st->execute($context->params)) { |
|
| 546 | 546 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 547 | - if ($dictAs){ |
|
| 548 | - $dict= []; |
|
| 549 | - foreach ($res as $i){ |
|
| 550 | - $dict[$i[$dictAs]]=$i; |
|
| 547 | + if ($dictAs) { |
|
| 548 | + $dict = []; |
|
| 549 | + foreach ($res as $i) { |
|
| 550 | + $dict[$i[$dictAs]] = $i; |
|
| 551 | 551 | } |
| 552 | 552 | return $context->handleResult($dict); |
| 553 | 553 | } |
| 554 | 554 | return $context->handleResult($res); |
| 555 | - }else{ |
|
| 555 | + }else { |
|
| 556 | 556 | return false; |
| 557 | 557 | } |
| 558 | - }catch (\Exception $e){ |
|
| 559 | - \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
|
| 558 | + }catch (\Exception $e) { |
|
| 559 | + \PhpBoot\abort(new DBException($context, $e->getMessage(), $e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params]); |
|
| 560 | 560 | return false; |
| 561 | 561 | } |
| 562 | 562 | |
@@ -567,23 +567,23 @@ discard block |
||
| 567 | 567 | * @return int|false |
| 568 | 568 | * @throws DBException|\Exception |
| 569 | 569 | */ |
| 570 | - static public function count($context){ |
|
| 570 | + static public function count($context) { |
|
| 571 | 571 | |
| 572 | 572 | $found = []; |
| 573 | - if(!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 574 | - count($found)==0){ |
|
| 573 | + if (!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 574 | + count($found) == 0) { |
|
| 575 | 575 | \PhpBoot\abort(new \PDOException("can not use count(*) without select")); |
| 576 | 576 | } |
| 577 | 577 | list($chars, $columnBegin) = $found[0]; |
| 578 | - $columnBegin = $columnBegin + strlen('select')+1; |
|
| 578 | + $columnBegin = $columnBegin + strlen('select') + 1; |
|
| 579 | 579 | |
| 580 | 580 | $columnEnd = 0; |
| 581 | 581 | $found = []; |
| 582 | - try{ |
|
| 583 | - if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 584 | - count($found)==0){ |
|
| 582 | + try { |
|
| 583 | + if (!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 584 | + count($found) == 0) { |
|
| 585 | 585 | $columnEnd = strlen($context->sql); |
| 586 | - }else{ |
|
| 586 | + }else { |
|
| 587 | 587 | list($chars, $columnEnd) = $found[0]; |
| 588 | 588 | } |
| 589 | 589 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -591,61 +591,61 @@ discard block |
||
| 591 | 591 | $sql .= substr($context->sql, $columnEnd); |
| 592 | 592 | |
| 593 | 593 | $st = $context->connection->prepare($sql); |
| 594 | - if($st->execute($context->params)){ |
|
| 594 | + if ($st->execute($context->params)) { |
|
| 595 | 595 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 596 | 596 | return intval($res[0]['count']); |
| 597 | - }else{ |
|
| 597 | + }else { |
|
| 598 | 598 | return false; |
| 599 | 599 | } |
| 600 | - }catch (\Exception $e){ |
|
| 601 | - \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
|
| 600 | + }catch (\Exception $e) { |
|
| 601 | + \PhpBoot\abort(new DBException($context, $e->getMessage(), $e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params]); |
|
| 602 | 602 | return false; |
| 603 | 603 | } |
| 604 | 604 | } |
| 605 | 605 | } |
| 606 | 606 | class OnDuplicateKeyUpdateImpl |
| 607 | 607 | { |
| 608 | - public function set($context, $column, $value){ |
|
| 609 | - if(is_string($column)){ |
|
| 608 | + public function set($context, $column, $value) { |
|
| 609 | + if (is_string($column)) { |
|
| 610 | 610 | $this->setExpr($context, $column, $value); |
| 611 | - }else{ |
|
| 611 | + }else { |
|
| 612 | 612 | $this->setArgs($context, $column); |
| 613 | 613 | } |
| 614 | 614 | } |
| 615 | 615 | |
| 616 | - public function setExpr($context, $expr, $args){ |
|
| 616 | + public function setExpr($context, $expr, $args) { |
|
| 617 | 617 | $prefix = ''; |
| 618 | - if($this->first){ |
|
| 618 | + if ($this->first) { |
|
| 619 | 619 | $this->first = false; |
| 620 | 620 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
| 621 | - }else{ |
|
| 621 | + }else { |
|
| 622 | 622 | $prefix = ','; |
| 623 | 623 | } |
| 624 | 624 | |
| 625 | - $context->appendSql("$prefix$expr",$prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 625 | + $context->appendSql("$prefix$expr", $prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 626 | 626 | $context->appendParams($args); |
| 627 | 627 | |
| 628 | 628 | } |
| 629 | - public function setArgs($context, $values){ |
|
| 629 | + public function setArgs($context, $values) { |
|
| 630 | 630 | $set = []; |
| 631 | 631 | $params = []; |
| 632 | - foreach ($values as $k=>$v){ |
|
| 632 | + foreach ($values as $k=>$v) { |
|
| 633 | 633 | $k = DB::wrap($k); |
| 634 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 635 | - $set[]= "$k=".$v->get(); |
|
| 636 | - }else{ |
|
| 637 | - $set[]= "$k=?"; |
|
| 638 | - $params[]=$v; |
|
| 634 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 635 | + $set[] = "$k=".$v->get(); |
|
| 636 | + }else { |
|
| 637 | + $set[] = "$k=?"; |
|
| 638 | + $params[] = $v; |
|
| 639 | 639 | } |
| 640 | 640 | } |
| 641 | - if($this->first){ |
|
| 641 | + if ($this->first) { |
|
| 642 | 642 | $this->first = false; |
| 643 | 643 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
| 644 | 644 | $context->appendParams($params); |
| 645 | - }else{ |
|
| 646 | - $context->appendSql(','.implode(',', $set),false); |
|
| 645 | + }else { |
|
| 646 | + $context->appendSql(','.implode(',', $set), false); |
|
| 647 | 647 | $context->appendParams($params); |
| 648 | 648 | } |
| 649 | 649 | } |
| 650 | - private $first=true; |
|
| 650 | + private $first = true; |
|
| 651 | 651 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | if($tables instanceof BasicRule){ |
| 54 | 54 | $context->appendSql("FROM (".$tables->context->sql.')'); |
| 55 | 55 | $context->params = array_merge($context->params,$tables->context->params); |
| 56 | - }else { |
|
| 56 | + } else { |
|
| 57 | 57 | $context->appendSql("FROM ".DB::wrap($tables)); |
| 58 | 58 | } |
| 59 | 59 | if($as){ |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | $table = DB::wrap($table); |
| 77 | 77 | if($type){ |
| 78 | 78 | $context->appendSql("$type JOIN $table"); |
| 79 | - }else{ |
|
| 79 | + } else{ |
|
| 80 | 80 | $context->appendSql("JOIN $table"); |
| 81 | 81 | } |
| 82 | 82 | } |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | foreach ($values as $v){ |
| 127 | 127 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
| 128 | 128 | $stubs[]=$v->get(); |
| 129 | - }else{ |
|
| 129 | + } else{ |
|
| 130 | 130 | $stubs[]='?'; |
| 131 | 131 | $params[] = $v; |
| 132 | 132 | } |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | //VALUES(val0, val1, val2) |
| 138 | 138 | $context->appendSql("VALUES($stubs)"); |
| 139 | 139 | |
| 140 | - }else{ |
|
| 140 | + } else{ |
|
| 141 | 141 | //(col0, col1, col2) VALUES(val0, val1, val2) |
| 142 | 142 | $columns = implode(',', array_map(function($k){return DB::wrap($k);}, array_keys($values))); |
| 143 | 143 | $context->appendSql("($columns) VALUES($stubs)",false); |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | if($keys === range(0, count($keys) - 1)){ |
| 156 | 156 | //VALUES(val0, val1, val2) |
| 157 | 157 | $context->appendSql("VALUES($row)"); |
| 158 | - }else{ |
|
| 158 | + } else{ |
|
| 159 | 159 | //(col0, col1, col2) VALUES(val0, val1, val2) |
| 160 | 160 | $columns = implode(',', array_map(function($k){return DB::wrap($k);}, $keys)); |
| 161 | 161 | $context->appendSql("($columns) VALUES($row)",false); |
@@ -181,9 +181,9 @@ discard block |
||
| 181 | 181 | foreach ($values as &$v){ |
| 182 | 182 | if($v instanceof Raw){ |
| 183 | 183 | $v = $v->get(); |
| 184 | - }elseif(is_bool($v)){ |
|
| 184 | + } elseif(is_bool($v)){ |
|
| 185 | 185 | $v = $v?'true':'false'; |
| 186 | - }elseif(!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])){ |
|
| 186 | + } elseif(!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])){ |
|
| 187 | 187 | $v = (string)$v; |
| 188 | 188 | $v = str_replace("\\", "\\\\", $v); |
| 189 | 189 | $v = str_replace("'", "\\'", $v); |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | public function set(Context $context, $expr, $args){ |
| 208 | 208 | if(is_string($expr)){ |
| 209 | 209 | return $this->setExpr($context, $expr, $args); |
| 210 | - }else{ |
|
| 210 | + } else{ |
|
| 211 | 211 | return $this->setArgs($context, $expr); |
| 212 | 212 | } |
| 213 | 213 | } |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | if($this->first){ |
| 217 | 217 | $this->first = false; |
| 218 | 218 | $prefix = 'SET '; |
| 219 | - }else{ |
|
| 219 | + } else{ |
|
| 220 | 220 | $prefix = ','; |
| 221 | 221 | } |
| 222 | 222 | |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | $k = DB::wrap($k); |
| 232 | 232 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
| 233 | 233 | $set[]= "$k=".$v->get(); |
| 234 | - }else{ |
|
| 234 | + } else{ |
|
| 235 | 235 | $set[]= "$k=?"; |
| 236 | 236 | $params[]=$v; |
| 237 | 237 | } |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | $this->first = false; |
| 241 | 241 | $context->appendSql('SET '.implode(',', $set)); |
| 242 | 242 | $context->appendParams($params); |
| 243 | - }else{ |
|
| 243 | + } else{ |
|
| 244 | 244 | $context->appendSql(','.implode(',', $set),false); |
| 245 | 245 | $context->appendParams($params); |
| 246 | 246 | } |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | foreach ($orders as $k=>$v){ |
| 258 | 258 | if(is_integer($k)){ |
| 259 | 259 | $params[] = DB::wrap($v); |
| 260 | - }else{ |
|
| 260 | + } else{ |
|
| 261 | 261 | $k = DB::wrap($k); |
| 262 | 262 | |
| 263 | 263 | $v = strtoupper($v); |
@@ -269,7 +269,7 @@ discard block |
||
| 269 | 269 | if($this->first){ |
| 270 | 270 | $this->first = false; |
| 271 | 271 | $context->appendSql('ORDER BY '.implode(',', $params)); |
| 272 | - }else{ |
|
| 272 | + } else{ |
|
| 273 | 273 | $context->appendSql(','.implode(',', $params),false); |
| 274 | 274 | } |
| 275 | 275 | return $this; |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | if(is_string($column)){ |
| 279 | 279 | if($order === null){ |
| 280 | 280 | $column = [$column]; |
| 281 | - }else{ |
|
| 281 | + } else{ |
|
| 282 | 282 | $column = [$column=>$order]; |
| 283 | 283 | } |
| 284 | 284 | } |
@@ -322,9 +322,9 @@ discard block |
||
| 322 | 322 | } |
| 323 | 323 | if(is_callable($expr)){ |
| 324 | 324 | self::conditionClosure($context,$prefix, $expr); |
| 325 | - }elseif (is_string($expr)){ |
|
| 325 | + } elseif (is_string($expr)){ |
|
| 326 | 326 | self::condition($context, $prefix, $expr, $args); |
| 327 | - }else{ |
|
| 327 | + } else{ |
|
| 328 | 328 | self::conditionArgs($context, $prefix, $expr); |
| 329 | 329 | } |
| 330 | 330 | |
@@ -383,14 +383,14 @@ discard block |
||
| 383 | 383 | $stubs = "({$var->context->sql})"; |
| 384 | 384 | $params = array_merge($params, $var->context->params); |
| 385 | 385 | $exprs[] = "$k $op $stubs"; |
| 386 | - }else{ |
|
| 386 | + } else{ |
|
| 387 | 387 | foreach ($var as $i){ |
| 388 | 388 | if(is_a($i, Raw::class)){ |
| 389 | 389 | $stubs[]=strval($i); |
| 390 | - }elseif($i instanceof BasicRule){ |
|
| 390 | + } elseif($i instanceof BasicRule){ |
|
| 391 | 391 | $stubs = "({$i->context->sql})"; |
| 392 | 392 | $params = array_merge($params, $i->context->params); |
| 393 | - }else{ |
|
| 393 | + } else{ |
|
| 394 | 394 | $stubs[]='?'; |
| 395 | 395 | $params[] = $i; |
| 396 | 396 | } |
@@ -398,34 +398,34 @@ discard block |
||
| 398 | 398 | $stubs = implode(',', $stubs); |
| 399 | 399 | $exprs[] = "$k $op ($stubs)"; |
| 400 | 400 | } |
| 401 | - }else if($op == 'BETWEEN'){ |
|
| 401 | + } else if($op == 'BETWEEN'){ |
|
| 402 | 402 | $cond = "$k BETWEEN"; |
| 403 | 403 | if(is_a($var[0], Raw::class)){ |
| 404 | 404 | $cond = "$cond ".strval($var[0]); |
| 405 | - }elseif($var[0] instanceof BasicRule){ |
|
| 405 | + } elseif($var[0] instanceof BasicRule){ |
|
| 406 | 406 | $cond = "$cond ({$var[0]->context->sql})"; |
| 407 | 407 | $params = array_merge($params, $var[0]->context->params); |
| 408 | - }else{ |
|
| 408 | + } else{ |
|
| 409 | 409 | $cond = "$cond ?"; |
| 410 | 410 | $params[] = $var[0]; |
| 411 | 411 | } |
| 412 | 412 | if(is_a($var[1], Raw::class)){ |
| 413 | 413 | $cond = "$cond AND ".strval($var[1]); |
| 414 | - }elseif($var[1] instanceof BasicRule){ |
|
| 414 | + } elseif($var[1] instanceof BasicRule){ |
|
| 415 | 415 | $cond = "$cond AND ({$var[1]->context->sql})"; |
| 416 | 416 | $params = array_merge($params, $var[1]->context->params); |
| 417 | - }else{ |
|
| 417 | + } else{ |
|
| 418 | 418 | $cond = "$cond AND ?"; |
| 419 | 419 | $params[] = $var[1]; |
| 420 | 420 | } |
| 421 | 421 | $exprs[] = $cond; |
| 422 | - }else{ |
|
| 422 | + } else{ |
|
| 423 | 423 | if(is_a($var, Raw::class)){ |
| 424 | 424 | $exprs[] = "$k $op ".strval($var); |
| 425 | - }elseif($var instanceof BasicRule){ |
|
| 425 | + } elseif($var instanceof BasicRule){ |
|
| 426 | 426 | $exprs[] = "$k $op {$var->context->sql}"; |
| 427 | 427 | $params = array_merge($params, $var->context->params); |
| 428 | - }else{ |
|
| 428 | + } else{ |
|
| 429 | 429 | $exprs[] = "$k $op ?"; |
| 430 | 430 | $params[] = $var; |
| 431 | 431 | } |
@@ -464,21 +464,21 @@ discard block |
||
| 464 | 464 | foreach ($arg as $i){ |
| 465 | 465 | if(is_a($i, Raw::class)){ |
| 466 | 466 | $stubs[] = strval($i); |
| 467 | - }else{ |
|
| 467 | + } else{ |
|
| 468 | 468 | $stubs[] = '?'; |
| 469 | 469 | $newArgs[] = $i; |
| 470 | 470 | } |
| 471 | 471 | } |
| 472 | 472 | $stubs = implode(',', $stubs); |
| 473 | - }elseif($arg instanceof BasicRule){ |
|
| 473 | + } elseif($arg instanceof BasicRule){ |
|
| 474 | 474 | $stubs = "({$arg->context->sql})"; |
| 475 | 475 | $newArgs = array_merge($newArgs, $arg->context->params); |
| 476 | - }else{ |
|
| 476 | + } else{ |
|
| 477 | 477 | $stubs = strval($arg); |
| 478 | 478 | } |
| 479 | 479 | $toReplace[] = [$pos, $stubs]; |
| 480 | 480 | |
| 481 | - }else{ |
|
| 481 | + } else{ |
|
| 482 | 482 | $newArgs[]=$arg; |
| 483 | 483 | } |
| 484 | 484 | } |
@@ -494,7 +494,7 @@ discard block |
||
| 494 | 494 | } |
| 495 | 495 | if($prefix){ |
| 496 | 496 | $context->appendSql($prefix.' '.$expr); |
| 497 | - }else{ |
|
| 497 | + } else{ |
|
| 498 | 498 | $context->appendSql($expr); |
| 499 | 499 | } |
| 500 | 500 | |
@@ -526,7 +526,7 @@ discard block |
||
| 526 | 526 | $st = $context->connection->prepare($context->sql); |
| 527 | 527 | $success = $st->execute($context->params); |
| 528 | 528 | return new ExecResult($success, $context->connection, $st); |
| 529 | - }catch (\Exception $e){ |
|
| 529 | + } catch (\Exception $e){ |
|
| 530 | 530 | \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
| 531 | 531 | return null; |
| 532 | 532 | } |
@@ -552,10 +552,10 @@ discard block |
||
| 552 | 552 | return $context->handleResult($dict); |
| 553 | 553 | } |
| 554 | 554 | return $context->handleResult($res); |
| 555 | - }else{ |
|
| 555 | + } else{ |
|
| 556 | 556 | return false; |
| 557 | 557 | } |
| 558 | - }catch (\Exception $e){ |
|
| 558 | + } catch (\Exception $e){ |
|
| 559 | 559 | \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
| 560 | 560 | return false; |
| 561 | 561 | } |
@@ -583,7 +583,7 @@ discard block |
||
| 583 | 583 | if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
| 584 | 584 | count($found)==0){ |
| 585 | 585 | $columnEnd = strlen($context->sql); |
| 586 | - }else{ |
|
| 586 | + } else{ |
|
| 587 | 587 | list($chars, $columnEnd) = $found[0]; |
| 588 | 588 | } |
| 589 | 589 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -594,10 +594,10 @@ discard block |
||
| 594 | 594 | if($st->execute($context->params)){ |
| 595 | 595 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 596 | 596 | return intval($res[0]['count']); |
| 597 | - }else{ |
|
| 597 | + } else{ |
|
| 598 | 598 | return false; |
| 599 | 599 | } |
| 600 | - }catch (\Exception $e){ |
|
| 600 | + } catch (\Exception $e){ |
|
| 601 | 601 | \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
| 602 | 602 | return false; |
| 603 | 603 | } |
@@ -608,7 +608,7 @@ discard block |
||
| 608 | 608 | public function set($context, $column, $value){ |
| 609 | 609 | if(is_string($column)){ |
| 610 | 610 | $this->setExpr($context, $column, $value); |
| 611 | - }else{ |
|
| 611 | + } else{ |
|
| 612 | 612 | $this->setArgs($context, $column); |
| 613 | 613 | } |
| 614 | 614 | } |
@@ -618,7 +618,7 @@ discard block |
||
| 618 | 618 | if($this->first){ |
| 619 | 619 | $this->first = false; |
| 620 | 620 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
| 621 | - }else{ |
|
| 621 | + } else{ |
|
| 622 | 622 | $prefix = ','; |
| 623 | 623 | } |
| 624 | 624 | |
@@ -633,7 +633,7 @@ discard block |
||
| 633 | 633 | $k = DB::wrap($k); |
| 634 | 634 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
| 635 | 635 | $set[]= "$k=".$v->get(); |
| 636 | - }else{ |
|
| 636 | + } else{ |
|
| 637 | 637 | $set[]= "$k=?"; |
| 638 | 638 | $params[]=$v; |
| 639 | 639 | } |
@@ -642,7 +642,7 @@ discard block |
||
| 642 | 642 | $this->first = false; |
| 643 | 643 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
| 644 | 644 | $context->appendParams($params); |
| 645 | - }else{ |
|
| 645 | + } else{ |
|
| 646 | 646 | $context->appendSql(','.implode(',', $set),false); |
| 647 | 647 | $context->appendParams($params); |
| 648 | 648 | } |