@@ -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 $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 | } |
@@ -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 | |
@@ -335,14 +335,14 @@ discard block |
||
| 335 | 335 | $stubs = "({$var->context->sql})"; |
| 336 | 336 | $params = array_merge($params, $var->context->params); |
| 337 | 337 | $exprs[] = "$k $op $stubs"; |
| 338 | - }else{ |
|
| 338 | + } else{ |
|
| 339 | 339 | foreach ($var as $i){ |
| 340 | 340 | if(is_a($i, Raw::class)){ |
| 341 | 341 | $stubs[]=strval($i); |
| 342 | - }elseif($i instanceof BasicRule){ |
|
| 342 | + } elseif($i instanceof BasicRule){ |
|
| 343 | 343 | $stubs = "({$i->context->sql})"; |
| 344 | 344 | $params = array_merge($params, $i->context->params); |
| 345 | - }else{ |
|
| 345 | + } else{ |
|
| 346 | 346 | $stubs[]='?'; |
| 347 | 347 | $params[] = $i; |
| 348 | 348 | } |
@@ -350,34 +350,34 @@ discard block |
||
| 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 | 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 | 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{ |
|
| 374 | + } else{ |
|
| 375 | 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 | } |
@@ -416,21 +416,21 @@ discard block |
||
| 416 | 416 | foreach ($arg as $i){ |
| 417 | 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{ |
|
| 433 | + } else{ |
|
| 434 | 434 | $newArgs[]=$arg; |
| 435 | 435 | } |
| 436 | 436 | } |
@@ -446,7 +446,7 @@ discard block |
||
| 446 | 446 | } |
| 447 | 447 | if($prefix){ |
| 448 | 448 | $context->appendSql($prefix.' '.$expr); |
| 449 | - }else{ |
|
| 449 | + } else{ |
|
| 450 | 450 | $context->appendSql($expr); |
| 451 | 451 | } |
| 452 | 452 | |
@@ -496,7 +496,7 @@ discard block |
||
| 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 | } |
@@ -520,7 +520,7 @@ discard block |
||
| 520 | 520 | if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
| 521 | 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); |
@@ -531,7 +531,7 @@ discard block |
||
| 531 | 531 | if($st->execute($context->params)){ |
| 532 | 532 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 533 | 533 | return $res[0]['count']; |
| 534 | - }else{ |
|
| 534 | + } else{ |
|
| 535 | 535 | return false; |
| 536 | 536 | } |
| 537 | 537 | |
@@ -542,7 +542,7 @@ discard block |
||
| 542 | 542 | public function set($context, $column, $value){ |
| 543 | 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 | } |
@@ -552,7 +552,7 @@ discard block |
||
| 552 | 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 | |
@@ -567,7 +567,7 @@ discard block |
||
| 567 | 567 | $k = DB::wrap($k); |
| 568 | 568 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
| 569 | 569 | $set[]= "$k=".$v->get(); |
| 570 | - }else{ |
|
| 570 | + } else{ |
|
| 571 | 571 | $set[]= "$k=?"; |
| 572 | 572 | $params[]=$v; |
| 573 | 573 | } |
@@ -576,7 +576,7 @@ discard block |
||
| 576 | 576 | $this->first = false; |
| 577 | 577 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
| 578 | 578 | $context->appendParams($params); |
| 579 | - }else{ |
|
| 579 | + } else{ |
|
| 580 | 580 | $context->appendSql(','.implode(',', $set),false); |
| 581 | 581 | $context->appendParams($params); |
| 582 | 582 | } |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * |
| 51 | 51 | * @author caoym <[email protected]> |
| 52 | 52 | */ |
| 53 | -class DB{ |
|
| 53 | +class DB { |
|
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | 56 | * DB constructor. |
@@ -90,18 +90,18 @@ discard block |
||
| 90 | 90 | * @param string $column0 |
| 91 | 91 | * @return \PhpBoot\DB\rules\select\FromRule |
| 92 | 92 | */ |
| 93 | - function select($column0=null, $_=null){ |
|
| 93 | + function select($column0 = null, $_ = null) { |
|
| 94 | 94 | $obj = new SelectRule(new Context($this->connection)); |
| 95 | - if($column0 == null){ |
|
| 95 | + if ($column0 == null) { |
|
| 96 | 96 | $args = ['*']; |
| 97 | - }elseif(is_array($column0)){ |
|
| 97 | + }elseif (is_array($column0)) { |
|
| 98 | 98 | $args = $column0; |
| 99 | - }else{ |
|
| 99 | + }else { |
|
| 100 | 100 | $args = func_get_args(); |
| 101 | 101 | } |
| 102 | - foreach ($args as &$arg){ |
|
| 102 | + foreach ($args as &$arg) { |
|
| 103 | 103 | $arg = DB::wrap($arg); |
| 104 | - if($arg == '*'){ |
|
| 104 | + if ($arg == '*') { |
|
| 105 | 105 | continue; |
| 106 | 106 | } |
| 107 | 107 | } |
@@ -132,8 +132,8 @@ discard block |
||
| 132 | 132 | * @param string $table |
| 133 | 133 | * @return \PhpBoot\DB\rules\basic\WhereRule |
| 134 | 134 | */ |
| 135 | - public function deleteFrom($table){ |
|
| 136 | - $obj = new DeleteRule(new Context($this->connection)); |
|
| 135 | + public function deleteFrom($table) { |
|
| 136 | + $obj = new DeleteRule(new Context($this->connection)); |
|
| 137 | 137 | return $obj->deleteFrom($table); |
| 138 | 138 | } |
| 139 | 139 | /** |
@@ -141,8 +141,8 @@ discard block |
||
| 141 | 141 | * @param string $table |
| 142 | 142 | * @return \PhpBoot\DB\rules\replace\ValuesRule |
| 143 | 143 | */ |
| 144 | - public function replaceInto($table){ |
|
| 145 | - $obj = new ReplaceIntoRule(new Context($this->connection)); |
|
| 144 | + public function replaceInto($table) { |
|
| 145 | + $obj = new ReplaceIntoRule(new Context($this->connection)); |
|
| 146 | 146 | return $obj->replaceInto($table); |
| 147 | 147 | } |
| 148 | 148 | |
@@ -153,16 +153,16 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | public function transaction(callable $callback) |
| 155 | 155 | { |
| 156 | - if($this->inTransaction){ |
|
| 156 | + if ($this->inTransaction) { |
|
| 157 | 157 | return $callback($this); |
| 158 | 158 | } |
| 159 | 159 | $this->getConnection()->beginTransaction() or \PhpBoot\abort('beginTransaction failed'); |
| 160 | 160 | $this->inTransaction = true; |
| 161 | - try{ |
|
| 161 | + try { |
|
| 162 | 162 | $res = $callback($this); |
| 163 | 163 | $this->getConnection()->commit() or \PhpBoot\abort('commit failed'); |
| 164 | 164 | return $res; |
| 165 | - }catch (\Exception $e){ |
|
| 165 | + }catch (\Exception $e) { |
|
| 166 | 166 | $this->getConnection()->rollBack(); |
| 167 | 167 | Logger::warning('commit failed with '.get_class($e).' '.$e->getMessage()); |
| 168 | 168 | throw $e; |
@@ -183,12 +183,12 @@ discard block |
||
| 183 | 183 | * @param string $str |
| 184 | 184 | * @return Raw |
| 185 | 185 | */ |
| 186 | - static public function raw($str){ |
|
| 186 | + static public function raw($str) { |
|
| 187 | 187 | return new Raw($str); |
| 188 | 188 | } |
| 189 | 189 | static public function wrap($value) |
| 190 | 190 | { |
| 191 | - if($value instanceof Raw){ |
|
| 191 | + if ($value instanceof Raw) { |
|
| 192 | 192 | return $value->get(); |
| 193 | 193 | } |
| 194 | 194 | $value = trim($value); |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | return $value; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | - if(strpos($value, '.') !== false && !preg_match('/\\s+/', $value)){ |
|
| 199 | + if (strpos($value, '.') !== false && !preg_match('/\\s+/', $value)) { |
|
| 200 | 200 | return $value; |
| 201 | 201 | } |
| 202 | 202 | return '`'.str_replace('`', '``', $value).'`'; |
@@ -209,8 +209,8 @@ discard block |
||
| 209 | 209 | { |
| 210 | 210 | return $this->app; |
| 211 | 211 | } |
| 212 | - const ORDER_BY_ASC ='ASC'; |
|
| 213 | - const ORDER_BY_DESC ='DESC'; |
|
| 212 | + const ORDER_BY_ASC = 'ASC'; |
|
| 213 | + const ORDER_BY_DESC = 'DESC'; |
|
| 214 | 214 | |
| 215 | 215 | /** |
| 216 | 216 | * @var \PDO |
@@ -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 ScopedQuery($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 ScopedQuery($context); |
| 135 | 135 | $conditions($rule); |
| 136 | 136 | }; |
@@ -156,17 +156,17 @@ discard block |
||
| 156 | 156 | * @param null $_ |
| 157 | 157 | * @return ScopedQuery |
| 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 ScopedQuery($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 ScopedQuery($this->context, false); |
@@ -176,9 +176,9 @@ discard block |
||
| 176 | 176 | * @param null $_ |
| 177 | 177 | * @return ScopedQuery |
| 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 ScopedQuery($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 ScopedQuery($this->context, false); |
@@ -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 ScopedQuery($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 ScopedQuery($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 ScopedQuery($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 ScopedQuery($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 | } |