|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace suda\database\statement; |
|
4
|
|
|
|
|
5
|
|
|
use function func_get_args; |
|
6
|
|
|
use function func_num_args; |
|
7
|
|
|
use function is_array; |
|
8
|
|
|
use suda\database\struct\TableStruct; |
|
9
|
|
|
use suda\database\middleware\Middleware; |
|
10
|
|
|
use suda\database\exception\SQLException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ReadStatement |
|
14
|
|
|
* @package suda\database\statement |
|
15
|
|
|
*/ |
|
16
|
|
|
class ReadStatement extends QueryStatement |
|
17
|
|
|
{ |
|
18
|
|
|
use PrepareTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* 数据表结构 |
|
22
|
|
|
* |
|
23
|
|
|
* @var TableStruct |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $struct; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* 数据原始表名 |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $table; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $distinct = ''; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $select = '*'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $where = ''; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $groupBy = ''; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $having = ''; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $orderBy = ''; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $limit = ''; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $rawRead = true; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* 创建写 |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $rawTableName |
|
78
|
|
|
* @param TableStruct $struct |
|
79
|
|
|
* @param Middleware $middleware |
|
80
|
|
|
* @throws SQLException |
|
81
|
|
|
*/ |
|
82
|
|
|
public function __construct(string $rawTableName, TableStruct $struct, Middleware $middleware) |
|
83
|
|
|
{ |
|
84
|
|
|
parent::__construct(''); |
|
85
|
|
|
$this->struct = $struct; |
|
86
|
|
|
$this->table = $rawTableName; |
|
87
|
|
|
$this->type = self::READ; |
|
88
|
|
|
$this->fetch = self::FETCH_ONE; |
|
89
|
|
|
$this->middleware = $middleware; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* 单独去重复 |
|
94
|
|
|
* |
|
95
|
|
|
* @return $this |
|
96
|
|
|
*/ |
|
97
|
|
|
public function distinct() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->distinct = 'DISTINCT'; |
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* 查询的列 |
|
105
|
|
|
* |
|
106
|
|
|
* @param array|string $fields |
|
107
|
|
|
* @return $this |
|
108
|
|
|
*/ |
|
109
|
|
|
public function read($fields) |
|
110
|
|
|
{ |
|
111
|
|
|
if (func_num_args() > 1) { |
|
112
|
|
|
$fields = func_get_args(); |
|
113
|
|
|
} |
|
114
|
|
|
if (is_array($fields)) { |
|
115
|
|
|
$this->rawRead = false; |
|
116
|
|
|
foreach ($fields as $index => $name) { |
|
117
|
|
|
$fields[$index] = $this->middleware->inputName($name); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
$this->select = $this->prepareReadFields($fields); |
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
public function isRawRead(): bool |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->rawRead; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* 条件 |
|
135
|
|
|
* @param $where |
|
136
|
|
|
* @param mixed ...$args |
|
137
|
|
|
* @return $this |
|
138
|
|
|
* @throws SQLException |
|
139
|
|
|
*/ |
|
140
|
|
|
public function where($where, ...$args) |
|
141
|
|
|
{ |
|
142
|
|
|
if (is_array($where)) { |
|
143
|
|
|
$where = $this->aliasKeyField($where); |
|
144
|
|
|
$this->whereArray($where, $args[0] ?? []); |
|
145
|
|
|
} elseif (count($args) > 0 && is_array($args[0])) { |
|
146
|
|
|
$this->whereStringArray($where, $args[0]); |
|
147
|
|
|
} else { |
|
148
|
|
|
list($string, $array) = $this->prepareQueryMark($where, $args); |
|
149
|
|
|
$this->whereStringArray($string, $array); |
|
150
|
|
|
} |
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* 处理输入的键 |
|
156
|
|
|
* |
|
157
|
|
|
* @param array $fields |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function aliasKeyField(array $fields) |
|
161
|
|
|
{ |
|
162
|
|
|
$values = []; |
|
163
|
|
|
foreach ($fields as $name => $value) { |
|
164
|
|
|
$index = $this->middleware->inputName($name); |
|
165
|
|
|
$values[$index] = $value; |
|
166
|
|
|
} |
|
167
|
|
|
return $values; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param array $where |
|
172
|
|
|
* @param array $binders |
|
173
|
|
|
* @throws SQLException |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function whereArray(array $where, array $binders) |
|
176
|
|
|
{ |
|
177
|
|
|
list($where, $whereBinder) = $this->prepareWhere($where); |
|
178
|
|
|
$this->whereStringArray($where, array_merge($whereBinder, $binders)); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param string $where |
|
183
|
|
|
* @param array $whereBinder |
|
184
|
|
|
* @throws SQLException |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function whereStringArray(string $where, array $whereBinder) |
|
187
|
|
|
{ |
|
188
|
|
|
list($where, $whereBinder) = $this->prepareWhereString($where, $whereBinder); |
|
189
|
|
|
$this->where = 'WHERE ' . $where; |
|
190
|
|
|
$this->binder = $this->mergeBinder($this->binder, $whereBinder); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* 分组 |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $what |
|
197
|
|
|
* @return $this |
|
198
|
|
|
*/ |
|
199
|
|
|
public function groupBy(string $what) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->groupBy = 'GROUP BY `' . $what . '`'; |
|
202
|
|
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* 含 |
|
207
|
|
|
* |
|
208
|
|
|
* @param string|array $what |
|
209
|
|
|
* @param array $args |
|
210
|
|
|
* @return $this |
|
211
|
|
|
* @throws SQLException |
|
212
|
|
|
*/ |
|
213
|
|
|
public function having($what, ...$args) |
|
214
|
|
|
{ |
|
215
|
|
|
if (is_array($what)) { |
|
216
|
|
|
$this->havingArray($what); |
|
217
|
|
|
} elseif (is_array($args[0])) { |
|
218
|
|
|
$this->havingStringArray($what, $args[0]); |
|
219
|
|
|
} else { |
|
220
|
|
|
list($string, $array) = $this->prepareQueryMark($what, $args); |
|
221
|
|
|
$this->havingStringArray($string, $array); |
|
222
|
|
|
} |
|
223
|
|
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param array $want |
|
228
|
|
|
* @throws SQLException |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function havingArray(array $want) |
|
231
|
|
|
{ |
|
232
|
|
|
list($having, $havingBinder) = $this->prepareWhere($want); |
|
233
|
|
|
$this->havingStringArray($having, $havingBinder); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param string $having |
|
238
|
|
|
* @param array $havingBinder |
|
239
|
|
|
* @throws SQLException |
|
240
|
|
|
*/ |
|
241
|
|
|
protected function havingStringArray(string $having, array $havingBinder) |
|
242
|
|
|
{ |
|
243
|
|
|
list($having, $havingBinder) = $this->prepareWhereString($having, $havingBinder); |
|
244
|
|
|
$this->having = 'HAVING ' . $having; |
|
245
|
|
|
$this->binder = $this->mergeBinder($this->binder, $havingBinder); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* 排序 |
|
250
|
|
|
* |
|
251
|
|
|
* @param string $what |
|
252
|
|
|
* @param string $order |
|
253
|
|
|
* @return $this |
|
254
|
|
|
*/ |
|
255
|
|
|
public function orderBy(string $what, string $order = 'ASC') |
|
256
|
|
|
{ |
|
257
|
|
|
$order = strtoupper($order); |
|
258
|
|
|
if (strlen($this->orderBy) > 0) { |
|
259
|
|
|
$this->orderBy .= ',`' . $what . '` ' . $order; |
|
260
|
|
|
} else { |
|
261
|
|
|
$this->orderBy = 'ORDER BY `' . $what . '` ' . $order; |
|
262
|
|
|
} |
|
263
|
|
|
return $this; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* 清除排序语句 |
|
268
|
|
|
*/ |
|
269
|
|
|
public function clearOrderBy() { |
|
270
|
|
|
$this->orderBy = ''; |
|
271
|
|
|
return $this; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* 限制 |
|
276
|
|
|
* |
|
277
|
|
|
* @param int $start |
|
278
|
|
|
* @param integer $length |
|
279
|
|
|
* @return $this |
|
280
|
|
|
*/ |
|
281
|
|
|
public function limit(int $start, int $length = null) |
|
282
|
|
|
{ |
|
283
|
|
|
$this->limit = 'LIMIT ' . $start . ($length !== null ? ',' . $length : ''); |
|
284
|
|
|
return $this; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* 分页 |
|
289
|
|
|
* |
|
290
|
|
|
* @param integer $page |
|
291
|
|
|
* @param integer $length |
|
292
|
|
|
* @return $this |
|
293
|
|
|
*/ |
|
294
|
|
|
public function page(int $page, int $length) |
|
295
|
|
|
{ |
|
296
|
|
|
if ($page <= 0) { |
|
297
|
|
|
$page = 1; |
|
298
|
|
|
} |
|
299
|
|
|
$this->limit(($page - 1) * $length, $length); |
|
300
|
|
|
return $this; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* 获取字符串 |
|
305
|
|
|
* |
|
306
|
|
|
* @return Query |
|
307
|
|
|
*/ |
|
308
|
|
|
protected function prepareQuery(): Query |
|
309
|
|
|
{ |
|
310
|
|
|
$where = [$this->where, $this->groupBy, $this->having, $this->orderBy, $this->limit]; |
|
311
|
|
|
$condition = implode(' ', array_filter(array_map('trim', $where), 'strlen')); |
|
312
|
|
|
$select = [$this->distinct, $this->select]; |
|
313
|
|
|
$selection = implode(' ', array_filter(array_map('trim', $select), 'strlen')); |
|
314
|
|
|
$string = sprintf("SELECT %s FROM %s %s", $selection, $this->table, $condition); |
|
315
|
|
|
return new Query($string, $this->binder); |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|