1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\database\statement; |
4
|
|
|
|
5
|
|
|
use PDOStatement; |
6
|
|
|
use suda\database\Binder; |
7
|
|
|
use suda\database\exception\SQLException; |
8
|
|
|
use suda\database\middleware\Middleware; |
9
|
|
|
|
10
|
|
|
abstract class Statement extends StatementConfig |
11
|
|
|
{ |
12
|
|
|
use PrepareTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* 绑定 |
16
|
|
|
* |
17
|
|
|
* @var Binder[] |
18
|
|
|
*/ |
19
|
|
|
protected $binder = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* SQL语句 |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $string; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* PDOStatement |
30
|
|
|
* |
31
|
|
|
* @var PDOStatement|null |
32
|
|
|
*/ |
33
|
|
|
protected $statement = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Query |
37
|
|
|
* |
38
|
|
|
* @var Query |
39
|
|
|
*/ |
40
|
|
|
protected $query; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* 类 |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $fetchClass; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* 参数 |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $fetchClassArgs = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 数据处理中间件 |
58
|
|
|
* |
59
|
|
|
* @var Middleware |
60
|
|
|
*/ |
61
|
|
|
protected $middleware; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* 运行结果 |
65
|
|
|
* |
66
|
|
|
* @var bool |
67
|
|
|
*/ |
68
|
|
|
protected $success; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Statement constructor. |
72
|
|
|
* @param string $sql |
73
|
|
|
* @param mixed ...$args |
74
|
|
|
* @throws SQLException |
75
|
|
|
*/ |
76
|
|
|
public function __construct(string $sql, ...$args) |
77
|
|
|
{ |
78
|
|
|
if (count($args) === 1 && is_array($args[0])) { |
79
|
|
|
$this->create($sql, $args[0]); |
80
|
|
|
} else { |
81
|
|
|
list($this->string, $this->binder) = $this->prepareQueryMark($sql, $args); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $sql |
87
|
|
|
* @param array $parameter |
88
|
|
|
* @throws SQLException |
89
|
|
|
*/ |
90
|
|
|
protected function create(string $sql, array $parameter) |
91
|
|
|
{ |
92
|
|
|
list($this->string, $binder) = $this->prepareWhereString($sql, $parameter); |
93
|
|
|
$this->binder = $this->mergeBinder($this->binder, $binder); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* 设置记录类 |
98
|
|
|
* |
99
|
|
|
* @param string|null $class |
100
|
|
|
* @param array $args |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
public function setFetchType(?string $class = null, array $args = []) |
104
|
|
|
{ |
105
|
|
|
$this->fetchClass = $class; |
106
|
|
|
$this->fetchClassArgs = $args; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* 获取取值类 |
112
|
|
|
* |
113
|
|
|
* @return string|null |
114
|
|
|
*/ |
115
|
|
|
public function getFetchClass(): ?string |
116
|
|
|
{ |
117
|
|
|
return $this->fetchClass ?? null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* 获取SQL字符串 |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getString() |
127
|
|
|
{ |
128
|
|
|
return $this->getQuery()->getQuery(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* 准备查询对象 |
133
|
|
|
* |
134
|
|
|
* @return Query |
135
|
|
|
*/ |
136
|
|
|
protected function prepareQuery(): Query |
137
|
|
|
{ |
138
|
|
|
return new Query($this->string, $this->binder); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* 准备查询对象 |
143
|
|
|
* |
144
|
|
|
* @return Query |
145
|
|
|
*/ |
146
|
|
|
public function prepare(): Query |
147
|
|
|
{ |
148
|
|
|
return $this->query = $this->prepareQuery(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* 获取查询对象 |
153
|
|
|
* |
154
|
|
|
* @return Query |
155
|
|
|
*/ |
156
|
|
|
public function getQuery(): Query |
157
|
|
|
{ |
158
|
|
|
if ($this->query === null) { |
159
|
|
|
$this->query = $this->prepare(); |
160
|
|
|
} |
161
|
|
|
return $this->query; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param Query $query |
166
|
|
|
*/ |
167
|
|
|
public function setQuery(Query $query): void |
168
|
|
|
{ |
169
|
|
|
$this->query = $query; |
170
|
|
|
$this->string = $query->getQuery(); |
171
|
|
|
$this->binder = $query->getBinder(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* 获取绑定信息 |
176
|
|
|
* |
177
|
|
|
* @return Binder[] |
178
|
|
|
*/ |
179
|
|
|
public function getBinder() |
180
|
|
|
{ |
181
|
|
|
return $this->binder; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function __toString() |
188
|
|
|
{ |
189
|
|
|
return $this->getString(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get PDOStatement |
195
|
|
|
* |
196
|
|
|
* @return PDOStatement |
197
|
|
|
*/ |
198
|
|
|
public function getStatement(): ?PDOStatement |
199
|
|
|
{ |
200
|
|
|
return $this->statement; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Set PDOStatement |
205
|
|
|
* |
206
|
|
|
* @param PDOStatement $statement PDOStatement |
207
|
|
|
* |
208
|
|
|
* @return $this |
209
|
|
|
*/ |
210
|
|
|
public function setStatement(PDOStatement $statement) |
211
|
|
|
{ |
212
|
|
|
$this->statement = $statement; |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get 参数 |
219
|
|
|
* |
220
|
|
|
* @return array |
221
|
|
|
*/ |
222
|
|
|
public function getFetchClassArgs() |
223
|
|
|
{ |
224
|
|
|
return $this->fetchClassArgs; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get 数据处理中间件 |
229
|
|
|
* @return Middleware |
230
|
|
|
*/ |
231
|
|
|
public function getMiddleware() |
232
|
|
|
{ |
233
|
|
|
return $this->middleware; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Set 数据处理中间件 |
238
|
|
|
* |
239
|
|
|
* @param Middleware $middleware 数据处理中间件 |
240
|
|
|
* @return $this |
241
|
|
|
*/ |
242
|
|
|
public function setMiddleware(Middleware $middleware) |
243
|
|
|
{ |
244
|
|
|
$this->middleware = $middleware; |
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param Binder|Binder[] $binder |
250
|
|
|
* @return $this |
251
|
|
|
*/ |
252
|
|
|
public function addBinder($binder) |
253
|
|
|
{ |
254
|
|
|
if ($binder instanceof Binder) { |
255
|
|
|
$binder = [$binder]; |
256
|
|
|
} |
257
|
|
|
return $this->addBinderArray($binder); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param Binder[] $binder |
262
|
|
|
* @return $this |
263
|
|
|
*/ |
264
|
|
|
protected function addBinderArray(array $binder) |
265
|
|
|
{ |
266
|
|
|
$this->binder = $this->mergeBinder($this->binder, $binder); |
267
|
|
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* 添加参数 |
272
|
|
|
* |
273
|
|
|
* @param string $name |
274
|
|
|
* @param mixed $value |
275
|
|
|
* @param string|null $key |
276
|
|
|
* @return $this |
277
|
|
|
*/ |
278
|
|
|
public function addValue(string $name, $value, ?string $key = null) |
279
|
|
|
{ |
280
|
|
|
return $this->addBinder(new Binder($name, $value, $key)); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return bool |
285
|
|
|
*/ |
286
|
|
|
public function isSuccess(): bool |
287
|
|
|
{ |
288
|
|
|
return $this->success; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param bool $success |
293
|
|
|
*/ |
294
|
|
|
public function setSuccess(bool $success): void |
295
|
|
|
{ |
296
|
|
|
$this->success = $success; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|