@@ -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 | $v = ['='=>$v]; |
| 321 | 321 | } |
| 322 | 322 | |
@@ -328,56 +328,56 @@ discard block |
||
| 328 | 328 | new \InvalidArgumentException("invalid param $op for whereArgs")); |
| 329 | 329 | |
| 330 | 330 | $var = array_values($v)[0]; |
| 331 | - if($op == 'IN' || $op == 'NOT IN'){ |
|
| 331 | + if ($op == 'IN' || $op == 'NOT IN') { |
|
| 332 | 332 | $stubs = []; |
| 333 | 333 | |
| 334 | - if($var instanceof BasicRule){ |
|
| 334 | + if ($var instanceof BasicRule) { |
|
| 335 | 335 | $stubs = "({$var->context->sql})"; |
| 336 | 336 | $params = array_merge($params, $var->context->params); |
| 337 | 337 | $exprs[] = "$k $op $stubs"; |
| 338 | - }else{ |
|
| 339 | - foreach ($var as $i){ |
|
| 340 | - if(is_a($i, Raw::class)){ |
|
| 341 | - $stubs[]=strval($i); |
|
| 342 | - }elseif($i instanceof BasicRule){ |
|
| 338 | + }else { |
|
| 339 | + foreach ($var as $i) { |
|
| 340 | + if (is_a($i, Raw::class)) { |
|
| 341 | + $stubs[] = strval($i); |
|
| 342 | + }elseif ($i instanceof BasicRule) { |
|
| 343 | 343 | $stubs = "({$i->context->sql})"; |
| 344 | 344 | $params = array_merge($params, $i->context->params); |
| 345 | - }else{ |
|
| 346 | - $stubs[]='?'; |
|
| 345 | + }else { |
|
| 346 | + $stubs[] = '?'; |
|
| 347 | 347 | $params[] = $i; |
| 348 | 348 | } |
| 349 | 349 | } |
| 350 | 350 | $stubs = implode(',', $stubs); |
| 351 | 351 | $exprs[] = "$k $op ($stubs)"; |
| 352 | 352 | } |
| 353 | - }else if($op == 'BETWEEN'){ |
|
| 353 | + }else if ($op == 'BETWEEN') { |
|
| 354 | 354 | $cond = "$k BETWEEN"; |
| 355 | - if(is_a($var[0], Raw::class)){ |
|
| 355 | + if (is_a($var[0], Raw::class)) { |
|
| 356 | 356 | $cond = "$cond ".strval($var[0]); |
| 357 | - }elseif($var[0] instanceof BasicRule){ |
|
| 357 | + }elseif ($var[0] instanceof BasicRule) { |
|
| 358 | 358 | $cond = "$cond ({$var[0]->context->sql})"; |
| 359 | 359 | $params = array_merge($params, $var[0]->context->params); |
| 360 | - }else{ |
|
| 360 | + }else { |
|
| 361 | 361 | $cond = "$cond ?"; |
| 362 | 362 | $params[] = $var[0]; |
| 363 | 363 | } |
| 364 | - if(is_a($var[1], Raw::class)){ |
|
| 364 | + if (is_a($var[1], Raw::class)) { |
|
| 365 | 365 | $cond = "$cond AND ".strval($var[1]); |
| 366 | - }elseif($var[1] instanceof BasicRule){ |
|
| 366 | + }elseif ($var[1] instanceof BasicRule) { |
|
| 367 | 367 | $cond = "$cond AND ({$var[1]->context->sql})"; |
| 368 | 368 | $params = array_merge($params, $var[1]->context->params); |
| 369 | - }else{ |
|
| 369 | + }else { |
|
| 370 | 370 | $cond = "$cond AND ?"; |
| 371 | 371 | $params[] = $var[1]; |
| 372 | 372 | } |
| 373 | 373 | $exprs[] = $cond; |
| 374 | - }else{ |
|
| 375 | - if(is_a($var, Raw::class)){ |
|
| 374 | + }else { |
|
| 375 | + if (is_a($var, Raw::class)) { |
|
| 376 | 376 | $exprs[] = "$k $op ".strval($var); |
| 377 | - }elseif($var instanceof BasicRule){ |
|
| 377 | + }elseif ($var instanceof BasicRule) { |
|
| 378 | 378 | $exprs[] = "$k $op {$var->context->sql}"; |
| 379 | 379 | $params = array_merge($params, $var->context->params); |
| 380 | - }else{ |
|
| 380 | + }else { |
|
| 381 | 381 | $exprs[] = "$k $op ?"; |
| 382 | 382 | $params[] = $var; |
| 383 | 383 | } |
@@ -386,22 +386,22 @@ discard block |
||
| 386 | 386 | |
| 387 | 387 | self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
| 388 | 388 | } |
| 389 | - static public function condition(Context $context, $prefix, $expr, $args){ |
|
| 390 | - if(!empty($expr)){ |
|
| 389 | + static public function condition(Context $context, $prefix, $expr, $args) { |
|
| 390 | + if (!empty($expr)) { |
|
| 391 | 391 | $expr = "($expr)"; |
| 392 | - if($args){ |
|
| 392 | + if ($args) { |
|
| 393 | 393 | //因为PDO不支持绑定数组变量, 这里需要手动展开数组 |
| 394 | 394 | //也就是说把 where("id IN(?)", [1,2]) 展开成 where("id IN(?,?)", 1,2) |
| 395 | 395 | $cutted = null; |
| 396 | 396 | $cut = null; |
| 397 | 397 | $toReplace = array(); |
| 398 | 398 | |
| 399 | - $newArgs=array(); |
|
| 399 | + $newArgs = array(); |
|
| 400 | 400 | //找到所有数组对应的?符位置 |
| 401 | - foreach ($args as $k =>$arg){ |
|
| 402 | - if(is_array($arg) || is_a($arg, Raw::class) || is_a($arg, BasicRule::class)){ |
|
| 401 | + foreach ($args as $k =>$arg) { |
|
| 402 | + if (is_array($arg) || is_a($arg, Raw::class) || is_a($arg, BasicRule::class)) { |
|
| 403 | 403 | |
| 404 | - if(!$cutted){ |
|
| 404 | + if (!$cutted) { |
|
| 405 | 405 | $cut = new NestedStringCut($expr); |
| 406 | 406 | $cutted = $cut->getText(); |
| 407 | 407 | } |
@@ -411,54 +411,54 @@ discard block |
||
| 411 | 411 | $pos !== false or \PhpBoot\abort( |
| 412 | 412 | new \InvalidArgumentException("unmatched params and ? @ $expr")); |
| 413 | 413 | |
| 414 | - if(is_array($arg)){ |
|
| 414 | + if (is_array($arg)) { |
|
| 415 | 415 | $stubs = []; |
| 416 | - foreach ($arg as $i){ |
|
| 417 | - if(is_a($i, Raw::class)){ |
|
| 416 | + foreach ($arg as $i) { |
|
| 417 | + if (is_a($i, Raw::class)) { |
|
| 418 | 418 | $stubs[] = strval($i); |
| 419 | - }else{ |
|
| 419 | + }else { |
|
| 420 | 420 | $stubs[] = '?'; |
| 421 | 421 | $newArgs[] = $i; |
| 422 | 422 | } |
| 423 | 423 | } |
| 424 | 424 | $stubs = implode(',', $stubs); |
| 425 | - }elseif($arg instanceof BasicRule){ |
|
| 425 | + }elseif ($arg instanceof BasicRule) { |
|
| 426 | 426 | $stubs = "({$arg->context->sql})"; |
| 427 | 427 | $newArgs = array_merge($newArgs, $arg->context->params); |
| 428 | - }else{ |
|
| 428 | + }else { |
|
| 429 | 429 | $stubs = strval($arg); |
| 430 | 430 | } |
| 431 | 431 | $toReplace[] = [$pos, $stubs]; |
| 432 | 432 | |
| 433 | - }else{ |
|
| 434 | - $newArgs[]=$arg; |
|
| 433 | + }else { |
|
| 434 | + $newArgs[] = $arg; |
|
| 435 | 435 | } |
| 436 | 436 | } |
| 437 | 437 | |
| 438 | - if(count($toReplace)){ |
|
| 438 | + if (count($toReplace)) { |
|
| 439 | 439 | $toReplace = array_reverse($toReplace); |
| 440 | - foreach ($toReplace as $i){ |
|
| 440 | + foreach ($toReplace as $i) { |
|
| 441 | 441 | list($pos, $v) = $i; |
| 442 | - $expr = substr($expr, 0, $pos).$v.substr($expr, $pos+1); |
|
| 442 | + $expr = substr($expr, 0, $pos).$v.substr($expr, $pos + 1); |
|
| 443 | 443 | } |
| 444 | 444 | $args = $newArgs; |
| 445 | 445 | } |
| 446 | 446 | } |
| 447 | - if($prefix){ |
|
| 447 | + if ($prefix) { |
|
| 448 | 448 | $context->appendSql($prefix.' '.$expr); |
| 449 | - }else{ |
|
| 449 | + }else { |
|
| 450 | 450 | $context->appendSql($expr); |
| 451 | 451 | } |
| 452 | 452 | |
| 453 | - if($args){ |
|
| 453 | + if ($args) { |
|
| 454 | 454 | $context->appendParams($args); |
| 455 | 455 | } |
| 456 | 456 | } |
| 457 | 457 | } |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | -class GroupByImpl{ |
|
| 461 | - static public function groupBy(Context $context, $column){ |
|
| 460 | +class GroupByImpl { |
|
| 461 | + static public function groupBy(Context $context, $column) { |
|
| 462 | 462 | $column = DB::wrap($column); |
| 463 | 463 | $context->appendSql("GROUP BY $column"); |
| 464 | 464 | } |
@@ -483,20 +483,20 @@ discard block |
||
| 483 | 483 | * @param string|false $asDict return as dict or array |
| 484 | 484 | * @return false|array |
| 485 | 485 | */ |
| 486 | - static public function get($context, $dictAs=false){ |
|
| 486 | + static public function get($context, $dictAs = false) { |
|
| 487 | 487 | |
| 488 | 488 | $st = $context->connection->prepare($context->sql); |
| 489 | - if($st->execute($context->params)){ |
|
| 489 | + if ($st->execute($context->params)) { |
|
| 490 | 490 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 491 | - if ($dictAs){ |
|
| 492 | - $dict= []; |
|
| 493 | - foreach ($res as $i){ |
|
| 494 | - $dict[$i[$dictAs]]=$i; |
|
| 491 | + if ($dictAs) { |
|
| 492 | + $dict = []; |
|
| 493 | + foreach ($res as $i) { |
|
| 494 | + $dict[$i[$dictAs]] = $i; |
|
| 495 | 495 | } |
| 496 | 496 | return $context->handleResult($dict); |
| 497 | 497 | } |
| 498 | 498 | return $context->handleResult($res); |
| 499 | - }else{ |
|
| 499 | + }else { |
|
| 500 | 500 | return false; |
| 501 | 501 | } |
| 502 | 502 | } |
@@ -505,22 +505,22 @@ discard block |
||
| 505 | 505 | * @param Context $context |
| 506 | 506 | * @return int|false |
| 507 | 507 | */ |
| 508 | - static public function count($context){ |
|
| 508 | + static public function count($context) { |
|
| 509 | 509 | |
| 510 | 510 | $found = []; |
| 511 | - if(!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 512 | - count($found)==0){ |
|
| 511 | + if (!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 512 | + count($found) == 0) { |
|
| 513 | 513 | \PhpBoot\abort(new \PDOException("can not use count(*) without select")); |
| 514 | 514 | } |
| 515 | 515 | list($chars, $columnBegin) = $found[0]; |
| 516 | - $columnBegin = $columnBegin + strlen('select')+1; |
|
| 516 | + $columnBegin = $columnBegin + strlen('select') + 1; |
|
| 517 | 517 | |
| 518 | 518 | $columnEnd = 0; |
| 519 | 519 | $found = []; |
| 520 | - if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 521 | - count($found)==0){ |
|
| 520 | + if (!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 521 | + count($found) == 0) { |
|
| 522 | 522 | $columnEnd = strlen($context->sql); |
| 523 | - }else{ |
|
| 523 | + }else { |
|
| 524 | 524 | list($chars, $columnEnd) = $found[0]; |
| 525 | 525 | } |
| 526 | 526 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -528,10 +528,10 @@ discard block |
||
| 528 | 528 | $sql .= substr($context->sql, $columnEnd); |
| 529 | 529 | |
| 530 | 530 | $st = $context->connection->prepare($sql); |
| 531 | - if($st->execute($context->params)){ |
|
| 531 | + if ($st->execute($context->params)) { |
|
| 532 | 532 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 533 | 533 | return intval($res[0]['count']); |
| 534 | - }else{ |
|
| 534 | + }else { |
|
| 535 | 535 | return false; |
| 536 | 536 | } |
| 537 | 537 | |
@@ -539,47 +539,47 @@ discard block |
||
| 539 | 539 | } |
| 540 | 540 | class OnDuplicateKeyUpdateImpl |
| 541 | 541 | { |
| 542 | - public function set($context, $column, $value){ |
|
| 543 | - if(is_string($column)){ |
|
| 542 | + public function set($context, $column, $value) { |
|
| 543 | + if (is_string($column)) { |
|
| 544 | 544 | $this->setExpr($context, $column, $value); |
| 545 | - }else{ |
|
| 545 | + }else { |
|
| 546 | 546 | $this->setArgs($context, $column); |
| 547 | 547 | } |
| 548 | 548 | } |
| 549 | 549 | |
| 550 | - public function setExpr($context, $expr, $args){ |
|
| 550 | + public function setExpr($context, $expr, $args) { |
|
| 551 | 551 | $prefix = ''; |
| 552 | - if($this->first){ |
|
| 552 | + if ($this->first) { |
|
| 553 | 553 | $this->first = false; |
| 554 | 554 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
| 555 | - }else{ |
|
| 555 | + }else { |
|
| 556 | 556 | $prefix = ','; |
| 557 | 557 | } |
| 558 | 558 | |
| 559 | - $context->appendSql("$prefix$expr",$prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 559 | + $context->appendSql("$prefix$expr", $prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 560 | 560 | $context->appendParams($args); |
| 561 | 561 | |
| 562 | 562 | } |
| 563 | - public function setArgs($context, $values){ |
|
| 563 | + public function setArgs($context, $values) { |
|
| 564 | 564 | $set = []; |
| 565 | 565 | $params = []; |
| 566 | - foreach ($values as $k=>$v){ |
|
| 566 | + foreach ($values as $k=>$v) { |
|
| 567 | 567 | $k = DB::wrap($k); |
| 568 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 569 | - $set[]= "$k=".$v->get(); |
|
| 570 | - }else{ |
|
| 571 | - $set[]= "$k=?"; |
|
| 572 | - $params[]=$v; |
|
| 568 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 569 | + $set[] = "$k=".$v->get(); |
|
| 570 | + }else { |
|
| 571 | + $set[] = "$k=?"; |
|
| 572 | + $params[] = $v; |
|
| 573 | 573 | } |
| 574 | 574 | } |
| 575 | - if($this->first){ |
|
| 575 | + if ($this->first) { |
|
| 576 | 576 | $this->first = false; |
| 577 | 577 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
| 578 | 578 | $context->appendParams($params); |
| 579 | - }else{ |
|
| 580 | - $context->appendSql(','.implode(',', $set),false); |
|
| 579 | + }else { |
|
| 580 | + $context->appendSql(','.implode(',', $set), false); |
|
| 581 | 581 | $context->appendParams($params); |
| 582 | 582 | } |
| 583 | 583 | } |
| 584 | - private $first=true; |
|
| 584 | + private $first = true; |
|
| 585 | 585 | } |