@@ -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){ |
@@ -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 | } |