@@ -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 | } |
@@ -21,20 +21,20 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public function __invoke(ControllerContainer $container, $ann, EntityContainerBuilder $entityBuilder) |
23 | 23 | { |
24 | - if(!$ann->parent){ |
|
24 | + if (!$ann->parent) { |
|
25 | 25 | //Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()} should be used with parent route"); |
26 | 26 | return; |
27 | 27 | } |
28 | 28 | $target = $ann->parent->name; |
29 | 29 | $route = $container->getRoute($target); |
30 | - if(!$route){ |
|
30 | + if (!$route) { |
|
31 | 31 | //Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::$target should be used with parent route"); |
32 | - return ; |
|
32 | + return; |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | $params = new AnnotationParams($ann->description, 2); |
36 | 36 | $type = $doc = null; |
37 | - if(count($params)>0){ |
|
37 | + if (count($params)>0) { |
|
38 | 38 | $type = TypeHint::normalize($params[0], $container->getClassName()); |
39 | 39 | } |
40 | 40 | $doc = $params->getRawParam(1, ''); |
@@ -42,10 +42,10 @@ discard block |
||
42 | 42 | list($_, $meta) = $route |
43 | 43 | ->getResponseHandler() |
44 | 44 | ->getMappingBySource('return'); |
45 | - if($meta){ |
|
45 | + if ($meta) { |
|
46 | 46 | $meta->description = $doc; |
47 | 47 | $meta->type = $type; |
48 | - $meta->container = $type == 'void'?null:ContainerFactory::create($entityBuilder, $type); |
|
48 | + $meta->container = $type == 'void' ?null:ContainerFactory::create($entityBuilder, $type); |
|
49 | 49 | } |
50 | 50 | } |
51 | 51 | } |
52 | 52 | \ No newline at end of file |
@@ -55,12 +55,12 @@ |
||
55 | 55 | */ |
56 | 56 | public function rule($rule, $fields) |
57 | 57 | { |
58 | - if(is_string($rule)){ |
|
58 | + if (is_string($rule)) { |
|
59 | 59 | $rules = explode('|', $rule); |
60 | - foreach ($rules as $r){ |
|
60 | + foreach ($rules as $r) { |
|
61 | 61 | $params = explode(':', trim($r)); |
62 | 62 | $rule = $params[0]; |
63 | - $params = isset($params[1])?explode(',', $params[1]):[]; |
|
63 | + $params = isset($params[1]) ?explode(',', $params[1]) : []; |
|
64 | 64 | |
65 | 65 | call_user_func_array([$this, 'parent::rule'], array_merge([$rule, $fields], $params)); |
66 | 66 |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | * |
57 | 57 | * @author caoym <[email protected]> |
58 | 58 | */ |
59 | -class DB{ |
|
59 | +class DB { |
|
60 | 60 | |
61 | 61 | /** |
62 | 62 | * DB constructor. |
@@ -97,18 +97,18 @@ discard block |
||
97 | 97 | * @param string $column0 |
98 | 98 | * @return \PhpBoot\DB\rules\select\FromRule |
99 | 99 | */ |
100 | - function select($column0=null, $_=null){ |
|
100 | + function select($column0 = null, $_ = null) { |
|
101 | 101 | $obj = new SelectRule(new Context($this->connection)); |
102 | - if($column0 == null){ |
|
102 | + if ($column0 == null) { |
|
103 | 103 | $args = ['*']; |
104 | - }elseif(is_array($column0)){ |
|
104 | + }elseif (is_array($column0)) { |
|
105 | 105 | $args = $column0; |
106 | - }else{ |
|
106 | + }else { |
|
107 | 107 | $args = func_get_args(); |
108 | 108 | } |
109 | - foreach ($args as &$arg){ |
|
109 | + foreach ($args as &$arg) { |
|
110 | 110 | $arg = DB::wrap($arg); |
111 | - if($arg == '*'){ |
|
111 | + if ($arg == '*') { |
|
112 | 112 | continue; |
113 | 113 | } |
114 | 114 | |
@@ -140,8 +140,8 @@ discard block |
||
140 | 140 | * @param string $table |
141 | 141 | * @return \PhpBoot\DB\rules\basic\WhereRule |
142 | 142 | */ |
143 | - public function deleteFrom($table){ |
|
144 | - $obj = new DeleteRule(new Context($this->connection)); |
|
143 | + public function deleteFrom($table) { |
|
144 | + $obj = new DeleteRule(new Context($this->connection)); |
|
145 | 145 | return $obj->deleteFrom($table); |
146 | 146 | } |
147 | 147 | /** |
@@ -149,8 +149,8 @@ discard block |
||
149 | 149 | * @param string $table |
150 | 150 | * @return \PhpBoot\DB\rules\replace\ValuesRule |
151 | 151 | */ |
152 | - public function replaceInto($table){ |
|
153 | - $obj = new ReplaceIntoRule(new Context($this->connection)); |
|
152 | + public function replaceInto($table) { |
|
153 | + $obj = new ReplaceIntoRule(new Context($this->connection)); |
|
154 | 154 | return $obj->replaceInto($table); |
155 | 155 | } |
156 | 156 | |
@@ -161,16 +161,16 @@ discard block |
||
161 | 161 | */ |
162 | 162 | public function transaction(callable $callback) |
163 | 163 | { |
164 | - if($this->inTransaction){ |
|
164 | + if ($this->inTransaction) { |
|
165 | 165 | return $callback($this); |
166 | 166 | } |
167 | 167 | $this->getConnection()->beginTransaction() or \PhpBoot\abort('beginTransaction failed'); |
168 | 168 | $this->inTransaction = true; |
169 | - try{ |
|
169 | + try { |
|
170 | 170 | $res = $callback($this); |
171 | 171 | $this->getConnection()->commit() or \PhpBoot\abort('commit failed'); |
172 | 172 | return $res; |
173 | - }catch (\Exception $e){ |
|
173 | + }catch (\Exception $e) { |
|
174 | 174 | $this->getConnection()->rollBack(); |
175 | 175 | Logger::warning('commit failed with '.get_class($e).' '.$e->getMessage()); |
176 | 176 | throw $e; |
@@ -191,12 +191,12 @@ discard block |
||
191 | 191 | * @param string $str |
192 | 192 | * @return Raw |
193 | 193 | */ |
194 | - static public function raw($str){ |
|
194 | + static public function raw($str) { |
|
195 | 195 | return new Raw($str); |
196 | 196 | } |
197 | 197 | static public function wrap($value) |
198 | 198 | { |
199 | - if($value instanceof Raw){ |
|
199 | + if ($value instanceof Raw) { |
|
200 | 200 | return $value->get(); |
201 | 201 | } |
202 | 202 | $value = trim($value); |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | return $value; |
205 | 205 | } |
206 | 206 | |
207 | - if(strpos($value, '.') !== false && !preg_match('/\\s+/', $value)){ |
|
207 | + if (strpos($value, '.') !== false && !preg_match('/\\s+/', $value)) { |
|
208 | 208 | return $value; |
209 | 209 | } |
210 | 210 | return '`'.str_replace('`', '``', $value).'`'; |
@@ -217,8 +217,8 @@ discard block |
||
217 | 217 | { |
218 | 218 | return $this->app; |
219 | 219 | } |
220 | - const ORDER_BY_ASC ='ASC'; |
|
221 | - const ORDER_BY_DESC ='DESC'; |
|
220 | + const ORDER_BY_ASC = 'ASC'; |
|
221 | + const ORDER_BY_DESC = 'DESC'; |
|
222 | 222 | |
223 | 223 | /** |
224 | 224 | * @var \PDO |
@@ -29,9 +29,9 @@ discard block |
||
29 | 29 | ->from($this->entity->getTable()) |
30 | 30 | ->where("`{$this->entity->getPK()}` = ?", $id) |
31 | 31 | ->getFirst(); |
32 | - if($row){ |
|
32 | + if ($row) { |
|
33 | 33 | return $this->entity->make($row, false); |
34 | - }else{ |
|
34 | + }else { |
|
35 | 35 | return null; |
36 | 36 | } |
37 | 37 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | * @param mixed|null $_ |
54 | 54 | * @return \PhpBoot\DB\rules\basic\WhereRule |
55 | 55 | */ |
56 | - public function deleteWhere($conditions, $_=null) |
|
56 | + public function deleteWhere($conditions, $_ = null) |
|
57 | 57 | { |
58 | 58 | $query = $this->db->deleteFrom($this->entity->getTable()); |
59 | 59 | return call_user_func_array([$query, 'where'], func_get_args()); |
@@ -75,12 +75,12 @@ discard block |
||
75 | 75 | * @param string $_ |
76 | 76 | * @return \PhpBoot\DB\rules\select\WhereRule |
77 | 77 | */ |
78 | - public function findWhere($conditions=null, $_=null) |
|
78 | + public function findWhere($conditions = null, $_ = null) |
|
79 | 79 | { |
80 | - $query = $this->db->select($this->getColumns()) |
|
80 | + $query = $this->db->select($this->getColumns()) |
|
81 | 81 | ->from($this->entity->getTable()); |
82 | - $query->context->resultHandler = function ($result){ |
|
83 | - foreach ($result as &$i){ |
|
82 | + $query->context->resultHandler = function($result) { |
|
83 | + foreach ($result as &$i) { |
|
84 | 84 | $i = $this->entity->make($i, false); |
85 | 85 | } |
86 | 86 | return $result; |
@@ -107,16 +107,16 @@ discard block |
||
107 | 107 | * @param string $_ |
108 | 108 | * @return \PhpBoot\DB\rules\basic\WhereRule |
109 | 109 | */ |
110 | - public function updateWhere($values, $conditions, $_=null) |
|
110 | + public function updateWhere($values, $conditions, $_ = null) |
|
111 | 111 | { |
112 | - $query = $this->db->update($this->entity->getTable())->set($values); |
|
113 | - return call_user_func_array([$query, 'where'], array_slice(func_get_args(),1)); |
|
112 | + $query = $this->db->update($this->entity->getTable())->set($values); |
|
113 | + return call_user_func_array([$query, 'where'], array_slice(func_get_args(), 1)); |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | protected function getColumns() |
117 | 117 | { |
118 | 118 | $columns = []; |
119 | - foreach ($this->entity->getProperties() as $p){ |
|
119 | + foreach ($this->entity->getProperties() as $p) { |
|
120 | 120 | $columns[] = $p->name; |
121 | 121 | } |
122 | 122 | return $columns; |