@@ -15,19 +15,19 @@ |
||
15 | 15 | { |
16 | 16 | $response = new Response(); |
17 | 17 | $response->headers->set('Content-Type', 'application/json'); |
18 | - foreach ($output as $key=>$value){ |
|
18 | + foreach ($output as $key=>$value) { |
|
19 | 19 | //TODO 支持自定义格式输出 |
20 | 20 | //TODO 支持更多的输出目标 |
21 | - if($key == 'content'){ |
|
21 | + if ($key == 'content') { |
|
22 | 22 | //if(is_array($value) || is_object($value)){ |
23 | 23 | $value = json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
24 | 24 | //} |
25 | 25 | $response->setContent($value); |
26 | - }elseif($key == 'headers'){ |
|
27 | - foreach ($value as $k=>$v){ |
|
26 | + }elseif ($key == 'headers') { |
|
27 | + foreach ($value as $k=>$v) { |
|
28 | 28 | $response->headers->set($k, $v); |
29 | 29 | } |
30 | - }else{ |
|
30 | + }else { |
|
31 | 31 | \PhpBoot\abort(new \UnexpectedValueException("Unexpected output target $key")); |
32 | 32 | } |
33 | 33 |
@@ -23,11 +23,11 @@ |
||
23 | 23 | $value = json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
24 | 24 | //} |
25 | 25 | $response->setContent($value); |
26 | - }elseif($key == 'headers'){ |
|
26 | + } elseif($key == 'headers'){ |
|
27 | 27 | foreach ($value as $k=>$v){ |
28 | 28 | $response->headers->set($k, $v); |
29 | 29 | } |
30 | - }else{ |
|
30 | + } else{ |
|
31 | 31 | \PhpBoot\abort(new \UnexpectedValueException("Unexpected output target $key")); |
32 | 32 | } |
33 | 33 |
@@ -41,6 +41,9 @@ discard block |
||
41 | 41 | |
42 | 42 | class OnDuplicateKeyUpdateRule extends ExecRule |
43 | 43 | { |
44 | + /** |
|
45 | + * @param \PhpBoot\DB\Context $context |
|
46 | + */ |
|
44 | 47 | public function __construct($context) |
45 | 48 | { |
46 | 49 | parent::__construct($context); |
@@ -91,8 +94,6 @@ discard block |
||
91 | 94 | * ->onDuplicateKeyUpdate('a=a+1') |
92 | 95 | * => "INSERT INTO table(a,b) VALUES(1,now()) ON DUPLICATE KEY UPDATE a=a+1" |
93 | 96 | * |
94 | - * @param string $column |
|
95 | - * @param mixed $value |
|
96 | 97 | * @return \PhpBoot\DB\rules\basic\ExecRule |
97 | 98 | */ |
98 | 99 | public function onDuplicateKeyUpdate($expr, $_=null) { |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | * @param array $values |
45 | 45 | * @return \PhpBoot\DB\rules\insert\OnDuplicateKeyUpdateRule |
46 | 46 | */ |
47 | - public function batchValues(array $values){ |
|
47 | + public function batchValues(array $values) { |
|
48 | 48 | ValuesImpl::batchValues($this->context, $values); |
49 | 49 | return new OnDuplicateKeyUpdateRule($this->context); |
50 | 50 | } |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | * @param mixed $value |
107 | 107 | * @return \PhpBoot\DB\rules\basic\ExecRule |
108 | 108 | */ |
109 | - public function onDuplicateKeyUpdate($expr, $_=null) { |
|
109 | + public function onDuplicateKeyUpdate($expr, $_ = null) { |
|
110 | 110 | $this->impl->set($this->context, $expr, array_slice(func_get_args(), 1)); |
111 | 111 | return new ExecRule($this->context); |
112 | 112 | } |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | */ |
28 | 28 | public function eraseMapping($target) |
29 | 29 | { |
30 | - if(!isset($this->mappings[$target])){ |
|
30 | + if (!isset($this->mappings[$target])) { |
|
31 | 31 | return null; |
32 | 32 | } |
33 | 33 | $ori = $this->mappings[$target]; |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | */ |
42 | 42 | public function getMapping($target) |
43 | 43 | { |
44 | - if(!array_key_exists($target, $this->mappings)){ |
|
44 | + if (!array_key_exists($target, $this->mappings)) { |
|
45 | 45 | return null; |
46 | 46 | } |
47 | 47 | return $this->mappings[$target]; |
@@ -53,12 +53,12 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function getMappingBySource($source) |
55 | 55 | { |
56 | - foreach ($this->mappings as $k=>$v){ |
|
57 | - if($v->source == $source){ |
|
56 | + foreach ($this->mappings as $k=>$v) { |
|
57 | + if ($v->source == $source) { |
|
58 | 58 | return [$k, $v]; |
59 | 59 | } |
60 | 60 | } |
61 | - return [null,null]; |
|
61 | + return [null, null]; |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | |
@@ -75,15 +75,15 @@ discard block |
||
75 | 75 | 'params'=>$params |
76 | 76 | ]; |
77 | 77 | |
78 | - if($return instanceof Response){ //直接返回Response时, 对return不再做映射 |
|
78 | + if ($return instanceof Response) { //直接返回Response时, 对return不再做映射 |
|
79 | 79 | return $return; |
80 | 80 | } |
81 | 81 | $mappings = $this->getMappings(); |
82 | 82 | |
83 | 83 | $output = []; |
84 | - foreach($mappings as $key=>$map){ |
|
84 | + foreach ($mappings as $key=>$map) { |
|
85 | 85 | $val = \JmesPath\search($map->source, $input); |
86 | - if(substr($key, 0, strlen('response.')) == 'response.'){ |
|
86 | + if (substr($key, 0, strlen('response.')) == 'response.') { |
|
87 | 87 | $key = substr($key, strlen('response.')); |
88 | 88 | } |
89 | 89 | ArrayHelper::set($output, $key, $val); |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | 'DELETE' |
44 | 44 | ]) or \PhpBoot\abort(new AnnotationSyntaxException("unknown method http $httpMethod in {$container->getClassName()}::$target")); |
45 | 45 | //获取方法参数信息 |
46 | - $rfl = new \ReflectionClass($container->getClassName()); |
|
46 | + $rfl = new \ReflectionClass($container->getClassName()); |
|
47 | 47 | $method = $rfl->getMethod($target); |
48 | 48 | $methodParams = $method->getParameters(); |
49 | 49 | |
@@ -68,9 +68,9 @@ discard block |
||
68 | 68 | $routeParser = new Std(); |
69 | 69 | $uri = $params->getParam(1); |
70 | 70 | $info = $routeParser->parse($uri); //0.4和1.0返回值不同, 不兼容 |
71 | - if(isset($info[0])){ |
|
72 | - foreach ($info[0] as $i){ |
|
73 | - if(is_array($i)) { |
|
71 | + if (isset($info[0])) { |
|
72 | + foreach ($info[0] as $i) { |
|
73 | + if (is_array($i)) { |
|
74 | 74 | $route->addPathParam($i[0]); |
75 | 75 | } |
76 | 76 | } |
@@ -79,31 +79,31 @@ discard block |
||
79 | 79 | $hasRefParam = false; |
80 | 80 | //设置参数列表 |
81 | 81 | $paramsMeta = []; |
82 | - foreach ($methodParams as $param){ |
|
82 | + foreach ($methodParams as $param) { |
|
83 | 83 | $paramName = $param->getName(); |
84 | 84 | $source = "request.$paramName"; |
85 | - if($route->hasPathParam($paramName)){ //参数来自路由 |
|
85 | + if ($route->hasPathParam($paramName)) { //参数来自路由 |
|
86 | 86 | $source = "request.$paramName"; |
87 | - }elseif($httpMethod == 'GET'){ |
|
87 | + }elseif ($httpMethod == 'GET') { |
|
88 | 88 | $source = "request.$paramName"; //GET请求显示指定来自query string |
89 | 89 | } |
90 | 90 | $paramClass = $param->getClass(); |
91 | - if($paramClass){ |
|
91 | + if ($paramClass) { |
|
92 | 92 | $paramClass = $paramClass->getName(); |
93 | 93 | } |
94 | 94 | $entityContainer = ContainerFactory::create($entityBuilder, $paramClass); |
95 | 95 | $meta = new ParamMeta($paramName, |
96 | 96 | $source, |
97 | - $paramClass?:'mixed', |
|
97 | + $paramClass ?: 'mixed', |
|
98 | 98 | $param->isOptional(), |
99 | - $param->isOptional()?$param->getDefaultValue():null, |
|
99 | + $param->isOptional() ? $param->getDefaultValue() : null, |
|
100 | 100 | $param->isPassedByReference(), |
101 | 101 | null, |
102 | 102 | '', |
103 | 103 | $entityContainer |
104 | 104 | ); |
105 | 105 | $paramsMeta[] = $meta; |
106 | - if($meta->isPassedByReference){ |
|
106 | + if ($meta->isPassedByReference) { |
|
107 | 107 | $hasRefParam = true; |
108 | 108 | $responseHandler->setMapping('response.content.'.$meta->name, new ReturnMeta( |
109 | 109 | 'params.'.$meta->name, |
@@ -114,16 +114,16 @@ discard block |
||
114 | 114 | } |
115 | 115 | |
116 | 116 | $requestHandler->setParamMetas($paramsMeta); |
117 | - if(!$hasRefParam){ |
|
118 | - $responseHandler->setMapping('response.content', new ReturnMeta('return','mixed','', new MixedTypeContainer())); |
|
119 | - }else{ |
|
117 | + if (!$hasRefParam) { |
|
118 | + $responseHandler->setMapping('response.content', new ReturnMeta('return', 'mixed', '', new MixedTypeContainer())); |
|
119 | + }else { |
|
120 | 120 | //当存在引用参数作为输出时, 默认将 return 数据绑定的到 data 下, 以防止和引用参数作为输出重叠 |
121 | - $responseHandler->setMapping($this->returnTarget, new ReturnMeta('return','mixed','', new MixedTypeContainer())); |
|
121 | + $responseHandler->setMapping($this->returnTarget, new ReturnMeta('return', 'mixed', '', new MixedTypeContainer())); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | |
125 | 125 | $container->addRoute($target, $route); |
126 | 126 | } |
127 | 127 | |
128 | - public $returnTarget='response.content.data'; |
|
128 | + public $returnTarget = 'response.content.data'; |
|
129 | 129 | } |
130 | 130 | \ No newline at end of file |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | $source = "request.$paramName"; |
85 | 85 | if($route->hasPathParam($paramName)){ //参数来自路由 |
86 | 86 | $source = "request.$paramName"; |
87 | - }elseif($httpMethod == 'GET'){ |
|
87 | + } elseif($httpMethod == 'GET'){ |
|
88 | 88 | $source = "request.$paramName"; //GET请求显示指定来自query string |
89 | 89 | } |
90 | 90 | $paramClass = $param->getClass(); |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | $requestHandler->setParamMetas($paramsMeta); |
117 | 117 | if(!$hasRefParam){ |
118 | 118 | $responseHandler->setMapping('response.content', new ReturnMeta('return','mixed','', new MixedTypeContainer())); |
119 | - }else{ |
|
119 | + } else{ |
|
120 | 120 | //当存在引用参数作为输出时, 默认将 return 数据绑定的到 data 下, 以防止和引用参数作为输出重叠 |
121 | 121 | $responseHandler->setMapping($this->returnTarget, new ReturnMeta('return','mixed','', new MixedTypeContainer())); |
122 | 122 | } |
@@ -48,6 +48,9 @@ |
||
48 | 48 | |
49 | 49 | class OrderByRule extends LimitRule |
50 | 50 | { |
51 | + /** |
|
52 | + * @param Context $context |
|
53 | + */ |
|
51 | 54 | public function __construct($context){ |
52 | 55 | parent::__construct($context); |
53 | 56 | $this->impl = new OrderByImpl(); |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | |
12 | 12 | class BasicRule |
13 | 13 | { |
14 | - public function __construct(Context $context){ |
|
14 | + public function __construct(Context $context) { |
|
15 | 15 | $this->context = $context; |
16 | 16 | } |
17 | 17 | |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | |
49 | 49 | class OrderByRule extends LimitRule |
50 | 50 | { |
51 | - public function __construct($context){ |
|
51 | + public function __construct($context) { |
|
52 | 52 | parent::__construct($context); |
53 | 53 | $this->impl = new OrderByImpl(); |
54 | 54 | } |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | * |
64 | 64 | * @return \PhpBoot\DB\rules\basic\LimitRule |
65 | 65 | */ |
66 | - public function orderBy($column, $order=null) { |
|
66 | + public function orderBy($column, $order = null) { |
|
67 | 67 | $this->impl->orderBy($this->context, $column, $order); |
68 | 68 | return new LimitRule($this->context); |
69 | 69 | } |
@@ -95,17 +95,17 @@ discard block |
||
95 | 95 | * @param mixed $_ |
96 | 96 | * @return NextWhereRule |
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 NextWhereRule($this->context, false); |
@@ -133,9 +133,9 @@ discard block |
||
133 | 133 | * @param mixed $_ |
134 | 134 | * @return WhereRule |
135 | 135 | */ |
136 | - public function orWhere($conditions=null, $_=null) { |
|
137 | - if(is_callable($conditions)){ |
|
138 | - $callback = function ($context)use($conditions){ |
|
136 | + public function orWhere($conditions = null, $_ = null) { |
|
137 | + if (is_callable($conditions)) { |
|
138 | + $callback = function($context)use($conditions){ |
|
139 | 139 | $rule = new ScopedQuery($context); |
140 | 140 | $conditions($rule); |
141 | 141 | }; |
@@ -160,17 +160,17 @@ discard block |
||
160 | 160 | * @param null $_ |
161 | 161 | * @return NextScopedQuery |
162 | 162 | */ |
163 | - public function where($expr, $_= null){ |
|
164 | - if(is_callable($expr)){ |
|
165 | - $callback = function ($context)use($expr){ |
|
163 | + public function where($expr, $_ = null) { |
|
164 | + if (is_callable($expr)) { |
|
165 | + $callback = function($context)use($expr){ |
|
166 | 166 | $rule = new ScopedQuery($context, true); |
167 | 167 | $expr($rule); |
168 | 168 | }; |
169 | 169 | $expr = $callback; |
170 | 170 | } |
171 | - if($this->isTheFirst){ |
|
171 | + if ($this->isTheFirst) { |
|
172 | 172 | WhereImpl::where($this->context, '', $expr, array_slice(func_get_args(), 1)); |
173 | - }else{ |
|
173 | + }else { |
|
174 | 174 | WhereImpl::where($this->context, 'AND', $expr, array_slice(func_get_args(), 1)); |
175 | 175 | } |
176 | 176 | return new NextScopedQuery($this->context, false); |
@@ -186,9 +186,9 @@ discard block |
||
186 | 186 | * @param null $_ |
187 | 187 | * @return ScopedQuery |
188 | 188 | */ |
189 | - public function orWhere($expr, $_= null){ |
|
190 | - if(is_callable($expr)){ |
|
191 | - $callback = function ($context)use($expr){ |
|
189 | + public function orWhere($expr, $_ = null) { |
|
190 | + if (is_callable($expr)) { |
|
191 | + $callback = function($context)use($expr){ |
|
192 | 192 | $rule = new ScopedQuery($context, true); |
193 | 193 | $expr($rule); |
194 | 194 | }; |
@@ -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 NextWhereRule($this->context, false); |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | } |
171 | 171 | if($this->isTheFirst){ |
172 | 172 | WhereImpl::where($this->context, '', $expr, array_slice(func_get_args(), 1)); |
173 | - }else{ |
|
173 | + } else{ |
|
174 | 174 | WhereImpl::where($this->context, 'AND', $expr, array_slice(func_get_args(), 1)); |
175 | 175 | } |
176 | 176 | return new NextScopedQuery($this->context, false); |
@@ -30,7 +30,6 @@ |
||
30 | 30 | } |
31 | 31 | /** |
32 | 32 | * update('table')->set(['a'=>1]) => "UPDATE table SET a=1" |
33 | - |
|
34 | 33 | * update('table')->set('a=?',1) => "UPDATE table SET a=1" |
35 | 34 | * @param array|string $expr |
36 | 35 | * @param mixed $_ |
@@ -25,6 +25,9 @@ |
||
25 | 25 | |
26 | 26 | class UpdateSetRule extends BasicRule |
27 | 27 | { |
28 | + /** |
|
29 | + * @param Context $context |
|
30 | + */ |
|
28 | 31 | public function __construct($context){ |
29 | 32 | parent::__construct($context); |
30 | 33 | $this->impl = new UpdateSetImpl(); |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | |
26 | 26 | class UpdateSetRule extends BasicRule |
27 | 27 | { |
28 | - public function __construct($context){ |
|
28 | + public function __construct($context) { |
|
29 | 29 | parent::__construct($context); |
30 | 30 | $this->impl = new UpdateSetImpl(); |
31 | 31 | } |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * @param mixed $_ |
38 | 38 | * @return UpdateSetWhereRule |
39 | 39 | */ |
40 | - public function set($expr, $_=null) { |
|
40 | + public function set($expr, $_ = null) { |
|
41 | 41 | $this->impl->set($this->context, $expr, array_slice(func_get_args(), 1)); |
42 | 42 | return new UpdateSetWhereRule($this->context, $this->impl); |
43 | 43 | } |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | |
47 | 47 | class UpdateSetWhereRule extends WhereRule |
48 | 48 | { |
49 | - public function __construct(Context $context, UpdateSetImpl $impl){ |
|
49 | + public function __construct(Context $context, UpdateSetImpl $impl) { |
|
50 | 50 | parent::__construct($context); |
51 | 51 | $this->impl = $impl; |
52 | 52 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * @param mixed $_ |
58 | 58 | * @return UpdateSetWhereRule |
59 | 59 | */ |
60 | - public function set($expr, $_=null) { |
|
60 | + public function set($expr, $_ = null) { |
|
61 | 61 | $this->impl->set($this->context, $expr, array_slice(func_get_args(), 1)); |
62 | 62 | return new UpdateSetWhereRule($this->context, $this->impl); |
63 | 63 | } |
@@ -21,20 +21,20 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public function __invoke(ControllerContainer $container, $ann, EntityContainerBuilder $entityBuilder) |
23 | 23 | { |
24 | - if(!$ann->parent){ |
|
24 | + if (!$ann->parent) { |
|
25 | 25 | //Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()} should be used with parent route"); |
26 | 26 | return; |
27 | 27 | } |
28 | 28 | $target = $ann->parent->name; |
29 | 29 | $route = $container->getRoute($target); |
30 | - if(!$route){ |
|
30 | + if (!$route) { |
|
31 | 31 | //Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::$target should be used with parent route"); |
32 | - return ; |
|
32 | + return; |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | $params = new AnnotationParams($ann->description, 2); |
36 | 36 | $type = $doc = null; |
37 | - if(count($params)>0){ |
|
37 | + if (count($params)>0) { |
|
38 | 38 | $type = TypeHint::normalize($params[0], $container->getClassName()); |
39 | 39 | } |
40 | 40 | $doc = $params->getRawParam(1, ''); |
@@ -42,10 +42,10 @@ discard block |
||
42 | 42 | list($_, $meta) = $route |
43 | 43 | ->getResponseHandler() |
44 | 44 | ->getMappingBySource('return'); |
45 | - if($meta){ |
|
45 | + if ($meta) { |
|
46 | 46 | $meta->description = $doc; |
47 | 47 | $meta->type = $type; |
48 | - $meta->container = $type == 'void'?null:ContainerFactory::create($entityBuilder, $type); |
|
48 | + $meta->container = $type == 'void' ?null:ContainerFactory::create($entityBuilder, $type); |
|
49 | 49 | } |
50 | 50 | } |
51 | 51 | } |
52 | 52 | \ No newline at end of file |
@@ -113,6 +113,9 @@ discard block |
||
113 | 113 | |
114 | 114 | class OrderByRule extends LimitRule |
115 | 115 | { |
116 | + /** |
|
117 | + * @param Context $context |
|
118 | + */ |
|
116 | 119 | public function __construct($context){ |
117 | 120 | parent::__construct($context); |
118 | 121 | $this->order = new OrderByImpl(); |
@@ -260,7 +263,7 @@ discard block |
||
260 | 263 | * "WHERE a=1 AND b IN(1,2) AND c BETWEEN 1 AND 2 AND d<>1" |
261 | 264 | * |
262 | 265 | * @param string|array|callable $conditions |
263 | - * @param mixed $_ |
|
266 | + * @param string $_ |
|
264 | 267 | * @return \PhpBoot\DB\rules\select\NextWhereRule |
265 | 268 | */ |
266 | 269 | public function where($conditions=null, $_=null) { |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | * @param string|false $asDict |
40 | 40 | * @return array |
41 | 41 | */ |
42 | - public function get($asDict=false) { |
|
42 | + public function get($asDict = false) { |
|
43 | 43 | return ExecImpl::get($this->context, $asDict); |
44 | 44 | } |
45 | 45 | |
@@ -53,9 +53,9 @@ discard block |
||
53 | 53 | * Execute sql and get one response |
54 | 54 | * @return null |
55 | 55 | */ |
56 | - public function getFirst(){ |
|
56 | + public function getFirst() { |
|
57 | 57 | $res = ExecImpl::get($this->context); |
58 | - if(count($res)){ |
|
58 | + if (count($res)) { |
|
59 | 59 | return $res[0]; |
60 | 60 | } |
61 | 61 | return null; |
@@ -68,8 +68,8 @@ discard block |
||
68 | 68 | * @param string $table |
69 | 69 | * @return \PhpBoot\DB\rules\select\JoinRule |
70 | 70 | */ |
71 | - public function from($table, $as=null){ |
|
72 | - FromImpl::from($this->context, $table,$as); |
|
71 | + public function from($table, $as = null) { |
|
72 | + FromImpl::from($this->context, $table, $as); |
|
73 | 73 | return new JoinRule($this->context); |
74 | 74 | } |
75 | 75 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | * @param string $column |
81 | 81 | * @return \PhpBoot\DB\rules\select\GetRule |
82 | 82 | */ |
83 | - public function of($column){ |
|
83 | + public function of($column) { |
|
84 | 84 | ForUpdateOfImpl::of($this->context, $column); |
85 | 85 | return new GetRule($this->context); |
86 | 86 | } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | * forUpdate() => 'FOR UPDATE' |
92 | 92 | * @return \PhpBoot\DB\rules\select\ForUpdateOfRule |
93 | 93 | */ |
94 | - public function forUpdate(){ |
|
94 | + public function forUpdate() { |
|
95 | 95 | ForUpdateImpl::forUpdate($this->context); |
96 | 96 | return new ForUpdateOfRule($this->context); |
97 | 97 | } |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | |
114 | 114 | class OrderByRule extends LimitRule |
115 | 115 | { |
116 | - public function __construct($context){ |
|
116 | + public function __construct($context) { |
|
117 | 117 | parent::__construct($context); |
118 | 118 | $this->order = new OrderByImpl(); |
119 | 119 | } |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | * @param string $order Sql::ORDER_BY_ASC or Sql::ORDER_BY_DESC |
129 | 129 | * @return \PhpBoot\DB\rules\select\OrderByRule |
130 | 130 | */ |
131 | - public function orderBy($column, $order=null) { |
|
131 | + public function orderBy($column, $order = null) { |
|
132 | 132 | $this->order->orderBy($this->context, $column, $order); |
133 | 133 | return $this; |
134 | 134 | } |
@@ -174,17 +174,17 @@ discard block |
||
174 | 174 | * @param string $_ |
175 | 175 | * @return \PhpBoot\DB\rules\select\HavingRule |
176 | 176 | */ |
177 | - public function having($expr, $_=null) { |
|
178 | - if(is_callable($expr)){ |
|
179 | - $callback = function ($context)use($expr){ |
|
177 | + public function having($expr, $_ = null) { |
|
178 | + if (is_callable($expr)) { |
|
179 | + $callback = function($context)use($expr){ |
|
180 | 180 | $rule = new 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\NextWhereRule |
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 NextWhereRule($this->context, false); |
@@ -304,9 +304,9 @@ discard block |
||
304 | 304 | * |
305 | 305 | * @TODO orWhere 只能跟在 Where 后 |
306 | 306 | */ |
307 | - public function orWhere($conditions=null, $_=null) { |
|
308 | - if(is_callable($conditions)){ |
|
309 | - $callback = function ($context)use($conditions){ |
|
307 | + public function orWhere($conditions = null, $_ = null) { |
|
308 | + if (is_callable($conditions)) { |
|
309 | + $callback = function($context)use($conditions){ |
|
310 | 310 | $rule = new ScopedQuery($context); |
311 | 311 | $conditions($rule); |
312 | 312 | }; |
@@ -325,8 +325,8 @@ discard block |
||
325 | 325 | * @param string $table |
326 | 326 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
327 | 327 | */ |
328 | - public function join($table){ |
|
329 | - JoinImpl::join($this->context,null, $table); |
|
328 | + public function join($table) { |
|
329 | + JoinImpl::join($this->context, null, $table); |
|
330 | 330 | return new JoinOnRule($this->context); |
331 | 331 | } |
332 | 332 | /** |
@@ -334,8 +334,8 @@ discard block |
||
334 | 334 | * @param string $table |
335 | 335 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
336 | 336 | */ |
337 | - public function leftJoin($table){ |
|
338 | - JoinImpl::join($this->context,'LEFT', $table); |
|
337 | + public function leftJoin($table) { |
|
338 | + JoinImpl::join($this->context, 'LEFT', $table); |
|
339 | 339 | return new JoinOnRule($this->context); |
340 | 340 | } |
341 | 341 | /** |
@@ -344,7 +344,7 @@ discard block |
||
344 | 344 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
345 | 345 | */ |
346 | 346 | public function rightJoin($table) { |
347 | - JoinImpl::join($this->context,'RIGHT', $table); |
|
347 | + JoinImpl::join($this->context, 'RIGHT', $table); |
|
348 | 348 | return new JoinOnRule($this->context); |
349 | 349 | } |
350 | 350 | /** |
@@ -353,7 +353,7 @@ discard block |
||
353 | 353 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
354 | 354 | */ |
355 | 355 | public function innerJoin($table) { |
356 | - JoinImpl::join($this->context,'INNER', $table); |
|
356 | + JoinImpl::join($this->context, 'INNER', $table); |
|
357 | 357 | return new JoinOnRule($this->context); |
358 | 358 | } |
359 | 359 | } |
@@ -365,7 +365,7 @@ discard block |
||
365 | 365 | * @param string $condition |
366 | 366 | * @return \PhpBoot\DB\rules\select\JoinRule |
367 | 367 | */ |
368 | - public function on($condition){ |
|
368 | + public function on($condition) { |
|
369 | 369 | JoinOnImpl::on($this->context, $condition); |
370 | 370 | return new JoinRule($this->context); |
371 | 371 | } |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | } |
185 | 185 | if($this->isTheFirst){ |
186 | 186 | WhereImpl::where($this->context, 'HAVING', $expr, array_slice(func_get_args(), 1)); |
187 | - }else{ |
|
187 | + } else{ |
|
188 | 188 | WhereImpl::where($this->context, 'AND', $expr, array_slice(func_get_args(), 1)); |
189 | 189 | } |
190 | 190 | |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | } |
274 | 274 | if($this->isTheFirst){ |
275 | 275 | WhereImpl::where($this->context, 'WHERE' ,$conditions, array_slice(func_get_args(), 1)); |
276 | - }else{ |
|
276 | + } else{ |
|
277 | 277 | WhereImpl::where($this->context, 'AND', $conditions, array_slice(func_get_args(), 1)); |
278 | 278 | } |
279 | 279 | return new NextWhereRule($this->context, false); |
@@ -119,7 +119,7 @@ |
||
119 | 119 | } |
120 | 120 | class ValuesImpl |
121 | 121 | { |
122 | - static public function values(Context $context, array $values){ |
|
122 | + static public function values(Context $context, array $values){ |
|
123 | 123 | $params = []; |
124 | 124 | $stubs = []; |
125 | 125 | foreach ($values as $v){ |
@@ -9,6 +9,12 @@ discard block |
||
9 | 9 | use PhpBoot\DB\Context; |
10 | 10 | |
11 | 11 | class ExecResult{ |
12 | + |
|
13 | + /** |
|
14 | + * @param boolean $success |
|
15 | + * @param \PDO $pdo |
|
16 | + * @param \PDOStatement $st |
|
17 | + */ |
|
12 | 18 | public function __construct($success, $pdo, $st){ |
13 | 19 | $this->pdo = $pdo; |
14 | 20 | $this->st = $st; |
@@ -42,6 +48,10 @@ discard block |
||
42 | 48 | |
43 | 49 | class SelectImpl |
44 | 50 | { |
51 | + /** |
|
52 | + * @param Context $context |
|
53 | + * @param string $columns |
|
54 | + */ |
|
45 | 55 | static public function select($context, $columns){ |
46 | 56 | $context->appendSql("SELECT $columns"); |
47 | 57 | } |
@@ -49,6 +59,10 @@ discard block |
||
49 | 59 | |
50 | 60 | class FromImpl |
51 | 61 | { |
62 | + /** |
|
63 | + * @param Context $context |
|
64 | + * @param string $tables |
|
65 | + */ |
|
52 | 66 | static public function from($context, $tables,$as=null){ |
53 | 67 | if($tables instanceof BasicRule){ |
54 | 68 | $context->appendSql("FROM (".$tables->context->sql.')'); |
@@ -64,6 +78,10 @@ discard block |
||
64 | 78 | |
65 | 79 | class DeleteImpl |
66 | 80 | { |
81 | + /** |
|
82 | + * @param Context $context |
|
83 | + * @param string $from |
|
84 | + */ |
|
67 | 85 | static public function deleteFrom($context, $from) |
68 | 86 | { |
69 | 87 | $context->appendSql("DELETE FROM ".DB::wrap($from)); |
@@ -72,6 +90,11 @@ discard block |
||
72 | 90 | |
73 | 91 | class JoinImpl |
74 | 92 | { |
93 | + /** |
|
94 | + * @param Context $context |
|
95 | + * @param null|string $type |
|
96 | + * @param string $table |
|
97 | + */ |
|
75 | 98 | static public function join($context, $type, $table) { |
76 | 99 | $table = DB::wrap($table); |
77 | 100 | if($type){ |
@@ -84,6 +107,10 @@ discard block |
||
84 | 107 | |
85 | 108 | class JoinOnImpl |
86 | 109 | { |
110 | + /** |
|
111 | + * @param Context $context |
|
112 | + * @param string $condition |
|
113 | + */ |
|
87 | 114 | static public function on($context, $condition) { |
88 | 115 | $context->appendSql("ON $condition"); |
89 | 116 | } |
@@ -91,6 +118,9 @@ discard block |
||
91 | 118 | |
92 | 119 | class ForUpdateImpl |
93 | 120 | { |
121 | + /** |
|
122 | + * @param Context $context |
|
123 | + */ |
|
94 | 124 | static public function forUpdate($context){ |
95 | 125 | $context->appendSql("FOR UPDATE"); |
96 | 126 | } |
@@ -98,6 +128,10 @@ discard block |
||
98 | 128 | |
99 | 129 | class ForUpdateOfImpl |
100 | 130 | { |
131 | + /** |
|
132 | + * @param Context $context |
|
133 | + * @param string $column |
|
134 | + */ |
|
101 | 135 | static public function of($context, $column){ |
102 | 136 | $column = DB::wrap($column); |
103 | 137 | $context->appendSql("OF $column"); |
@@ -106,6 +140,10 @@ discard block |
||
106 | 140 | |
107 | 141 | class InsertImpl |
108 | 142 | { |
143 | + /** |
|
144 | + * @param Context $context |
|
145 | + * @param string $table |
|
146 | + */ |
|
109 | 147 | static public function insertInto($context, $table) { |
110 | 148 | $table = DB::wrap($table); |
111 | 149 | $context->appendSql("INSERT INTO $table"); |
@@ -113,6 +151,10 @@ discard block |
||
113 | 151 | } |
114 | 152 | class ReplaceImpl |
115 | 153 | { |
154 | + /** |
|
155 | + * @param Context $context |
|
156 | + * @param string $table |
|
157 | + */ |
|
116 | 158 | static public function replaceInto($context, $table) { |
117 | 159 | $table = DB::wrap($table); |
118 | 160 | $context->appendSql("REPLACE INTO $table"); |
@@ -196,6 +238,10 @@ discard block |
||
196 | 238 | |
197 | 239 | class UpdateImpl |
198 | 240 | { |
241 | + /** |
|
242 | + * @param Context $context |
|
243 | + * @param string $table |
|
244 | + */ |
|
199 | 245 | static public function update($context, $table){ |
200 | 246 | $table = DB::wrap($table); |
201 | 247 | $context->appendSql("UPDATE $table"); |
@@ -212,6 +258,9 @@ discard block |
||
212 | 258 | } |
213 | 259 | } |
214 | 260 | |
261 | + /** |
|
262 | + * @param string $expr |
|
263 | + */ |
|
215 | 264 | public function setExpr(Context $context, $expr, $args){ |
216 | 265 | if($this->first){ |
217 | 266 | $this->first = false; |
@@ -274,6 +323,11 @@ discard block |
||
274 | 323 | } |
275 | 324 | return $this; |
276 | 325 | } |
326 | + |
|
327 | + /** |
|
328 | + * @param string $column |
|
329 | + * @param string $order |
|
330 | + */ |
|
277 | 331 | public function orderBy(Context $context, $column, $order=null){ |
278 | 332 | if(is_string($column)){ |
279 | 333 | if($order === null){ |
@@ -291,12 +345,20 @@ discard block |
||
291 | 345 | |
292 | 346 | class LimitImpl |
293 | 347 | { |
348 | + /** |
|
349 | + * @param integer $size |
|
350 | + */ |
|
294 | 351 | static public function limit(Context $context, $size){ |
295 | 352 | $intSize = intval($size); |
296 | 353 | strval($intSize) == $size or \PhpBoot\abort( |
297 | 354 | new \InvalidArgumentException("invalid params for limit($size)")); |
298 | 355 | $context->appendSql("LIMIT $size"); |
299 | 356 | } |
357 | + |
|
358 | + /** |
|
359 | + * @param integer $start |
|
360 | + * @param integer $size |
|
361 | + */ |
|
300 | 362 | static public function limitWithOffset(Context $context, $start, $size){ |
301 | 363 | $intStart = intval($start); |
302 | 364 | $intSize = intval($size); |
@@ -308,6 +370,9 @@ discard block |
||
308 | 370 | |
309 | 371 | class WhereImpl{ |
310 | 372 | |
373 | + /** |
|
374 | + * @param string $str |
|
375 | + */ |
|
311 | 376 | static private function findQ($str,$offset = 0,$no=0){ |
312 | 377 | $found = strpos($str, '?', $offset); |
313 | 378 | if($no == 0 || $found === false){ |
@@ -316,6 +381,9 @@ discard block |
||
316 | 381 | return self::findQ($str, $found+1, $no-1); |
317 | 382 | } |
318 | 383 | |
384 | + /** |
|
385 | + * @param string $prefix |
|
386 | + */ |
|
319 | 387 | static public function where(Context $context, $prefix, $expr, $args){ |
320 | 388 | if(empty($expr)){ |
321 | 389 | return; |
@@ -434,6 +502,10 @@ discard block |
||
434 | 502 | |
435 | 503 | self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
436 | 504 | } |
505 | + |
|
506 | + /** |
|
507 | + * @param string $expr |
|
508 | + */ |
|
437 | 509 | static public function condition(Context $context, $prefix, $expr, $args){ |
438 | 510 | if(!empty($expr)){ |
439 | 511 | $expr = "($expr)"; |
@@ -506,6 +578,10 @@ discard block |
||
506 | 578 | } |
507 | 579 | |
508 | 580 | class GroupByImpl{ |
581 | + |
|
582 | + /** |
|
583 | + * @param string $column |
|
584 | + */ |
|
509 | 585 | static public function groupBy(Context $context, $column){ |
510 | 586 | $column = DB::wrap($column); |
511 | 587 | $context->appendSql("GROUP BY $column"); |
@@ -534,7 +610,6 @@ discard block |
||
534 | 610 | /** |
535 | 611 | * |
536 | 612 | * @param Context $context |
537 | - * @param string|false $asDict return as dict or array |
|
538 | 613 | * @return false|array |
539 | 614 | * @throws DBException|\Exception |
540 | 615 | */ |
@@ -605,6 +680,9 @@ discard block |
||
605 | 680 | } |
606 | 681 | class OnDuplicateKeyUpdateImpl |
607 | 682 | { |
683 | + /** |
|
684 | + * @param Context $context |
|
685 | + */ |
|
608 | 686 | public function set($context, $column, $value){ |
609 | 687 | if(is_string($column)){ |
610 | 688 | $this->setExpr($context, $column, $value); |
@@ -613,6 +691,9 @@ discard block |
||
613 | 691 | } |
614 | 692 | } |
615 | 693 | |
694 | + /** |
|
695 | + * @param string $expr |
|
696 | + */ |
|
616 | 697 | public function setExpr($context, $expr, $args){ |
617 | 698 | $prefix = ''; |
618 | 699 | if($this->first){ |
@@ -8,14 +8,14 @@ discard block |
||
8 | 8 | use PhpBoot\DB\rules\basic\BasicRule; |
9 | 9 | use PhpBoot\DB\Context; |
10 | 10 | |
11 | -class ExecResult{ |
|
12 | - public function __construct($success, $pdo, $st){ |
|
11 | +class ExecResult { |
|
12 | + public function __construct($success, $pdo, $st) { |
|
13 | 13 | $this->pdo = $pdo; |
14 | 14 | $this->st = $st; |
15 | 15 | $this->success = $success; |
16 | 16 | $this->rows = $this->st->rowCount(); |
17 | 17 | } |
18 | - public function lastInsertId($name=null){ |
|
18 | + public function lastInsertId($name = null) { |
|
19 | 19 | return $this->pdo->lastInsertId($name); |
20 | 20 | } |
21 | 21 | /** |
@@ -42,21 +42,21 @@ discard block |
||
42 | 42 | |
43 | 43 | class SelectImpl |
44 | 44 | { |
45 | - static public function select($context, $columns){ |
|
45 | + static public function select($context, $columns) { |
|
46 | 46 | $context->appendSql("SELECT $columns"); |
47 | 47 | } |
48 | 48 | } |
49 | 49 | |
50 | 50 | class FromImpl |
51 | 51 | { |
52 | - static public function from($context, $tables,$as=null){ |
|
53 | - if($tables instanceof BasicRule){ |
|
52 | + static public function from($context, $tables, $as = null) { |
|
53 | + if ($tables instanceof BasicRule) { |
|
54 | 54 | $context->appendSql("FROM (".$tables->context->sql.')'); |
55 | - $context->params = array_merge($context->params,$tables->context->params); |
|
55 | + $context->params = array_merge($context->params, $tables->context->params); |
|
56 | 56 | }else { |
57 | 57 | $context->appendSql("FROM ".DB::wrap($tables)); |
58 | 58 | } |
59 | - if($as){ |
|
59 | + if ($as) { |
|
60 | 60 | $context->appendSql("AS ".DB::wrap($as)); |
61 | 61 | } |
62 | 62 | } |
@@ -74,9 +74,9 @@ discard block |
||
74 | 74 | { |
75 | 75 | static public function join($context, $type, $table) { |
76 | 76 | $table = DB::wrap($table); |
77 | - if($type){ |
|
77 | + if ($type) { |
|
78 | 78 | $context->appendSql("$type JOIN $table"); |
79 | - }else{ |
|
79 | + }else { |
|
80 | 80 | $context->appendSql("JOIN $table"); |
81 | 81 | } |
82 | 82 | } |
@@ -91,14 +91,14 @@ discard block |
||
91 | 91 | |
92 | 92 | class ForUpdateImpl |
93 | 93 | { |
94 | - static public function forUpdate($context){ |
|
94 | + static public function forUpdate($context) { |
|
95 | 95 | $context->appendSql("FOR UPDATE"); |
96 | 96 | } |
97 | 97 | } |
98 | 98 | |
99 | 99 | class ForUpdateOfImpl |
100 | 100 | { |
101 | - static public function of($context, $column){ |
|
101 | + static public function of($context, $column) { |
|
102 | 102 | $column = DB::wrap($column); |
103 | 103 | $context->appendSql("OF $column"); |
104 | 104 | } |
@@ -120,57 +120,57 @@ discard block |
||
120 | 120 | } |
121 | 121 | class ValuesImpl |
122 | 122 | { |
123 | - static public function values(Context $context, array $values){ |
|
123 | + static public function values(Context $context, array $values) { |
|
124 | 124 | $params = []; |
125 | 125 | $stubs = []; |
126 | - foreach ($values as $v){ |
|
127 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
128 | - $stubs[]=$v->get(); |
|
129 | - }else{ |
|
130 | - $stubs[]='?'; |
|
126 | + foreach ($values as $v) { |
|
127 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
128 | + $stubs[] = $v->get(); |
|
129 | + }else { |
|
130 | + $stubs[] = '?'; |
|
131 | 131 | $params[] = $v; |
132 | 132 | } |
133 | 133 | } |
134 | 134 | $stubs = implode(',', $stubs); |
135 | 135 | |
136 | - if(array_keys($values) === range(0, count($values) - 1)){ |
|
136 | + if (array_keys($values) === range(0, count($values) - 1)) { |
|
137 | 137 | //VALUES(val0, val1, val2) |
138 | 138 | $context->appendSql("VALUES($stubs)"); |
139 | 139 | |
140 | - }else{ |
|
140 | + }else { |
|
141 | 141 | //(col0, col1, col2) VALUES(val0, val1, val2) |
142 | - $columns = implode(',', array_map(function($k){return DB::wrap($k);}, array_keys($values))); |
|
143 | - $context->appendSql("($columns) VALUES($stubs)",false); |
|
142 | + $columns = implode(',', array_map(function($k) {return DB::wrap($k); }, array_keys($values))); |
|
143 | + $context->appendSql("($columns) VALUES($stubs)", false); |
|
144 | 144 | } |
145 | 145 | $context->appendParams($params); |
146 | 146 | } |
147 | 147 | static public function batchValues(Context $context, array $values) |
148 | 148 | { |
149 | 149 | $count = count($values); |
150 | - if($count == 0){ |
|
150 | + if ($count == 0) { |
|
151 | 151 | return; |
152 | 152 | } |
153 | 153 | $keys = array_keys($values[0]); |
154 | 154 | $row = implode(',', self::toSql(array_values($values[0]))); |
155 | - if($keys === range(0, count($keys) - 1)){ |
|
155 | + if ($keys === range(0, count($keys) - 1)) { |
|
156 | 156 | //VALUES(val0, val1, val2) |
157 | 157 | $context->appendSql("VALUES($row)"); |
158 | - }else{ |
|
158 | + }else { |
|
159 | 159 | //(col0, col1, col2) VALUES(val0, val1, val2) |
160 | - $columns = implode(',', array_map(function($k){return DB::wrap($k);}, $keys)); |
|
161 | - $context->appendSql("($columns) VALUES($row)",false); |
|
160 | + $columns = implode(',', array_map(function($k) {return DB::wrap($k); }, $keys)); |
|
161 | + $context->appendSql("($columns) VALUES($row)", false); |
|
162 | 162 | } |
163 | - for($i=1; $i<$count; $i++){ |
|
163 | + for ($i = 1; $i<$count; $i++) { |
|
164 | 164 | $value = self::pick($keys, $values[$i]); |
165 | 165 | $row = implode(',', self::toSql($value)); |
166 | - $context->appendSql(", ($row)",false); |
|
166 | + $context->appendSql(", ($row)", false); |
|
167 | 167 | } |
168 | 168 | } |
169 | 169 | |
170 | 170 | static protected function pick(array $keys, array $values) |
171 | 171 | { |
172 | 172 | $res = []; |
173 | - foreach ($keys as $key){ |
|
173 | + foreach ($keys as $key) { |
|
174 | 174 | array_key_exists($key, $values) or \PhpBoot\abort("key $key not exist from the given array"); |
175 | 175 | $res[$key] = $values[$key]; |
176 | 176 | } |
@@ -178,12 +178,12 @@ discard block |
||
178 | 178 | } |
179 | 179 | static protected function toSql(array $values) |
180 | 180 | { |
181 | - foreach ($values as &$v){ |
|
182 | - if($v instanceof Raw){ |
|
181 | + foreach ($values as &$v) { |
|
182 | + if ($v instanceof Raw) { |
|
183 | 183 | $v = $v->get(); |
184 | - }elseif(is_bool($v)){ |
|
185 | - $v = $v?'true':'false'; |
|
186 | - }elseif(!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])){ |
|
184 | + }elseif (is_bool($v)) { |
|
185 | + $v = $v ? 'true' : 'false'; |
|
186 | + }elseif (!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])) { |
|
187 | 187 | $v = (string)$v; |
188 | 188 | $v = str_replace("\\", "\\\\", $v); |
189 | 189 | $v = str_replace("'", "\\'", $v); |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | |
197 | 197 | class UpdateImpl |
198 | 198 | { |
199 | - static public function update($context, $table){ |
|
199 | + static public function update($context, $table) { |
|
200 | 200 | $table = DB::wrap($table); |
201 | 201 | $context->appendSql("UPDATE $table"); |
202 | 202 | } |
@@ -204,81 +204,81 @@ discard block |
||
204 | 204 | |
205 | 205 | class UpdateSetImpl |
206 | 206 | { |
207 | - public function set(Context $context, $expr, $args){ |
|
208 | - if(is_string($expr)){ |
|
207 | + public function set(Context $context, $expr, $args) { |
|
208 | + if (is_string($expr)) { |
|
209 | 209 | return $this->setExpr($context, $expr, $args); |
210 | - }else{ |
|
210 | + }else { |
|
211 | 211 | return $this->setArgs($context, $expr); |
212 | 212 | } |
213 | 213 | } |
214 | 214 | |
215 | - public function setExpr(Context $context, $expr, $args){ |
|
216 | - if($this->first){ |
|
215 | + public function setExpr(Context $context, $expr, $args) { |
|
216 | + if ($this->first) { |
|
217 | 217 | $this->first = false; |
218 | 218 | $prefix = 'SET '; |
219 | - }else{ |
|
219 | + }else { |
|
220 | 220 | $prefix = ','; |
221 | 221 | } |
222 | 222 | |
223 | - $context->appendSql("$prefix$expr",$prefix == 'SET '); |
|
223 | + $context->appendSql("$prefix$expr", $prefix == 'SET '); |
|
224 | 224 | $context->appendParams($args); |
225 | 225 | |
226 | 226 | } |
227 | - public function setArgs(Context $context, $values){ |
|
227 | + public function setArgs(Context $context, $values) { |
|
228 | 228 | $set = []; |
229 | 229 | $params = []; |
230 | - foreach ($values as $k=>$v){ |
|
230 | + foreach ($values as $k=>$v) { |
|
231 | 231 | $k = DB::wrap($k); |
232 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
233 | - $set[]= "$k=".$v->get(); |
|
234 | - }else{ |
|
235 | - $set[]= "$k=?"; |
|
236 | - $params[]=$v; |
|
232 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
233 | + $set[] = "$k=".$v->get(); |
|
234 | + }else { |
|
235 | + $set[] = "$k=?"; |
|
236 | + $params[] = $v; |
|
237 | 237 | } |
238 | 238 | } |
239 | - if($this->first){ |
|
239 | + if ($this->first) { |
|
240 | 240 | $this->first = false; |
241 | 241 | $context->appendSql('SET '.implode(',', $set)); |
242 | 242 | $context->appendParams($params); |
243 | - }else{ |
|
244 | - $context->appendSql(','.implode(',', $set),false); |
|
243 | + }else { |
|
244 | + $context->appendSql(','.implode(',', $set), false); |
|
245 | 245 | $context->appendParams($params); |
246 | 246 | } |
247 | 247 | } |
248 | - private $first=true; |
|
248 | + private $first = true; |
|
249 | 249 | } |
250 | 250 | class OrderByImpl |
251 | 251 | { |
252 | - public function orderByArgs(Context $context, $orders){ |
|
253 | - if(empty($orders)){ |
|
252 | + public function orderByArgs(Context $context, $orders) { |
|
253 | + if (empty($orders)) { |
|
254 | 254 | return $this; |
255 | 255 | } |
256 | 256 | $params = array(); |
257 | - foreach ($orders as $k=>$v){ |
|
258 | - if(is_integer($k)){ |
|
257 | + foreach ($orders as $k=>$v) { |
|
258 | + if (is_integer($k)) { |
|
259 | 259 | $params[] = DB::wrap($v); |
260 | - }else{ |
|
260 | + }else { |
|
261 | 261 | $k = DB::wrap($k); |
262 | 262 | |
263 | 263 | $v = strtoupper($v); |
264 | - ($v =='DESC' || $v =='ASC') or \PhpBoot\abort( new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
264 | + ($v == 'DESC' || $v == 'ASC') or \PhpBoot\abort(new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
265 | 265 | |
266 | 266 | $params[] = "$k $v"; |
267 | 267 | } |
268 | 268 | } |
269 | - if($this->first){ |
|
269 | + if ($this->first) { |
|
270 | 270 | $this->first = false; |
271 | 271 | $context->appendSql('ORDER BY '.implode(',', $params)); |
272 | - }else{ |
|
273 | - $context->appendSql(','.implode(',', $params),false); |
|
272 | + }else { |
|
273 | + $context->appendSql(','.implode(',', $params), false); |
|
274 | 274 | } |
275 | 275 | return $this; |
276 | 276 | } |
277 | - public function orderBy(Context $context, $column, $order=null){ |
|
278 | - if(is_string($column)){ |
|
279 | - if($order === null){ |
|
277 | + public function orderBy(Context $context, $column, $order = null) { |
|
278 | + if (is_string($column)) { |
|
279 | + if ($order === null) { |
|
280 | 280 | $column = [$column]; |
281 | - }else{ |
|
281 | + }else { |
|
282 | 282 | $column = [$column=>$order]; |
283 | 283 | } |
284 | 284 | } |
@@ -286,18 +286,18 @@ discard block |
||
286 | 286 | |
287 | 287 | |
288 | 288 | } |
289 | - private $first=true; |
|
289 | + private $first = true; |
|
290 | 290 | } |
291 | 291 | |
292 | 292 | class LimitImpl |
293 | 293 | { |
294 | - static public function limit(Context $context, $size){ |
|
294 | + static public function limit(Context $context, $size) { |
|
295 | 295 | $intSize = intval($size); |
296 | 296 | strval($intSize) == $size or \PhpBoot\abort( |
297 | 297 | new \InvalidArgumentException("invalid params for limit($size)")); |
298 | 298 | $context->appendSql("LIMIT $size"); |
299 | 299 | } |
300 | - static public function limitWithOffset(Context $context, $start, $size){ |
|
300 | + static public function limitWithOffset(Context $context, $start, $size) { |
|
301 | 301 | $intStart = intval($start); |
302 | 302 | $intSize = intval($size); |
303 | 303 | strval($intStart) == $start && strval($intSize) == $size or \PhpBoot\abort( |
@@ -306,31 +306,31 @@ discard block |
||
306 | 306 | } |
307 | 307 | } |
308 | 308 | |
309 | -class WhereImpl{ |
|
309 | +class WhereImpl { |
|
310 | 310 | |
311 | - static private function findQ($str,$offset = 0,$no=0){ |
|
311 | + static private function findQ($str, $offset = 0, $no = 0) { |
|
312 | 312 | $found = strpos($str, '?', $offset); |
313 | - if($no == 0 || $found === false){ |
|
313 | + if ($no == 0 || $found === false) { |
|
314 | 314 | return $found; |
315 | 315 | } |
316 | - return self::findQ($str, $found+1, $no-1); |
|
316 | + return self::findQ($str, $found + 1, $no - 1); |
|
317 | 317 | } |
318 | 318 | |
319 | - static public function where(Context $context, $prefix, $expr, $args){ |
|
320 | - if(empty($expr)){ |
|
319 | + static public function where(Context $context, $prefix, $expr, $args) { |
|
320 | + if (empty($expr)) { |
|
321 | 321 | return; |
322 | 322 | } |
323 | - if(is_callable($expr)){ |
|
324 | - self::conditionClosure($context,$prefix, $expr); |
|
325 | - }elseif (is_string($expr)){ |
|
323 | + if (is_callable($expr)) { |
|
324 | + self::conditionClosure($context, $prefix, $expr); |
|
325 | + }elseif (is_string($expr)) { |
|
326 | 326 | self::condition($context, $prefix, $expr, $args); |
327 | - }else{ |
|
327 | + }else { |
|
328 | 328 | self::conditionArgs($context, $prefix, $expr); |
329 | 329 | } |
330 | 330 | |
331 | 331 | } |
332 | 332 | |
333 | - static public function conditionClosure(Context $context, $prefix, callable $callback){ |
|
333 | + static public function conditionClosure(Context $context, $prefix, callable $callback) { |
|
334 | 334 | $context->appendSql($prefix.' ('); |
335 | 335 | $callback($context); |
336 | 336 | $context->appendSql(')'); |
@@ -356,15 +356,15 @@ discard block |
||
356 | 356 | * NOT IN 'id'=>['NOT IN' => [1,2,3]] |
357 | 357 | * @return void |
358 | 358 | */ |
359 | - static public function conditionArgs(Context $context, $prefix, $args=[]){ |
|
360 | - if($args ===null){ |
|
361 | - return ; |
|
359 | + static public function conditionArgs(Context $context, $prefix, $args = []) { |
|
360 | + if ($args === null) { |
|
361 | + return; |
|
362 | 362 | } |
363 | 363 | $exprs = array(); |
364 | 364 | $params = array(); |
365 | - foreach ($args as $k => $v){ |
|
365 | + foreach ($args as $k => $v) { |
|
366 | 366 | $k = DB::wrap($k); |
367 | - if(!is_array($v)){ |
|
367 | + if (!is_array($v)) { |
|
368 | 368 | $v = ['='=>$v]; |
369 | 369 | } |
370 | 370 | |
@@ -376,56 +376,56 @@ discard block |
||
376 | 376 | new \InvalidArgumentException("invalid param $op for whereArgs")); |
377 | 377 | |
378 | 378 | $var = array_values($v)[0]; |
379 | - if($op == 'IN' || $op == 'NOT IN'){ |
|
379 | + if ($op == 'IN' || $op == 'NOT IN') { |
|
380 | 380 | $stubs = []; |
381 | 381 | |
382 | - if($var instanceof BasicRule){ |
|
382 | + if ($var instanceof BasicRule) { |
|
383 | 383 | $stubs = "({$var->context->sql})"; |
384 | 384 | $params = array_merge($params, $var->context->params); |
385 | 385 | $exprs[] = "$k $op $stubs"; |
386 | - }else{ |
|
387 | - foreach ($var as $i){ |
|
388 | - if(is_a($i, Raw::class)){ |
|
389 | - $stubs[]=strval($i); |
|
390 | - }elseif($i instanceof BasicRule){ |
|
386 | + }else { |
|
387 | + foreach ($var as $i) { |
|
388 | + if (is_a($i, Raw::class)) { |
|
389 | + $stubs[] = strval($i); |
|
390 | + }elseif ($i instanceof BasicRule) { |
|
391 | 391 | $stubs = "({$i->context->sql})"; |
392 | 392 | $params = array_merge($params, $i->context->params); |
393 | - }else{ |
|
394 | - $stubs[]='?'; |
|
393 | + }else { |
|
394 | + $stubs[] = '?'; |
|
395 | 395 | $params[] = $i; |
396 | 396 | } |
397 | 397 | } |
398 | 398 | $stubs = implode(',', $stubs); |
399 | 399 | $exprs[] = "$k $op ($stubs)"; |
400 | 400 | } |
401 | - }else if($op == 'BETWEEN'){ |
|
401 | + }else if ($op == 'BETWEEN') { |
|
402 | 402 | $cond = "$k BETWEEN"; |
403 | - if(is_a($var[0], Raw::class)){ |
|
403 | + if (is_a($var[0], Raw::class)) { |
|
404 | 404 | $cond = "$cond ".strval($var[0]); |
405 | - }elseif($var[0] instanceof BasicRule){ |
|
405 | + }elseif ($var[0] instanceof BasicRule) { |
|
406 | 406 | $cond = "$cond ({$var[0]->context->sql})"; |
407 | 407 | $params = array_merge($params, $var[0]->context->params); |
408 | - }else{ |
|
408 | + }else { |
|
409 | 409 | $cond = "$cond ?"; |
410 | 410 | $params[] = $var[0]; |
411 | 411 | } |
412 | - if(is_a($var[1], Raw::class)){ |
|
412 | + if (is_a($var[1], Raw::class)) { |
|
413 | 413 | $cond = "$cond AND ".strval($var[1]); |
414 | - }elseif($var[1] instanceof BasicRule){ |
|
414 | + }elseif ($var[1] instanceof BasicRule) { |
|
415 | 415 | $cond = "$cond AND ({$var[1]->context->sql})"; |
416 | 416 | $params = array_merge($params, $var[1]->context->params); |
417 | - }else{ |
|
417 | + }else { |
|
418 | 418 | $cond = "$cond AND ?"; |
419 | 419 | $params[] = $var[1]; |
420 | 420 | } |
421 | 421 | $exprs[] = $cond; |
422 | - }else{ |
|
423 | - if(is_a($var, Raw::class)){ |
|
422 | + }else { |
|
423 | + if (is_a($var, Raw::class)) { |
|
424 | 424 | $exprs[] = "$k $op ".strval($var); |
425 | - }elseif($var instanceof BasicRule){ |
|
425 | + }elseif ($var instanceof BasicRule) { |
|
426 | 426 | $exprs[] = "$k $op {$var->context->sql}"; |
427 | 427 | $params = array_merge($params, $var->context->params); |
428 | - }else{ |
|
428 | + }else { |
|
429 | 429 | $exprs[] = "$k $op ?"; |
430 | 430 | $params[] = $var; |
431 | 431 | } |
@@ -434,22 +434,22 @@ discard block |
||
434 | 434 | |
435 | 435 | self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
436 | 436 | } |
437 | - static public function condition(Context $context, $prefix, $expr, $args){ |
|
438 | - if(!empty($expr)){ |
|
437 | + static public function condition(Context $context, $prefix, $expr, $args) { |
|
438 | + if (!empty($expr)) { |
|
439 | 439 | $expr = "($expr)"; |
440 | - if($args){ |
|
440 | + if ($args) { |
|
441 | 441 | //因为PDO不支持绑定数组变量, 这里需要手动展开数组 |
442 | 442 | //也就是说把 where("id IN(?)", [1,2]) 展开成 where("id IN(?,?)", 1,2) |
443 | 443 | $cutted = null; |
444 | 444 | $cut = null; |
445 | 445 | $toReplace = array(); |
446 | 446 | |
447 | - $newArgs=array(); |
|
447 | + $newArgs = array(); |
|
448 | 448 | //找到所有数组对应的?符位置 |
449 | - foreach ($args as $k =>$arg){ |
|
450 | - if(is_array($arg) || is_a($arg, Raw::class) || is_a($arg, BasicRule::class)){ |
|
449 | + foreach ($args as $k =>$arg) { |
|
450 | + if (is_array($arg) || is_a($arg, Raw::class) || is_a($arg, BasicRule::class)) { |
|
451 | 451 | |
452 | - if(!$cutted){ |
|
452 | + if (!$cutted) { |
|
453 | 453 | $cut = new NestedStringCut($expr); |
454 | 454 | $cutted = $cut->getText(); |
455 | 455 | } |
@@ -459,54 +459,54 @@ discard block |
||
459 | 459 | $pos !== false or \PhpBoot\abort( |
460 | 460 | new \InvalidArgumentException("unmatched params and ? @ $expr")); |
461 | 461 | |
462 | - if(is_array($arg)){ |
|
462 | + if (is_array($arg)) { |
|
463 | 463 | $stubs = []; |
464 | - foreach ($arg as $i){ |
|
465 | - if(is_a($i, Raw::class)){ |
|
464 | + foreach ($arg as $i) { |
|
465 | + if (is_a($i, Raw::class)) { |
|
466 | 466 | $stubs[] = strval($i); |
467 | - }else{ |
|
467 | + }else { |
|
468 | 468 | $stubs[] = '?'; |
469 | 469 | $newArgs[] = $i; |
470 | 470 | } |
471 | 471 | } |
472 | 472 | $stubs = implode(',', $stubs); |
473 | - }elseif($arg instanceof BasicRule){ |
|
473 | + }elseif ($arg instanceof BasicRule) { |
|
474 | 474 | $stubs = "({$arg->context->sql})"; |
475 | 475 | $newArgs = array_merge($newArgs, $arg->context->params); |
476 | - }else{ |
|
476 | + }else { |
|
477 | 477 | $stubs = strval($arg); |
478 | 478 | } |
479 | 479 | $toReplace[] = [$pos, $stubs]; |
480 | 480 | |
481 | - }else{ |
|
482 | - $newArgs[]=$arg; |
|
481 | + }else { |
|
482 | + $newArgs[] = $arg; |
|
483 | 483 | } |
484 | 484 | } |
485 | 485 | |
486 | - if(count($toReplace)){ |
|
486 | + if (count($toReplace)) { |
|
487 | 487 | $toReplace = array_reverse($toReplace); |
488 | - foreach ($toReplace as $i){ |
|
488 | + foreach ($toReplace as $i) { |
|
489 | 489 | list($pos, $v) = $i; |
490 | - $expr = substr($expr, 0, $pos).$v.substr($expr, $pos+1); |
|
490 | + $expr = substr($expr, 0, $pos).$v.substr($expr, $pos + 1); |
|
491 | 491 | } |
492 | 492 | $args = $newArgs; |
493 | 493 | } |
494 | 494 | } |
495 | - if($prefix){ |
|
495 | + if ($prefix) { |
|
496 | 496 | $context->appendSql($prefix.' '.$expr); |
497 | - }else{ |
|
497 | + }else { |
|
498 | 498 | $context->appendSql($expr); |
499 | 499 | } |
500 | 500 | |
501 | - if($args){ |
|
501 | + if ($args) { |
|
502 | 502 | $context->appendParams($args); |
503 | 503 | } |
504 | 504 | } |
505 | 505 | } |
506 | 506 | } |
507 | 507 | |
508 | -class GroupByImpl{ |
|
509 | - static public function groupBy(Context $context, $column){ |
|
508 | +class GroupByImpl { |
|
509 | + static public function groupBy(Context $context, $column) { |
|
510 | 510 | $column = DB::wrap($column); |
511 | 511 | $context->appendSql("GROUP BY $column"); |
512 | 512 | } |
@@ -522,12 +522,12 @@ discard block |
||
522 | 522 | * @throws DBException|\Exception |
523 | 523 | */ |
524 | 524 | static public function exec($context) { |
525 | - try{ |
|
525 | + try { |
|
526 | 526 | $st = $context->connection->prepare($context->sql); |
527 | 527 | $success = $st->execute($context->params); |
528 | 528 | return new ExecResult($success, $context->connection, $st); |
529 | - }catch (\Exception $e){ |
|
530 | - \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
|
529 | + }catch (\Exception $e) { |
|
530 | + \PhpBoot\abort(new DBException($context, $e->getMessage(), $e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params]); |
|
531 | 531 | return null; |
532 | 532 | } |
533 | 533 | } |
@@ -538,25 +538,25 @@ discard block |
||
538 | 538 | * @return false|array |
539 | 539 | * @throws DBException|\Exception |
540 | 540 | */ |
541 | - static public function get($context, $dictAs=false){ |
|
541 | + static public function get($context, $dictAs = false) { |
|
542 | 542 | |
543 | - try{ |
|
543 | + try { |
|
544 | 544 | $st = $context->connection->prepare($context->sql); |
545 | - if($st->execute($context->params)){ |
|
545 | + if ($st->execute($context->params)) { |
|
546 | 546 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
547 | - if ($dictAs){ |
|
548 | - $dict= []; |
|
549 | - foreach ($res as $i){ |
|
550 | - $dict[$i[$dictAs]]=$i; |
|
547 | + if ($dictAs) { |
|
548 | + $dict = []; |
|
549 | + foreach ($res as $i) { |
|
550 | + $dict[$i[$dictAs]] = $i; |
|
551 | 551 | } |
552 | 552 | return $context->handleResult($dict); |
553 | 553 | } |
554 | 554 | return $context->handleResult($res); |
555 | - }else{ |
|
555 | + }else { |
|
556 | 556 | return false; |
557 | 557 | } |
558 | - }catch (\Exception $e){ |
|
559 | - \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
|
558 | + }catch (\Exception $e) { |
|
559 | + \PhpBoot\abort(new DBException($context, $e->getMessage(), $e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params]); |
|
560 | 560 | return false; |
561 | 561 | } |
562 | 562 | |
@@ -567,23 +567,23 @@ discard block |
||
567 | 567 | * @return int|false |
568 | 568 | * @throws DBException|\Exception |
569 | 569 | */ |
570 | - static public function count($context){ |
|
570 | + static public function count($context) { |
|
571 | 571 | |
572 | 572 | $found = []; |
573 | - if(!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
574 | - count($found)==0){ |
|
573 | + if (!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
574 | + count($found) == 0) { |
|
575 | 575 | \PhpBoot\abort(new \PDOException("can not use count(*) without select")); |
576 | 576 | } |
577 | 577 | list($chars, $columnBegin) = $found[0]; |
578 | - $columnBegin = $columnBegin + strlen('select')+1; |
|
578 | + $columnBegin = $columnBegin + strlen('select') + 1; |
|
579 | 579 | |
580 | 580 | $columnEnd = 0; |
581 | 581 | $found = []; |
582 | - try{ |
|
583 | - if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
584 | - count($found)==0){ |
|
582 | + try { |
|
583 | + if (!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
584 | + count($found) == 0) { |
|
585 | 585 | $columnEnd = strlen($context->sql); |
586 | - }else{ |
|
586 | + }else { |
|
587 | 587 | list($chars, $columnEnd) = $found[0]; |
588 | 588 | } |
589 | 589 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -591,61 +591,61 @@ discard block |
||
591 | 591 | $sql .= substr($context->sql, $columnEnd); |
592 | 592 | |
593 | 593 | $st = $context->connection->prepare($sql); |
594 | - if($st->execute($context->params)){ |
|
594 | + if ($st->execute($context->params)) { |
|
595 | 595 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
596 | 596 | return intval($res[0]['count']); |
597 | - }else{ |
|
597 | + }else { |
|
598 | 598 | return false; |
599 | 599 | } |
600 | - }catch (\Exception $e){ |
|
601 | - \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
|
600 | + }catch (\Exception $e) { |
|
601 | + \PhpBoot\abort(new DBException($context, $e->getMessage(), $e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params]); |
|
602 | 602 | return false; |
603 | 603 | } |
604 | 604 | } |
605 | 605 | } |
606 | 606 | class OnDuplicateKeyUpdateImpl |
607 | 607 | { |
608 | - public function set($context, $column, $value){ |
|
609 | - if(is_string($column)){ |
|
608 | + public function set($context, $column, $value) { |
|
609 | + if (is_string($column)) { |
|
610 | 610 | $this->setExpr($context, $column, $value); |
611 | - }else{ |
|
611 | + }else { |
|
612 | 612 | $this->setArgs($context, $column); |
613 | 613 | } |
614 | 614 | } |
615 | 615 | |
616 | - public function setExpr($context, $expr, $args){ |
|
616 | + public function setExpr($context, $expr, $args) { |
|
617 | 617 | $prefix = ''; |
618 | - if($this->first){ |
|
618 | + if ($this->first) { |
|
619 | 619 | $this->first = false; |
620 | 620 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
621 | - }else{ |
|
621 | + }else { |
|
622 | 622 | $prefix = ','; |
623 | 623 | } |
624 | 624 | |
625 | - $context->appendSql("$prefix$expr",$prefix == 'ON DUPLICATE KEY UPDATE '); |
|
625 | + $context->appendSql("$prefix$expr", $prefix == 'ON DUPLICATE KEY UPDATE '); |
|
626 | 626 | $context->appendParams($args); |
627 | 627 | |
628 | 628 | } |
629 | - public function setArgs($context, $values){ |
|
629 | + public function setArgs($context, $values) { |
|
630 | 630 | $set = []; |
631 | 631 | $params = []; |
632 | - foreach ($values as $k=>$v){ |
|
632 | + foreach ($values as $k=>$v) { |
|
633 | 633 | $k = DB::wrap($k); |
634 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
635 | - $set[]= "$k=".$v->get(); |
|
636 | - }else{ |
|
637 | - $set[]= "$k=?"; |
|
638 | - $params[]=$v; |
|
634 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
635 | + $set[] = "$k=".$v->get(); |
|
636 | + }else { |
|
637 | + $set[] = "$k=?"; |
|
638 | + $params[] = $v; |
|
639 | 639 | } |
640 | 640 | } |
641 | - if($this->first){ |
|
641 | + if ($this->first) { |
|
642 | 642 | $this->first = false; |
643 | 643 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
644 | 644 | $context->appendParams($params); |
645 | - }else{ |
|
646 | - $context->appendSql(','.implode(',', $set),false); |
|
645 | + }else { |
|
646 | + $context->appendSql(','.implode(',', $set), false); |
|
647 | 647 | $context->appendParams($params); |
648 | 648 | } |
649 | 649 | } |
650 | - private $first=true; |
|
650 | + private $first = true; |
|
651 | 651 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | if($tables instanceof BasicRule){ |
54 | 54 | $context->appendSql("FROM (".$tables->context->sql.')'); |
55 | 55 | $context->params = array_merge($context->params,$tables->context->params); |
56 | - }else { |
|
56 | + } else { |
|
57 | 57 | $context->appendSql("FROM ".DB::wrap($tables)); |
58 | 58 | } |
59 | 59 | if($as){ |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | $table = DB::wrap($table); |
77 | 77 | if($type){ |
78 | 78 | $context->appendSql("$type JOIN $table"); |
79 | - }else{ |
|
79 | + } else{ |
|
80 | 80 | $context->appendSql("JOIN $table"); |
81 | 81 | } |
82 | 82 | } |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | foreach ($values as $v){ |
127 | 127 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
128 | 128 | $stubs[]=$v->get(); |
129 | - }else{ |
|
129 | + } else{ |
|
130 | 130 | $stubs[]='?'; |
131 | 131 | $params[] = $v; |
132 | 132 | } |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | //VALUES(val0, val1, val2) |
138 | 138 | $context->appendSql("VALUES($stubs)"); |
139 | 139 | |
140 | - }else{ |
|
140 | + } else{ |
|
141 | 141 | //(col0, col1, col2) VALUES(val0, val1, val2) |
142 | 142 | $columns = implode(',', array_map(function($k){return DB::wrap($k);}, array_keys($values))); |
143 | 143 | $context->appendSql("($columns) VALUES($stubs)",false); |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | if($keys === range(0, count($keys) - 1)){ |
156 | 156 | //VALUES(val0, val1, val2) |
157 | 157 | $context->appendSql("VALUES($row)"); |
158 | - }else{ |
|
158 | + } else{ |
|
159 | 159 | //(col0, col1, col2) VALUES(val0, val1, val2) |
160 | 160 | $columns = implode(',', array_map(function($k){return DB::wrap($k);}, $keys)); |
161 | 161 | $context->appendSql("($columns) VALUES($row)",false); |
@@ -181,9 +181,9 @@ discard block |
||
181 | 181 | foreach ($values as &$v){ |
182 | 182 | if($v instanceof Raw){ |
183 | 183 | $v = $v->get(); |
184 | - }elseif(is_bool($v)){ |
|
184 | + } elseif(is_bool($v)){ |
|
185 | 185 | $v = $v?'true':'false'; |
186 | - }elseif(!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])){ |
|
186 | + } elseif(!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])){ |
|
187 | 187 | $v = (string)$v; |
188 | 188 | $v = str_replace("\\", "\\\\", $v); |
189 | 189 | $v = str_replace("'", "\\'", $v); |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | public function set(Context $context, $expr, $args){ |
208 | 208 | if(is_string($expr)){ |
209 | 209 | return $this->setExpr($context, $expr, $args); |
210 | - }else{ |
|
210 | + } else{ |
|
211 | 211 | return $this->setArgs($context, $expr); |
212 | 212 | } |
213 | 213 | } |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | if($this->first){ |
217 | 217 | $this->first = false; |
218 | 218 | $prefix = 'SET '; |
219 | - }else{ |
|
219 | + } else{ |
|
220 | 220 | $prefix = ','; |
221 | 221 | } |
222 | 222 | |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | $k = DB::wrap($k); |
232 | 232 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
233 | 233 | $set[]= "$k=".$v->get(); |
234 | - }else{ |
|
234 | + } else{ |
|
235 | 235 | $set[]= "$k=?"; |
236 | 236 | $params[]=$v; |
237 | 237 | } |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | $this->first = false; |
241 | 241 | $context->appendSql('SET '.implode(',', $set)); |
242 | 242 | $context->appendParams($params); |
243 | - }else{ |
|
243 | + } else{ |
|
244 | 244 | $context->appendSql(','.implode(',', $set),false); |
245 | 245 | $context->appendParams($params); |
246 | 246 | } |
@@ -257,7 +257,7 @@ discard block |
||
257 | 257 | foreach ($orders as $k=>$v){ |
258 | 258 | if(is_integer($k)){ |
259 | 259 | $params[] = DB::wrap($v); |
260 | - }else{ |
|
260 | + } else{ |
|
261 | 261 | $k = DB::wrap($k); |
262 | 262 | |
263 | 263 | $v = strtoupper($v); |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | if($this->first){ |
270 | 270 | $this->first = false; |
271 | 271 | $context->appendSql('ORDER BY '.implode(',', $params)); |
272 | - }else{ |
|
272 | + } else{ |
|
273 | 273 | $context->appendSql(','.implode(',', $params),false); |
274 | 274 | } |
275 | 275 | return $this; |
@@ -278,7 +278,7 @@ discard block |
||
278 | 278 | if(is_string($column)){ |
279 | 279 | if($order === null){ |
280 | 280 | $column = [$column]; |
281 | - }else{ |
|
281 | + } else{ |
|
282 | 282 | $column = [$column=>$order]; |
283 | 283 | } |
284 | 284 | } |
@@ -322,9 +322,9 @@ discard block |
||
322 | 322 | } |
323 | 323 | if(is_callable($expr)){ |
324 | 324 | self::conditionClosure($context,$prefix, $expr); |
325 | - }elseif (is_string($expr)){ |
|
325 | + } elseif (is_string($expr)){ |
|
326 | 326 | self::condition($context, $prefix, $expr, $args); |
327 | - }else{ |
|
327 | + } else{ |
|
328 | 328 | self::conditionArgs($context, $prefix, $expr); |
329 | 329 | } |
330 | 330 | |
@@ -383,14 +383,14 @@ discard block |
||
383 | 383 | $stubs = "({$var->context->sql})"; |
384 | 384 | $params = array_merge($params, $var->context->params); |
385 | 385 | $exprs[] = "$k $op $stubs"; |
386 | - }else{ |
|
386 | + } else{ |
|
387 | 387 | foreach ($var as $i){ |
388 | 388 | if(is_a($i, Raw::class)){ |
389 | 389 | $stubs[]=strval($i); |
390 | - }elseif($i instanceof BasicRule){ |
|
390 | + } elseif($i instanceof BasicRule){ |
|
391 | 391 | $stubs = "({$i->context->sql})"; |
392 | 392 | $params = array_merge($params, $i->context->params); |
393 | - }else{ |
|
393 | + } else{ |
|
394 | 394 | $stubs[]='?'; |
395 | 395 | $params[] = $i; |
396 | 396 | } |
@@ -398,34 +398,34 @@ discard block |
||
398 | 398 | $stubs = implode(',', $stubs); |
399 | 399 | $exprs[] = "$k $op ($stubs)"; |
400 | 400 | } |
401 | - }else if($op == 'BETWEEN'){ |
|
401 | + } else if($op == 'BETWEEN'){ |
|
402 | 402 | $cond = "$k BETWEEN"; |
403 | 403 | if(is_a($var[0], Raw::class)){ |
404 | 404 | $cond = "$cond ".strval($var[0]); |
405 | - }elseif($var[0] instanceof BasicRule){ |
|
405 | + } elseif($var[0] instanceof BasicRule){ |
|
406 | 406 | $cond = "$cond ({$var[0]->context->sql})"; |
407 | 407 | $params = array_merge($params, $var[0]->context->params); |
408 | - }else{ |
|
408 | + } else{ |
|
409 | 409 | $cond = "$cond ?"; |
410 | 410 | $params[] = $var[0]; |
411 | 411 | } |
412 | 412 | if(is_a($var[1], Raw::class)){ |
413 | 413 | $cond = "$cond AND ".strval($var[1]); |
414 | - }elseif($var[1] instanceof BasicRule){ |
|
414 | + } elseif($var[1] instanceof BasicRule){ |
|
415 | 415 | $cond = "$cond AND ({$var[1]->context->sql})"; |
416 | 416 | $params = array_merge($params, $var[1]->context->params); |
417 | - }else{ |
|
417 | + } else{ |
|
418 | 418 | $cond = "$cond AND ?"; |
419 | 419 | $params[] = $var[1]; |
420 | 420 | } |
421 | 421 | $exprs[] = $cond; |
422 | - }else{ |
|
422 | + } else{ |
|
423 | 423 | if(is_a($var, Raw::class)){ |
424 | 424 | $exprs[] = "$k $op ".strval($var); |
425 | - }elseif($var instanceof BasicRule){ |
|
425 | + } elseif($var instanceof BasicRule){ |
|
426 | 426 | $exprs[] = "$k $op {$var->context->sql}"; |
427 | 427 | $params = array_merge($params, $var->context->params); |
428 | - }else{ |
|
428 | + } else{ |
|
429 | 429 | $exprs[] = "$k $op ?"; |
430 | 430 | $params[] = $var; |
431 | 431 | } |
@@ -464,21 +464,21 @@ discard block |
||
464 | 464 | foreach ($arg as $i){ |
465 | 465 | if(is_a($i, Raw::class)){ |
466 | 466 | $stubs[] = strval($i); |
467 | - }else{ |
|
467 | + } else{ |
|
468 | 468 | $stubs[] = '?'; |
469 | 469 | $newArgs[] = $i; |
470 | 470 | } |
471 | 471 | } |
472 | 472 | $stubs = implode(',', $stubs); |
473 | - }elseif($arg instanceof BasicRule){ |
|
473 | + } elseif($arg instanceof BasicRule){ |
|
474 | 474 | $stubs = "({$arg->context->sql})"; |
475 | 475 | $newArgs = array_merge($newArgs, $arg->context->params); |
476 | - }else{ |
|
476 | + } else{ |
|
477 | 477 | $stubs = strval($arg); |
478 | 478 | } |
479 | 479 | $toReplace[] = [$pos, $stubs]; |
480 | 480 | |
481 | - }else{ |
|
481 | + } else{ |
|
482 | 482 | $newArgs[]=$arg; |
483 | 483 | } |
484 | 484 | } |
@@ -494,7 +494,7 @@ discard block |
||
494 | 494 | } |
495 | 495 | if($prefix){ |
496 | 496 | $context->appendSql($prefix.' '.$expr); |
497 | - }else{ |
|
497 | + } else{ |
|
498 | 498 | $context->appendSql($expr); |
499 | 499 | } |
500 | 500 | |
@@ -526,7 +526,7 @@ discard block |
||
526 | 526 | $st = $context->connection->prepare($context->sql); |
527 | 527 | $success = $st->execute($context->params); |
528 | 528 | return new ExecResult($success, $context->connection, $st); |
529 | - }catch (\Exception $e){ |
|
529 | + } catch (\Exception $e){ |
|
530 | 530 | \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
531 | 531 | return null; |
532 | 532 | } |
@@ -552,10 +552,10 @@ discard block |
||
552 | 552 | return $context->handleResult($dict); |
553 | 553 | } |
554 | 554 | return $context->handleResult($res); |
555 | - }else{ |
|
555 | + } else{ |
|
556 | 556 | return false; |
557 | 557 | } |
558 | - }catch (\Exception $e){ |
|
558 | + } catch (\Exception $e){ |
|
559 | 559 | \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
560 | 560 | return false; |
561 | 561 | } |
@@ -583,7 +583,7 @@ discard block |
||
583 | 583 | if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
584 | 584 | count($found)==0){ |
585 | 585 | $columnEnd = strlen($context->sql); |
586 | - }else{ |
|
586 | + } else{ |
|
587 | 587 | list($chars, $columnEnd) = $found[0]; |
588 | 588 | } |
589 | 589 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -594,10 +594,10 @@ discard block |
||
594 | 594 | if($st->execute($context->params)){ |
595 | 595 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
596 | 596 | return intval($res[0]['count']); |
597 | - }else{ |
|
597 | + } else{ |
|
598 | 598 | return false; |
599 | 599 | } |
600 | - }catch (\Exception $e){ |
|
600 | + } catch (\Exception $e){ |
|
601 | 601 | \PhpBoot\abort(new DBException($context, $e->getMessage(),$e->getCode(), $e), ['sql'=>$context->sql, 'params'=>$context->params] ); |
602 | 602 | return false; |
603 | 603 | } |
@@ -608,7 +608,7 @@ discard block |
||
608 | 608 | public function set($context, $column, $value){ |
609 | 609 | if(is_string($column)){ |
610 | 610 | $this->setExpr($context, $column, $value); |
611 | - }else{ |
|
611 | + } else{ |
|
612 | 612 | $this->setArgs($context, $column); |
613 | 613 | } |
614 | 614 | } |
@@ -618,7 +618,7 @@ discard block |
||
618 | 618 | if($this->first){ |
619 | 619 | $this->first = false; |
620 | 620 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
621 | - }else{ |
|
621 | + } else{ |
|
622 | 622 | $prefix = ','; |
623 | 623 | } |
624 | 624 | |
@@ -633,7 +633,7 @@ discard block |
||
633 | 633 | $k = DB::wrap($k); |
634 | 634 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
635 | 635 | $set[]= "$k=".$v->get(); |
636 | - }else{ |
|
636 | + } else{ |
|
637 | 637 | $set[]= "$k=?"; |
638 | 638 | $params[]=$v; |
639 | 639 | } |
@@ -642,7 +642,7 @@ discard block |
||
642 | 642 | $this->first = false; |
643 | 643 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
644 | 644 | $context->appendParams($params); |
645 | - }else{ |
|
645 | + } else{ |
|
646 | 646 | $context->appendSql(','.implode(',', $set),false); |
647 | 647 | $context->appendParams($params); |
648 | 648 | } |