@@ -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"); |
@@ -119,6 +160,9 @@ discard block |
||
| 119 | 160 | } |
| 120 | 161 | class ValuesImpl |
| 121 | 162 | { |
| 163 | + /** |
|
| 164 | + * @param Context $context |
|
| 165 | + */ |
|
| 122 | 166 | static public function values($context, $values){ |
| 123 | 167 | $params = []; |
| 124 | 168 | $stubs = []; |
@@ -148,6 +192,10 @@ discard block |
||
| 148 | 192 | |
| 149 | 193 | class UpdateImpl |
| 150 | 194 | { |
| 195 | + /** |
|
| 196 | + * @param Context $context |
|
| 197 | + * @param string $table |
|
| 198 | + */ |
|
| 151 | 199 | static public function update($context, $table){ |
| 152 | 200 | $table = DB::wrap($table); |
| 153 | 201 | $context->appendSql("UPDATE $table"); |
@@ -164,6 +212,9 @@ discard block |
||
| 164 | 212 | } |
| 165 | 213 | } |
| 166 | 214 | |
| 215 | + /** |
|
| 216 | + * @param string $expr |
|
| 217 | + */ |
|
| 167 | 218 | public function setExpr(Context $context, $expr, $args){ |
| 168 | 219 | if($this->first){ |
| 169 | 220 | $this->first = false; |
@@ -226,6 +277,11 @@ discard block |
||
| 226 | 277 | } |
| 227 | 278 | return $this; |
| 228 | 279 | } |
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * @param string $column |
|
| 283 | + * @param string $order |
|
| 284 | + */ |
|
| 229 | 285 | public function orderBy(Context $context, $column, $order=null){ |
| 230 | 286 | if(is_string($column)){ |
| 231 | 287 | if($order === null){ |
@@ -243,12 +299,20 @@ discard block |
||
| 243 | 299 | |
| 244 | 300 | class LimitImpl |
| 245 | 301 | { |
| 302 | + /** |
|
| 303 | + * @param integer $size |
|
| 304 | + */ |
|
| 246 | 305 | static public function limit(Context $context, $size){ |
| 247 | 306 | $intSize = intval($size); |
| 248 | 307 | strval($intSize) == $size or \PhpBoot\abort( |
| 249 | 308 | new \InvalidArgumentException("invalid params for limit($size)")); |
| 250 | 309 | $context->appendSql("LIMIT $size"); |
| 251 | 310 | } |
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * @param integer $start |
|
| 314 | + * @param integer $size |
|
| 315 | + */ |
|
| 252 | 316 | static public function limitWithOffset(Context $context, $start, $size){ |
| 253 | 317 | $intStart = intval($start); |
| 254 | 318 | $intSize = intval($size); |
@@ -260,6 +324,9 @@ discard block |
||
| 260 | 324 | |
| 261 | 325 | class WhereImpl{ |
| 262 | 326 | |
| 327 | + /** |
|
| 328 | + * @param string $str |
|
| 329 | + */ |
|
| 263 | 330 | static private function findQ($str,$offset = 0,$no=0){ |
| 264 | 331 | $found = strpos($str, '?', $offset); |
| 265 | 332 | if($no == 0 || $found === false){ |
@@ -268,6 +335,9 @@ discard block |
||
| 268 | 335 | return self::findQ($str, $found+1, $no-1); |
| 269 | 336 | } |
| 270 | 337 | |
| 338 | + /** |
|
| 339 | + * @param string $prefix |
|
| 340 | + */ |
|
| 271 | 341 | static public function where(Context $context, $prefix, $expr, $args){ |
| 272 | 342 | if(empty($expr)){ |
| 273 | 343 | return; |
@@ -373,6 +443,10 @@ discard block |
||
| 373 | 443 | |
| 374 | 444 | self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
| 375 | 445 | } |
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * @param string $expr |
|
| 449 | + */ |
|
| 376 | 450 | static public function condition(Context $context, $prefix, $expr, $args){ |
| 377 | 451 | if(!empty($expr)){ |
| 378 | 452 | $expr = "($expr)"; |
@@ -441,6 +515,10 @@ discard block |
||
| 441 | 515 | } |
| 442 | 516 | |
| 443 | 517 | class GroupByImpl{ |
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * @param string $column |
|
| 521 | + */ |
|
| 444 | 522 | static public function groupBy(Context $context, $column){ |
| 445 | 523 | $column = DB::wrap($column); |
| 446 | 524 | $context->appendSql("GROUP BY $column"); |
@@ -463,7 +541,6 @@ discard block |
||
| 463 | 541 | /** |
| 464 | 542 | * |
| 465 | 543 | * @param Context $context |
| 466 | - * @param string|false $asDict return as dict or array |
|
| 467 | 544 | * @return false|array |
| 468 | 545 | */ |
| 469 | 546 | static public function get($context, $dictAs=false){ |
@@ -522,6 +599,9 @@ discard block |
||
| 522 | 599 | } |
| 523 | 600 | class OnDuplicateKeyUpdateImpl |
| 524 | 601 | { |
| 602 | + /** |
|
| 603 | + * @param Context $context |
|
| 604 | + */ |
|
| 525 | 605 | public function set($context, $column, $value){ |
| 526 | 606 | if(is_string($column)){ |
| 527 | 607 | $this->setExpr($context, $column, $value); |
@@ -530,6 +610,9 @@ discard block |
||
| 530 | 610 | } |
| 531 | 611 | } |
| 532 | 612 | |
| 613 | + /** |
|
| 614 | + * @param string $expr |
|
| 615 | + */ |
|
| 533 | 616 | public function setExpr($context, $expr, $args){ |
| 534 | 617 | $prefix = ''; |
| 535 | 618 | if($this->first){ |
@@ -7,14 +7,14 @@ discard block |
||
| 7 | 7 | use PhpBoot\DB\rules\basic\BasicRule; |
| 8 | 8 | use PhpBoot\DB\Context; |
| 9 | 9 | |
| 10 | -class ExecResult{ |
|
| 11 | - public function __construct($success, $pdo, $st){ |
|
| 10 | +class ExecResult { |
|
| 11 | + public function __construct($success, $pdo, $st) { |
|
| 12 | 12 | $this->pdo = $pdo; |
| 13 | 13 | $this->st = $st; |
| 14 | 14 | $this->success = $success; |
| 15 | 15 | $this->rows = $this->st->rowCount(); |
| 16 | 16 | } |
| 17 | - public function lastInsertId($name=null){ |
|
| 17 | + public function lastInsertId($name = null) { |
|
| 18 | 18 | return $this->pdo->lastInsertId($name); |
| 19 | 19 | } |
| 20 | 20 | /** |
@@ -41,21 +41,21 @@ discard block |
||
| 41 | 41 | |
| 42 | 42 | class SelectImpl |
| 43 | 43 | { |
| 44 | - static public function select($context, $columns){ |
|
| 44 | + static public function select($context, $columns) { |
|
| 45 | 45 | $context->appendSql("SELECT $columns"); |
| 46 | 46 | } |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | class FromImpl |
| 50 | 50 | { |
| 51 | - static public function from($context, $tables,$as=null){ |
|
| 52 | - if($tables instanceof BasicRule){ |
|
| 51 | + static public function from($context, $tables, $as = null) { |
|
| 52 | + if ($tables instanceof BasicRule) { |
|
| 53 | 53 | $context->appendSql("FROM (".$tables->context->sql.')'); |
| 54 | - $context->params = array_merge($context->params,$tables->context->params); |
|
| 54 | + $context->params = array_merge($context->params, $tables->context->params); |
|
| 55 | 55 | }else { |
| 56 | 56 | $context->appendSql("FROM ".DB::wrap($tables)); |
| 57 | 57 | } |
| 58 | - if($as){ |
|
| 58 | + if ($as) { |
|
| 59 | 59 | $context->appendSql("as ".DB::wrap($as)); |
| 60 | 60 | } |
| 61 | 61 | } |
@@ -73,9 +73,9 @@ discard block |
||
| 73 | 73 | { |
| 74 | 74 | static public function join($context, $type, $table) { |
| 75 | 75 | $table = DB::wrap($table); |
| 76 | - if($type){ |
|
| 76 | + if ($type) { |
|
| 77 | 77 | $context->appendSql("$type JOIN $table"); |
| 78 | - }else{ |
|
| 78 | + }else { |
|
| 79 | 79 | $context->appendSql("JOIN $table"); |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -90,14 +90,14 @@ discard block |
||
| 90 | 90 | |
| 91 | 91 | class ForUpdateImpl |
| 92 | 92 | { |
| 93 | - static public function forUpdate($context){ |
|
| 93 | + static public function forUpdate($context) { |
|
| 94 | 94 | $context->appendSql("FOR UPDATE"); |
| 95 | 95 | } |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | class ForUpdateOfImpl |
| 99 | 99 | { |
| 100 | - static public function of($context, $column){ |
|
| 100 | + static public function of($context, $column) { |
|
| 101 | 101 | $column = DB::wrap($column); |
| 102 | 102 | $context->appendSql("OF $column"); |
| 103 | 103 | } |
@@ -119,27 +119,27 @@ discard block |
||
| 119 | 119 | } |
| 120 | 120 | class ValuesImpl |
| 121 | 121 | { |
| 122 | - static public function values($context, $values){ |
|
| 122 | + static public function values($context, $values) { |
|
| 123 | 123 | $params = []; |
| 124 | 124 | $stubs = []; |
| 125 | - foreach ($values as $v){ |
|
| 126 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 127 | - $stubs[]=$v->get(); |
|
| 128 | - }else{ |
|
| 129 | - $stubs[]='?'; |
|
| 125 | + foreach ($values as $v) { |
|
| 126 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 127 | + $stubs[] = $v->get(); |
|
| 128 | + }else { |
|
| 129 | + $stubs[] = '?'; |
|
| 130 | 130 | $params[] = $v; |
| 131 | 131 | } |
| 132 | 132 | } |
| 133 | 133 | $stubs = implode(',', $stubs); |
| 134 | 134 | |
| 135 | - if(array_keys($values) === range(0, count($values) - 1)){ |
|
| 135 | + if (array_keys($values) === range(0, count($values) - 1)) { |
|
| 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 | - $columns = implode(',', array_map(function($k){return DB::wrap($k);}, array_keys($values))); |
|
| 142 | - $context->appendSql("($columns) VALUES($stubs)",false); |
|
| 141 | + $columns = implode(',', array_map(function($k) {return DB::wrap($k); }, array_keys($values))); |
|
| 142 | + $context->appendSql("($columns) VALUES($stubs)", false); |
|
| 143 | 143 | } |
| 144 | 144 | $context->appendParams($params); |
| 145 | 145 | } |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | |
| 149 | 149 | class UpdateImpl |
| 150 | 150 | { |
| 151 | - static public function update($context, $table){ |
|
| 151 | + static public function update($context, $table) { |
|
| 152 | 152 | $table = DB::wrap($table); |
| 153 | 153 | $context->appendSql("UPDATE $table"); |
| 154 | 154 | } |
@@ -156,81 +156,81 @@ discard block |
||
| 156 | 156 | |
| 157 | 157 | class UpdateSetImpl |
| 158 | 158 | { |
| 159 | - public function set(Context $context, $expr, $args){ |
|
| 160 | - if(is_string($expr)){ |
|
| 159 | + public function set(Context $context, $expr, $args) { |
|
| 160 | + if (is_string($expr)) { |
|
| 161 | 161 | return $this->setExpr($context, $expr, $args); |
| 162 | - }else{ |
|
| 162 | + }else { |
|
| 163 | 163 | return $this->setArgs($context, $expr); |
| 164 | 164 | } |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - public function setExpr(Context $context, $expr, $args){ |
|
| 168 | - if($this->first){ |
|
| 167 | + public function setExpr(Context $context, $expr, $args) { |
|
| 168 | + if ($this->first) { |
|
| 169 | 169 | $this->first = false; |
| 170 | 170 | $prefix = 'SET '; |
| 171 | - }else{ |
|
| 171 | + }else { |
|
| 172 | 172 | $prefix = ','; |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | - $context->appendSql("$prefix$expr",$prefix == 'SET '); |
|
| 175 | + $context->appendSql("$prefix$expr", $prefix == 'SET '); |
|
| 176 | 176 | $context->appendParams($args); |
| 177 | 177 | |
| 178 | 178 | } |
| 179 | - public function setArgs(Context $context, $values){ |
|
| 179 | + public function setArgs(Context $context, $values) { |
|
| 180 | 180 | $set = []; |
| 181 | 181 | $params = []; |
| 182 | - foreach ($values as $k=>$v){ |
|
| 182 | + foreach ($values as $k=>$v) { |
|
| 183 | 183 | $k = DB::wrap($k); |
| 184 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 185 | - $set[]= "$k=".$v->get(); |
|
| 186 | - }else{ |
|
| 187 | - $set[]= "$k=?"; |
|
| 188 | - $params[]=$v; |
|
| 184 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 185 | + $set[] = "$k=".$v->get(); |
|
| 186 | + }else { |
|
| 187 | + $set[] = "$k=?"; |
|
| 188 | + $params[] = $v; |
|
| 189 | 189 | } |
| 190 | 190 | } |
| 191 | - if($this->first){ |
|
| 191 | + if ($this->first) { |
|
| 192 | 192 | $this->first = false; |
| 193 | 193 | $context->appendSql('SET '.implode(',', $set)); |
| 194 | 194 | $context->appendParams($params); |
| 195 | - }else{ |
|
| 196 | - $context->appendSql(','.implode(',', $set),false); |
|
| 195 | + }else { |
|
| 196 | + $context->appendSql(','.implode(',', $set), false); |
|
| 197 | 197 | $context->appendParams($params); |
| 198 | 198 | } |
| 199 | 199 | } |
| 200 | - private $first=true; |
|
| 200 | + private $first = true; |
|
| 201 | 201 | } |
| 202 | 202 | class OrderByImpl |
| 203 | 203 | { |
| 204 | - public function orderByArgs(Context $context, $orders){ |
|
| 205 | - if(empty($orders)){ |
|
| 204 | + public function orderByArgs(Context $context, $orders) { |
|
| 205 | + if (empty($orders)) { |
|
| 206 | 206 | return $this; |
| 207 | 207 | } |
| 208 | 208 | $params = array(); |
| 209 | - foreach ($orders as $k=>$v){ |
|
| 210 | - if(is_integer($k)){ |
|
| 209 | + foreach ($orders as $k=>$v) { |
|
| 210 | + if (is_integer($k)) { |
|
| 211 | 211 | $params[] = DB::wrap($v); |
| 212 | - }else{ |
|
| 212 | + }else { |
|
| 213 | 213 | $k = DB::wrap($k); |
| 214 | 214 | |
| 215 | 215 | $v = strtoupper($v); |
| 216 | - ($v =='DESC' || $v =='ASC') or \PhpBoot\abort( new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
| 216 | + ($v == 'DESC' || $v == 'ASC') or \PhpBoot\abort(new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
| 217 | 217 | |
| 218 | 218 | $params[] = "$k $v"; |
| 219 | 219 | } |
| 220 | 220 | } |
| 221 | - if($this->first){ |
|
| 221 | + if ($this->first) { |
|
| 222 | 222 | $this->first = false; |
| 223 | 223 | $context->appendSql('ORDER BY '.implode(',', $params)); |
| 224 | - }else{ |
|
| 225 | - $context->appendSql(','.implode(',', $params),false); |
|
| 224 | + }else { |
|
| 225 | + $context->appendSql(','.implode(',', $params), false); |
|
| 226 | 226 | } |
| 227 | 227 | return $this; |
| 228 | 228 | } |
| 229 | - public function orderBy(Context $context, $column, $order=null){ |
|
| 230 | - if(is_string($column)){ |
|
| 231 | - if($order === null){ |
|
| 229 | + public function orderBy(Context $context, $column, $order = null) { |
|
| 230 | + if (is_string($column)) { |
|
| 231 | + if ($order === null) { |
|
| 232 | 232 | $column = [$column]; |
| 233 | - }else{ |
|
| 233 | + }else { |
|
| 234 | 234 | $column = [$column=>$order]; |
| 235 | 235 | } |
| 236 | 236 | } |
@@ -238,18 +238,18 @@ discard block |
||
| 238 | 238 | |
| 239 | 239 | |
| 240 | 240 | } |
| 241 | - private $first=true; |
|
| 241 | + private $first = true; |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | class LimitImpl |
| 245 | 245 | { |
| 246 | - static public function limit(Context $context, $size){ |
|
| 246 | + static public function limit(Context $context, $size) { |
|
| 247 | 247 | $intSize = intval($size); |
| 248 | 248 | strval($intSize) == $size or \PhpBoot\abort( |
| 249 | 249 | new \InvalidArgumentException("invalid params for limit($size)")); |
| 250 | 250 | $context->appendSql("LIMIT $size"); |
| 251 | 251 | } |
| 252 | - static public function limitWithOffset(Context $context, $start, $size){ |
|
| 252 | + static public function limitWithOffset(Context $context, $start, $size) { |
|
| 253 | 253 | $intStart = intval($start); |
| 254 | 254 | $intSize = intval($size); |
| 255 | 255 | strval($intStart) == $start && strval($intSize) == $size or \PhpBoot\abort( |
@@ -258,31 +258,31 @@ discard block |
||
| 258 | 258 | } |
| 259 | 259 | } |
| 260 | 260 | |
| 261 | -class WhereImpl{ |
|
| 261 | +class WhereImpl { |
|
| 262 | 262 | |
| 263 | - static private function findQ($str,$offset = 0,$no=0){ |
|
| 263 | + static private function findQ($str, $offset = 0, $no = 0) { |
|
| 264 | 264 | $found = strpos($str, '?', $offset); |
| 265 | - if($no == 0 || $found === false){ |
|
| 265 | + if ($no == 0 || $found === false) { |
|
| 266 | 266 | return $found; |
| 267 | 267 | } |
| 268 | - return self::findQ($str, $found+1, $no-1); |
|
| 268 | + return self::findQ($str, $found + 1, $no - 1); |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | - static public function where(Context $context, $prefix, $expr, $args){ |
|
| 272 | - if(empty($expr)){ |
|
| 271 | + static public function where(Context $context, $prefix, $expr, $args) { |
|
| 272 | + if (empty($expr)) { |
|
| 273 | 273 | return; |
| 274 | 274 | } |
| 275 | - if(is_callable($expr)){ |
|
| 276 | - self::conditionClosure($context,$prefix, $expr); |
|
| 277 | - }elseif (is_string($expr)){ |
|
| 275 | + if (is_callable($expr)) { |
|
| 276 | + self::conditionClosure($context, $prefix, $expr); |
|
| 277 | + }elseif (is_string($expr)) { |
|
| 278 | 278 | self::condition($context, $prefix, $expr, $args); |
| 279 | - }else{ |
|
| 279 | + }else { |
|
| 280 | 280 | self::conditionArgs($context, $prefix, $expr); |
| 281 | 281 | } |
| 282 | 282 | |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | - static public function conditionClosure(Context $context, $prefix, callable $callback){ |
|
| 285 | + static public function conditionClosure(Context $context, $prefix, callable $callback) { |
|
| 286 | 286 | $context->appendSql($prefix.' ('); |
| 287 | 287 | $callback($context); |
| 288 | 288 | $context->appendSql(')'); |
@@ -308,15 +308,15 @@ discard block |
||
| 308 | 308 | * NOT IN 'id'=>['NOT IN' => [1,2,3]] |
| 309 | 309 | * @return void |
| 310 | 310 | */ |
| 311 | - static public function conditionArgs(Context $context, $prefix, $args=[]){ |
|
| 312 | - if($args ===null){ |
|
| 313 | - return ; |
|
| 311 | + static public function conditionArgs(Context $context, $prefix, $args = []) { |
|
| 312 | + if ($args === null) { |
|
| 313 | + return; |
|
| 314 | 314 | } |
| 315 | 315 | $exprs = array(); |
| 316 | 316 | $params = array(); |
| 317 | - foreach ($args as $k => $v){ |
|
| 317 | + foreach ($args as $k => $v) { |
|
| 318 | 318 | $k = DB::wrap($k); |
| 319 | - if(is_array($v)){ |
|
| 319 | + if (is_array($v)) { |
|
| 320 | 320 | $ops = ['=', '>', '<', '<>', '>=', '<=', 'IN', 'NOT IN', 'BETWEEN', 'LIKE']; |
| 321 | 321 | $op = array_keys($v)[0]; |
| 322 | 322 | $op = strtoupper($op); |
@@ -325,46 +325,46 @@ discard block |
||
| 325 | 325 | new \InvalidArgumentException("invalid param $op for whereArgs")); |
| 326 | 326 | |
| 327 | 327 | $var = array_values($v)[0]; |
| 328 | - if($op == 'IN' || $op == 'NOT IN'){ |
|
| 328 | + if ($op == 'IN' || $op == 'NOT IN') { |
|
| 329 | 329 | $stubs = []; |
| 330 | - foreach ($var as $i){ |
|
| 331 | - if(is_a($i, Raw::class)){ |
|
| 332 | - $stubs[]=strval($i); |
|
| 333 | - }else{ |
|
| 334 | - $stubs[]='?'; |
|
| 330 | + foreach ($var as $i) { |
|
| 331 | + if (is_a($i, Raw::class)) { |
|
| 332 | + $stubs[] = strval($i); |
|
| 333 | + }else { |
|
| 334 | + $stubs[] = '?'; |
|
| 335 | 335 | $params[] = $i; |
| 336 | 336 | } |
| 337 | 337 | } |
| 338 | 338 | $stubs = implode(',', $stubs); |
| 339 | 339 | $exprs[] = "$k $op ($stubs)"; |
| 340 | - }else if($op == 'BETWEEN'){ |
|
| 340 | + }else if ($op == 'BETWEEN') { |
|
| 341 | 341 | $cond = "$k BETWEEN"; |
| 342 | - if(is_a($var[0], Raw::class)){ |
|
| 342 | + if (is_a($var[0], Raw::class)) { |
|
| 343 | 343 | $cond = "$cond ".strval($var[0]); |
| 344 | - }else{ |
|
| 344 | + }else { |
|
| 345 | 345 | $cond = "$cond ?"; |
| 346 | 346 | $params[] = $var[0]; |
| 347 | 347 | } |
| 348 | - if(is_a($var[1], Raw::class)){ |
|
| 348 | + if (is_a($var[1], Raw::class)) { |
|
| 349 | 349 | $cond = "$cond AND ".strval($var[1]); |
| 350 | - }else{ |
|
| 350 | + }else { |
|
| 351 | 351 | $cond = "$cond AND ?"; |
| 352 | 352 | $params[] = $var[1]; |
| 353 | 353 | } |
| 354 | 354 | $exprs[] = $cond; |
| 355 | - }else{ |
|
| 356 | - if(is_a($var, Raw::class)){ |
|
| 355 | + }else { |
|
| 356 | + if (is_a($var, Raw::class)) { |
|
| 357 | 357 | $exprs[] = "$k $op ".strval($var); |
| 358 | - }else{ |
|
| 358 | + }else { |
|
| 359 | 359 | $exprs[] = "$k $op ?"; |
| 360 | 360 | $params[] = $var; |
| 361 | 361 | } |
| 362 | 362 | } |
| 363 | - }else{ |
|
| 364 | - if(is_a($v, Raw::class)){ |
|
| 363 | + }else { |
|
| 364 | + if (is_a($v, Raw::class)) { |
|
| 365 | 365 | $exprs[] = "$k = ".strval($v); |
| 366 | 366 | |
| 367 | - }else{ |
|
| 367 | + }else { |
|
| 368 | 368 | $exprs[] = "$k = ?"; |
| 369 | 369 | $params[] = $v; |
| 370 | 370 | } |
@@ -373,21 +373,21 @@ discard block |
||
| 373 | 373 | |
| 374 | 374 | self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
| 375 | 375 | } |
| 376 | - static public function condition(Context $context, $prefix, $expr, $args){ |
|
| 377 | - if(!empty($expr)){ |
|
| 376 | + static public function condition(Context $context, $prefix, $expr, $args) { |
|
| 377 | + if (!empty($expr)) { |
|
| 378 | 378 | $expr = "($expr)"; |
| 379 | - if($args){ |
|
| 379 | + if ($args) { |
|
| 380 | 380 | //因为PDO不支持绑定数组变量, 这里需要手动展开数组 |
| 381 | 381 | //也就是说把 where("id IN(?)", [1,2]) 展开成 where("id IN(?,?)", 1,2) |
| 382 | 382 | $cutted = null; |
| 383 | 383 | $cut = null; |
| 384 | 384 | $toReplace = array(); |
| 385 | 385 | |
| 386 | - $newArgs=array(); |
|
| 386 | + $newArgs = array(); |
|
| 387 | 387 | //找到所有数组对应的?符位置 |
| 388 | - foreach ($args as $k =>$arg){ |
|
| 389 | - if(is_array($arg) || is_a($arg, Raw::class)){ |
|
| 390 | - if(!$cutted){ |
|
| 388 | + foreach ($args as $k =>$arg) { |
|
| 389 | + if (is_array($arg) || is_a($arg, Raw::class)) { |
|
| 390 | + if (!$cutted) { |
|
| 391 | 391 | $cut = new NestedStringCut($expr); |
| 392 | 392 | $cutted = $cut->getText(); |
| 393 | 393 | } |
@@ -397,51 +397,51 @@ discard block |
||
| 397 | 397 | $pos !== false or \PhpBoot\abort( |
| 398 | 398 | new \InvalidArgumentException("unmatched params and ? @ $expr")); |
| 399 | 399 | |
| 400 | - if(is_array($arg)){ |
|
| 400 | + if (is_array($arg)) { |
|
| 401 | 401 | $stubs = []; |
| 402 | - foreach ($arg as $i){ |
|
| 403 | - if(is_a($i, Raw::class)){ |
|
| 402 | + foreach ($arg as $i) { |
|
| 403 | + if (is_a($i, Raw::class)) { |
|
| 404 | 404 | $stubs[] = strval($i); |
| 405 | - }else{ |
|
| 405 | + }else { |
|
| 406 | 406 | $stubs[] = '?'; |
| 407 | 407 | $newArgs[] = $i; |
| 408 | 408 | } |
| 409 | 409 | } |
| 410 | 410 | $stubs = implode(',', $stubs); |
| 411 | - }else{ |
|
| 411 | + }else { |
|
| 412 | 412 | $stubs = strval($arg); |
| 413 | 413 | } |
| 414 | 414 | $toReplace[] = [$pos, $stubs]; |
| 415 | 415 | |
| 416 | - }else{ |
|
| 417 | - $newArgs[]=$arg; |
|
| 416 | + }else { |
|
| 417 | + $newArgs[] = $arg; |
|
| 418 | 418 | } |
| 419 | 419 | } |
| 420 | 420 | |
| 421 | - if(count($toReplace)){ |
|
| 421 | + if (count($toReplace)) { |
|
| 422 | 422 | $toReplace = array_reverse($toReplace); |
| 423 | - foreach ($toReplace as $i){ |
|
| 423 | + foreach ($toReplace as $i) { |
|
| 424 | 424 | list($pos, $v) = $i; |
| 425 | - $expr = substr($expr, 0, $pos).$v. substr($expr, $pos+1); |
|
| 425 | + $expr = substr($expr, 0, $pos).$v.substr($expr, $pos + 1); |
|
| 426 | 426 | } |
| 427 | 427 | $args = $newArgs; |
| 428 | 428 | } |
| 429 | 429 | } |
| 430 | - if($prefix){ |
|
| 430 | + if ($prefix) { |
|
| 431 | 431 | $context->appendSql($prefix.' '.$expr); |
| 432 | - }else{ |
|
| 432 | + }else { |
|
| 433 | 433 | $context->appendSql($expr); |
| 434 | 434 | } |
| 435 | 435 | |
| 436 | - if($args){ |
|
| 436 | + if ($args) { |
|
| 437 | 437 | $context->appendParams($args); |
| 438 | 438 | } |
| 439 | 439 | } |
| 440 | 440 | } |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | -class GroupByImpl{ |
|
| 444 | - static public function groupBy(Context $context, $column){ |
|
| 443 | +class GroupByImpl { |
|
| 444 | + static public function groupBy(Context $context, $column) { |
|
| 445 | 445 | $column = DB::wrap($column); |
| 446 | 446 | $context->appendSql("GROUP BY $column"); |
| 447 | 447 | } |
@@ -466,20 +466,20 @@ discard block |
||
| 466 | 466 | * @param string|false $asDict return as dict or array |
| 467 | 467 | * @return false|array |
| 468 | 468 | */ |
| 469 | - static public function get($context, $dictAs=false){ |
|
| 469 | + static public function get($context, $dictAs = false) { |
|
| 470 | 470 | |
| 471 | 471 | $st = $context->connection->prepare($context->sql); |
| 472 | - if($st->execute($context->params)){ |
|
| 472 | + if ($st->execute($context->params)) { |
|
| 473 | 473 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 474 | - if ($dictAs){ |
|
| 475 | - $dict= []; |
|
| 476 | - foreach ($res as $i){ |
|
| 477 | - $dict[$i[$dictAs]]=$i; |
|
| 474 | + if ($dictAs) { |
|
| 475 | + $dict = []; |
|
| 476 | + foreach ($res as $i) { |
|
| 477 | + $dict[$i[$dictAs]] = $i; |
|
| 478 | 478 | } |
| 479 | 479 | return $context->handleResult($dict); |
| 480 | 480 | } |
| 481 | 481 | return $context->handleResult($res); |
| 482 | - }else{ |
|
| 482 | + }else { |
|
| 483 | 483 | return false; |
| 484 | 484 | } |
| 485 | 485 | } |
@@ -488,22 +488,22 @@ discard block |
||
| 488 | 488 | * @param Context $context |
| 489 | 489 | * @return int|false |
| 490 | 490 | */ |
| 491 | - static public function count($context){ |
|
| 491 | + static public function count($context) { |
|
| 492 | 492 | |
| 493 | 493 | $found = []; |
| 494 | - if(!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 495 | - count($found)==0){ |
|
| 494 | + if (!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 495 | + count($found) == 0) { |
|
| 496 | 496 | \PhpBoot\abort(new \PDOException("can not use count(*) without select")); |
| 497 | 497 | } |
| 498 | 498 | list($chars, $columnBegin) = $found[0]; |
| 499 | - $columnBegin = $columnBegin + strlen('select')+1; |
|
| 499 | + $columnBegin = $columnBegin + strlen('select') + 1; |
|
| 500 | 500 | |
| 501 | 501 | $columnEnd = 0; |
| 502 | 502 | $found = []; |
| 503 | - if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 504 | - count($found)==0){ |
|
| 503 | + if (!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 504 | + count($found) == 0) { |
|
| 505 | 505 | $columnEnd = strlen($context->sql); |
| 506 | - }else{ |
|
| 506 | + }else { |
|
| 507 | 507 | list($chars, $columnEnd) = $found[0]; |
| 508 | 508 | } |
| 509 | 509 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -511,10 +511,10 @@ discard block |
||
| 511 | 511 | $sql .= substr($context->sql, $columnEnd); |
| 512 | 512 | |
| 513 | 513 | $st = $context->connection->prepare($sql); |
| 514 | - if($st->execute($context->params)){ |
|
| 514 | + if ($st->execute($context->params)) { |
|
| 515 | 515 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 516 | 516 | return $res[0]['count']; |
| 517 | - }else{ |
|
| 517 | + }else { |
|
| 518 | 518 | return false; |
| 519 | 519 | } |
| 520 | 520 | |
@@ -522,47 +522,47 @@ discard block |
||
| 522 | 522 | } |
| 523 | 523 | class OnDuplicateKeyUpdateImpl |
| 524 | 524 | { |
| 525 | - public function set($context, $column, $value){ |
|
| 526 | - if(is_string($column)){ |
|
| 525 | + public function set($context, $column, $value) { |
|
| 526 | + if (is_string($column)) { |
|
| 527 | 527 | $this->setExpr($context, $column, $value); |
| 528 | - }else{ |
|
| 528 | + }else { |
|
| 529 | 529 | $this->setArgs($context, $column); |
| 530 | 530 | } |
| 531 | 531 | } |
| 532 | 532 | |
| 533 | - public function setExpr($context, $expr, $args){ |
|
| 533 | + public function setExpr($context, $expr, $args) { |
|
| 534 | 534 | $prefix = ''; |
| 535 | - if($this->first){ |
|
| 535 | + if ($this->first) { |
|
| 536 | 536 | $this->first = false; |
| 537 | 537 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
| 538 | - }else{ |
|
| 538 | + }else { |
|
| 539 | 539 | $prefix = ','; |
| 540 | 540 | } |
| 541 | 541 | |
| 542 | - $context->appendSql("$prefix$expr",$prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 542 | + $context->appendSql("$prefix$expr", $prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 543 | 543 | $context->appendParams($args); |
| 544 | 544 | |
| 545 | 545 | } |
| 546 | - public function setArgs($context, $values){ |
|
| 546 | + public function setArgs($context, $values) { |
|
| 547 | 547 | $set = []; |
| 548 | 548 | $params = []; |
| 549 | - foreach ($values as $k=>$v){ |
|
| 549 | + foreach ($values as $k=>$v) { |
|
| 550 | 550 | $k = DB::wrap($k); |
| 551 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 552 | - $set[]= "$k=".$v->get(); |
|
| 553 | - }else{ |
|
| 554 | - $set[]= "$k=?"; |
|
| 555 | - $params[]=$v; |
|
| 551 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 552 | + $set[] = "$k=".$v->get(); |
|
| 553 | + }else { |
|
| 554 | + $set[] = "$k=?"; |
|
| 555 | + $params[] = $v; |
|
| 556 | 556 | } |
| 557 | 557 | } |
| 558 | - if($this->first){ |
|
| 558 | + if ($this->first) { |
|
| 559 | 559 | $this->first = false; |
| 560 | 560 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
| 561 | 561 | $context->appendParams($params); |
| 562 | - }else{ |
|
| 563 | - $context->appendSql(','.implode(',', $set),false); |
|
| 562 | + }else { |
|
| 563 | + $context->appendSql(','.implode(',', $set), false); |
|
| 564 | 564 | $context->appendParams($params); |
| 565 | 565 | } |
| 566 | 566 | } |
| 567 | - private $first=true; |
|
| 567 | + private $first = true; |
|
| 568 | 568 | } |
@@ -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); |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | public function set(Context $context, $expr, $args){ |
| 160 | 160 | if(is_string($expr)){ |
| 161 | 161 | return $this->setExpr($context, $expr, $args); |
| 162 | - }else{ |
|
| 162 | + } else{ |
|
| 163 | 163 | return $this->setArgs($context, $expr); |
| 164 | 164 | } |
| 165 | 165 | } |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | if($this->first){ |
| 169 | 169 | $this->first = false; |
| 170 | 170 | $prefix = 'SET '; |
| 171 | - }else{ |
|
| 171 | + } else{ |
|
| 172 | 172 | $prefix = ','; |
| 173 | 173 | } |
| 174 | 174 | |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | $k = DB::wrap($k); |
| 184 | 184 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
| 185 | 185 | $set[]= "$k=".$v->get(); |
| 186 | - }else{ |
|
| 186 | + } else{ |
|
| 187 | 187 | $set[]= "$k=?"; |
| 188 | 188 | $params[]=$v; |
| 189 | 189 | } |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | $this->first = false; |
| 193 | 193 | $context->appendSql('SET '.implode(',', $set)); |
| 194 | 194 | $context->appendParams($params); |
| 195 | - }else{ |
|
| 195 | + } else{ |
|
| 196 | 196 | $context->appendSql(','.implode(',', $set),false); |
| 197 | 197 | $context->appendParams($params); |
| 198 | 198 | } |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | foreach ($orders as $k=>$v){ |
| 210 | 210 | if(is_integer($k)){ |
| 211 | 211 | $params[] = DB::wrap($v); |
| 212 | - }else{ |
|
| 212 | + } else{ |
|
| 213 | 213 | $k = DB::wrap($k); |
| 214 | 214 | |
| 215 | 215 | $v = strtoupper($v); |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | if($this->first){ |
| 222 | 222 | $this->first = false; |
| 223 | 223 | $context->appendSql('ORDER BY '.implode(',', $params)); |
| 224 | - }else{ |
|
| 224 | + } else{ |
|
| 225 | 225 | $context->appendSql(','.implode(',', $params),false); |
| 226 | 226 | } |
| 227 | 227 | return $this; |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | if(is_string($column)){ |
| 231 | 231 | if($order === null){ |
| 232 | 232 | $column = [$column]; |
| 233 | - }else{ |
|
| 233 | + } else{ |
|
| 234 | 234 | $column = [$column=>$order]; |
| 235 | 235 | } |
| 236 | 236 | } |
@@ -274,9 +274,9 @@ discard block |
||
| 274 | 274 | } |
| 275 | 275 | if(is_callable($expr)){ |
| 276 | 276 | self::conditionClosure($context,$prefix, $expr); |
| 277 | - }elseif (is_string($expr)){ |
|
| 277 | + } elseif (is_string($expr)){ |
|
| 278 | 278 | self::condition($context, $prefix, $expr, $args); |
| 279 | - }else{ |
|
| 279 | + } else{ |
|
| 280 | 280 | self::conditionArgs($context, $prefix, $expr); |
| 281 | 281 | } |
| 282 | 282 | |
@@ -330,41 +330,41 @@ discard block |
||
| 330 | 330 | foreach ($var as $i){ |
| 331 | 331 | if(is_a($i, Raw::class)){ |
| 332 | 332 | $stubs[]=strval($i); |
| 333 | - }else{ |
|
| 333 | + } else{ |
|
| 334 | 334 | $stubs[]='?'; |
| 335 | 335 | $params[] = $i; |
| 336 | 336 | } |
| 337 | 337 | } |
| 338 | 338 | $stubs = implode(',', $stubs); |
| 339 | 339 | $exprs[] = "$k $op ($stubs)"; |
| 340 | - }else if($op == 'BETWEEN'){ |
|
| 340 | + } else if($op == 'BETWEEN'){ |
|
| 341 | 341 | $cond = "$k BETWEEN"; |
| 342 | 342 | if(is_a($var[0], Raw::class)){ |
| 343 | 343 | $cond = "$cond ".strval($var[0]); |
| 344 | - }else{ |
|
| 344 | + } else{ |
|
| 345 | 345 | $cond = "$cond ?"; |
| 346 | 346 | $params[] = $var[0]; |
| 347 | 347 | } |
| 348 | 348 | if(is_a($var[1], Raw::class)){ |
| 349 | 349 | $cond = "$cond AND ".strval($var[1]); |
| 350 | - }else{ |
|
| 350 | + } else{ |
|
| 351 | 351 | $cond = "$cond AND ?"; |
| 352 | 352 | $params[] = $var[1]; |
| 353 | 353 | } |
| 354 | 354 | $exprs[] = $cond; |
| 355 | - }else{ |
|
| 355 | + } else{ |
|
| 356 | 356 | if(is_a($var, Raw::class)){ |
| 357 | 357 | $exprs[] = "$k $op ".strval($var); |
| 358 | - }else{ |
|
| 358 | + } else{ |
|
| 359 | 359 | $exprs[] = "$k $op ?"; |
| 360 | 360 | $params[] = $var; |
| 361 | 361 | } |
| 362 | 362 | } |
| 363 | - }else{ |
|
| 363 | + } else{ |
|
| 364 | 364 | if(is_a($v, Raw::class)){ |
| 365 | 365 | $exprs[] = "$k = ".strval($v); |
| 366 | 366 | |
| 367 | - }else{ |
|
| 367 | + } else{ |
|
| 368 | 368 | $exprs[] = "$k = ?"; |
| 369 | 369 | $params[] = $v; |
| 370 | 370 | } |
@@ -402,18 +402,18 @@ discard block |
||
| 402 | 402 | foreach ($arg as $i){ |
| 403 | 403 | if(is_a($i, Raw::class)){ |
| 404 | 404 | $stubs[] = strval($i); |
| 405 | - }else{ |
|
| 405 | + } else{ |
|
| 406 | 406 | $stubs[] = '?'; |
| 407 | 407 | $newArgs[] = $i; |
| 408 | 408 | } |
| 409 | 409 | } |
| 410 | 410 | $stubs = implode(',', $stubs); |
| 411 | - }else{ |
|
| 411 | + } else{ |
|
| 412 | 412 | $stubs = strval($arg); |
| 413 | 413 | } |
| 414 | 414 | $toReplace[] = [$pos, $stubs]; |
| 415 | 415 | |
| 416 | - }else{ |
|
| 416 | + } else{ |
|
| 417 | 417 | $newArgs[]=$arg; |
| 418 | 418 | } |
| 419 | 419 | } |
@@ -429,7 +429,7 @@ discard block |
||
| 429 | 429 | } |
| 430 | 430 | if($prefix){ |
| 431 | 431 | $context->appendSql($prefix.' '.$expr); |
| 432 | - }else{ |
|
| 432 | + } else{ |
|
| 433 | 433 | $context->appendSql($expr); |
| 434 | 434 | } |
| 435 | 435 | |
@@ -479,7 +479,7 @@ discard block |
||
| 479 | 479 | return $context->handleResult($dict); |
| 480 | 480 | } |
| 481 | 481 | return $context->handleResult($res); |
| 482 | - }else{ |
|
| 482 | + } else{ |
|
| 483 | 483 | return false; |
| 484 | 484 | } |
| 485 | 485 | } |
@@ -503,7 +503,7 @@ discard block |
||
| 503 | 503 | if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
| 504 | 504 | count($found)==0){ |
| 505 | 505 | $columnEnd = strlen($context->sql); |
| 506 | - }else{ |
|
| 506 | + } else{ |
|
| 507 | 507 | list($chars, $columnEnd) = $found[0]; |
| 508 | 508 | } |
| 509 | 509 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -514,7 +514,7 @@ discard block |
||
| 514 | 514 | if($st->execute($context->params)){ |
| 515 | 515 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 516 | 516 | return $res[0]['count']; |
| 517 | - }else{ |
|
| 517 | + } else{ |
|
| 518 | 518 | return false; |
| 519 | 519 | } |
| 520 | 520 | |
@@ -525,7 +525,7 @@ discard block |
||
| 525 | 525 | public function set($context, $column, $value){ |
| 526 | 526 | if(is_string($column)){ |
| 527 | 527 | $this->setExpr($context, $column, $value); |
| 528 | - }else{ |
|
| 528 | + } else{ |
|
| 529 | 529 | $this->setArgs($context, $column); |
| 530 | 530 | } |
| 531 | 531 | } |
@@ -535,7 +535,7 @@ discard block |
||
| 535 | 535 | if($this->first){ |
| 536 | 536 | $this->first = false; |
| 537 | 537 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
| 538 | - }else{ |
|
| 538 | + } else{ |
|
| 539 | 539 | $prefix = ','; |
| 540 | 540 | } |
| 541 | 541 | |
@@ -550,7 +550,7 @@ discard block |
||
| 550 | 550 | $k = DB::wrap($k); |
| 551 | 551 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
| 552 | 552 | $set[]= "$k=".$v->get(); |
| 553 | - }else{ |
|
| 553 | + } else{ |
|
| 554 | 554 | $set[]= "$k=?"; |
| 555 | 555 | $params[]=$v; |
| 556 | 556 | } |
@@ -559,7 +559,7 @@ discard block |
||
| 559 | 559 | $this->first = false; |
| 560 | 560 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
| 561 | 561 | $context->appendParams($params); |
| 562 | - }else{ |
|
| 562 | + } else{ |
|
| 563 | 563 | $context->appendSql(','.implode(',', $set),false); |
| 564 | 564 | $context->appendParams($params); |
| 565 | 565 | } |
@@ -48,6 +48,9 @@ |
||
| 48 | 48 | |
| 49 | 49 | class OrderByRule extends LimitRule |
| 50 | 50 | { |
| 51 | + /** |
|
| 52 | + * @param Context $context |
|
| 53 | + */ |
|
| 51 | 54 | public function __construct($context){ |
| 52 | 55 | parent::__construct($context); |
| 53 | 56 | $this->impl = new OrderByImpl(); |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | class BasicRule |
| 13 | 13 | { |
| 14 | - public function __construct(Context $context){ |
|
| 14 | + public function __construct(Context $context) { |
|
| 15 | 15 | $this->context = $context; |
| 16 | 16 | } |
| 17 | 17 | |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | |
| 49 | 49 | class OrderByRule extends LimitRule |
| 50 | 50 | { |
| 51 | - public function __construct($context){ |
|
| 51 | + public function __construct($context) { |
|
| 52 | 52 | parent::__construct($context); |
| 53 | 53 | $this->impl = new OrderByImpl(); |
| 54 | 54 | } |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | * |
| 64 | 64 | * @return \PhpBoot\DB\rules\basic\LimitRule |
| 65 | 65 | */ |
| 66 | - public function orderBy($column, $order=null) { |
|
| 66 | + public function orderBy($column, $order = null) { |
|
| 67 | 67 | $this->impl->orderBy($this->context, $column, $order); |
| 68 | 68 | return new LimitRule($this->context); |
| 69 | 69 | } |
@@ -95,17 +95,17 @@ discard block |
||
| 95 | 95 | * @param mixed $_ |
| 96 | 96 | * @return WhereRule |
| 97 | 97 | */ |
| 98 | - public function where($conditions=null, $_=null) { |
|
| 99 | - if(is_callable($conditions)){ |
|
| 100 | - $callback = function ($context)use($conditions){ |
|
| 98 | + public function where($conditions = null, $_ = null) { |
|
| 99 | + if (is_callable($conditions)) { |
|
| 100 | + $callback = function($context)use($conditions){ |
|
| 101 | 101 | $rule = new SubQuery($context); |
| 102 | 102 | $conditions($rule); |
| 103 | 103 | }; |
| 104 | 104 | $conditions = $callback; |
| 105 | 105 | } |
| 106 | - if($this->isTheFirst){ |
|
| 107 | - WhereImpl::where($this->context, 'WHERE' ,$conditions, array_slice(func_get_args(), 1)); |
|
| 108 | - }else{ |
|
| 106 | + if ($this->isTheFirst) { |
|
| 107 | + WhereImpl::where($this->context, 'WHERE', $conditions, array_slice(func_get_args(), 1)); |
|
| 108 | + }else { |
|
| 109 | 109 | WhereImpl::where($this->context, 'AND', $conditions, array_slice(func_get_args(), 1)); |
| 110 | 110 | } |
| 111 | 111 | return new WhereRule($this->context, false); |
@@ -128,9 +128,9 @@ discard block |
||
| 128 | 128 | * @param mixed $_ |
| 129 | 129 | * @return WhereRule |
| 130 | 130 | */ |
| 131 | - public function orWhere($conditions=null, $_=null) { |
|
| 132 | - if(is_callable($conditions)){ |
|
| 133 | - $callback = function ($context)use($conditions){ |
|
| 131 | + public function orWhere($conditions = null, $_ = null) { |
|
| 132 | + if (is_callable($conditions)) { |
|
| 133 | + $callback = function($context)use($conditions){ |
|
| 134 | 134 | $rule = new SubQuery($context); |
| 135 | 135 | $conditions($rule); |
| 136 | 136 | }; |
@@ -156,17 +156,17 @@ discard block |
||
| 156 | 156 | * @param null $_ |
| 157 | 157 | * @return SubQuery |
| 158 | 158 | */ |
| 159 | - public function where($expr, $_= null){ |
|
| 160 | - if(is_callable($expr)){ |
|
| 161 | - $callback = function ($context)use($expr){ |
|
| 159 | + public function where($expr, $_ = null) { |
|
| 160 | + if (is_callable($expr)) { |
|
| 161 | + $callback = function($context)use($expr){ |
|
| 162 | 162 | $rule = new SubQuery($context, true); |
| 163 | 163 | $expr($rule); |
| 164 | 164 | }; |
| 165 | 165 | $expr = $callback; |
| 166 | 166 | } |
| 167 | - if($this->isTheFirst){ |
|
| 167 | + if ($this->isTheFirst) { |
|
| 168 | 168 | WhereImpl::where($this->context, '', $expr, array_slice(func_get_args(), 1)); |
| 169 | - }else{ |
|
| 169 | + }else { |
|
| 170 | 170 | WhereImpl::where($this->context, 'AND', $expr, array_slice(func_get_args(), 1)); |
| 171 | 171 | } |
| 172 | 172 | return new SubQuery($this->context, false); |
@@ -176,9 +176,9 @@ discard block |
||
| 176 | 176 | * @param null $_ |
| 177 | 177 | * @return SubQuery |
| 178 | 178 | */ |
| 179 | - public function orWhere($expr, $_= null){ |
|
| 180 | - if(is_callable($expr)){ |
|
| 181 | - $callback = function ($context)use($expr){ |
|
| 179 | + public function orWhere($expr, $_ = null) { |
|
| 180 | + if (is_callable($expr)) { |
|
| 181 | + $callback = function($context)use($expr){ |
|
| 182 | 182 | $rule = new SubQuery($context, true); |
| 183 | 183 | $expr($rule); |
| 184 | 184 | }; |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | } |
| 106 | 106 | if($this->isTheFirst){ |
| 107 | 107 | WhereImpl::where($this->context, 'WHERE' ,$conditions, array_slice(func_get_args(), 1)); |
| 108 | - }else{ |
|
| 108 | + } else{ |
|
| 109 | 109 | WhereImpl::where($this->context, 'AND', $conditions, array_slice(func_get_args(), 1)); |
| 110 | 110 | } |
| 111 | 111 | return new WhereRule($this->context, false); |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | } |
| 167 | 167 | if($this->isTheFirst){ |
| 168 | 168 | WhereImpl::where($this->context, '', $expr, array_slice(func_get_args(), 1)); |
| 169 | - }else{ |
|
| 169 | + } else{ |
|
| 170 | 170 | WhereImpl::where($this->context, 'AND', $expr, array_slice(func_get_args(), 1)); |
| 171 | 171 | } |
| 172 | 172 | return new SubQuery($this->context, false); |
@@ -113,6 +113,9 @@ discard block |
||
| 113 | 113 | |
| 114 | 114 | class OrderByRule extends LimitRule |
| 115 | 115 | { |
| 116 | + /** |
|
| 117 | + * @param Context $context |
|
| 118 | + */ |
|
| 116 | 119 | public function __construct($context){ |
| 117 | 120 | parent::__construct($context); |
| 118 | 121 | $this->order = new OrderByImpl(); |
@@ -260,7 +263,7 @@ discard block |
||
| 260 | 263 | * "WHERE a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1" |
| 261 | 264 | * |
| 262 | 265 | * @param string|array|callable $conditions |
| 263 | - * @param mixed $_ |
|
| 266 | + * @param string $_ |
|
| 264 | 267 | * @return \PhpBoot\DB\rules\select\WhereRule |
| 265 | 268 | */ |
| 266 | 269 | public function where($conditions=null, $_=null) { |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | * @param string|false $asDict |
| 40 | 40 | * @return array |
| 41 | 41 | */ |
| 42 | - public function get($asDict=false) { |
|
| 42 | + public function get($asDict = false) { |
|
| 43 | 43 | return ExecImpl::get($this->context, $asDict); |
| 44 | 44 | } |
| 45 | 45 | |
@@ -53,9 +53,9 @@ discard block |
||
| 53 | 53 | * Execute sql and get one response |
| 54 | 54 | * @return null |
| 55 | 55 | */ |
| 56 | - public function getFirst(){ |
|
| 56 | + public function getFirst() { |
|
| 57 | 57 | $res = ExecImpl::get($this->context); |
| 58 | - if(count($res)){ |
|
| 58 | + if (count($res)) { |
|
| 59 | 59 | return $res[0]; |
| 60 | 60 | } |
| 61 | 61 | return null; |
@@ -68,8 +68,8 @@ discard block |
||
| 68 | 68 | * @param string $table |
| 69 | 69 | * @return \PhpBoot\DB\rules\select\JoinRule |
| 70 | 70 | */ |
| 71 | - public function from($table, $as=null){ |
|
| 72 | - FromImpl::from($this->context, $table,$as); |
|
| 71 | + public function from($table, $as = null) { |
|
| 72 | + FromImpl::from($this->context, $table, $as); |
|
| 73 | 73 | return new JoinRule($this->context); |
| 74 | 74 | } |
| 75 | 75 | } |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | * @param string $column |
| 81 | 81 | * @return \PhpBoot\DB\rules\select\GetRule |
| 82 | 82 | */ |
| 83 | - public function of($column){ |
|
| 83 | + public function of($column) { |
|
| 84 | 84 | ForUpdateOfImpl::of($this->context, $column); |
| 85 | 85 | return new GetRule($this->context); |
| 86 | 86 | } |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * forUpdate() => 'FOR UPDATE' |
| 92 | 92 | * @return \PhpBoot\DB\rules\select\ForUpdateOfRule |
| 93 | 93 | */ |
| 94 | - public function forUpdate(){ |
|
| 94 | + public function forUpdate() { |
|
| 95 | 95 | ForUpdateImpl::forUpdate($this->context); |
| 96 | 96 | return new ForUpdateOfRule($this->context); |
| 97 | 97 | } |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | |
| 114 | 114 | class OrderByRule extends LimitRule |
| 115 | 115 | { |
| 116 | - public function __construct($context){ |
|
| 116 | + public function __construct($context) { |
|
| 117 | 117 | parent::__construct($context); |
| 118 | 118 | $this->order = new OrderByImpl(); |
| 119 | 119 | } |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | * @param string $order Sql::ORDER_BY_ASC or Sql::ORDER_BY_DESC |
| 129 | 129 | * @return \PhpBoot\DB\rules\select\OrderByRule |
| 130 | 130 | */ |
| 131 | - public function orderBy($column, $order=null) { |
|
| 131 | + public function orderBy($column, $order = null) { |
|
| 132 | 132 | $this->order->orderBy($this->context, $column, $order); |
| 133 | 133 | return $this; |
| 134 | 134 | } |
@@ -174,17 +174,17 @@ discard block |
||
| 174 | 174 | * @param string $_ |
| 175 | 175 | * @return \PhpBoot\DB\rules\select\HavingRule |
| 176 | 176 | */ |
| 177 | - public function having($expr, $_=null) { |
|
| 178 | - if(is_callable($expr)){ |
|
| 179 | - $callback = function ($context)use($expr){ |
|
| 177 | + public function having($expr, $_ = null) { |
|
| 178 | + if (is_callable($expr)) { |
|
| 179 | + $callback = function($context)use($expr){ |
|
| 180 | 180 | $rule = new SubQuery($context); |
| 181 | 181 | $expr($rule); |
| 182 | 182 | }; |
| 183 | 183 | $expr = $callback; |
| 184 | 184 | } |
| 185 | - if($this->isTheFirst){ |
|
| 185 | + if ($this->isTheFirst) { |
|
| 186 | 186 | WhereImpl::where($this->context, 'HAVING', $expr, array_slice(func_get_args(), 1)); |
| 187 | - }else{ |
|
| 187 | + }else { |
|
| 188 | 188 | WhereImpl::where($this->context, 'AND', $expr, array_slice(func_get_args(), 1)); |
| 189 | 189 | } |
| 190 | 190 | |
@@ -211,9 +211,9 @@ discard block |
||
| 211 | 211 | * @param string $_ |
| 212 | 212 | * @return \PhpBoot\DB\rules\select\HavingRule |
| 213 | 213 | */ |
| 214 | - public function orHaving($expr, $_=null) { |
|
| 215 | - if(is_callable($expr)){ |
|
| 216 | - $callback = function ($context)use($expr){ |
|
| 214 | + public function orHaving($expr, $_ = null) { |
|
| 215 | + if (is_callable($expr)) { |
|
| 216 | + $callback = function($context)use($expr){ |
|
| 217 | 217 | $rule = new SubQuery($context); |
| 218 | 218 | $expr($rule); |
| 219 | 219 | }; |
@@ -263,17 +263,17 @@ discard block |
||
| 263 | 263 | * @param mixed $_ |
| 264 | 264 | * @return \PhpBoot\DB\rules\select\WhereRule |
| 265 | 265 | */ |
| 266 | - public function where($conditions=null, $_=null) { |
|
| 267 | - if(is_callable($conditions)){ |
|
| 268 | - $callback = function ($context)use($conditions){ |
|
| 266 | + public function where($conditions = null, $_ = null) { |
|
| 267 | + if (is_callable($conditions)) { |
|
| 268 | + $callback = function($context)use($conditions){ |
|
| 269 | 269 | $rule = new SubQuery($context); |
| 270 | 270 | $conditions($rule); |
| 271 | 271 | }; |
| 272 | 272 | $conditions = $callback; |
| 273 | 273 | } |
| 274 | - if($this->isTheFirst){ |
|
| 275 | - WhereImpl::where($this->context, 'WHERE' ,$conditions, array_slice(func_get_args(), 1)); |
|
| 276 | - }else{ |
|
| 274 | + if ($this->isTheFirst) { |
|
| 275 | + WhereImpl::where($this->context, 'WHERE', $conditions, array_slice(func_get_args(), 1)); |
|
| 276 | + }else { |
|
| 277 | 277 | WhereImpl::where($this->context, 'AND', $conditions, array_slice(func_get_args(), 1)); |
| 278 | 278 | } |
| 279 | 279 | return new WhereRule($this->context, false); |
@@ -296,9 +296,9 @@ discard block |
||
| 296 | 296 | * @param mixed $_ |
| 297 | 297 | * @return \PhpBoot\DB\rules\select\WhereRule |
| 298 | 298 | */ |
| 299 | - public function orWhere($conditions=null, $_=null) { |
|
| 300 | - if(is_callable($conditions)){ |
|
| 301 | - $callback = function ($context)use($conditions){ |
|
| 299 | + public function orWhere($conditions = null, $_ = null) { |
|
| 300 | + if (is_callable($conditions)) { |
|
| 301 | + $callback = function($context)use($conditions){ |
|
| 302 | 302 | $rule = new SubQuery($context); |
| 303 | 303 | $conditions($rule); |
| 304 | 304 | }; |
@@ -317,8 +317,8 @@ discard block |
||
| 317 | 317 | * @param string $table |
| 318 | 318 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
| 319 | 319 | */ |
| 320 | - public function join($table){ |
|
| 321 | - JoinImpl::join($this->context,null, $table); |
|
| 320 | + public function join($table) { |
|
| 321 | + JoinImpl::join($this->context, null, $table); |
|
| 322 | 322 | return new JoinOnRule($this->context); |
| 323 | 323 | } |
| 324 | 324 | /** |
@@ -326,8 +326,8 @@ discard block |
||
| 326 | 326 | * @param string $table |
| 327 | 327 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
| 328 | 328 | */ |
| 329 | - public function leftJoin($table){ |
|
| 330 | - JoinImpl::join($this->context,'LEFT', $table); |
|
| 329 | + public function leftJoin($table) { |
|
| 330 | + JoinImpl::join($this->context, 'LEFT', $table); |
|
| 331 | 331 | return new JoinOnRule($this->context); |
| 332 | 332 | } |
| 333 | 333 | /** |
@@ -336,7 +336,7 @@ discard block |
||
| 336 | 336 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
| 337 | 337 | */ |
| 338 | 338 | public function rightJoin($table) { |
| 339 | - JoinImpl::join($this->context,'RIGHT', $table); |
|
| 339 | + JoinImpl::join($this->context, 'RIGHT', $table); |
|
| 340 | 340 | return new JoinOnRule($this->context); |
| 341 | 341 | } |
| 342 | 342 | /** |
@@ -345,7 +345,7 @@ discard block |
||
| 345 | 345 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
| 346 | 346 | */ |
| 347 | 347 | public function innerJoin($table) { |
| 348 | - JoinImpl::join($this->context,'INNER', $table); |
|
| 348 | + JoinImpl::join($this->context, 'INNER', $table); |
|
| 349 | 349 | return new JoinOnRule($this->context); |
| 350 | 350 | } |
| 351 | 351 | } |
@@ -357,7 +357,7 @@ discard block |
||
| 357 | 357 | * @param string $condition |
| 358 | 358 | * @return \PhpBoot\DB\rules\select\JoinRule |
| 359 | 359 | */ |
| 360 | - public function on($condition){ |
|
| 360 | + public function on($condition) { |
|
| 361 | 361 | JoinOnImpl::on($this->context, $condition); |
| 362 | 362 | return new JoinRule($this->context); |
| 363 | 363 | } |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | } |
| 185 | 185 | if($this->isTheFirst){ |
| 186 | 186 | WhereImpl::where($this->context, 'HAVING', $expr, array_slice(func_get_args(), 1)); |
| 187 | - }else{ |
|
| 187 | + } else{ |
|
| 188 | 188 | WhereImpl::where($this->context, 'AND', $expr, array_slice(func_get_args(), 1)); |
| 189 | 189 | } |
| 190 | 190 | |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | } |
| 274 | 274 | if($this->isTheFirst){ |
| 275 | 275 | WhereImpl::where($this->context, 'WHERE' ,$conditions, array_slice(func_get_args(), 1)); |
| 276 | - }else{ |
|
| 276 | + } else{ |
|
| 277 | 277 | WhereImpl::where($this->context, 'AND', $conditions, array_slice(func_get_args(), 1)); |
| 278 | 278 | } |
| 279 | 279 | return new WhereRule($this->context, false); |
@@ -24,6 +24,9 @@ |
||
| 24 | 24 | |
| 25 | 25 | class UpdateSetRule extends BasicRule |
| 26 | 26 | { |
| 27 | + /** |
|
| 28 | + * @param \PhpBoot\DB\Context $context |
|
| 29 | + */ |
|
| 27 | 30 | public function __construct($context){ |
| 28 | 31 | parent::__construct($context); |
| 29 | 32 | $this->impl = new UpdateSetImpl(); |
@@ -30,7 +30,6 @@ |
||
| 30 | 30 | } |
| 31 | 31 | /** |
| 32 | 32 | * update('table')->set(['a'=>1]) => "UPDATE table SET a=1" |
| 33 | - |
|
| 34 | 33 | * update('table')->set('a=?',1) => "UPDATE table SET a=1" |
| 35 | 34 | * @param array|string $expr |
| 36 | 35 | * @param mixed $_ |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | class UpdateSetRule extends BasicRule |
| 26 | 26 | { |
| 27 | - public function __construct($context){ |
|
| 27 | + public function __construct($context) { |
|
| 28 | 28 | parent::__construct($context); |
| 29 | 29 | $this->impl = new UpdateSetImpl(); |
| 30 | 30 | } |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | * @param mixed $_ |
| 37 | 37 | * @return WhereRule |
| 38 | 38 | */ |
| 39 | - public function set($expr, $_=null) { |
|
| 39 | + public function set($expr, $_ = null) { |
|
| 40 | 40 | $this->impl->set($this->context, $expr, array_slice(func_get_args(), 1)); |
| 41 | 41 | return new WhereRule($this->context); |
| 42 | 42 | } |
@@ -13,13 +13,13 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | public function __construct(array $threads, callable $waitAll) |
| 15 | 15 | { |
| 16 | - foreach ($threads as $thread){ |
|
| 16 | + foreach ($threads as $thread) { |
|
| 17 | 17 | $pos = count($this->threadResults); |
| 18 | - $this->threadResults[] = [null,null]; |
|
| 19 | - $this->threads[] = function ()use($thread, $pos){ |
|
| 20 | - try{ |
|
| 18 | + $this->threadResults[] = [null, null]; |
|
| 19 | + $this->threads[] = function()use($thread, $pos){ |
|
| 20 | + try { |
|
| 21 | 21 | $this->threadResults[$pos][0] = $thread(); |
| 22 | - }catch (\Exception $e){ |
|
| 22 | + }catch (\Exception $e) { |
|
| 23 | 23 | $this->threadResults[$pos][1] = $e; |
| 24 | 24 | } |
| 25 | 25 | }; |
@@ -29,25 +29,25 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | public function run() |
| 31 | 31 | { |
| 32 | - while ($thread = array_pop($this->threads)){ |
|
| 32 | + while ($thread = array_pop($this->threads)) { |
|
| 33 | 33 | $thread(); |
| 34 | 34 | }; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - public function wait($waitAble){ |
|
| 37 | + public function wait($waitAble) { |
|
| 38 | 38 | array_push($this->waits, $waitAble); |
| 39 | 39 | $this->run(); |
| 40 | 40 | |
| 41 | - if(count($this->waits)){ |
|
| 41 | + if (count($this->waits)) { |
|
| 42 | 42 | $waitAll = $this->waitAll; |
| 43 | 43 | $this->waitResults = $waitAll($this->waits); |
| 44 | 44 | $this->waits = []; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - $res = array_pop($this->waitResults); |
|
| 48 | - if(isset($res[1])){ |
|
| 47 | + $res = array_pop($this->waitResults); |
|
| 48 | + if (isset($res[1])) { |
|
| 49 | 49 | \PhpBoot\abort(new RpcException($res['reason'])); |
| 50 | - }else{ |
|
| 50 | + }else { |
|
| 51 | 51 | return $res[0]; |
| 52 | 52 | } |
| 53 | 53 | } |
@@ -93,11 +93,11 @@ discard block |
||
| 93 | 93 | |
| 94 | 94 | Request::class => \DI\factory([Application::class, 'createRequestFromGlobals']), |
| 95 | 95 | ]; |
| 96 | - if(function_exists('apc_fetch')){ |
|
| 96 | + if (function_exists('apc_fetch')) { |
|
| 97 | 97 | $default += [ |
| 98 | 98 | Cache::class => \DI\object(ApcCache::class) |
| 99 | 99 | ]; |
| 100 | - }else{ |
|
| 100 | + }else { |
|
| 101 | 101 | $default += [ |
| 102 | 102 | Cache::class => \DI\object(FilesystemCache::class) |
| 103 | 103 | ->constructorParameter('directory', sys_get_temp_dir()) |
@@ -139,11 +139,11 @@ discard block |
||
| 139 | 139 | * @param string[] $hooks hook class names |
| 140 | 140 | * @return void |
| 141 | 141 | */ |
| 142 | - public function loadRoutesFromClass($className, $hooks=[]) |
|
| 142 | + public function loadRoutesFromClass($className, $hooks = []) |
|
| 143 | 143 | { |
| 144 | 144 | $cache = new CheckableCache($this->cache); |
| 145 | 145 | |
| 146 | - $key = 'loadRoutesFromClass:' . md5(__CLASS__ . ':' . $className); |
|
| 146 | + $key = 'loadRoutesFromClass:'.md5(__CLASS__.':'.$className); |
|
| 147 | 147 | $routes = $cache->get($key, $this); |
| 148 | 148 | |
| 149 | 149 | $controller = null; |
@@ -160,9 +160,9 @@ discard block |
||
| 160 | 160 | $this->routes[] = [ |
| 161 | 161 | $method, |
| 162 | 162 | $uri, |
| 163 | - function (Application $app, Request $request) use ($cache, $className, $actionName, $controller) { |
|
| 163 | + function(Application $app, Request $request) use ($cache, $className, $actionName, $controller) { |
|
| 164 | 164 | |
| 165 | - $key = 'loadRoutesFromClass:route:' . md5(__CLASS__ . ':' . $className . ':' . $actionName); |
|
| 165 | + $key = 'loadRoutesFromClass:route:'.md5(__CLASS__.':'.$className.':'.$actionName); |
|
| 166 | 166 | |
| 167 | 167 | $routeInstance = $cache->get($key, $this); |
| 168 | 168 | if ($routeInstance == $this) { |
@@ -190,11 +190,11 @@ discard block |
||
| 190 | 190 | * @param string[] $hooks |
| 191 | 191 | * @return void |
| 192 | 192 | */ |
| 193 | - public function loadRoutesFromPath($fromPath, $namespace = '', $hooks=[]) |
|
| 193 | + public function loadRoutesFromPath($fromPath, $namespace = '', $hooks = []) |
|
| 194 | 194 | { |
| 195 | 195 | $dir = @dir($fromPath) or abort("dir $fromPath not exist"); |
| 196 | 196 | |
| 197 | - $getEach = function () use ($dir) { |
|
| 197 | + $getEach = function() use ($dir) { |
|
| 198 | 198 | $name = $dir->read(); |
| 199 | 199 | if (!$name) { |
| 200 | 200 | return $name; |
@@ -206,11 +206,11 @@ discard block |
||
| 206 | 206 | if ($entry == '.' || $entry == '..') { |
| 207 | 207 | continue; |
| 208 | 208 | } |
| 209 | - $path = $fromPath . '/' . str_replace('\\', '/', $entry); |
|
| 209 | + $path = $fromPath.'/'.str_replace('\\', '/', $entry); |
|
| 210 | 210 | if (is_file($path) && substr_compare($entry, '.php', strlen($entry) - 4, 4, true) == 0) { |
| 211 | - $class_name = $namespace . '\\' . substr($entry, 0, strlen($entry) - 4); |
|
| 211 | + $class_name = $namespace.'\\'.substr($entry, 0, strlen($entry) - 4); |
|
| 212 | 212 | $this->loadRoutesFromClass($class_name, $hooks); |
| 213 | - } else { |
|
| 213 | + }else { |
|
| 214 | 214 | //\Log::debug($path.' ignored'); |
| 215 | 215 | } |
| 216 | 216 | } |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | * @param callable $handler function(Application $app, Request $request):Response |
| 224 | 224 | * @param string[] $hooks |
| 225 | 225 | */ |
| 226 | - public function addRoute($method, $uri, callable $handler, $hooks=[]) |
|
| 226 | + public function addRoute($method, $uri, callable $handler, $hooks = []) |
|
| 227 | 227 | { |
| 228 | 228 | $this->routes[] = [$method, $uri, $handler, $hooks]; |
| 229 | 229 | } |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | { |
| 250 | 250 | // TODO 把 Route里的异常处理 ExceptionRenderer 移到这里更妥? |
| 251 | 251 | $renderer = $this->get(ExceptionRenderer::class); |
| 252 | - try{ |
|
| 252 | + try { |
|
| 253 | 253 | if ($request == null) { |
| 254 | 254 | $request = $this->make(Request::class); |
| 255 | 255 | } |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | } |
| 260 | 260 | $uri = rawurldecode($uri); |
| 261 | 261 | |
| 262 | - $next = function (Request $request)use($uri){ |
|
| 262 | + $next = function(Request $request)use($uri){ |
|
| 263 | 263 | $dispatcher = $this->getDispatcher(); |
| 264 | 264 | $res = $dispatcher->dispatch($request->getMethod(), $uri); |
| 265 | 265 | if ($res[0] == Dispatcher::FOUND) { |
@@ -268,10 +268,10 @@ discard block |
||
| 268 | 268 | $request->attributes->add($res[2]); |
| 269 | 269 | } |
| 270 | 270 | list($handler, $hooks) = $res[1]; |
| 271 | - $next = function (Request $request)use($handler){ |
|
| 271 | + $next = function(Request $request)use($handler){ |
|
| 272 | 272 | return $handler($this, $request); |
| 273 | 273 | }; |
| 274 | - foreach (array_reverse($hooks) as $hookName){ |
|
| 274 | + foreach (array_reverse($hooks) as $hookName) { |
|
| 275 | 275 | $next = function($request)use($hookName, $next){ |
| 276 | 276 | $hook = $this->get($hookName); |
| 277 | 277 | /**@var $hook HookInterface*/ |
@@ -284,12 +284,12 @@ discard block |
||
| 284 | 284 | \PhpBoot\abort(new NotFoundHttpException(), [$request->getMethod(), $uri]); |
| 285 | 285 | } elseif ($res[0] == Dispatcher::METHOD_NOT_ALLOWED) { |
| 286 | 286 | \PhpBoot\abort(new MethodNotAllowedHttpException($res[1]), [$request->getMethod(), $uri]); |
| 287 | - } else { |
|
| 287 | + }else { |
|
| 288 | 288 | \PhpBoot\abort("unknown dispatch return {$res[0]}"); |
| 289 | 289 | } |
| 290 | 290 | }; |
| 291 | 291 | |
| 292 | - foreach (array_reverse($this->getGlobalHooks()) as $hookName){ |
|
| 292 | + foreach (array_reverse($this->getGlobalHooks()) as $hookName) { |
|
| 293 | 293 | $next = function($request)use($hookName, $next){ |
| 294 | 294 | $hook = $this->get($hookName); |
| 295 | 295 | /**@var $hook HookInterface*/ |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | } |
| 305 | 305 | return $response; |
| 306 | 306 | |
| 307 | - }catch (\Exception $e){ |
|
| 307 | + }catch (\Exception $e) { |
|
| 308 | 308 | $renderer->render($e); |
| 309 | 309 | } |
| 310 | 310 | |