@@ -8,6 +8,11 @@ discard block |
||
8 | 8 | use PhpBoot\DB\Context; |
9 | 9 | |
10 | 10 | class ExecResult{ |
11 | + |
|
12 | + /** |
|
13 | + * @param \PDO $pdo |
|
14 | + * @param \PDOStatement $st |
|
15 | + */ |
|
11 | 16 | public function __construct($success, $pdo, $st){ |
12 | 17 | $this->pdo = $pdo; |
13 | 18 | $this->st = $st; |
@@ -41,6 +46,10 @@ discard block |
||
41 | 46 | |
42 | 47 | class SelectImpl |
43 | 48 | { |
49 | + /** |
|
50 | + * @param Context $context |
|
51 | + * @param string $columns |
|
52 | + */ |
|
44 | 53 | static public function select($context, $columns){ |
45 | 54 | $context->appendSql("SELECT $columns"); |
46 | 55 | } |
@@ -48,6 +57,10 @@ discard block |
||
48 | 57 | |
49 | 58 | class FromImpl |
50 | 59 | { |
60 | + /** |
|
61 | + * @param Context $context |
|
62 | + * @param string $tables |
|
63 | + */ |
|
51 | 64 | static public function from($context, $tables,$as=null){ |
52 | 65 | if($tables instanceof BasicRule){ |
53 | 66 | $context->appendSql("FROM (".$tables->context->sql.')'); |
@@ -63,6 +76,10 @@ discard block |
||
63 | 76 | |
64 | 77 | class DeleteImpl |
65 | 78 | { |
79 | + /** |
|
80 | + * @param Context $context |
|
81 | + * @param string $from |
|
82 | + */ |
|
66 | 83 | static public function deleteFrom($context, $from) |
67 | 84 | { |
68 | 85 | $context->appendSql("DELETE FROM ".DB::wrap($from)); |
@@ -71,6 +88,11 @@ discard block |
||
71 | 88 | |
72 | 89 | class JoinImpl |
73 | 90 | { |
91 | + /** |
|
92 | + * @param Context $context |
|
93 | + * @param null|string $type |
|
94 | + * @param string $table |
|
95 | + */ |
|
74 | 96 | static public function join($context, $type, $table) { |
75 | 97 | $table = DB::wrap($table); |
76 | 98 | if($type){ |
@@ -83,6 +105,10 @@ discard block |
||
83 | 105 | |
84 | 106 | class JoinOnImpl |
85 | 107 | { |
108 | + /** |
|
109 | + * @param Context $context |
|
110 | + * @param string $condition |
|
111 | + */ |
|
86 | 112 | static public function on($context, $condition) { |
87 | 113 | $context->appendSql("ON $condition"); |
88 | 114 | } |
@@ -90,6 +116,9 @@ discard block |
||
90 | 116 | |
91 | 117 | class ForUpdateImpl |
92 | 118 | { |
119 | + /** |
|
120 | + * @param Context $context |
|
121 | + */ |
|
93 | 122 | static public function forUpdate($context){ |
94 | 123 | $context->appendSql("FOR UPDATE"); |
95 | 124 | } |
@@ -97,6 +126,10 @@ discard block |
||
97 | 126 | |
98 | 127 | class ForUpdateOfImpl |
99 | 128 | { |
129 | + /** |
|
130 | + * @param Context $context |
|
131 | + * @param string $column |
|
132 | + */ |
|
100 | 133 | static public function of($context, $column){ |
101 | 134 | $column = DB::wrap($column); |
102 | 135 | $context->appendSql("OF $column"); |
@@ -105,6 +138,10 @@ discard block |
||
105 | 138 | |
106 | 139 | class InsertImpl |
107 | 140 | { |
141 | + /** |
|
142 | + * @param Context $context |
|
143 | + * @param string $table |
|
144 | + */ |
|
108 | 145 | static public function insertInto($context, $table) { |
109 | 146 | $table = DB::wrap($table); |
110 | 147 | $context->appendSql("INSERT INTO $table"); |
@@ -112,6 +149,10 @@ discard block |
||
112 | 149 | } |
113 | 150 | class ReplaceImpl |
114 | 151 | { |
152 | + /** |
|
153 | + * @param Context $context |
|
154 | + * @param string $table |
|
155 | + */ |
|
115 | 156 | static public function replaceInto($context, $table) { |
116 | 157 | $table = DB::wrap($table); |
117 | 158 | $context->appendSql("REPLACE INTO $table"); |
@@ -195,6 +236,10 @@ discard block |
||
195 | 236 | |
196 | 237 | class UpdateImpl |
197 | 238 | { |
239 | + /** |
|
240 | + * @param Context $context |
|
241 | + * @param string $table |
|
242 | + */ |
|
198 | 243 | static public function update($context, $table){ |
199 | 244 | $table = DB::wrap($table); |
200 | 245 | $context->appendSql("UPDATE $table"); |
@@ -211,6 +256,9 @@ discard block |
||
211 | 256 | } |
212 | 257 | } |
213 | 258 | |
259 | + /** |
|
260 | + * @param string $expr |
|
261 | + */ |
|
214 | 262 | public function setExpr(Context $context, $expr, $args){ |
215 | 263 | if($this->first){ |
216 | 264 | $this->first = false; |
@@ -273,6 +321,11 @@ discard block |
||
273 | 321 | } |
274 | 322 | return $this; |
275 | 323 | } |
324 | + |
|
325 | + /** |
|
326 | + * @param string $column |
|
327 | + * @param string $order |
|
328 | + */ |
|
276 | 329 | public function orderBy(Context $context, $column, $order=null){ |
277 | 330 | if(is_string($column)){ |
278 | 331 | if($order === null){ |
@@ -290,12 +343,20 @@ discard block |
||
290 | 343 | |
291 | 344 | class LimitImpl |
292 | 345 | { |
346 | + /** |
|
347 | + * @param integer $size |
|
348 | + */ |
|
293 | 349 | static public function limit(Context $context, $size){ |
294 | 350 | $intSize = intval($size); |
295 | 351 | strval($intSize) == $size or \PhpBoot\abort( |
296 | 352 | new \InvalidArgumentException("invalid params for limit($size)")); |
297 | 353 | $context->appendSql("LIMIT $size"); |
298 | 354 | } |
355 | + |
|
356 | + /** |
|
357 | + * @param integer $start |
|
358 | + * @param integer $size |
|
359 | + */ |
|
299 | 360 | static public function limitWithOffset(Context $context, $start, $size){ |
300 | 361 | $intStart = intval($start); |
301 | 362 | $intSize = intval($size); |
@@ -307,6 +368,9 @@ discard block |
||
307 | 368 | |
308 | 369 | class WhereImpl{ |
309 | 370 | |
371 | + /** |
|
372 | + * @param string $str |
|
373 | + */ |
|
310 | 374 | static private function findQ($str,$offset = 0,$no=0){ |
311 | 375 | $found = strpos($str, '?', $offset); |
312 | 376 | if($no == 0 || $found === false){ |
@@ -315,6 +379,9 @@ discard block |
||
315 | 379 | return self::findQ($str, $found+1, $no-1); |
316 | 380 | } |
317 | 381 | |
382 | + /** |
|
383 | + * @param string $prefix |
|
384 | + */ |
|
318 | 385 | static public function where(Context $context, $prefix, $expr, $args){ |
319 | 386 | if(empty($expr)){ |
320 | 387 | return; |
@@ -433,6 +500,10 @@ discard block |
||
433 | 500 | |
434 | 501 | self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
435 | 502 | } |
503 | + |
|
504 | + /** |
|
505 | + * @param string $expr |
|
506 | + */ |
|
436 | 507 | static public function condition(Context $context, $prefix, $expr, $args){ |
437 | 508 | if(!empty($expr)){ |
438 | 509 | $expr = "($expr)"; |
@@ -505,6 +576,10 @@ discard block |
||
505 | 576 | } |
506 | 577 | |
507 | 578 | class GroupByImpl{ |
579 | + |
|
580 | + /** |
|
581 | + * @param string $column |
|
582 | + */ |
|
508 | 583 | static public function groupBy(Context $context, $column){ |
509 | 584 | $column = DB::wrap($column); |
510 | 585 | $context->appendSql("GROUP BY $column"); |
@@ -527,7 +602,6 @@ discard block |
||
527 | 602 | /** |
528 | 603 | * |
529 | 604 | * @param Context $context |
530 | - * @param string|false $asDict return as dict or array |
|
531 | 605 | * @return false|array |
532 | 606 | */ |
533 | 607 | static public function get($context, $dictAs=false){ |
@@ -586,6 +660,9 @@ discard block |
||
586 | 660 | } |
587 | 661 | class OnDuplicateKeyUpdateImpl |
588 | 662 | { |
663 | + /** |
|
664 | + * @param Context $context |
|
665 | + */ |
|
589 | 666 | public function set($context, $column, $value){ |
590 | 667 | if(is_string($column)){ |
591 | 668 | $this->setExpr($context, $column, $value); |
@@ -594,6 +671,9 @@ discard block |
||
594 | 671 | } |
595 | 672 | } |
596 | 673 | |
674 | + /** |
|
675 | + * @param string $expr |
|
676 | + */ |
|
597 | 677 | public function setExpr($context, $expr, $args){ |
598 | 678 | $prefix = ''; |
599 | 679 | if($this->first){ |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | if($tables instanceof BasicRule){ |
53 | 53 | $context->appendSql("FROM (".$tables->context->sql.')'); |
54 | 54 | $context->params = array_merge($context->params,$tables->context->params); |
55 | - }else { |
|
55 | + } else { |
|
56 | 56 | $context->appendSql("FROM ".DB::wrap($tables)); |
57 | 57 | } |
58 | 58 | if($as){ |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | $table = DB::wrap($table); |
76 | 76 | if($type){ |
77 | 77 | $context->appendSql("$type JOIN $table"); |
78 | - }else{ |
|
78 | + } else{ |
|
79 | 79 | $context->appendSql("JOIN $table"); |
80 | 80 | } |
81 | 81 | } |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | foreach ($values as $v){ |
126 | 126 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
127 | 127 | $stubs[]=$v->get(); |
128 | - }else{ |
|
128 | + } else{ |
|
129 | 129 | $stubs[]='?'; |
130 | 130 | $params[] = $v; |
131 | 131 | } |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | //VALUES(val0, val1, val2) |
137 | 137 | $context->appendSql("VALUES($stubs)"); |
138 | 138 | |
139 | - }else{ |
|
139 | + } else{ |
|
140 | 140 | //(col0, col1, col2) VALUES(val0, val1, val2) |
141 | 141 | $columns = implode(',', array_map(function($k){return DB::wrap($k);}, array_keys($values))); |
142 | 142 | $context->appendSql("($columns) VALUES($stubs)",false); |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | if($keys === range(0, count($keys) - 1)){ |
155 | 155 | //VALUES(val0, val1, val2) |
156 | 156 | $context->appendSql("VALUES($row)"); |
157 | - }else{ |
|
157 | + } else{ |
|
158 | 158 | //(col0, col1, col2) VALUES(val0, val1, val2) |
159 | 159 | $columns = implode(',', array_map(function($k){return DB::wrap($k);}, $keys)); |
160 | 160 | $context->appendSql("($columns) VALUES($row)",false); |
@@ -180,9 +180,9 @@ discard block |
||
180 | 180 | foreach ($values as &$v){ |
181 | 181 | if($v instanceof Raw){ |
182 | 182 | $v = $v->get(); |
183 | - }elseif(is_bool($v)){ |
|
183 | + } elseif(is_bool($v)){ |
|
184 | 184 | $v = $v?'true':'false'; |
185 | - }elseif(!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])){ |
|
185 | + } elseif(!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])){ |
|
186 | 186 | $v = (string)$v; |
187 | 187 | $v = str_replace("\\", "\\\\", $v); |
188 | 188 | $v = str_replace("'", "\\'", $v); |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | public function set(Context $context, $expr, $args){ |
207 | 207 | if(is_string($expr)){ |
208 | 208 | return $this->setExpr($context, $expr, $args); |
209 | - }else{ |
|
209 | + } else{ |
|
210 | 210 | return $this->setArgs($context, $expr); |
211 | 211 | } |
212 | 212 | } |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | if($this->first){ |
216 | 216 | $this->first = false; |
217 | 217 | $prefix = 'SET '; |
218 | - }else{ |
|
218 | + } else{ |
|
219 | 219 | $prefix = ','; |
220 | 220 | } |
221 | 221 | |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | $k = DB::wrap($k); |
231 | 231 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
232 | 232 | $set[]= "$k=".$v->get(); |
233 | - }else{ |
|
233 | + } else{ |
|
234 | 234 | $set[]= "$k=?"; |
235 | 235 | $params[]=$v; |
236 | 236 | } |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | $this->first = false; |
240 | 240 | $context->appendSql('SET '.implode(',', $set)); |
241 | 241 | $context->appendParams($params); |
242 | - }else{ |
|
242 | + } else{ |
|
243 | 243 | $context->appendSql(','.implode(',', $set),false); |
244 | 244 | $context->appendParams($params); |
245 | 245 | } |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | foreach ($orders as $k=>$v){ |
257 | 257 | if(is_integer($k)){ |
258 | 258 | $params[] = DB::wrap($v); |
259 | - }else{ |
|
259 | + } else{ |
|
260 | 260 | $k = DB::wrap($k); |
261 | 261 | |
262 | 262 | $v = strtoupper($v); |
@@ -268,7 +268,7 @@ discard block |
||
268 | 268 | if($this->first){ |
269 | 269 | $this->first = false; |
270 | 270 | $context->appendSql('ORDER BY '.implode(',', $params)); |
271 | - }else{ |
|
271 | + } else{ |
|
272 | 272 | $context->appendSql(','.implode(',', $params),false); |
273 | 273 | } |
274 | 274 | return $this; |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | if(is_string($column)){ |
278 | 278 | if($order === null){ |
279 | 279 | $column = [$column]; |
280 | - }else{ |
|
280 | + } else{ |
|
281 | 281 | $column = [$column=>$order]; |
282 | 282 | } |
283 | 283 | } |
@@ -321,9 +321,9 @@ discard block |
||
321 | 321 | } |
322 | 322 | if(is_callable($expr)){ |
323 | 323 | self::conditionClosure($context,$prefix, $expr); |
324 | - }elseif (is_string($expr)){ |
|
324 | + } elseif (is_string($expr)){ |
|
325 | 325 | self::condition($context, $prefix, $expr, $args); |
326 | - }else{ |
|
326 | + } else{ |
|
327 | 327 | self::conditionArgs($context, $prefix, $expr); |
328 | 328 | } |
329 | 329 | |
@@ -382,14 +382,14 @@ discard block |
||
382 | 382 | $stubs = "({$var->context->sql})"; |
383 | 383 | $params = array_merge($params, $var->context->params); |
384 | 384 | $exprs[] = "$k $op $stubs"; |
385 | - }else{ |
|
385 | + } else{ |
|
386 | 386 | foreach ($var as $i){ |
387 | 387 | if(is_a($i, Raw::class)){ |
388 | 388 | $stubs[]=strval($i); |
389 | - }elseif($i instanceof BasicRule){ |
|
389 | + } elseif($i instanceof BasicRule){ |
|
390 | 390 | $stubs = "({$i->context->sql})"; |
391 | 391 | $params = array_merge($params, $i->context->params); |
392 | - }else{ |
|
392 | + } else{ |
|
393 | 393 | $stubs[]='?'; |
394 | 394 | $params[] = $i; |
395 | 395 | } |
@@ -397,34 +397,34 @@ discard block |
||
397 | 397 | $stubs = implode(',', $stubs); |
398 | 398 | $exprs[] = "$k $op ($stubs)"; |
399 | 399 | } |
400 | - }else if($op == 'BETWEEN'){ |
|
400 | + } else if($op == 'BETWEEN'){ |
|
401 | 401 | $cond = "$k BETWEEN"; |
402 | 402 | if(is_a($var[0], Raw::class)){ |
403 | 403 | $cond = "$cond ".strval($var[0]); |
404 | - }elseif($var[0] instanceof BasicRule){ |
|
404 | + } elseif($var[0] instanceof BasicRule){ |
|
405 | 405 | $cond = "$cond ({$var[0]->context->sql})"; |
406 | 406 | $params = array_merge($params, $var[0]->context->params); |
407 | - }else{ |
|
407 | + } else{ |
|
408 | 408 | $cond = "$cond ?"; |
409 | 409 | $params[] = $var[0]; |
410 | 410 | } |
411 | 411 | if(is_a($var[1], Raw::class)){ |
412 | 412 | $cond = "$cond AND ".strval($var[1]); |
413 | - }elseif($var[1] instanceof BasicRule){ |
|
413 | + } elseif($var[1] instanceof BasicRule){ |
|
414 | 414 | $cond = "$cond AND ({$var[1]->context->sql})"; |
415 | 415 | $params = array_merge($params, $var[1]->context->params); |
416 | - }else{ |
|
416 | + } else{ |
|
417 | 417 | $cond = "$cond AND ?"; |
418 | 418 | $params[] = $var[1]; |
419 | 419 | } |
420 | 420 | $exprs[] = $cond; |
421 | - }else{ |
|
421 | + } else{ |
|
422 | 422 | if(is_a($var, Raw::class)){ |
423 | 423 | $exprs[] = "$k $op ".strval($var); |
424 | - }elseif($var instanceof BasicRule){ |
|
424 | + } elseif($var instanceof BasicRule){ |
|
425 | 425 | $exprs[] = "$k $op {$var->context->sql}"; |
426 | 426 | $params = array_merge($params, $var->context->params); |
427 | - }else{ |
|
427 | + } else{ |
|
428 | 428 | $exprs[] = "$k $op ?"; |
429 | 429 | $params[] = $var; |
430 | 430 | } |
@@ -463,21 +463,21 @@ discard block |
||
463 | 463 | foreach ($arg as $i){ |
464 | 464 | if(is_a($i, Raw::class)){ |
465 | 465 | $stubs[] = strval($i); |
466 | - }else{ |
|
466 | + } else{ |
|
467 | 467 | $stubs[] = '?'; |
468 | 468 | $newArgs[] = $i; |
469 | 469 | } |
470 | 470 | } |
471 | 471 | $stubs = implode(',', $stubs); |
472 | - }elseif($arg instanceof BasicRule){ |
|
472 | + } elseif($arg instanceof BasicRule){ |
|
473 | 473 | $stubs = "({$arg->context->sql})"; |
474 | 474 | $newArgs = array_merge($newArgs, $arg->context->params); |
475 | - }else{ |
|
475 | + } else{ |
|
476 | 476 | $stubs = strval($arg); |
477 | 477 | } |
478 | 478 | $toReplace[] = [$pos, $stubs]; |
479 | 479 | |
480 | - }else{ |
|
480 | + } else{ |
|
481 | 481 | $newArgs[]=$arg; |
482 | 482 | } |
483 | 483 | } |
@@ -493,7 +493,7 @@ discard block |
||
493 | 493 | } |
494 | 494 | if($prefix){ |
495 | 495 | $context->appendSql($prefix.' '.$expr); |
496 | - }else{ |
|
496 | + } else{ |
|
497 | 497 | $context->appendSql($expr); |
498 | 498 | } |
499 | 499 | |
@@ -543,7 +543,7 @@ discard block |
||
543 | 543 | return $context->handleResult($dict); |
544 | 544 | } |
545 | 545 | return $context->handleResult($res); |
546 | - }else{ |
|
546 | + } else{ |
|
547 | 547 | return false; |
548 | 548 | } |
549 | 549 | } |
@@ -567,7 +567,7 @@ discard block |
||
567 | 567 | if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
568 | 568 | count($found)==0){ |
569 | 569 | $columnEnd = strlen($context->sql); |
570 | - }else{ |
|
570 | + } else{ |
|
571 | 571 | list($chars, $columnEnd) = $found[0]; |
572 | 572 | } |
573 | 573 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -578,7 +578,7 @@ discard block |
||
578 | 578 | if($st->execute($context->params)){ |
579 | 579 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
580 | 580 | return intval($res[0]['count']); |
581 | - }else{ |
|
581 | + } else{ |
|
582 | 582 | return false; |
583 | 583 | } |
584 | 584 | |
@@ -589,7 +589,7 @@ discard block |
||
589 | 589 | public function set($context, $column, $value){ |
590 | 590 | if(is_string($column)){ |
591 | 591 | $this->setExpr($context, $column, $value); |
592 | - }else{ |
|
592 | + } else{ |
|
593 | 593 | $this->setArgs($context, $column); |
594 | 594 | } |
595 | 595 | } |
@@ -599,7 +599,7 @@ discard block |
||
599 | 599 | if($this->first){ |
600 | 600 | $this->first = false; |
601 | 601 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
602 | - }else{ |
|
602 | + } else{ |
|
603 | 603 | $prefix = ','; |
604 | 604 | } |
605 | 605 | |
@@ -614,7 +614,7 @@ discard block |
||
614 | 614 | $k = DB::wrap($k); |
615 | 615 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
616 | 616 | $set[]= "$k=".$v->get(); |
617 | - }else{ |
|
617 | + } else{ |
|
618 | 618 | $set[]= "$k=?"; |
619 | 619 | $params[]=$v; |
620 | 620 | } |
@@ -623,7 +623,7 @@ discard block |
||
623 | 623 | $this->first = false; |
624 | 624 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
625 | 625 | $context->appendParams($params); |
626 | - }else{ |
|
626 | + } else{ |
|
627 | 627 | $context->appendSql(','.implode(',', $set),false); |
628 | 628 | $context->appendParams($params); |
629 | 629 | } |