|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: liu21st <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
declare (strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace think\db; |
|
14
|
|
|
|
|
15
|
|
|
use think\App; |
|
16
|
|
|
use think\Collection; |
|
17
|
|
|
use think\db\exception\BindParamException; |
|
18
|
|
|
use think\db\exception\DataNotFoundException; |
|
19
|
|
|
use think\db\exception\ModelNotFoundException; |
|
20
|
|
|
use think\Exception; |
|
21
|
|
|
use think\exception\DbException; |
|
22
|
|
|
use think\exception\PDOException; |
|
23
|
|
|
use think\Model; |
|
24
|
|
|
use think\Paginator; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* 数据查询类 |
|
28
|
|
|
*/ |
|
29
|
|
|
class BaseQuery |
|
30
|
|
|
{ |
|
31
|
|
|
use concern\TimeFieldQuery; |
|
32
|
|
|
use concern\AggregateQuery; |
|
33
|
|
|
use concern\ModelRelationQuery; |
|
34
|
|
|
use concern\ResultOperation; |
|
35
|
|
|
use concern\Transaction; |
|
36
|
|
|
use concern\WhereQuery; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* 当前数据库连接对象 |
|
40
|
|
|
* @var Connection |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $connection; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* 当前数据表名称(不含前缀) |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $name = ''; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* 当前数据表主键 |
|
52
|
|
|
* @var string|array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $pk; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* 当前数据表前缀 |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $prefix = ''; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* 当前查询参数 |
|
64
|
|
|
* @var array |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $options = []; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* 架构函数 |
|
70
|
|
|
* @access public |
|
71
|
|
|
* @param Connection $connection 数据库连接对象 |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __construct(Connection $connection) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->connection = $connection; |
|
76
|
|
|
|
|
77
|
|
|
$this->prefix = $this->connection->getConfig('prefix'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* 创建一个新的查询对象 |
|
82
|
|
|
* @access public |
|
83
|
|
|
* @return BaseQuery |
|
84
|
|
|
*/ |
|
85
|
|
|
public function newQuery(): BaseQuery |
|
86
|
|
|
{ |
|
87
|
|
|
$query = new static($this->connection); |
|
88
|
|
|
|
|
89
|
|
|
if ($this->model) { |
|
90
|
|
|
$query->model($this->model); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (isset($this->options['table'])) { |
|
94
|
|
|
$query->table($this->options['table']); |
|
95
|
|
|
} else { |
|
96
|
|
|
$query->name($this->name); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $query; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* 利用__call方法实现一些特殊的Model方法 |
|
104
|
|
|
* @access public |
|
105
|
|
|
* @param string $method 方法名称 |
|
106
|
|
|
* @param array $args 调用参数 |
|
107
|
|
|
* @return mixed |
|
108
|
|
|
* @throws DbException |
|
109
|
|
|
* @throws Exception |
|
110
|
|
|
*/ |
|
111
|
|
|
public function __call(string $method, array $args) |
|
112
|
|
|
{ |
|
113
|
|
|
if (strtolower(substr($method, 0, 5)) == 'getby') { |
|
114
|
|
|
// 根据某个字段获取记录 |
|
115
|
|
|
$field = App::parseName(substr($method, 5)); |
|
116
|
|
|
return $this->where($field, '=', $args[0])->find(); |
|
117
|
|
|
} elseif (strtolower(substr($method, 0, 10)) == 'getfieldby') { |
|
118
|
|
|
// 根据某个字段获取记录的某个值 |
|
119
|
|
|
$name = App::parseName(substr($method, 10)); |
|
120
|
|
|
return $this->where($name, '=', $args[0])->value($args[1]); |
|
121
|
|
|
} elseif (strtolower(substr($method, 0, 7)) == 'whereor') { |
|
122
|
|
|
$name = App::parseName(substr($method, 7)); |
|
123
|
|
|
array_unshift($args, $name); |
|
124
|
|
|
return call_user_func_array([$this, 'whereOr'], $args); |
|
125
|
|
|
} elseif (strtolower(substr($method, 0, 5)) == 'where') { |
|
126
|
|
|
$name = App::parseName(substr($method, 5)); |
|
127
|
|
|
array_unshift($args, $name); |
|
128
|
|
|
return call_user_func_array([$this, 'where'], $args); |
|
129
|
|
|
} elseif ($this->model && method_exists($this->model, 'scope' . $method)) { |
|
130
|
|
|
// 动态调用命名范围 |
|
131
|
|
|
$method = 'scope' . $method; |
|
132
|
|
|
array_unshift($args, $this); |
|
133
|
|
|
|
|
134
|
|
|
call_user_func_array([$this->model, $method], $args); |
|
135
|
|
|
return $this; |
|
136
|
|
|
} else { |
|
137
|
|
|
throw new Exception('method not exist:' . static::class . '->' . $method); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* 获取当前的数据库Connection对象 |
|
143
|
|
|
* @access public |
|
144
|
|
|
* @return Connection |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getConnection(): Connection |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->connection; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* 设置当前的数据库Connection对象 |
|
153
|
|
|
* @access public |
|
154
|
|
|
* @param Connection $connection 数据库连接对象 |
|
155
|
|
|
* @return $this |
|
156
|
|
|
*/ |
|
157
|
|
|
public function setConnection(Connection $connection) |
|
158
|
|
|
{ |
|
159
|
|
|
$this->connection = $connection; |
|
160
|
|
|
|
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* 指定当前数据表名(不含前缀) |
|
166
|
|
|
* @access public |
|
167
|
|
|
* @param string $name 不含前缀的数据表名字 |
|
168
|
|
|
* @return $this |
|
169
|
|
|
*/ |
|
170
|
|
|
public function name(string $name) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->name = $name; |
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* 获取当前的数据表名称 |
|
178
|
|
|
* @access public |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getName(): string |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->name ?: $this->model->getName(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* 获取数据库的配置参数 |
|
188
|
|
|
* @access public |
|
189
|
|
|
* @param string $name 参数名称 |
|
190
|
|
|
* @return mixed |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getConfig(string $name = '') |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->connection->getConfig($name); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* 得到当前或者指定名称的数据表 |
|
199
|
|
|
* @access public |
|
200
|
|
|
* @param string $name 不含前缀的数据表名字 |
|
201
|
|
|
* @return mixed |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getTable(string $name = '') |
|
204
|
|
|
{ |
|
205
|
|
|
if (empty($name) && isset($this->options['table'])) { |
|
206
|
|
|
return $this->options['table']; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$name = $name ?: $this->name; |
|
210
|
|
|
|
|
211
|
|
|
return $this->prefix . App::parseName($name); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* 执行查询 返回数据集 |
|
216
|
|
|
* @access public |
|
217
|
|
|
* @param string $sql sql指令 |
|
218
|
|
|
* @param array $bind 参数绑定 |
|
219
|
|
|
* @return array |
|
220
|
|
|
* @throws BindParamException |
|
221
|
|
|
* @throws PDOException |
|
222
|
|
|
*/ |
|
223
|
|
|
public function query(string $sql, array $bind = []): array |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->connection->query($this, $sql, $bind); |
|
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* 执行语句 |
|
230
|
|
|
* @access public |
|
231
|
|
|
* @param string $sql sql指令 |
|
232
|
|
|
* @param array $bind 参数绑定 |
|
233
|
|
|
* @return int |
|
234
|
|
|
* @throws BindParamException |
|
235
|
|
|
* @throws PDOException |
|
236
|
|
|
*/ |
|
237
|
|
|
public function execute(string $sql, array $bind = []): int |
|
238
|
|
|
{ |
|
239
|
|
|
return $this->connection->execute($this, $sql, $bind, true); |
|
|
|
|
|
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* 获取返回或者影响的记录数 |
|
244
|
|
|
* @access public |
|
245
|
|
|
* @return integer |
|
246
|
|
|
*/ |
|
247
|
|
|
public function getNumRows(): int |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->connection->getNumRows(); |
|
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* 获取最近一次查询的sql语句 |
|
254
|
|
|
* @access public |
|
255
|
|
|
* @return string |
|
256
|
|
|
*/ |
|
257
|
|
|
public function getLastSql(): string |
|
258
|
|
|
{ |
|
259
|
|
|
return $this->connection->getLastSql(); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* 获取最近插入的ID |
|
264
|
|
|
* @access public |
|
265
|
|
|
* @param string $sequence 自增序列名 |
|
266
|
|
|
* @return mixed |
|
267
|
|
|
*/ |
|
268
|
|
|
public function getLastInsID(string $sequence = null) |
|
269
|
|
|
{ |
|
270
|
|
|
return $this->connection->getLastInsID($this, $sequence); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* 批处理执行SQL语句 |
|
275
|
|
|
* 批处理的指令都认为是execute操作 |
|
276
|
|
|
* @access public |
|
277
|
|
|
* @param array $sql SQL批处理指令 |
|
278
|
|
|
* @return bool |
|
279
|
|
|
*/ |
|
280
|
|
|
public function batchQuery(array $sql = []): bool |
|
281
|
|
|
{ |
|
282
|
|
|
return $this->connection->batchQuery($this, $sql); |
|
|
|
|
|
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* 得到某个字段的值 |
|
287
|
|
|
* @access public |
|
288
|
|
|
* @param string $field 字段名 |
|
289
|
|
|
* @param mixed $default 默认值 |
|
290
|
|
|
* @return mixed |
|
291
|
|
|
*/ |
|
292
|
|
|
public function value(string $field, $default = null) |
|
293
|
|
|
{ |
|
294
|
|
|
return $this->connection->value($this, $field, $default); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* 得到某个列的数组 |
|
299
|
|
|
* @access public |
|
300
|
|
|
* @param string $field 字段名 多个字段用逗号分隔 |
|
301
|
|
|
* @param string $key 索引 |
|
302
|
|
|
* @return array |
|
303
|
|
|
*/ |
|
304
|
|
|
public function column(string $field, string $key = ''): array |
|
305
|
|
|
{ |
|
306
|
|
|
return $this->connection->column($this, $field, $key); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* 查询SQL组装 union |
|
311
|
|
|
* @access public |
|
312
|
|
|
* @param mixed $union UNION |
|
313
|
|
|
* @param boolean $all 是否适用UNION ALL |
|
314
|
|
|
* @return $this |
|
315
|
|
|
*/ |
|
316
|
|
|
public function union($union, bool $all = false) |
|
317
|
|
|
{ |
|
318
|
|
|
$this->options['union']['type'] = $all ? 'UNION ALL' : 'UNION'; |
|
319
|
|
|
|
|
320
|
|
|
if (is_array($union)) { |
|
321
|
|
|
$this->options['union'] = array_merge($this->options['union'], $union); |
|
322
|
|
|
} else { |
|
323
|
|
|
$this->options['union'][] = $union; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
return $this; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* 查询SQL组装 union all |
|
331
|
|
|
* @access public |
|
332
|
|
|
* @param mixed $union UNION数据 |
|
333
|
|
|
* @return $this |
|
334
|
|
|
*/ |
|
335
|
|
|
public function unionAll($union) |
|
336
|
|
|
{ |
|
337
|
|
|
return $this->union($union, true); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* 指定查询字段 |
|
342
|
|
|
* @access public |
|
343
|
|
|
* @param mixed $field 字段信息 |
|
344
|
|
|
* @return $this |
|
345
|
|
|
*/ |
|
346
|
|
|
public function field($field) |
|
347
|
|
|
{ |
|
348
|
|
|
if (empty($field)) { |
|
349
|
|
|
return $this; |
|
350
|
|
|
} elseif ($field instanceof Raw) { |
|
351
|
|
|
$this->options['field'][] = $field; |
|
352
|
|
|
return $this; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
if (is_string($field)) { |
|
356
|
|
|
if (preg_match('/[\<\'\"\(]/', $field)) { |
|
357
|
|
|
return $this->fieldRaw($field); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
$field = array_map('trim', explode(',', $field)); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
if (true === $field) { |
|
364
|
|
|
// 获取全部字段 |
|
365
|
|
|
$fields = $this->getTableFields(); |
|
|
|
|
|
|
366
|
|
|
$field = $fields ?: ['*']; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
if (isset($this->options['field'])) { |
|
370
|
|
|
$field = array_merge((array) $this->options['field'], $field); |
|
|
|
|
|
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
$this->options['field'] = array_unique($field); |
|
|
|
|
|
|
374
|
|
|
|
|
375
|
|
|
return $this; |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
/** |
|
379
|
|
|
* 指定要排除的查询字段 |
|
380
|
|
|
* @access public |
|
381
|
|
|
* @param array|string $field 要排除的字段 |
|
382
|
|
|
* @return $this |
|
383
|
|
|
*/ |
|
384
|
|
|
public function withoutField($field) |
|
385
|
|
|
{ |
|
386
|
|
|
if (empty($field)) { |
|
387
|
|
|
return $this; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
if (is_string($field)) { |
|
391
|
|
|
$field = array_map('trim', explode(',', $field)); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
// 字段排除 |
|
395
|
|
|
$fields = $this->getTableFields(); |
|
396
|
|
|
$field = $fields ? array_diff($fields, $field) : $field; |
|
|
|
|
|
|
397
|
|
|
|
|
398
|
|
|
if (isset($this->options['field'])) { |
|
399
|
|
|
$field = array_merge((array) $this->options['field'], $field); |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
$this->options['field'] = array_unique($field); |
|
403
|
|
|
|
|
404
|
|
|
return $this; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* 指定其它数据表的查询字段 |
|
409
|
|
|
* @access public |
|
410
|
|
|
* @param mixed $field 字段信息 |
|
411
|
|
|
* @param string $tableName 数据表名 |
|
412
|
|
|
* @param string $prefix 字段前缀 |
|
413
|
|
|
* @param string $alias 别名前缀 |
|
414
|
|
|
* @return $this |
|
415
|
|
|
*/ |
|
416
|
|
|
public function tableField($field, string $tableName, string $prefix = '', string $alias = '') |
|
417
|
|
|
{ |
|
418
|
|
|
if (empty($field)) { |
|
419
|
|
|
return $this; |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
if (is_string($field)) { |
|
423
|
|
|
$field = array_map('trim', explode(',', $field)); |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
if (true === $field) { |
|
427
|
|
|
// 获取全部字段 |
|
428
|
|
|
$fields = $this->getTableFields($tableName); |
|
429
|
|
|
$field = $fields ?: ['*']; |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
// 添加统一的前缀 |
|
433
|
|
|
$prefix = $prefix ?: $tableName; |
|
434
|
|
|
foreach ($field as $key => &$val) { |
|
435
|
|
|
if (is_numeric($key) && $alias) { |
|
436
|
|
|
$field[$prefix . '.' . $val] = $alias . $val; |
|
437
|
|
|
unset($field[$key]); |
|
438
|
|
|
} elseif (is_numeric($key)) { |
|
439
|
|
|
$val = $prefix . '.' . $val; |
|
440
|
|
|
} |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
if (isset($this->options['field'])) { |
|
444
|
|
|
$field = array_merge((array) $this->options['field'], $field); |
|
|
|
|
|
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
|
|
$this->options['field'] = array_unique($field); |
|
|
|
|
|
|
448
|
|
|
|
|
449
|
|
|
return $this; |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
/** |
|
453
|
|
|
* 表达式方式指定查询字段 |
|
454
|
|
|
* @access public |
|
455
|
|
|
* @param string $field 字段名 |
|
456
|
|
|
* @return $this |
|
457
|
|
|
*/ |
|
458
|
|
|
public function fieldRaw(string $field) |
|
459
|
|
|
{ |
|
460
|
|
|
$this->options['field'][] = new Raw($field); |
|
461
|
|
|
|
|
462
|
|
|
return $this; |
|
463
|
|
|
} |
|
464
|
|
|
|
|
465
|
|
|
/** |
|
466
|
|
|
* 设置数据 |
|
467
|
|
|
* @access public |
|
468
|
|
|
* @param array $data 数据 |
|
469
|
|
|
* @return $this |
|
470
|
|
|
*/ |
|
471
|
|
|
public function data(array $data) |
|
472
|
|
|
{ |
|
473
|
|
|
$this->options['data'] = $data; |
|
474
|
|
|
|
|
475
|
|
|
return $this; |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
/** |
|
479
|
|
|
* 字段值增长 |
|
480
|
|
|
* @access public |
|
481
|
|
|
* @param string $field 字段名 |
|
482
|
|
|
* @param float $step 增长值 |
|
483
|
|
|
* @param integer $lazyTime 延时时间(s) |
|
484
|
|
|
* @param string $op INC/DEC |
|
485
|
|
|
* @return $this |
|
486
|
|
|
*/ |
|
487
|
|
|
public function inc(string $field, float $step = 1, int $lazyTime = 0, string $op = 'INC') |
|
488
|
|
|
{ |
|
489
|
|
|
if ($lazyTime > 0) { |
|
490
|
|
|
// 延迟写入 |
|
491
|
|
|
$condition = $this->options['where'] ?? []; |
|
492
|
|
|
|
|
493
|
|
|
$guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition)); |
|
494
|
|
|
$step = $this->connection->lazyWrite($op, $guid, $step, $lazyTime); |
|
495
|
|
|
|
|
496
|
|
|
if (false === $step) { |
|
497
|
|
|
return $this; |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
$op = 'INC'; |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
$this->options['data'][$field] = [$op, $step]; |
|
504
|
|
|
|
|
505
|
|
|
return $this; |
|
506
|
|
|
} |
|
507
|
|
|
|
|
508
|
|
|
/** |
|
509
|
|
|
* 字段值减少 |
|
510
|
|
|
* @access public |
|
511
|
|
|
* @param string $field 字段名 |
|
512
|
|
|
* @param float $step 增长值 |
|
513
|
|
|
* @param integer $lazyTime 延时时间(s) |
|
514
|
|
|
* @return $this |
|
515
|
|
|
*/ |
|
516
|
|
|
public function dec(string $field, float $step = 1, int $lazyTime = 0) |
|
517
|
|
|
{ |
|
518
|
|
|
return $this->inc($field, $step, $lazyTime, 'DEC'); |
|
519
|
|
|
} |
|
520
|
|
|
|
|
521
|
|
|
/** |
|
522
|
|
|
* 使用表达式设置数据 |
|
523
|
|
|
* @access public |
|
524
|
|
|
* @param string $field 字段名 |
|
525
|
|
|
* @param string $value 字段值 |
|
526
|
|
|
* @return $this |
|
527
|
|
|
*/ |
|
528
|
|
|
public function exp(string $field, string $value) |
|
529
|
|
|
{ |
|
530
|
|
|
$this->options['data'][$field] = new Raw($value); |
|
531
|
|
|
return $this; |
|
532
|
|
|
} |
|
533
|
|
|
|
|
534
|
|
|
/** |
|
535
|
|
|
* 去除查询参数 |
|
536
|
|
|
* @access public |
|
537
|
|
|
* @param string $option 参数名 留空去除所有参数 |
|
538
|
|
|
* @return $this |
|
539
|
|
|
*/ |
|
540
|
|
|
public function removeOption(string $option = '') |
|
541
|
|
|
{ |
|
542
|
|
|
if ('' === $option) { |
|
543
|
|
|
$this->options = []; |
|
544
|
|
|
$this->bind = []; |
|
|
|
|
|
|
545
|
|
|
} elseif (isset($this->options[$option])) { |
|
546
|
|
|
unset($this->options[$option]); |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
|
|
return $this; |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
/** |
|
553
|
|
|
* 指定查询数量 |
|
554
|
|
|
* @access public |
|
555
|
|
|
* @param int $offset 起始位置 |
|
556
|
|
|
* @param int $length 查询数量 |
|
557
|
|
|
* @return $this |
|
558
|
|
|
*/ |
|
559
|
|
|
public function limit(int $offset, int $length = null) |
|
560
|
|
|
{ |
|
561
|
|
|
$this->options['limit'] = $offset . ($length ? ',' . $length : ''); |
|
562
|
|
|
|
|
563
|
|
|
return $this; |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
/** |
|
567
|
|
|
* 指定分页 |
|
568
|
|
|
* @access public |
|
569
|
|
|
* @param int $page 页数 |
|
570
|
|
|
* @param int $listRows 每页数量 |
|
571
|
|
|
* @return $this |
|
572
|
|
|
*/ |
|
573
|
|
|
public function page(int $page, int $listRows = null) |
|
574
|
|
|
{ |
|
575
|
|
|
$this->options['page'] = [$page, $listRows]; |
|
576
|
|
|
|
|
577
|
|
|
return $this; |
|
578
|
|
|
} |
|
579
|
|
|
|
|
580
|
|
|
/** |
|
581
|
|
|
* 分页查询 |
|
582
|
|
|
* @access public |
|
583
|
|
|
* @param int|array $listRows 每页数量 数组表示配置参数 |
|
584
|
|
|
* @param int|bool $simple 是否简洁模式或者总记录数 |
|
585
|
|
|
* @param array $config 配置参数 |
|
586
|
|
|
* @return Paginator |
|
587
|
|
|
* @throws DbException |
|
588
|
|
|
*/ |
|
589
|
|
|
public function paginate($listRows = null, $simple = false, $config = []) |
|
590
|
|
|
{ |
|
591
|
|
|
if (is_int($simple)) { |
|
592
|
|
|
$total = $simple; |
|
593
|
|
|
$simple = false; |
|
594
|
|
|
} |
|
595
|
|
|
|
|
596
|
|
|
$defaultConfig = [ |
|
597
|
|
|
'query' => [], //url额外参数 |
|
598
|
|
|
'fragment' => '', //url锚点 |
|
599
|
|
|
'var_page' => 'page', //分页变量 |
|
600
|
|
|
'list_rows' => 15, //每页数量 |
|
601
|
|
|
]; |
|
602
|
|
|
|
|
603
|
|
|
if (is_array($listRows)) { |
|
604
|
|
|
$config = array_merge($defaultConfig, $listRows); |
|
605
|
|
|
$listRows = intval($config['list_rows']); |
|
606
|
|
|
} else { |
|
607
|
|
|
$config = array_merge($defaultConfig, $config); |
|
608
|
|
|
$listRows = intval($listRows ?: $config['list_rows']); |
|
609
|
|
|
} |
|
610
|
|
|
|
|
611
|
|
|
$page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']); |
|
612
|
|
|
|
|
613
|
|
|
$page = $page < 1 ? 1 : $page; |
|
614
|
|
|
|
|
615
|
|
|
$config['path'] = $config['path'] ?? Paginator::getCurrentPath(); |
|
616
|
|
|
|
|
617
|
|
|
if (!isset($total) && !$simple) { |
|
618
|
|
|
$options = $this->getOptions(); |
|
619
|
|
|
|
|
620
|
|
|
unset($this->options['order'], $this->options['limit'], $this->options['page'], $this->options['field']); |
|
621
|
|
|
|
|
622
|
|
|
$bind = $this->bind; |
|
623
|
|
|
$total = $this->count(); |
|
624
|
|
|
$results = $this->options($options)->bind($bind)->page($page, $listRows)->select(); |
|
|
|
|
|
|
625
|
|
|
} elseif ($simple) { |
|
626
|
|
|
$results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select(); |
|
627
|
|
|
$total = null; |
|
628
|
|
|
} else { |
|
629
|
|
|
$results = $this->page($page, $listRows)->select(); |
|
630
|
|
|
} |
|
631
|
|
|
|
|
632
|
|
|
$this->removeOption('limit'); |
|
633
|
|
|
$this->removeOption('page'); |
|
634
|
|
|
|
|
635
|
|
|
return Paginator::make($results, $listRows, $page, $total, $simple, $config); |
|
|
|
|
|
|
636
|
|
|
} |
|
637
|
|
|
|
|
638
|
|
|
/** |
|
639
|
|
|
* 表达式方式指定当前操作的数据表 |
|
640
|
|
|
* @access public |
|
641
|
|
|
* @param mixed $table 表名 |
|
642
|
|
|
* @return $this |
|
643
|
|
|
*/ |
|
644
|
|
|
public function tableRaw(string $table) |
|
645
|
|
|
{ |
|
646
|
|
|
$this->options['table'] = new Raw($table); |
|
647
|
|
|
|
|
648
|
|
|
return $this; |
|
649
|
|
|
} |
|
650
|
|
|
|
|
651
|
|
|
/** |
|
652
|
|
|
* 指定当前操作的数据表 |
|
653
|
|
|
* @access public |
|
654
|
|
|
* @param mixed $table 表名 |
|
655
|
|
|
* @return $this |
|
656
|
|
|
*/ |
|
657
|
|
|
public function table($table) |
|
658
|
|
|
{ |
|
659
|
|
|
if (is_string($table)) { |
|
660
|
|
|
if (strpos($table, ')')) { |
|
661
|
|
|
// 子查询 |
|
662
|
|
|
} elseif (false === strpos($table, ',')) { |
|
663
|
|
|
if (strpos($table, ' ')) { |
|
664
|
|
|
list($item, $alias) = explode(' ', $table); |
|
665
|
|
|
$table = []; |
|
666
|
|
|
$this->alias([$item => $alias]); |
|
667
|
|
|
$table[$item] = $alias; |
|
668
|
|
|
} |
|
669
|
|
|
} else { |
|
670
|
|
|
$tables = explode(',', $table); |
|
671
|
|
|
$table = []; |
|
672
|
|
|
|
|
673
|
|
|
foreach ($tables as $item) { |
|
674
|
|
|
$item = trim($item); |
|
675
|
|
|
if (strpos($item, ' ')) { |
|
676
|
|
|
list($item, $alias) = explode(' ', $item); |
|
677
|
|
|
$this->alias([$item => $alias]); |
|
678
|
|
|
$table[$item] = $alias; |
|
679
|
|
|
} else { |
|
680
|
|
|
$table[] = $item; |
|
681
|
|
|
} |
|
682
|
|
|
} |
|
683
|
|
|
} |
|
684
|
|
|
} elseif (is_array($table)) { |
|
685
|
|
|
$tables = $table; |
|
686
|
|
|
$table = []; |
|
687
|
|
|
|
|
688
|
|
|
foreach ($tables as $key => $val) { |
|
689
|
|
|
if (is_numeric($key)) { |
|
690
|
|
|
$table[] = $val; |
|
691
|
|
|
} else { |
|
692
|
|
|
$this->alias([$key => $val]); |
|
693
|
|
|
$table[$key] = $val; |
|
694
|
|
|
} |
|
695
|
|
|
} |
|
696
|
|
|
} |
|
697
|
|
|
|
|
698
|
|
|
$this->options['table'] = $table; |
|
699
|
|
|
|
|
700
|
|
|
return $this; |
|
701
|
|
|
} |
|
702
|
|
|
|
|
703
|
|
|
/** |
|
704
|
|
|
* USING支持 用于多表删除 |
|
705
|
|
|
* @access public |
|
706
|
|
|
* @param mixed $using USING |
|
707
|
|
|
* @return $this |
|
708
|
|
|
*/ |
|
709
|
|
|
public function using($using) |
|
710
|
|
|
{ |
|
711
|
|
|
$this->options['using'] = $using; |
|
712
|
|
|
return $this; |
|
713
|
|
|
} |
|
714
|
|
|
|
|
715
|
|
|
/** |
|
716
|
|
|
* 存储过程调用 |
|
717
|
|
|
* @access public |
|
718
|
|
|
* @param bool $procedure 是否为存储过程查询 |
|
719
|
|
|
* @return $this |
|
720
|
|
|
*/ |
|
721
|
|
|
public function procedure(bool $procedure = true) |
|
722
|
|
|
{ |
|
723
|
|
|
$this->options['procedure'] = $procedure; |
|
724
|
|
|
return $this; |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
|
|
/** |
|
728
|
|
|
* 指定排序 order('id','desc') 或者 order(['id'=>'desc','create_time'=>'desc']) |
|
729
|
|
|
* @access public |
|
730
|
|
|
* @param string|array|Raw $field 排序字段 |
|
731
|
|
|
* @param string $order 排序 |
|
732
|
|
|
* @return $this |
|
733
|
|
|
*/ |
|
734
|
|
|
public function order($field, string $order = '') |
|
735
|
|
|
{ |
|
736
|
|
|
if (empty($field)) { |
|
737
|
|
|
return $this; |
|
738
|
|
|
} elseif ($field instanceof Raw) { |
|
739
|
|
|
$this->options['order'][] = $field; |
|
740
|
|
|
return $this; |
|
741
|
|
|
} |
|
742
|
|
|
|
|
743
|
|
|
if (is_string($field)) { |
|
744
|
|
|
if (!empty($this->options['via'])) { |
|
745
|
|
|
$field = $this->options['via'] . '.' . $field; |
|
746
|
|
|
} |
|
747
|
|
|
if (strpos($field, ',')) { |
|
748
|
|
|
$field = array_map('trim', explode(',', $field)); |
|
749
|
|
|
} else { |
|
750
|
|
|
$field = empty($order) ? $field : [$field => $order]; |
|
751
|
|
|
} |
|
752
|
|
|
} elseif (!empty($this->options['via'])) { |
|
753
|
|
|
foreach ($field as $key => $val) { |
|
754
|
|
|
if (is_numeric($key)) { |
|
755
|
|
|
$field[$key] = $this->options['via'] . '.' . $val; |
|
756
|
|
|
} else { |
|
757
|
|
|
$field[$this->options['via'] . '.' . $key] = $val; |
|
758
|
|
|
unset($field[$key]); |
|
759
|
|
|
} |
|
760
|
|
|
} |
|
761
|
|
|
} |
|
762
|
|
|
|
|
763
|
|
|
if (!isset($this->options['order'])) { |
|
764
|
|
|
$this->options['order'] = []; |
|
765
|
|
|
} |
|
766
|
|
|
|
|
767
|
|
|
if (is_array($field)) { |
|
768
|
|
|
$this->options['order'] = array_merge($this->options['order'], $field); |
|
769
|
|
|
} else { |
|
770
|
|
|
$this->options['order'][] = $field; |
|
771
|
|
|
} |
|
772
|
|
|
|
|
773
|
|
|
return $this; |
|
774
|
|
|
} |
|
775
|
|
|
|
|
776
|
|
|
/** |
|
777
|
|
|
* 表达式方式指定Field排序 |
|
778
|
|
|
* @access public |
|
779
|
|
|
* @param string $field 排序字段 |
|
780
|
|
|
* @param array $bind 参数绑定 |
|
781
|
|
|
* @return $this |
|
782
|
|
|
*/ |
|
783
|
|
|
public function orderRaw(string $field, array $bind = []) |
|
784
|
|
|
{ |
|
785
|
|
|
if (!empty($bind)) { |
|
786
|
|
|
$this->bindParams($field, $bind); |
|
|
|
|
|
|
787
|
|
|
} |
|
788
|
|
|
|
|
789
|
|
|
$this->options['order'][] = new Raw($field); |
|
790
|
|
|
|
|
791
|
|
|
return $this; |
|
792
|
|
|
} |
|
793
|
|
|
|
|
794
|
|
|
/** |
|
795
|
|
|
* 指定Field排序 orderField('id',[1,2,3],'desc') |
|
796
|
|
|
* @access public |
|
797
|
|
|
* @param string $field 排序字段 |
|
798
|
|
|
* @param array $values 排序值 |
|
799
|
|
|
* @param string $order 排序 desc/asc |
|
800
|
|
|
* @return $this |
|
801
|
|
|
*/ |
|
802
|
|
|
public function orderField(string $field, array $values, string $order = '') |
|
803
|
|
|
{ |
|
804
|
|
|
if (!empty($values)) { |
|
805
|
|
|
$values['sort'] = $order; |
|
806
|
|
|
|
|
807
|
|
|
$this->options['order'][$field] = $values; |
|
808
|
|
|
} |
|
809
|
|
|
|
|
810
|
|
|
return $this; |
|
811
|
|
|
} |
|
812
|
|
|
|
|
813
|
|
|
/** |
|
814
|
|
|
* 随机排序 |
|
815
|
|
|
* @access public |
|
816
|
|
|
* @return $this |
|
817
|
|
|
*/ |
|
818
|
|
|
public function orderRand() |
|
819
|
|
|
{ |
|
820
|
|
|
$this->options['order'][] = '[rand]'; |
|
821
|
|
|
return $this; |
|
822
|
|
|
} |
|
823
|
|
|
|
|
824
|
|
|
/** |
|
825
|
|
|
* 查询缓存 |
|
826
|
|
|
* @access public |
|
827
|
|
|
* @param mixed $key 缓存key |
|
828
|
|
|
* @param integer|\DateTime $expire 缓存有效期 |
|
829
|
|
|
* @param string $tag 缓存标签 |
|
830
|
|
|
* @return $this |
|
831
|
|
|
*/ |
|
832
|
|
|
public function cache($key = true, $expire = null, string $tag = null) |
|
833
|
|
|
{ |
|
834
|
|
|
if (false === $key) { |
|
835
|
|
|
return $this; |
|
836
|
|
|
} |
|
837
|
|
|
|
|
838
|
|
|
if ($key instanceof \DateTimeInterface || $key instanceof \DateInterval || (is_int($key) && is_null($expire))) { |
|
839
|
|
|
$expire = $key; |
|
840
|
|
|
$key = true; |
|
841
|
|
|
} |
|
842
|
|
|
|
|
843
|
|
|
$this->options['cache'] = [$key, $expire, $tag]; |
|
844
|
|
|
|
|
845
|
|
|
return $this; |
|
846
|
|
|
} |
|
847
|
|
|
|
|
848
|
|
|
/** |
|
849
|
|
|
* 指定group查询 |
|
850
|
|
|
* @access public |
|
851
|
|
|
* @param string|array $group GROUP |
|
852
|
|
|
* @return $this |
|
853
|
|
|
*/ |
|
854
|
|
|
public function group($group) |
|
855
|
|
|
{ |
|
856
|
|
|
$this->options['group'] = $group; |
|
857
|
|
|
return $this; |
|
858
|
|
|
} |
|
859
|
|
|
|
|
860
|
|
|
/** |
|
861
|
|
|
* 指定having查询 |
|
862
|
|
|
* @access public |
|
863
|
|
|
* @param string $having having |
|
864
|
|
|
* @return $this |
|
865
|
|
|
*/ |
|
866
|
|
|
public function having(string $having) |
|
867
|
|
|
{ |
|
868
|
|
|
$this->options['having'] = $having; |
|
869
|
|
|
return $this; |
|
870
|
|
|
} |
|
871
|
|
|
|
|
872
|
|
|
/** |
|
873
|
|
|
* 指定查询lock |
|
874
|
|
|
* @access public |
|
875
|
|
|
* @param bool|string $lock 是否lock |
|
876
|
|
|
* @return $this |
|
877
|
|
|
*/ |
|
878
|
|
|
public function lock($lock = false) |
|
879
|
|
|
{ |
|
880
|
|
|
$this->options['lock'] = $lock; |
|
881
|
|
|
|
|
882
|
|
|
if ($lock) { |
|
883
|
|
|
$this->options['master'] = true; |
|
884
|
|
|
} |
|
885
|
|
|
|
|
886
|
|
|
return $this; |
|
887
|
|
|
} |
|
888
|
|
|
|
|
889
|
|
|
/** |
|
890
|
|
|
* 指定distinct查询 |
|
891
|
|
|
* @access public |
|
892
|
|
|
* @param bool $distinct 是否唯一 |
|
893
|
|
|
* @return $this |
|
894
|
|
|
*/ |
|
895
|
|
|
public function distinct(bool $distinct = true) |
|
896
|
|
|
{ |
|
897
|
|
|
$this->options['distinct'] = $distinct; |
|
898
|
|
|
return $this; |
|
899
|
|
|
} |
|
900
|
|
|
|
|
901
|
|
|
/** |
|
902
|
|
|
* 指定数据表别名 |
|
903
|
|
|
* @access public |
|
904
|
|
|
* @param array|string $alias 数据表别名 |
|
905
|
|
|
* @return $this |
|
906
|
|
|
*/ |
|
907
|
|
|
public function alias($alias) |
|
908
|
|
|
{ |
|
909
|
|
|
if (is_array($alias)) { |
|
910
|
|
|
$this->options['alias'] = $alias; |
|
911
|
|
|
} else { |
|
912
|
|
|
$table = $this->getTable(); |
|
913
|
|
|
|
|
914
|
|
|
$this->options['alias'][$table] = $alias; |
|
915
|
|
|
} |
|
916
|
|
|
|
|
917
|
|
|
return $this; |
|
918
|
|
|
} |
|
919
|
|
|
|
|
920
|
|
|
/** |
|
921
|
|
|
* 指定强制索引 |
|
922
|
|
|
* @access public |
|
923
|
|
|
* @param string $force 索引名称 |
|
924
|
|
|
* @return $this |
|
925
|
|
|
*/ |
|
926
|
|
|
public function force(string $force) |
|
927
|
|
|
{ |
|
928
|
|
|
$this->options['force'] = $force; |
|
929
|
|
|
return $this; |
|
930
|
|
|
} |
|
931
|
|
|
|
|
932
|
|
|
/** |
|
933
|
|
|
* 查询注释 |
|
934
|
|
|
* @access public |
|
935
|
|
|
* @param string $comment 注释 |
|
936
|
|
|
* @return $this |
|
937
|
|
|
*/ |
|
938
|
|
|
public function comment(string $comment) |
|
939
|
|
|
{ |
|
940
|
|
|
$this->options['comment'] = $comment; |
|
941
|
|
|
return $this; |
|
942
|
|
|
} |
|
943
|
|
|
|
|
944
|
|
|
/** |
|
945
|
|
|
* 获取执行的SQL语句而不进行实际的查询 |
|
946
|
|
|
* @access public |
|
947
|
|
|
* @param bool $fetch 是否返回sql |
|
948
|
|
|
* @return $this|Fetch |
|
949
|
|
|
*/ |
|
950
|
|
|
public function fetchSql(bool $fetch = true) |
|
951
|
|
|
{ |
|
952
|
|
|
$this->options['fetch_sql'] = $fetch; |
|
953
|
|
|
|
|
954
|
|
|
if ($fetch) { |
|
955
|
|
|
return new Fetch($this); |
|
956
|
|
|
} |
|
957
|
|
|
|
|
958
|
|
|
return $this; |
|
959
|
|
|
} |
|
960
|
|
|
|
|
961
|
|
|
/** |
|
962
|
|
|
* 设置从主服务器读取数据 |
|
963
|
|
|
* @access public |
|
964
|
|
|
* @param bool $readMaster 是否从主服务器读取 |
|
965
|
|
|
* @return $this |
|
966
|
|
|
*/ |
|
967
|
|
|
public function master(bool $readMaster = true) |
|
968
|
|
|
{ |
|
969
|
|
|
$this->options['master'] = $readMaster; |
|
970
|
|
|
return $this; |
|
971
|
|
|
} |
|
972
|
|
|
|
|
973
|
|
|
/** |
|
974
|
|
|
* 设置是否严格检查字段名 |
|
975
|
|
|
* @access public |
|
976
|
|
|
* @param bool $strict 是否严格检查字段 |
|
977
|
|
|
* @return $this |
|
978
|
|
|
*/ |
|
979
|
|
|
public function strict(bool $strict = true) |
|
980
|
|
|
{ |
|
981
|
|
|
$this->options['strict'] = $strict; |
|
982
|
|
|
return $this; |
|
983
|
|
|
} |
|
984
|
|
|
|
|
985
|
|
|
/** |
|
986
|
|
|
* 设置自增序列名 |
|
987
|
|
|
* @access public |
|
988
|
|
|
* @param string $sequence 自增序列名 |
|
989
|
|
|
* @return $this |
|
990
|
|
|
*/ |
|
991
|
|
|
public function sequence(string $sequence = null) |
|
992
|
|
|
{ |
|
993
|
|
|
$this->options['sequence'] = $sequence; |
|
994
|
|
|
return $this; |
|
995
|
|
|
} |
|
996
|
|
|
|
|
997
|
|
|
/** |
|
998
|
|
|
* 设置是否REPLACE |
|
999
|
|
|
* @access public |
|
1000
|
|
|
* @param bool $replace 是否使用REPLACE写入数据 |
|
1001
|
|
|
* @return $this |
|
1002
|
|
|
*/ |
|
1003
|
|
|
public function replace(bool $replace = true) |
|
1004
|
|
|
{ |
|
1005
|
|
|
$this->options['replace'] = $replace; |
|
1006
|
|
|
return $this; |
|
1007
|
|
|
} |
|
1008
|
|
|
|
|
1009
|
|
|
/** |
|
1010
|
|
|
* 设置当前查询所在的分区 |
|
1011
|
|
|
* @access public |
|
1012
|
|
|
* @param string|array $partition 分区名称 |
|
1013
|
|
|
* @return $this |
|
1014
|
|
|
*/ |
|
1015
|
|
|
public function partition($partition) |
|
1016
|
|
|
{ |
|
1017
|
|
|
$this->options['partition'] = $partition; |
|
1018
|
|
|
return $this; |
|
1019
|
|
|
} |
|
1020
|
|
|
|
|
1021
|
|
|
/** |
|
1022
|
|
|
* 设置DUPLICATE |
|
1023
|
|
|
* @access public |
|
1024
|
|
|
* @param array|string|Raw $duplicate DUPLICATE信息 |
|
1025
|
|
|
* @return $this |
|
1026
|
|
|
*/ |
|
1027
|
|
|
public function duplicate($duplicate) |
|
1028
|
|
|
{ |
|
1029
|
|
|
$this->options['duplicate'] = $duplicate; |
|
1030
|
|
|
return $this; |
|
1031
|
|
|
} |
|
1032
|
|
|
|
|
1033
|
|
|
/** |
|
1034
|
|
|
* 设置查询的额外参数 |
|
1035
|
|
|
* @access public |
|
1036
|
|
|
* @param string $extra 额外信息 |
|
1037
|
|
|
* @return $this |
|
1038
|
|
|
*/ |
|
1039
|
|
|
public function extra(string $extra) |
|
1040
|
|
|
{ |
|
1041
|
|
|
$this->options['extra'] = $extra; |
|
1042
|
|
|
return $this; |
|
1043
|
|
|
} |
|
1044
|
|
|
|
|
1045
|
|
|
/** |
|
1046
|
|
|
* 设置JSON字段信息 |
|
1047
|
|
|
* @access public |
|
1048
|
|
|
* @param array $json JSON字段 |
|
1049
|
|
|
* @param bool $assoc 是否取出数组 |
|
1050
|
|
|
* @return $this |
|
1051
|
|
|
*/ |
|
1052
|
|
|
public function json(array $json = [], bool $assoc = false) |
|
1053
|
|
|
{ |
|
1054
|
|
|
$this->options['json'] = $json; |
|
1055
|
|
|
$this->options['json_assoc'] = $assoc; |
|
1056
|
|
|
return $this; |
|
1057
|
|
|
} |
|
1058
|
|
|
|
|
1059
|
|
|
/** |
|
1060
|
|
|
* 指定数据表主键 |
|
1061
|
|
|
* @access public |
|
1062
|
|
|
* @param string $pk 主键 |
|
1063
|
|
|
* @return $this |
|
1064
|
|
|
*/ |
|
1065
|
|
|
public function pk(string $pk) |
|
1066
|
|
|
{ |
|
1067
|
|
|
$this->pk = $pk; |
|
1068
|
|
|
return $this; |
|
1069
|
|
|
} |
|
1070
|
|
|
|
|
1071
|
|
|
/** |
|
1072
|
|
|
* 获取当前数据表的主键 |
|
1073
|
|
|
* @access public |
|
1074
|
|
|
* @return string|array |
|
1075
|
|
|
*/ |
|
1076
|
|
|
public function getPk() |
|
1077
|
|
|
{ |
|
1078
|
|
|
if (empty($this->pk)) { |
|
1079
|
|
|
$this->pk = $this->connection->getPk($this->getTable()); |
|
|
|
|
|
|
1080
|
|
|
} |
|
1081
|
|
|
|
|
1082
|
|
|
return $this->pk; |
|
1083
|
|
|
} |
|
1084
|
|
|
|
|
1085
|
|
|
/** |
|
1086
|
|
|
* 查询参数批量赋值 |
|
1087
|
|
|
* @access protected |
|
1088
|
|
|
* @param array $options 表达式参数 |
|
1089
|
|
|
* @return $this |
|
1090
|
|
|
*/ |
|
1091
|
|
|
protected function options(array $options) |
|
1092
|
|
|
{ |
|
1093
|
|
|
$this->options = $options; |
|
1094
|
|
|
return $this; |
|
1095
|
|
|
} |
|
1096
|
|
|
|
|
1097
|
|
|
/** |
|
1098
|
|
|
* 获取当前的查询参数 |
|
1099
|
|
|
* @access public |
|
1100
|
|
|
* @param string $name 参数名 |
|
1101
|
|
|
* @return mixed |
|
1102
|
|
|
*/ |
|
1103
|
|
|
public function getOptions(string $name = '') |
|
1104
|
|
|
{ |
|
1105
|
|
|
if ('' === $name) { |
|
1106
|
|
|
return $this->options; |
|
1107
|
|
|
} |
|
1108
|
|
|
|
|
1109
|
|
|
return $this->options[$name] ?? null; |
|
1110
|
|
|
} |
|
1111
|
|
|
|
|
1112
|
|
|
/** |
|
1113
|
|
|
* 设置当前的查询参数 |
|
1114
|
|
|
* @access public |
|
1115
|
|
|
* @param string $option 参数名 |
|
1116
|
|
|
* @param mixed $value 参数值 |
|
1117
|
|
|
* @return $this |
|
1118
|
|
|
*/ |
|
1119
|
|
|
public function setOption(string $option, $value) |
|
1120
|
|
|
{ |
|
1121
|
|
|
$this->options[$option] = $value; |
|
1122
|
|
|
return $this; |
|
1123
|
|
|
} |
|
1124
|
|
|
|
|
1125
|
|
|
/** |
|
1126
|
|
|
* 设置当前字段添加的表别名 |
|
1127
|
|
|
* @access public |
|
1128
|
|
|
* @param string $via 临时表别名 |
|
1129
|
|
|
* @return $this |
|
1130
|
|
|
*/ |
|
1131
|
|
|
public function via(string $via = '') |
|
1132
|
|
|
{ |
|
1133
|
|
|
$this->options['via'] = $via; |
|
1134
|
|
|
|
|
1135
|
|
|
return $this; |
|
1136
|
|
|
} |
|
1137
|
|
|
|
|
1138
|
|
|
/** |
|
1139
|
|
|
* 保存记录 自动判断insert或者update |
|
1140
|
|
|
* @access public |
|
1141
|
|
|
* @param array $data 数据 |
|
1142
|
|
|
* @param bool $forceInsert 是否强制insert |
|
1143
|
|
|
* @return integer |
|
1144
|
|
|
*/ |
|
1145
|
|
|
public function save(array $data = [], bool $forceInsert = false) |
|
1146
|
|
|
{ |
|
1147
|
|
|
if ($forceInsert) { |
|
1148
|
|
|
return $this->insert($data); |
|
|
|
|
|
|
1149
|
|
|
} |
|
1150
|
|
|
|
|
1151
|
|
|
$this->options['data'] = array_merge($this->options['data'] ?? [], $data); |
|
1152
|
|
|
|
|
1153
|
|
|
if (!empty($this->options['where'])) { |
|
1154
|
|
|
$isUpdate = true; |
|
1155
|
|
|
} else { |
|
1156
|
|
|
$isUpdate = $this->parseUpdateData($this->options['data']); |
|
1157
|
|
|
} |
|
1158
|
|
|
|
|
1159
|
|
|
return $isUpdate ? $this->update() : $this->insert(); |
|
|
|
|
|
|
1160
|
|
|
} |
|
1161
|
|
|
|
|
1162
|
|
|
/** |
|
1163
|
|
|
* 插入记录 |
|
1164
|
|
|
* @access public |
|
1165
|
|
|
* @param array $data 数据 |
|
1166
|
|
|
* @param boolean $getLastInsID 返回自增主键 |
|
1167
|
|
|
* @return integer|string |
|
1168
|
|
|
*/ |
|
1169
|
|
|
public function insert(array $data = [], bool $getLastInsID = false) |
|
1170
|
|
|
{ |
|
1171
|
|
|
if (!empty($data)) { |
|
1172
|
|
|
$this->options['data'] = $data; |
|
1173
|
|
|
} |
|
1174
|
|
|
|
|
1175
|
|
|
return $this->connection->insert($this, $getLastInsID); |
|
1176
|
|
|
} |
|
1177
|
|
|
|
|
1178
|
|
|
/** |
|
1179
|
|
|
* 插入记录并获取自增ID |
|
1180
|
|
|
* @access public |
|
1181
|
|
|
* @param array $data 数据 |
|
1182
|
|
|
* @return integer|string |
|
1183
|
|
|
*/ |
|
1184
|
|
|
public function insertGetId(array $data) |
|
1185
|
|
|
{ |
|
1186
|
|
|
return $this->insert($data, true); |
|
1187
|
|
|
} |
|
1188
|
|
|
|
|
1189
|
|
|
/** |
|
1190
|
|
|
* 批量插入记录 |
|
1191
|
|
|
* @access public |
|
1192
|
|
|
* @param array $dataSet 数据集 |
|
1193
|
|
|
* @param integer $limit 每次写入数据限制 |
|
1194
|
|
|
* @return integer |
|
1195
|
|
|
*/ |
|
1196
|
|
|
public function insertAll(array $dataSet = [], int $limit = 0): int |
|
1197
|
|
|
{ |
|
1198
|
|
|
if (empty($dataSet)) { |
|
1199
|
|
|
$dataSet = $this->options['data'] ?? []; |
|
1200
|
|
|
} |
|
1201
|
|
|
|
|
1202
|
|
|
if (empty($limit) && !empty($this->options['limit']) && is_numeric($this->options['limit'])) { |
|
1203
|
|
|
$limit = (int) $this->options['limit']; |
|
1204
|
|
|
} |
|
1205
|
|
|
|
|
1206
|
|
|
return $this->connection->insertAll($this, $dataSet, $limit); |
|
|
|
|
|
|
1207
|
|
|
} |
|
1208
|
|
|
|
|
1209
|
|
|
/** |
|
1210
|
|
|
* 通过Select方式插入记录 |
|
1211
|
|
|
* @access public |
|
1212
|
|
|
* @param array $fields 要插入的数据表字段名 |
|
1213
|
|
|
* @param string $table 要插入的数据表名 |
|
1214
|
|
|
* @return integer |
|
1215
|
|
|
* @throws PDOException |
|
1216
|
|
|
*/ |
|
1217
|
|
|
public function selectInsert(array $fields, string $table): int |
|
1218
|
|
|
{ |
|
1219
|
|
|
return $this->connection->selectInsert($this, $fields, $table); |
|
|
|
|
|
|
1220
|
|
|
} |
|
1221
|
|
|
|
|
1222
|
|
|
/** |
|
1223
|
|
|
* 更新记录 |
|
1224
|
|
|
* @access public |
|
1225
|
|
|
* @param mixed $data 数据 |
|
1226
|
|
|
* @return integer |
|
1227
|
|
|
* @throws Exception |
|
1228
|
|
|
* @throws PDOException |
|
1229
|
|
|
*/ |
|
1230
|
|
|
public function update(array $data = []): int |
|
1231
|
|
|
{ |
|
1232
|
|
|
if (!empty($data)) { |
|
1233
|
|
|
$this->options['data'] = array_merge($this->options['data'] ?? [], $data); |
|
1234
|
|
|
} |
|
1235
|
|
|
|
|
1236
|
|
|
if (empty($this->options['where'])) { |
|
1237
|
|
|
$this->parseUpdateData($this->options['data']); |
|
1238
|
|
|
} |
|
1239
|
|
|
|
|
1240
|
|
|
if (empty($this->options['where']) && $this->model) { |
|
1241
|
|
|
$this->where($this->model->getWhere()); |
|
1242
|
|
|
} |
|
1243
|
|
|
|
|
1244
|
|
|
if (empty($this->options['where'])) { |
|
1245
|
|
|
// 如果没有任何更新条件则不执行 |
|
1246
|
|
|
throw new Exception('miss update condition'); |
|
1247
|
|
|
} |
|
1248
|
|
|
|
|
1249
|
|
|
return $this->connection->update($this); |
|
1250
|
|
|
} |
|
1251
|
|
|
|
|
1252
|
|
|
/** |
|
1253
|
|
|
* 删除记录 |
|
1254
|
|
|
* @access public |
|
1255
|
|
|
* @param mixed $data 表达式 true 表示强制删除 |
|
1256
|
|
|
* @return int |
|
1257
|
|
|
* @throws Exception |
|
1258
|
|
|
* @throws PDOException |
|
1259
|
|
|
*/ |
|
1260
|
|
|
public function delete($data = null): int |
|
1261
|
|
|
{ |
|
1262
|
|
|
if (!is_null($data) && true !== $data) { |
|
1263
|
|
|
// AR模式分析主键条件 |
|
1264
|
|
|
$this->parsePkWhere($data); |
|
1265
|
|
|
} |
|
1266
|
|
|
|
|
1267
|
|
|
if (empty($this->options['where']) && $this->model) { |
|
1268
|
|
|
$this->where($this->model->getWhere()); |
|
1269
|
|
|
} |
|
1270
|
|
|
|
|
1271
|
|
|
if (true !== $data && empty($this->options['where'])) { |
|
1272
|
|
|
// 如果条件为空 不进行删除操作 除非设置 1=1 |
|
1273
|
|
|
throw new Exception('delete without condition'); |
|
1274
|
|
|
} |
|
1275
|
|
|
|
|
1276
|
|
|
if (!empty($this->options['soft_delete'])) { |
|
1277
|
|
|
// 软删除 |
|
1278
|
|
|
list($field, $condition) = $this->options['soft_delete']; |
|
1279
|
|
|
if ($condition) { |
|
1280
|
|
|
unset($this->options['soft_delete']); |
|
1281
|
|
|
$this->options['data'] = [$field => $condition]; |
|
1282
|
|
|
|
|
1283
|
|
|
return $this->connection->update($this); |
|
1284
|
|
|
} |
|
1285
|
|
|
} |
|
1286
|
|
|
|
|
1287
|
|
|
$this->options['data'] = $data; |
|
1288
|
|
|
|
|
1289
|
|
|
return $this->connection->delete($this); |
|
1290
|
|
|
} |
|
1291
|
|
|
|
|
1292
|
|
|
/** |
|
1293
|
|
|
* 查找记录 |
|
1294
|
|
|
* @access public |
|
1295
|
|
|
* @param mixed $data 数据 |
|
1296
|
|
|
* @return Collection |
|
1297
|
|
|
* @throws DbException |
|
1298
|
|
|
* @throws ModelNotFoundException |
|
1299
|
|
|
* @throws DataNotFoundException |
|
1300
|
|
|
*/ |
|
1301
|
|
|
public function select($data = null): Collection |
|
1302
|
|
|
{ |
|
1303
|
|
|
if (!is_null($data)) { |
|
1304
|
|
|
// 主键条件分析 |
|
1305
|
|
|
$this->parsePkWhere($data); |
|
1306
|
|
|
} |
|
1307
|
|
|
|
|
1308
|
|
|
$resultSet = $this->connection->select($this); |
|
1309
|
|
|
|
|
1310
|
|
|
// 返回结果处理 |
|
1311
|
|
|
if (!empty($this->options['fail']) && count($resultSet) == 0) { |
|
1312
|
|
|
$this->throwNotFound(); |
|
1313
|
|
|
} |
|
1314
|
|
|
|
|
1315
|
|
|
// 数据列表读取后的处理 |
|
1316
|
|
|
if (!empty($this->model)) { |
|
1317
|
|
|
// 生成模型对象 |
|
1318
|
|
|
$resultSet = $this->resultSetToModelCollection($resultSet); |
|
1319
|
|
|
} else { |
|
1320
|
|
|
$this->resultSet($resultSet); |
|
1321
|
|
|
} |
|
1322
|
|
|
|
|
1323
|
|
|
return $resultSet; |
|
1324
|
|
|
} |
|
1325
|
|
|
|
|
1326
|
|
|
/** |
|
1327
|
|
|
* 查找单条记录 |
|
1328
|
|
|
* @access public |
|
1329
|
|
|
* @param mixed $data 查询数据 |
|
1330
|
|
|
* @return array|Model|null |
|
1331
|
|
|
* @throws DbException |
|
1332
|
|
|
* @throws ModelNotFoundException |
|
1333
|
|
|
* @throws DataNotFoundException |
|
1334
|
|
|
*/ |
|
1335
|
|
|
public function find($data = null) |
|
1336
|
|
|
{ |
|
1337
|
|
|
if (!is_null($data)) { |
|
1338
|
|
|
// AR模式分析主键条件 |
|
1339
|
|
|
$this->parsePkWhere($data); |
|
1340
|
|
|
} |
|
1341
|
|
|
|
|
1342
|
|
|
if (empty($this->options['where'])) { |
|
1343
|
|
|
$result = []; |
|
1344
|
|
|
} else { |
|
1345
|
|
|
$result = $this->connection->find($this); |
|
1346
|
|
|
} |
|
1347
|
|
|
|
|
1348
|
|
|
// 数据处理 |
|
1349
|
|
|
if (empty($result)) { |
|
1350
|
|
|
return $this->resultToEmpty(); |
|
1351
|
|
|
} |
|
1352
|
|
|
|
|
1353
|
|
|
if (!empty($this->model)) { |
|
1354
|
|
|
// 返回模型对象 |
|
1355
|
|
|
$this->resultToModel($result, $this->options); |
|
1356
|
|
|
} else { |
|
1357
|
|
|
$this->result($result); |
|
1358
|
|
|
} |
|
1359
|
|
|
|
|
1360
|
|
|
return $result; |
|
1361
|
|
|
} |
|
1362
|
|
|
|
|
1363
|
|
|
/** |
|
1364
|
|
|
* 分批数据返回处理 |
|
1365
|
|
|
* @access public |
|
1366
|
|
|
* @param integer $count 每次处理的数据数量 |
|
1367
|
|
|
* @param callable $callback 处理回调方法 |
|
1368
|
|
|
* @param string|array $column 分批处理的字段名 |
|
1369
|
|
|
* @param string $order 字段排序 |
|
1370
|
|
|
* @return bool |
|
1371
|
|
|
* @throws DbException |
|
1372
|
|
|
*/ |
|
1373
|
|
|
public function chunk(int $count, callable $callback, $column = null, string $order = 'asc'): bool |
|
1374
|
|
|
{ |
|
1375
|
|
|
$options = $this->getOptions(); |
|
1376
|
|
|
$column = $column ?: $this->getPk(); |
|
1377
|
|
|
|
|
1378
|
|
|
if (isset($options['order'])) { |
|
1379
|
|
|
unset($options['order']); |
|
1380
|
|
|
} |
|
1381
|
|
|
|
|
1382
|
|
|
$bind = $this->bind; |
|
1383
|
|
|
|
|
1384
|
|
|
if (is_array($column)) { |
|
1385
|
|
|
$times = 1; |
|
1386
|
|
|
$query = $this->options($options)->page($times, $count); |
|
1387
|
|
|
} else { |
|
1388
|
|
|
$query = $this->options($options)->limit($count); |
|
1389
|
|
|
|
|
1390
|
|
|
if (strpos($column, '.')) { |
|
1391
|
|
|
list($alias, $key) = explode('.', $column); |
|
1392
|
|
|
} else { |
|
1393
|
|
|
$key = $column; |
|
1394
|
|
|
} |
|
1395
|
|
|
} |
|
1396
|
|
|
|
|
1397
|
|
|
$resultSet = $query->order($column, $order)->select(); |
|
1398
|
|
|
|
|
1399
|
|
|
while (count($resultSet) > 0) { |
|
1400
|
|
|
if (false === call_user_func($callback, $resultSet)) { |
|
1401
|
|
|
return false; |
|
1402
|
|
|
} |
|
1403
|
|
|
|
|
1404
|
|
|
if (isset($times)) { |
|
1405
|
|
|
$times++; |
|
1406
|
|
|
$query = $this->options($options)->page($times, $count); |
|
1407
|
|
|
} else { |
|
1408
|
|
|
$end = $resultSet->pop(); |
|
1409
|
|
|
$lastId = is_array($end) ? $end[$key] : $end->getData($key); |
|
|
|
|
|
|
1410
|
|
|
|
|
1411
|
|
|
$query = $this->options($options) |
|
1412
|
|
|
->limit($count) |
|
1413
|
|
|
->where($column, 'asc' == strtolower($order) ? '>' : '<', $lastId); |
|
1414
|
|
|
} |
|
1415
|
|
|
|
|
1416
|
|
|
$resultSet = $query->bind($bind)->order($column, $order)->select(); |
|
1417
|
|
|
} |
|
1418
|
|
|
|
|
1419
|
|
|
return true; |
|
1420
|
|
|
} |
|
1421
|
|
|
|
|
1422
|
|
|
/** |
|
1423
|
|
|
* 创建子查询SQL |
|
1424
|
|
|
* @access public |
|
1425
|
|
|
* @param bool $sub 是否添加括号 |
|
1426
|
|
|
* @return string |
|
1427
|
|
|
* @throws DbException |
|
1428
|
|
|
*/ |
|
1429
|
|
|
public function buildSql(bool $sub = true): string |
|
1430
|
|
|
{ |
|
1431
|
|
|
return $sub ? '( ' . $this->fetchSql()->select() . ' )' : $this->fetchSql()->select(); |
|
1432
|
|
|
} |
|
1433
|
|
|
|
|
1434
|
|
|
/** |
|
1435
|
|
|
* 分析表达式(可用于查询或者写入操作) |
|
1436
|
|
|
* @access public |
|
1437
|
|
|
* @return array |
|
1438
|
|
|
*/ |
|
1439
|
|
|
public function parseOptions(): array |
|
1440
|
|
|
{ |
|
1441
|
|
|
$options = $this->getOptions(); |
|
1442
|
|
|
|
|
1443
|
|
|
// 获取数据表 |
|
1444
|
|
|
if (empty($options['table'])) { |
|
1445
|
|
|
$options['table'] = $this->getTable(); |
|
1446
|
|
|
} |
|
1447
|
|
|
|
|
1448
|
|
|
if (!isset($options['where'])) { |
|
1449
|
|
|
$options['where'] = []; |
|
1450
|
|
|
} elseif (isset($options['view'])) { |
|
1451
|
|
|
// 视图查询条件处理 |
|
1452
|
|
|
$this->parseView($options); |
|
|
|
|
|
|
1453
|
|
|
} |
|
1454
|
|
|
|
|
1455
|
|
|
if (!isset($options['field'])) { |
|
1456
|
|
|
$options['field'] = '*'; |
|
1457
|
|
|
} |
|
1458
|
|
|
|
|
1459
|
|
|
foreach (['data', 'order', 'join', 'union'] as $name) { |
|
1460
|
|
|
if (!isset($options[$name])) { |
|
1461
|
|
|
$options[$name] = []; |
|
1462
|
|
|
} |
|
1463
|
|
|
} |
|
1464
|
|
|
|
|
1465
|
|
|
if (!isset($options['strict'])) { |
|
1466
|
|
|
$options['strict'] = $this->connection->getConfig('fields_strict'); |
|
1467
|
|
|
} |
|
1468
|
|
|
|
|
1469
|
|
|
foreach (['master', 'lock', 'fetch_sql', 'array', 'distinct', 'procedure'] as $name) { |
|
1470
|
|
|
if (!isset($options[$name])) { |
|
1471
|
|
|
$options[$name] = false; |
|
1472
|
|
|
} |
|
1473
|
|
|
} |
|
1474
|
|
|
|
|
1475
|
|
|
foreach (['group', 'having', 'limit', 'force', 'comment', 'partition', 'duplicate', 'extra'] as $name) { |
|
1476
|
|
|
if (!isset($options[$name])) { |
|
1477
|
|
|
$options[$name] = ''; |
|
1478
|
|
|
} |
|
1479
|
|
|
} |
|
1480
|
|
|
|
|
1481
|
|
|
if (isset($options['page'])) { |
|
1482
|
|
|
// 根据页数计算limit |
|
1483
|
|
|
list($page, $listRows) = $options['page']; |
|
1484
|
|
|
$page = $page > 0 ? $page : 1; |
|
1485
|
|
|
$listRows = $listRows ?: (is_numeric($options['limit']) ? $options['limit'] : 20); |
|
1486
|
|
|
$offset = $listRows * ($page - 1); |
|
1487
|
|
|
$options['limit'] = $offset . ',' . $listRows; |
|
1488
|
|
|
} |
|
1489
|
|
|
|
|
1490
|
|
|
$this->options = $options; |
|
1491
|
|
|
|
|
1492
|
|
|
return $options; |
|
1493
|
|
|
} |
|
1494
|
|
|
|
|
1495
|
|
|
/** |
|
1496
|
|
|
* 分析数据是否存在更新条件 |
|
1497
|
|
|
* @access public |
|
1498
|
|
|
* @param array $data 数据 |
|
1499
|
|
|
* @return bool |
|
1500
|
|
|
* @throws Exception |
|
1501
|
|
|
*/ |
|
1502
|
|
|
public function parseUpdateData(&$data): bool |
|
1503
|
|
|
{ |
|
1504
|
|
|
$pk = $this->getPk(); |
|
1505
|
|
|
$isUpdate = false; |
|
1506
|
|
|
// 如果存在主键数据 则自动作为更新条件 |
|
1507
|
|
|
if (is_string($pk) && isset($data[$pk])) { |
|
1508
|
|
|
$this->where($pk, '=', $data[$pk]); |
|
1509
|
|
|
$this->options['key'] = $data[$pk]; |
|
1510
|
|
|
unset($data[$pk]); |
|
1511
|
|
|
$isUpdate = true; |
|
1512
|
|
|
} elseif (is_array($pk)) { |
|
1513
|
|
|
foreach ($pk as $field) { |
|
1514
|
|
|
if (isset($data[$field])) { |
|
1515
|
|
|
$this->where($field, '=', $data[$field]); |
|
1516
|
|
|
$isUpdate = true; |
|
1517
|
|
|
} else { |
|
1518
|
|
|
// 如果缺少复合主键数据则不执行 |
|
1519
|
|
|
throw new Exception('miss complex primary data'); |
|
1520
|
|
|
} |
|
1521
|
|
|
unset($data[$field]); |
|
1522
|
|
|
} |
|
1523
|
|
|
} |
|
1524
|
|
|
|
|
1525
|
|
|
return $isUpdate; |
|
1526
|
|
|
} |
|
1527
|
|
|
|
|
1528
|
|
|
/** |
|
1529
|
|
|
* 把主键值转换为查询条件 支持复合主键 |
|
1530
|
|
|
* @access public |
|
1531
|
|
|
* @param array|string $data 主键数据 |
|
1532
|
|
|
* @return void |
|
1533
|
|
|
* @throws Exception |
|
1534
|
|
|
*/ |
|
1535
|
|
|
public function parsePkWhere($data): void |
|
1536
|
|
|
{ |
|
1537
|
|
|
$pk = $this->getPk(); |
|
1538
|
|
|
|
|
1539
|
|
|
if (is_string($pk)) { |
|
1540
|
|
|
// 获取数据表 |
|
1541
|
|
|
if (empty($this->options['table'])) { |
|
1542
|
|
|
$this->options['table'] = $this->getTable(); |
|
1543
|
|
|
} |
|
1544
|
|
|
|
|
1545
|
|
|
$table = is_array($this->options['table']) ? key($this->options['table']) : $this->options['table']; |
|
1546
|
|
|
|
|
1547
|
|
|
if (!empty($this->options['alias'][$table])) { |
|
1548
|
|
|
$alias = $this->options['alias'][$table]; |
|
1549
|
|
|
} |
|
1550
|
|
|
|
|
1551
|
|
|
$key = isset($alias) ? $alias . '.' . $pk : $pk; |
|
1552
|
|
|
// 根据主键查询 |
|
1553
|
|
|
if (is_array($data)) { |
|
1554
|
|
|
$this->where($key, 'in', $data); |
|
1555
|
|
|
} else { |
|
1556
|
|
|
$this->where($key, '=', $data); |
|
1557
|
|
|
$this->options['key'] = $data; |
|
1558
|
|
|
} |
|
1559
|
|
|
} |
|
1560
|
|
|
} |
|
1561
|
|
|
|
|
1562
|
|
|
/** |
|
1563
|
|
|
* 获取模型的更新条件 |
|
1564
|
|
|
* @access protected |
|
1565
|
|
|
* @param array $options 查询参数 |
|
1566
|
|
|
*/ |
|
|
|
|
|
|
1567
|
|
|
protected function getModelUpdateCondition(array $options) |
|
1568
|
|
|
{ |
|
1569
|
|
|
return $options['where']['AND'] ?? null; |
|
1570
|
|
|
} |
|
1571
|
|
|
} |
|
1572
|
|
|
|