@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | use PhpBoot\ORM\ModelWithObject; |
6 | 6 | use PhpBoot\Utils\Logger; |
7 | 7 | |
8 | -if (! function_exists('PhpBoot\abort')) { |
|
8 | +if (!function_exists('PhpBoot\abort')) { |
|
9 | 9 | /** |
10 | 10 | * 抛出异常, 并记录日志 |
11 | 11 | * @param string|\Exception $error |
@@ -13,29 +13,29 @@ discard block |
||
13 | 13 | * @param string $level "error"|"warning"|"info"|"debug"|null |
14 | 14 | * @throws \Exception |
15 | 15 | */ |
16 | - function abort($error = '', $context=[], $level='warning') |
|
16 | + function abort($error = '', $context = [], $level = 'warning') |
|
17 | 17 | { |
18 | - if(is_object($context)){ |
|
18 | + if (is_object($context)) { |
|
19 | 19 | $context = get_object_vars($context); |
20 | 20 | } |
21 | - if($error instanceof \Exception){ |
|
21 | + if ($error instanceof \Exception) { |
|
22 | 22 | $e = $error; |
23 | 23 | $message = "exception '".get_class($error)."' with message {$error->getMessage()}"; |
24 | - }else{ |
|
24 | + }else { |
|
25 | 25 | $e = new \RuntimeException($error); |
26 | 26 | $message = $error; |
27 | 27 | } |
28 | 28 | $trace = $e->getTrace(); |
29 | 29 | |
30 | - if($e->getFile() == __FILE__){ |
|
30 | + if ($e->getFile() == __FILE__) { |
|
31 | 31 | $file = $trace[0]['file']; |
32 | 32 | $line = $trace[0]['line']; |
33 | - }else{ |
|
33 | + }else { |
|
34 | 34 | $file = $e->getFile(); |
35 | 35 | $line = $e->getLine(); |
36 | 36 | } |
37 | - if($level){ |
|
38 | - Logger::$level($message, $context +['@file'=>$file, '@line'=>$line]); |
|
37 | + if ($level) { |
|
38 | + Logger::$level($message, $context + ['@file'=>$file, '@line'=>$line]); |
|
39 | 39 | } |
40 | 40 | throw $e; |
41 | 41 | } |
@@ -51,9 +51,9 @@ discard block |
||
51 | 51 | */ |
52 | 52 | function model(DB $db, $entity) |
53 | 53 | { |
54 | - if(is_object($entity)){ |
|
54 | + if (is_object($entity)) { |
|
55 | 55 | return $db->getApp()->make(ModelWithObject::class, ['db'=>$db, 'entity'=>$entity]); |
56 | - }else{ |
|
56 | + }else { |
|
57 | 57 | return $db->getApp()->make(ModelWithClass::class, ['db'=>$db, 'entityName'=>$entity]); |
58 | 58 | } |
59 | 59 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | /** |
4 | 4 | * @author caoym |
5 | 5 | */ |
6 | -class Context{ |
|
6 | +class Context { |
|
7 | 7 | |
8 | 8 | public function __construct($connection) |
9 | 9 | { |
@@ -14,13 +14,13 @@ discard block |
||
14 | 14 | * 拼接sql语句,并自动插入空格 |
15 | 15 | * @param string $sql 表达式 |
16 | 16 | */ |
17 | - public function appendSql($sql, $addSpace=true){ |
|
18 | - if($this->sql == ''){ |
|
17 | + public function appendSql($sql, $addSpace = true) { |
|
18 | + if ($this->sql == '') { |
|
19 | 19 | $this->sql = $sql; |
20 | - }else{ |
|
21 | - if($addSpace){ |
|
20 | + }else { |
|
21 | + if ($addSpace) { |
|
22 | 22 | $this->sql = $this->sql.' '.$sql; |
23 | - }else{ |
|
23 | + }else { |
|
24 | 24 | $this->sql = $this->sql.$sql; |
25 | 25 | } |
26 | 26 | } |
@@ -29,15 +29,15 @@ discard block |
||
29 | 29 | * 增加绑定变量值 |
30 | 30 | * @param array $params 变量 |
31 | 31 | */ |
32 | - public function appendParams($params){ |
|
32 | + public function appendParams($params) { |
|
33 | 33 | $this->params = array_merge($this->params, $params); |
34 | 34 | } |
35 | 35 | |
36 | 36 | public function handleResult($result) |
37 | 37 | { |
38 | - if($resultHandler = $this->resultHandler){ |
|
38 | + if ($resultHandler = $this->resultHandler) { |
|
39 | 39 | return $resultHandler($result); |
40 | - }else{ |
|
40 | + }else { |
|
41 | 41 | return $result; |
42 | 42 | } |
43 | 43 | } |
@@ -45,8 +45,8 @@ discard block |
||
45 | 45 | * @var callable |
46 | 46 | */ |
47 | 47 | public $resultHandler; |
48 | - public $sql=''; |
|
49 | - public $params=[]; |
|
48 | + public $sql = ''; |
|
49 | + public $params = []; |
|
50 | 50 | /** |
51 | 51 | * @var \PDO |
52 | 52 | */ |
@@ -7,26 +7,26 @@ discard block |
||
7 | 7 | * 既从aaa"bb\"b"ccc中, 取出"bb\"b" |
8 | 8 | * @author caoym |
9 | 9 | */ |
10 | -class NestedStringCut{ |
|
10 | +class NestedStringCut { |
|
11 | 11 | |
12 | - public function __construct($str){ |
|
12 | + public function __construct($str) { |
|
13 | 13 | |
14 | 14 | $pos = 0; |
15 | 15 | $state = 'stateNormal'; |
16 | - while (true){ |
|
16 | + while (true) { |
|
17 | 17 | $pos = $this->$state($str, $pos, $state); |
18 | - if($pos === false){ |
|
18 | + if ($pos === false) { |
|
19 | 19 | break; |
20 | 20 | } |
21 | 21 | }; |
22 | 22 | return false; |
23 | 23 | } |
24 | 24 | |
25 | - public function getSnippets(){ |
|
25 | + public function getSnippets() { |
|
26 | 26 | return $this->snippets; |
27 | 27 | } |
28 | 28 | |
29 | - public function getText(){ |
|
29 | + public function getText() { |
|
30 | 30 | return implode('', $this->snippets); |
31 | 31 | } |
32 | 32 | /** |
@@ -34,11 +34,11 @@ discard block |
||
34 | 34 | * @param int $pos |
35 | 35 | * @param int |
36 | 36 | */ |
37 | - public function mapPos($pos){ |
|
37 | + public function mapPos($pos) { |
|
38 | 38 | |
39 | - foreach ($this->snippets as $k => $v){ |
|
39 | + foreach ($this->snippets as $k => $v) { |
|
40 | 40 | $pos += $k; |
41 | - if($pos < $k + strlen($v)){ |
|
41 | + if ($pos<$k + strlen($v)) { |
|
42 | 42 | break; |
43 | 43 | } |
44 | 44 | $pos -= ($k + strlen($v)); |
@@ -49,21 +49,21 @@ discard block |
||
49 | 49 | /** |
50 | 50 | * 普通状态 |
51 | 51 | */ |
52 | - private function stateNormal($str, $pos, &$next){ |
|
52 | + private function stateNormal($str, $pos, &$next) { |
|
53 | 53 | $ori = $pos; |
54 | 54 | $posSQ = strpos($str, '\'', $pos); |
55 | 55 | $posDQ = strpos($str, '"', $pos); |
56 | 56 | $pos = $posSQ; |
57 | 57 | $this->subStateQ = '\''; |
58 | 58 | $next = 'stateQ'; |
59 | - if($posDQ !== false && (($posDQ < $pos) || ($pos === false)) ){ |
|
59 | + if ($posDQ !== false && (($posDQ<$pos) || ($pos === false))) { |
|
60 | 60 | $pos = $posDQ; |
61 | 61 | $this->subStateQ = '"'; |
62 | 62 | } |
63 | - if($pos !== false){ |
|
64 | - $this->snippets[$ori] = substr($str, $ori, $pos-$ori); |
|
65 | - $pos ++; |
|
66 | - }else{ |
|
63 | + if ($pos !== false) { |
|
64 | + $this->snippets[$ori] = substr($str, $ori, $pos - $ori); |
|
65 | + $pos++; |
|
66 | + }else { |
|
67 | 67 | $this->snippets[$ori] = substr($str, $ori); |
68 | 68 | } |
69 | 69 | return $pos; |
@@ -72,27 +72,27 @@ discard block |
||
72 | 72 | /** |
73 | 73 | * 进入引号状态 |
74 | 74 | */ |
75 | - private function stateQ($str, $pos, &$next){ |
|
75 | + private function stateQ($str, $pos, &$next) { |
|
76 | 76 | $posESC = strpos($str, '\\', $pos); |
77 | 77 | $posQ = strpos($str, $this->subStateQ, $pos); |
78 | 78 | $pos = $posESC; |
79 | 79 | $next = 'stateESC'; |
80 | 80 | |
81 | - if($posQ !== false && (($posQ<$posESC) || ($posESC === false))){ |
|
81 | + if ($posQ !== false && (($posQ<$posESC) || ($posESC === false))) { |
|
82 | 82 | $pos = $posQ; |
83 | 83 | $next = 'stateNormal'; |
84 | 84 | } |
85 | - if($pos !== false){ |
|
86 | - $pos ++; |
|
85 | + if ($pos !== false) { |
|
86 | + $pos++; |
|
87 | 87 | } |
88 | 88 | return $pos; |
89 | 89 | } |
90 | 90 | /** |
91 | 91 | * 进入转义状态 |
92 | 92 | */ |
93 | - private function stateESC($str, $pos, &$next){ |
|
93 | + private function stateESC($str, $pos, &$next) { |
|
94 | 94 | $pos++; |
95 | - if($pos >= strlen($str)){ |
|
95 | + if ($pos>=strlen($str)) { |
|
96 | 96 | return false; |
97 | 97 | } |
98 | 98 | $next = 'stateQ'; |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | * 去掉嵌套字符串后的内容 |
103 | 103 | * @var array |
104 | 104 | */ |
105 | - private $snippets=array(); |
|
105 | + private $snippets = array(); |
|
106 | 106 | |
107 | 107 | private $subStateQ; |
108 | 108 | } |
@@ -2,30 +2,30 @@ |
||
2 | 2 | |
3 | 3 | namespace PhpBoot\DB; |
4 | 4 | |
5 | -if(!function_exists("array_column")) |
|
5 | +if (!function_exists("array_column")) |
|
6 | 6 | { |
7 | 7 | |
8 | 8 | function array_column($array, $column_name) |
9 | 9 | { |
10 | 10 | |
11 | - return array_map(function($element) use($column_name){return $element[$column_name];}, $array); |
|
11 | + return array_map(function($element) use($column_name){return $element[$column_name]; }, $array); |
|
12 | 12 | |
13 | 13 | } |
14 | 14 | |
15 | 15 | } |
16 | 16 | class Rows |
17 | 17 | { |
18 | - static function column($array,$column_name) |
|
18 | + static function column($array, $column_name) |
|
19 | 19 | { |
20 | 20 | |
21 | - return array_map(function($element) use($column_name){return $element[$column_name];}, $array); |
|
21 | + return array_map(function($element) use($column_name){return $element[$column_name]; }, $array); |
|
22 | 22 | |
23 | 23 | } |
24 | - static public function leftJoin(&$lh, $rh, $lKey, $rkey, $destKey){ |
|
25 | - $map = array_combine(self::column($rh,$rkey),$rh); |
|
24 | + static public function leftJoin(&$lh, $rh, $lKey, $rkey, $destKey) { |
|
25 | + $map = array_combine(self::column($rh, $rkey), $rh); |
|
26 | 26 | |
27 | - foreach ($lh as &$v){ |
|
28 | - $v[$destKey]=$map[$v[$lKey]]; |
|
27 | + foreach ($lh as &$v) { |
|
28 | + $v[$destKey] = $map[$v[$lKey]]; |
|
29 | 29 | } |
30 | 30 | } |
31 | 31 | } |
32 | 32 | \ No newline at end of file |
@@ -13,10 +13,10 @@ |
||
13 | 13 | function __construct($str) { |
14 | 14 | $this->str = $str; |
15 | 15 | } |
16 | - public function __toString(){ |
|
16 | + public function __toString() { |
|
17 | 17 | return $this->str; |
18 | 18 | } |
19 | - public function get(){ |
|
19 | + public function get() { |
|
20 | 20 | return $this->str; |
21 | 21 | } |
22 | 22 | private $str; |
@@ -6,14 +6,14 @@ discard block |
||
6 | 6 | use PhpBoot\DB\rules\basic\BasicRule; |
7 | 7 | use PhpBoot\DB\Context; |
8 | 8 | |
9 | -class ExecResult{ |
|
10 | - public function __construct($success, $pdo, $st){ |
|
9 | +class ExecResult { |
|
10 | + public function __construct($success, $pdo, $st) { |
|
11 | 11 | $this->pdo = $pdo; |
12 | 12 | $this->st = $st; |
13 | 13 | $this->success = $success; |
14 | 14 | $this->rows = $this->st->rowCount(); |
15 | 15 | } |
16 | - public function lastInsertId($name=null){ |
|
16 | + public function lastInsertId($name = null) { |
|
17 | 17 | return $this->pdo->lastInsertId($name); |
18 | 18 | } |
19 | 19 | /** |
@@ -40,21 +40,21 @@ discard block |
||
40 | 40 | |
41 | 41 | class SelectImpl |
42 | 42 | { |
43 | - static public function select($context, $columns){ |
|
43 | + static public function select($context, $columns) { |
|
44 | 44 | $context->appendSql("SELECT $columns"); |
45 | 45 | } |
46 | 46 | } |
47 | 47 | |
48 | 48 | class FromImpl |
49 | 49 | { |
50 | - static public function from($context, $tables,$as=null){ |
|
51 | - if($tables instanceof BasicRule){ |
|
50 | + static public function from($context, $tables, $as = null) { |
|
51 | + if ($tables instanceof BasicRule) { |
|
52 | 52 | $context->appendSql("FROM (".$tables->context->sql.')'); |
53 | - $context->params = array_merge($context->params,$tables->context->params); |
|
53 | + $context->params = array_merge($context->params, $tables->context->params); |
|
54 | 54 | }else { |
55 | 55 | $context->appendSql("FROM `$tables`"); |
56 | 56 | } |
57 | - if($as){ |
|
57 | + if ($as) { |
|
58 | 58 | $context->appendSql("as `$as`"); |
59 | 59 | } |
60 | 60 | } |
@@ -71,9 +71,9 @@ discard block |
||
71 | 71 | class JoinImpl |
72 | 72 | { |
73 | 73 | static public function join($context, $type, $table) { |
74 | - if($type){ |
|
74 | + if ($type) { |
|
75 | 75 | $context->appendSql("$type JOIN $table"); |
76 | - }else{ |
|
76 | + }else { |
|
77 | 77 | $context->appendSql("JOIN $table"); |
78 | 78 | } |
79 | 79 | } |
@@ -88,14 +88,14 @@ discard block |
||
88 | 88 | |
89 | 89 | class ForUpdateImpl |
90 | 90 | { |
91 | - static public function forUpdate($context){ |
|
91 | + static public function forUpdate($context) { |
|
92 | 92 | $context->appendSql("FOR UPDATE"); |
93 | 93 | } |
94 | 94 | } |
95 | 95 | |
96 | 96 | class ForUpdateOfImpl |
97 | 97 | { |
98 | - static public function of($context, $column){ |
|
98 | + static public function of($context, $column) { |
|
99 | 99 | $context->appendSql("OF $column"); |
100 | 100 | } |
101 | 101 | } |
@@ -114,27 +114,27 @@ discard block |
||
114 | 114 | } |
115 | 115 | class ValuesImpl |
116 | 116 | { |
117 | - static public function values($context, $values){ |
|
117 | + static public function values($context, $values) { |
|
118 | 118 | $params = []; |
119 | 119 | $stubs = []; |
120 | - foreach ($values as $v){ |
|
121 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
122 | - $stubs[]=$v->get(); |
|
123 | - }else{ |
|
124 | - $stubs[]='?'; |
|
120 | + foreach ($values as $v) { |
|
121 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
122 | + $stubs[] = $v->get(); |
|
123 | + }else { |
|
124 | + $stubs[] = '?'; |
|
125 | 125 | $params[] = $v; |
126 | 126 | } |
127 | 127 | } |
128 | 128 | $stubs = implode(',', $stubs); |
129 | 129 | |
130 | - if(array_keys($values) === range(0, count($values) - 1)){ |
|
130 | + if (array_keys($values) === range(0, count($values) - 1)) { |
|
131 | 131 | //VALUES(val0, val1, val2) |
132 | 132 | $context->appendSql("VALUES($stubs)"); |
133 | 133 | |
134 | - }else{ |
|
134 | + }else { |
|
135 | 135 | //(col0, col1, col2) VALUES(val0, val1, val2) |
136 | 136 | $columns = implode(',', array_keys($values)); |
137 | - $context->appendSql("($columns) VALUES($stubs)",false); |
|
137 | + $context->appendSql("($columns) VALUES($stubs)", false); |
|
138 | 138 | } |
139 | 139 | $context->appendParams($params); |
140 | 140 | } |
@@ -143,117 +143,117 @@ discard block |
||
143 | 143 | |
144 | 144 | class UpdateImpl |
145 | 145 | { |
146 | - static public function update($context, $table){ |
|
146 | + static public function update($context, $table) { |
|
147 | 147 | $context->appendSql("UPDATE `$table`"); |
148 | 148 | } |
149 | 149 | } |
150 | 150 | |
151 | 151 | class UpdateSetImpl |
152 | 152 | { |
153 | - public function set($context, $column, $value){ |
|
153 | + public function set($context, $column, $value) { |
|
154 | 154 | $prefix = ''; |
155 | - if($this->first){ |
|
155 | + if ($this->first) { |
|
156 | 156 | $this->first = false; |
157 | 157 | $prefix = 'SET '; |
158 | - }else{ |
|
158 | + }else { |
|
159 | 159 | $prefix = ','; |
160 | 160 | } |
161 | - if(is_a($value, Raw::class)){ |
|
162 | - $context->appendSql("$prefix$column=$value",$prefix == 'SET '); |
|
163 | - }else{ |
|
164 | - $context->appendSql("$prefix$column=?",$prefix == 'SET '); |
|
161 | + if (is_a($value, Raw::class)) { |
|
162 | + $context->appendSql("$prefix$column=$value", $prefix == 'SET '); |
|
163 | + }else { |
|
164 | + $context->appendSql("$prefix$column=?", $prefix == 'SET '); |
|
165 | 165 | $context->appendParams([$value]); |
166 | 166 | } |
167 | 167 | } |
168 | 168 | |
169 | - public function setExpr($context, $expr, $args){ |
|
169 | + public function setExpr($context, $expr, $args) { |
|
170 | 170 | $prefix = ''; |
171 | - if($this->first){ |
|
171 | + if ($this->first) { |
|
172 | 172 | $this->first = false; |
173 | 173 | $prefix = 'SET '; |
174 | - }else{ |
|
174 | + }else { |
|
175 | 175 | $prefix = ','; |
176 | 176 | } |
177 | 177 | |
178 | - $context->appendSql("$prefix$expr",$prefix == 'SET '); |
|
178 | + $context->appendSql("$prefix$expr", $prefix == 'SET '); |
|
179 | 179 | $context->appendParams($args); |
180 | 180 | |
181 | 181 | } |
182 | - public function setArgs($context, $values){ |
|
182 | + public function setArgs($context, $values) { |
|
183 | 183 | $set = []; |
184 | 184 | $params = []; |
185 | - foreach ($values as $k=>$v){ |
|
186 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
187 | - $set[]= "$k=".$v->get(); |
|
188 | - }else{ |
|
189 | - $set[]= "$k=?"; |
|
190 | - $params[]=$v; |
|
185 | + foreach ($values as $k=>$v) { |
|
186 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
187 | + $set[] = "$k=".$v->get(); |
|
188 | + }else { |
|
189 | + $set[] = "$k=?"; |
|
190 | + $params[] = $v; |
|
191 | 191 | } |
192 | 192 | } |
193 | - if($this->first){ |
|
193 | + if ($this->first) { |
|
194 | 194 | $this->first = false; |
195 | 195 | $context->appendSql('SET '.implode(',', $set)); |
196 | 196 | $context->appendParams($params); |
197 | - }else{ |
|
198 | - $context->appendSql(','.implode(',', $set),false); |
|
197 | + }else { |
|
198 | + $context->appendSql(','.implode(',', $set), false); |
|
199 | 199 | $context->appendParams($params); |
200 | 200 | } |
201 | 201 | } |
202 | - private $first=true; |
|
202 | + private $first = true; |
|
203 | 203 | } |
204 | 204 | class OrderByImpl |
205 | 205 | { |
206 | - public function orderByArgs($context, $orders){ |
|
207 | - if(empty($orders)){ |
|
206 | + public function orderByArgs($context, $orders) { |
|
207 | + if (empty($orders)) { |
|
208 | 208 | return $this; |
209 | 209 | } |
210 | 210 | $params = array(); |
211 | - foreach ($orders as $k=>$v){ |
|
212 | - if(is_integer($k)){ |
|
211 | + foreach ($orders as $k=>$v) { |
|
212 | + if (is_integer($k)) { |
|
213 | 213 | preg_match('/^[a-zA-Z0-9_.]+$/', $v) or \PhpBoot\abort( |
214 | 214 | new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
215 | 215 | |
216 | 216 | $params[] = $v; |
217 | - }else{ |
|
217 | + }else { |
|
218 | 218 | $v = strtoupper($v); |
219 | 219 | preg_match('/^[a-zA-Z0-9_.]+$/', $k) && |
220 | - ($v =='DESC' || $v =='ASC') or \PhpBoot\abort( new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
220 | + ($v == 'DESC' || $v == 'ASC') or \PhpBoot\abort(new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
221 | 221 | |
222 | 222 | $params[] = "$k $v"; |
223 | 223 | } |
224 | 224 | } |
225 | - if($this->first){ |
|
225 | + if ($this->first) { |
|
226 | 226 | $this->first = false; |
227 | 227 | $context->appendSql('ORDER BY '.implode(',', $params)); |
228 | - }else{ |
|
229 | - $context->appendSql(','.implode(',', $params),false); |
|
228 | + }else { |
|
229 | + $context->appendSql(','.implode(',', $params), false); |
|
230 | 230 | } |
231 | 231 | return $this; |
232 | 232 | } |
233 | - public function orderBy($context, $column, $order=null){ |
|
234 | - if($this->first){ |
|
233 | + public function orderBy($context, $column, $order = null) { |
|
234 | + if ($this->first) { |
|
235 | 235 | $this->first = false; |
236 | 236 | $context->appendSql("ORDER BY $column"); |
237 | - }else{ |
|
237 | + }else { |
|
238 | 238 | $context->appendSql(",$column", false); |
239 | 239 | } |
240 | - if($order){ |
|
240 | + if ($order) { |
|
241 | 241 | $context->appendSql($order); |
242 | 242 | } |
243 | 243 | return $this; |
244 | 244 | } |
245 | - private $first=true; |
|
245 | + private $first = true; |
|
246 | 246 | } |
247 | 247 | |
248 | 248 | class LimitImpl |
249 | 249 | { |
250 | - static public function limit($context, $size){ |
|
250 | + static public function limit($context, $size) { |
|
251 | 251 | $intSize = intval($size); |
252 | 252 | strval($intSize) == $size or \PhpBoot\abort( |
253 | 253 | new \InvalidArgumentException("invalid params for limit($size)")); |
254 | 254 | $context->appendSql("LIMIT $size"); |
255 | 255 | } |
256 | - static public function limitWithOffset($context,$start, $size){ |
|
256 | + static public function limitWithOffset($context, $start, $size) { |
|
257 | 257 | $intStart = intval($start); |
258 | 258 | $intSize = intval($size); |
259 | 259 | strval($intStart) == $start && strval($intSize) == $size or \PhpBoot\abort( |
@@ -262,26 +262,26 @@ discard block |
||
262 | 262 | } |
263 | 263 | } |
264 | 264 | |
265 | -class WhereImpl{ |
|
265 | +class WhereImpl { |
|
266 | 266 | |
267 | - static private function findQ($str,$offset = 0,$no=0){ |
|
267 | + static private function findQ($str, $offset = 0, $no = 0) { |
|
268 | 268 | $found = strpos($str, '?', $offset); |
269 | - if($no == 0 || $found === false){ |
|
269 | + if ($no == 0 || $found === false) { |
|
270 | 270 | return $found; |
271 | 271 | } |
272 | - return self::findQ($str, $found+1, $no-1); |
|
272 | + return self::findQ($str, $found + 1, $no - 1); |
|
273 | 273 | } |
274 | - static public function having($context, $expr, $args){ |
|
274 | + static public function having($context, $expr, $args) { |
|
275 | 275 | self::condition($context, 'HAVING', $expr, $args); |
276 | 276 | } |
277 | - static public function where($context, $expr, $args){ |
|
277 | + static public function where($context, $expr, $args) { |
|
278 | 278 | self::condition($context, 'WHERE', $expr, $args); |
279 | 279 | } |
280 | 280 | |
281 | - static public function havingArgs($context, $args){ |
|
281 | + static public function havingArgs($context, $args) { |
|
282 | 282 | self::conditionArgs($context, 'HAVING', $args); |
283 | 283 | } |
284 | - static public function whereArgs($context, $args){ |
|
284 | + static public function whereArgs($context, $args) { |
|
285 | 285 | self::conditionArgs($context, 'WHERE', $args); |
286 | 286 | } |
287 | 287 | /** |
@@ -306,14 +306,14 @@ discard block |
||
306 | 306 | * |
307 | 307 | * @param array $args |
308 | 308 | */ |
309 | - static public function conditionArgs($context, $prefix, $args=[]){ |
|
310 | - if($args ===null){ |
|
311 | - return ; |
|
309 | + static public function conditionArgs($context, $prefix, $args = []) { |
|
310 | + if ($args === null) { |
|
311 | + return; |
|
312 | 312 | } |
313 | 313 | $exprs = array(); |
314 | 314 | $params = array(); |
315 | - foreach ($args as $k => $v){ |
|
316 | - if(is_array($v)){ |
|
315 | + foreach ($args as $k => $v) { |
|
316 | + if (is_array($v)) { |
|
317 | 317 | $ops = ['=', '>', '<', '<>', '>=', '<=', 'IN', 'NOT IN', 'BETWEEN', 'LIKE']; |
318 | 318 | $op = array_keys($v)[0]; |
319 | 319 | $op = strtoupper($op); |
@@ -322,46 +322,46 @@ discard block |
||
322 | 322 | new \InvalidArgumentException("invalid param $op for whereArgs")); |
323 | 323 | |
324 | 324 | $var = array_values($v)[0]; |
325 | - if($op == 'IN' || $op == 'NOT IN'){ |
|
325 | + if ($op == 'IN' || $op == 'NOT IN') { |
|
326 | 326 | $stubs = []; |
327 | - foreach ($var as $i){ |
|
328 | - if(is_a($i, Raw::class)){ |
|
329 | - $stubs[]=strval($i); |
|
330 | - }else{ |
|
331 | - $stubs[]='?'; |
|
327 | + foreach ($var as $i) { |
|
328 | + if (is_a($i, Raw::class)) { |
|
329 | + $stubs[] = strval($i); |
|
330 | + }else { |
|
331 | + $stubs[] = '?'; |
|
332 | 332 | $params[] = $i; |
333 | 333 | } |
334 | 334 | } |
335 | 335 | $stubs = implode(',', $stubs); |
336 | 336 | $exprs[] = "$k $op ($stubs)"; |
337 | - }else if($op == 'BETWEEN'){ |
|
337 | + }else if ($op == 'BETWEEN') { |
|
338 | 338 | $cond = "$k BETWEEN"; |
339 | - if(is_a($var[0], Raw::class)){ |
|
339 | + if (is_a($var[0], Raw::class)) { |
|
340 | 340 | $cond = "$cond ".strval($var[0]); |
341 | - }else{ |
|
341 | + }else { |
|
342 | 342 | $cond = "$cond ?"; |
343 | 343 | $params[] = $var[0]; |
344 | 344 | } |
345 | - if(is_a($var[1], Raw::class)){ |
|
345 | + if (is_a($var[1], Raw::class)) { |
|
346 | 346 | $cond = "$cond AND ".strval($var[1]); |
347 | - }else{ |
|
347 | + }else { |
|
348 | 348 | $cond = "$cond AND ?"; |
349 | 349 | $params[] = $var[1]; |
350 | 350 | } |
351 | 351 | $exprs[] = $cond; |
352 | - }else{ |
|
353 | - if(is_a($var, Raw::class)){ |
|
352 | + }else { |
|
353 | + if (is_a($var, Raw::class)) { |
|
354 | 354 | $exprs[] = "$k $op ".strval($var); |
355 | - }else{ |
|
355 | + }else { |
|
356 | 356 | $exprs[] = "$k $op ?"; |
357 | 357 | $params[] = $var; |
358 | 358 | } |
359 | 359 | } |
360 | - }else{ |
|
361 | - if(is_a($v, Raw::class)){ |
|
360 | + }else { |
|
361 | + if (is_a($v, Raw::class)) { |
|
362 | 362 | $exprs[] = "$k = ".strval($v); |
363 | 363 | |
364 | - }else{ |
|
364 | + }else { |
|
365 | 365 | $exprs[] = "$k = ?"; |
366 | 366 | $params[] = $v; |
367 | 367 | } |
@@ -370,20 +370,20 @@ discard block |
||
370 | 370 | |
371 | 371 | return self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
372 | 372 | } |
373 | - static public function condition($context, $prefix, $expr, $args){ |
|
374 | - if(!empty($expr)){ |
|
375 | - if($args){ |
|
373 | + static public function condition($context, $prefix, $expr, $args) { |
|
374 | + if (!empty($expr)) { |
|
375 | + if ($args) { |
|
376 | 376 | //因为PDO不支持绑定数组变量, 这里需要手动展开数组 |
377 | 377 | //也就是说把 where("id IN(?)", [1,2]) 展开成 where("id IN(?,?)", 1,2) |
378 | 378 | $cutted = null; |
379 | 379 | $cut = null; |
380 | 380 | $toReplace = array(); |
381 | 381 | |
382 | - $newArgs=array(); |
|
382 | + $newArgs = array(); |
|
383 | 383 | //找到所有数组对应的?符位置 |
384 | - foreach ($args as $k =>$arg){ |
|
385 | - if(is_array($arg) || is_a($arg, Raw::class)){ |
|
386 | - if(!$cutted){ |
|
384 | + foreach ($args as $k =>$arg) { |
|
385 | + if (is_array($arg) || is_a($arg, Raw::class)) { |
|
386 | + if (!$cutted) { |
|
387 | 387 | $cut = new NestedStringCut($expr); |
388 | 388 | $cutted = $cut->getText(); |
389 | 389 | } |
@@ -393,46 +393,46 @@ discard block |
||
393 | 393 | $pos !== false or \PhpBoot\abort( |
394 | 394 | new \InvalidArgumentException("unmatched params and ? @ $expr")); |
395 | 395 | |
396 | - if(is_array($arg)){ |
|
396 | + if (is_array($arg)) { |
|
397 | 397 | $stubs = []; |
398 | - foreach ($arg as $i){ |
|
399 | - if(is_a($i, Raw::class)){ |
|
398 | + foreach ($arg as $i) { |
|
399 | + if (is_a($i, Raw::class)) { |
|
400 | 400 | $stubs[] = strval($i); |
401 | - }else{ |
|
401 | + }else { |
|
402 | 402 | $stubs[] = '?'; |
403 | 403 | $newArgs[] = $i; |
404 | 404 | } |
405 | 405 | } |
406 | 406 | $stubs = implode(',', $stubs); |
407 | - }else{ |
|
407 | + }else { |
|
408 | 408 | $stubs = strval($arg); |
409 | 409 | } |
410 | 410 | $toReplace[] = [$pos, $stubs]; |
411 | 411 | |
412 | - }else{ |
|
413 | - $newArgs[]=$arg; |
|
412 | + }else { |
|
413 | + $newArgs[] = $arg; |
|
414 | 414 | } |
415 | 415 | } |
416 | 416 | |
417 | - if(count($toReplace)){ |
|
417 | + if (count($toReplace)) { |
|
418 | 418 | $toReplace = array_reverse($toReplace); |
419 | - foreach ($toReplace as $i){ |
|
419 | + foreach ($toReplace as $i) { |
|
420 | 420 | list($pos, $v) = $i; |
421 | - $expr = substr($expr, 0, $pos).$v. substr($expr, $pos+1); |
|
421 | + $expr = substr($expr, 0, $pos).$v.substr($expr, $pos + 1); |
|
422 | 422 | } |
423 | 423 | $args = $newArgs; |
424 | 424 | } |
425 | 425 | } |
426 | 426 | $context->appendSql($prefix.' '.$expr); |
427 | - if($args){ |
|
427 | + if ($args) { |
|
428 | 428 | $context->appendParams($args); |
429 | 429 | } |
430 | 430 | } |
431 | 431 | } |
432 | 432 | } |
433 | 433 | |
434 | -class GroupByImpl{ |
|
435 | - static public function groupBy($context, $column){ |
|
434 | +class GroupByImpl { |
|
435 | + static public function groupBy($context, $column) { |
|
436 | 436 | $context->appendSql("GROUP BY $column"); |
437 | 437 | } |
438 | 438 | } |
@@ -456,20 +456,20 @@ discard block |
||
456 | 456 | * @param string|false $asDict return as dict or array |
457 | 457 | * @return false|array |
458 | 458 | */ |
459 | - static public function get($context, $dictAs=false){ |
|
459 | + static public function get($context, $dictAs = false) { |
|
460 | 460 | |
461 | 461 | $st = $context->connection->prepare($context->sql); |
462 | - if($st->execute($context->params)){ |
|
462 | + if ($st->execute($context->params)) { |
|
463 | 463 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
464 | - if ($dictAs){ |
|
465 | - $dict= []; |
|
466 | - foreach ($res as $i){ |
|
467 | - $dict[$i[$dictAs]]=$i; |
|
464 | + if ($dictAs) { |
|
465 | + $dict = []; |
|
466 | + foreach ($res as $i) { |
|
467 | + $dict[$i[$dictAs]] = $i; |
|
468 | 468 | } |
469 | 469 | return $context->handleResult($dict); |
470 | 470 | } |
471 | 471 | return $context->handleResult($res); |
472 | - }else{ |
|
472 | + }else { |
|
473 | 473 | return false; |
474 | 474 | } |
475 | 475 | } |
@@ -478,22 +478,22 @@ discard block |
||
478 | 478 | * @param Context $context |
479 | 479 | * @return int|false |
480 | 480 | */ |
481 | - static public function count($context){ |
|
481 | + static public function count($context) { |
|
482 | 482 | |
483 | 483 | $found = []; |
484 | - if(!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
485 | - count($found)==0){ |
|
484 | + if (!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
485 | + count($found) == 0) { |
|
486 | 486 | \PhpBoot\abort(new \PDOException("can not use count(*) without select")); |
487 | 487 | } |
488 | 488 | list($chars, $columnBegin) = $found[0]; |
489 | - $columnBegin = $columnBegin + strlen('select')+1; |
|
489 | + $columnBegin = $columnBegin + strlen('select') + 1; |
|
490 | 490 | |
491 | 491 | $columnEnd = 0; |
492 | 492 | $found = []; |
493 | - if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
494 | - count($found)==0){ |
|
493 | + if (!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
494 | + count($found) == 0) { |
|
495 | 495 | $columnEnd = strlen($context->sql); |
496 | - }else{ |
|
496 | + }else { |
|
497 | 497 | list($chars, $columnEnd) = $found[0]; |
498 | 498 | } |
499 | 499 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -501,10 +501,10 @@ discard block |
||
501 | 501 | $sql .= substr($context->sql, $columnEnd); |
502 | 502 | |
503 | 503 | $st = $context->connection->prepare($sql); |
504 | - if($st->execute($context->params)){ |
|
504 | + if ($st->execute($context->params)) { |
|
505 | 505 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
506 | 506 | return $res[0]['count']; |
507 | - }else{ |
|
507 | + }else { |
|
508 | 508 | return false; |
509 | 509 | } |
510 | 510 | |
@@ -512,54 +512,54 @@ discard block |
||
512 | 512 | } |
513 | 513 | class OnDuplicateKeyUpdateImpl |
514 | 514 | { |
515 | - public function set($context, $column, $value){ |
|
515 | + public function set($context, $column, $value) { |
|
516 | 516 | $prefix = ''; |
517 | - if($this->first){ |
|
517 | + if ($this->first) { |
|
518 | 518 | $this->first = false; |
519 | 519 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
520 | - }else{ |
|
520 | + }else { |
|
521 | 521 | $prefix = ','; |
522 | 522 | } |
523 | - if(is_a($value, Raw::class)){ |
|
524 | - $context->appendSql("$prefix$column=$value",$prefix == 'ON DUPLICATE KEY UPDATE '); |
|
525 | - }else{ |
|
526 | - $context->appendSql("$prefix$column=?",$prefix == 'ON DUPLICATE KEY UPDATE '); |
|
523 | + if (is_a($value, Raw::class)) { |
|
524 | + $context->appendSql("$prefix$column=$value", $prefix == 'ON DUPLICATE KEY UPDATE '); |
|
525 | + }else { |
|
526 | + $context->appendSql("$prefix$column=?", $prefix == 'ON DUPLICATE KEY UPDATE '); |
|
527 | 527 | $context->appendParams([$value]); |
528 | 528 | } |
529 | 529 | } |
530 | 530 | |
531 | - public function setExpr($context, $expr, $args){ |
|
531 | + public function setExpr($context, $expr, $args) { |
|
532 | 532 | $prefix = ''; |
533 | - if($this->first){ |
|
533 | + if ($this->first) { |
|
534 | 534 | $this->first = false; |
535 | 535 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
536 | - }else{ |
|
536 | + }else { |
|
537 | 537 | $prefix = ','; |
538 | 538 | } |
539 | 539 | |
540 | - $context->appendSql("$prefix$expr",$prefix == 'ON DUPLICATE KEY UPDATE '); |
|
540 | + $context->appendSql("$prefix$expr", $prefix == 'ON DUPLICATE KEY UPDATE '); |
|
541 | 541 | $context->appendParams($args); |
542 | 542 | |
543 | 543 | } |
544 | - public function setArgs($context, $values){ |
|
544 | + public function setArgs($context, $values) { |
|
545 | 545 | $set = []; |
546 | 546 | $params = []; |
547 | - foreach ($values as $k=>$v){ |
|
548 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
549 | - $set[]= "$k=".$v->get(); |
|
550 | - }else{ |
|
551 | - $set[]= "$k=?"; |
|
552 | - $params[]=$v; |
|
547 | + foreach ($values as $k=>$v) { |
|
548 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
549 | + $set[] = "$k=".$v->get(); |
|
550 | + }else { |
|
551 | + $set[] = "$k=?"; |
|
552 | + $params[] = $v; |
|
553 | 553 | } |
554 | 554 | } |
555 | - if($this->first){ |
|
555 | + if ($this->first) { |
|
556 | 556 | $this->first = false; |
557 | 557 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
558 | 558 | $context->appendParams($params); |
559 | - }else{ |
|
560 | - $context->appendSql(','.implode(',', $set),false); |
|
559 | + }else { |
|
560 | + $context->appendSql(','.implode(',', $set), false); |
|
561 | 561 | $context->appendParams($params); |
562 | 562 | } |
563 | 563 | } |
564 | - private $first=true; |
|
564 | + private $first = true; |
|
565 | 565 | } |
@@ -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 | } |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | * |
73 | 73 | * @return \PhpBoot\DB\rules\basic\LimitRule |
74 | 74 | */ |
75 | - public function orderBy($column, $order=null) { |
|
75 | + public function orderBy($column, $order = null) { |
|
76 | 76 | $this->impl->orderBy($this->context, $column, $order); |
77 | 77 | return new LimitRule($this->context); |
78 | 78 | } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | * @param mixed $_ |
92 | 92 | * @return \PhpBoot\DB\rules\basic\OrderByRule |
93 | 93 | */ |
94 | - public function where($expr, $_= null) { |
|
94 | + public function where($expr, $_ = null) { |
|
95 | 95 | WhereImpl::where($this->context, $expr, array_slice(func_get_args(), 1)); |
96 | 96 | return new OrderByRule($this->context); |
97 | 97 | } |
@@ -89,7 +89,7 @@ |
||
89 | 89 | * @param mixed $value |
90 | 90 | * @return \PhpBoot\DB\rules\basic\ExecRule |
91 | 91 | */ |
92 | - public function onDuplicateKeyUpdateExpr($expr, $_=null) { |
|
92 | + public function onDuplicateKeyUpdateExpr($expr, $_ = null) { |
|
93 | 93 | $this->impl->setExpr($this->context, $expr, array_slice(func_get_args(), 1)); |
94 | 94 | return new ExecRule($this->context); |
95 | 95 | } |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | |
25 | 25 | class UpdateSetRule extends WhereRule |
26 | 26 | { |
27 | - public function __construct($context){ |
|
27 | + public function __construct($context) { |
|
28 | 28 | parent::__construct($context); |
29 | 29 | $this->impl = new UpdateSetImpl(); |
30 | 30 | } |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | * @param mixed $_ |
56 | 56 | * @return \PhpBoot\DB\rules\update\UpdateSetRule |
57 | 57 | */ |
58 | - public function setExpr($expr, $_=null) { |
|
58 | + public function setExpr($expr, $_ = null) { |
|
59 | 59 | $this->impl->setExpr($this->context, $expr, array_slice(func_get_args(), 1)); |
60 | 60 | return $this; |
61 | 61 | } |