|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class QueryBuilder |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource QueryBuilder.php |
|
6
|
|
|
* @created 28.06.2017 |
|
7
|
|
|
* @package chillerlan\Database\Query |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2017 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\Database\Query; |
|
14
|
|
|
|
|
15
|
|
|
use chillerlan\Database\{Dialects\Dialect, Drivers\DriverInterface, ResultInterface}; |
|
16
|
|
|
use Psr\Log\{ |
|
17
|
|
|
LoggerAwareInterface, LoggerAwareTrait, LoggerInterface |
|
18
|
|
|
}; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* one big ugly block of code |
|
22
|
|
|
* |
|
23
|
|
|
* @property \chillerlan\Database\Query\Alter $alter |
|
24
|
|
|
* @property \chillerlan\Database\Query\Create $create |
|
25
|
|
|
* @property \chillerlan\Database\Query\Delete $delete |
|
26
|
|
|
* @property \chillerlan\Database\Query\Drop $drop |
|
27
|
|
|
* @property \chillerlan\Database\Query\Insert $insert |
|
28
|
|
|
* @property \chillerlan\Database\Query\Select $select |
|
29
|
|
|
* @property \chillerlan\Database\Query\Show $show |
|
30
|
|
|
* @property \chillerlan\Database\Query\Truncate $truncate |
|
31
|
|
|
* @property \chillerlan\Database\Query\Update $update |
|
32
|
|
|
*/ |
|
33
|
|
|
class QueryBuilder implements LoggerAwareInterface{ |
|
34
|
|
|
use LoggerAwareTrait; |
|
35
|
|
|
|
|
36
|
|
|
protected const STATEMENTS = ['alter', 'create', 'delete', 'drop', 'insert', 'select', 'show', 'truncate', 'update']; |
|
37
|
|
|
|
|
38
|
|
|
protected DriverInterface $db; |
|
39
|
|
|
protected Dialect $dialect; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* QueryBuilder constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \chillerlan\Database\Drivers\DriverInterface $db |
|
45
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(DriverInterface $db, LoggerInterface $logger = null){ |
|
48
|
|
|
$this->db = $db; |
|
49
|
|
|
$this->logger = $logger; |
|
50
|
|
|
$this->dialect = $this->db->getDialect(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $name |
|
55
|
|
|
* |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
* @throws \chillerlan\Database\Query\QueryException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __get(string $name){ |
|
60
|
|
|
$name = strtolower($name); |
|
61
|
|
|
|
|
62
|
|
|
if(in_array($name, $this::STATEMENTS, true)){ |
|
63
|
|
|
return call_user_func([$this, $name]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
throw new QueryException('invalid statement'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return \chillerlan\Database\Query\Alter |
|
71
|
|
|
*/ |
|
72
|
|
|
public function alter():Alter{ |
|
73
|
|
|
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Alter{ |
|
74
|
|
|
|
|
75
|
|
|
/** @inheritdoc */ |
|
76
|
|
|
public function table(string $tablename):AlterTable{ |
|
77
|
|
|
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements AlterTable{ |
|
78
|
|
|
use NameTrait; |
|
79
|
|
|
|
|
80
|
|
|
})->name($tablename); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** @inheritdoc */ |
|
84
|
|
|
public function database(string $dbname):AlterDatabase{ |
|
85
|
|
|
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements AlterDatabase{ |
|
86
|
|
|
use NameTrait; |
|
87
|
|
|
|
|
88
|
|
|
})->name($dbname); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
}; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return \chillerlan\Database\Query\Create |
|
96
|
|
|
*/ |
|
97
|
|
|
public function create():Create{ |
|
98
|
|
|
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Create{ |
|
99
|
|
|
|
|
100
|
|
|
/** @inheritdoc */ |
|
101
|
|
|
public function database(string $dbname):CreateDatabase{ |
|
102
|
|
|
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements CreateDatabase, Query{ |
|
103
|
|
|
use CharsetTrait, IfNotExistsTrait, NameTrait, QueryTrait; |
|
104
|
|
|
|
|
105
|
|
|
/** @inheritdoc */ |
|
106
|
|
|
protected function getSQL():array{ |
|
107
|
|
|
return $this->dialect->createDatabase($this->name, $this->ifNotExists, $this->charset); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
})->name($dbname); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** @inheritdoc */ |
|
114
|
|
|
public function table(string $tablename):CreateTable{ |
|
115
|
|
|
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements CreateTable, Query{ |
|
116
|
|
|
use CharsetTrait, IfNotExistsTrait, NameTrait, QueryTrait; |
|
117
|
|
|
|
|
118
|
|
|
protected bool $temp = false; |
|
119
|
|
|
protected ?string $primaryKey = null; |
|
120
|
|
|
protected array $cols = []; |
|
121
|
|
|
protected ?string $dir = null; |
|
122
|
|
|
|
|
123
|
|
|
/** @inheritdoc */ |
|
124
|
|
|
protected function getSQL():array{ |
|
125
|
|
|
return $this->dialect->createTable($this->name, $this->cols, $this->primaryKey, $this->ifNotExists, $this->temp, $this->dir); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** @inheritdoc */ |
|
129
|
|
|
public function temp():CreateTable{ |
|
130
|
|
|
$this->temp = true; |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** @inheritdoc */ |
|
136
|
|
|
public function primaryKey(string $field, string $dir = null):CreateTable{ |
|
137
|
|
|
$this->primaryKey = trim($field); |
|
138
|
|
|
$this->dir = strtoupper($dir); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** @inheritdoc */ |
|
144
|
|
|
public function field(string $name, string $type, $length = null, string $attribute = null, string $collation = null, bool $isNull = null, string $defaultType = null, $defaultValue = null, string $extra = null):CreateTable{ |
|
145
|
|
|
|
|
146
|
|
|
if(is_scalar($defaultValue) && $defaultType === null){ |
|
147
|
|
|
$defaultType = 'USER_DEFINED'; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$this->cols[$name] = $this->dialect->fieldspec($name, $type, $length, $attribute, $collation, $isNull, $defaultType, $defaultValue, $extra); |
|
151
|
|
|
|
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** @inheritdoc */ |
|
156
|
|
|
public function enum(string $name, array $values, $defaultValue = null, bool $isNull = null):CreateTable{ |
|
157
|
|
|
$this->cols[$name] = $this->dialect->enum($name, $values, $defaultValue, $isNull); |
|
158
|
|
|
|
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** @inheritdoc */ |
|
163
|
|
|
public function tinyint(string $name, int $length = null, $defaultValue = null, bool $isNull = null, string $attribute = null):CreateTable{ |
|
164
|
|
|
return $this->field($name, 'TINYINT', $length, $attribute, null, $isNull, null, $defaultValue); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** @inheritdoc */ |
|
168
|
|
|
public function int(string $name, int $length = null, $defaultValue = null, bool $isNull = null, string $attribute = null):CreateTable{ |
|
169
|
|
|
return $this->field($name, 'INT', $length, $attribute, null, $isNull, null, $defaultValue); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** @inheritdoc */ |
|
173
|
|
|
public function varchar(string $name, int $length, $defaultValue = null, bool $isNull = null):CreateTable{ |
|
174
|
|
|
return $this->field($name, 'VARCHAR', $length, null, null, $isNull, null, $defaultValue); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** @inheritdoc */ |
|
178
|
|
|
public function decimal(string $name, string $length, $defaultValue = null, bool $isNull = null):CreateTable{ |
|
179
|
|
|
return $this->field($name, 'DECIMAL', $length, null, null, $isNull, null, $defaultValue); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** @inheritdoc */ |
|
183
|
|
|
public function tinytext(string $name, $defaultValue = null, bool $isNull = null):CreateTable{ |
|
184
|
|
|
return $this->field($name, 'TINYTEXT', null, null, null, $isNull, null, $defaultValue); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** @inheritdoc */ |
|
188
|
|
|
public function text(string $name, $defaultValue = null, bool $isNull = null):CreateTable{ |
|
189
|
|
|
return $this->field($name, 'TEXT', null, null, null, $isNull, null, $defaultValue); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
})->name($tablename); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
}; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @return \chillerlan\Database\Query\Delete |
|
200
|
|
|
*/ |
|
201
|
|
|
public function delete():Delete{ |
|
202
|
|
|
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Delete, Where, BindValues, Query{ |
|
203
|
|
|
use WhereTrait, QueryTrait, NameTrait { |
|
204
|
|
|
name as from; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** @inheritdoc */ |
|
208
|
|
|
protected function getSQL():array{ |
|
209
|
|
|
return $this->dialect->delete($this->name, $this->_getWhere()); |
|
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
}; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return \chillerlan\Database\Query\Drop |
|
217
|
|
|
*/ |
|
218
|
|
|
public function drop():Drop{ |
|
219
|
|
|
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Drop{ |
|
220
|
|
|
|
|
221
|
|
|
/** @inheritdoc */ |
|
222
|
|
|
public function database(string $dbname):DropItem{ |
|
223
|
|
|
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements DropItem, Query{ |
|
224
|
|
|
use IfExistsTrait, NameTrait, QueryTrait; |
|
225
|
|
|
|
|
226
|
|
|
/** @inheritdoc */ |
|
227
|
|
|
protected function getSQL():array{ |
|
228
|
|
|
return $this->dialect->dropDatabase($this->name, $this->ifExists); |
|
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
})->name($dbname); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** @inheritdoc */ |
|
235
|
|
|
public function table(string $tablename):DropItem{ |
|
236
|
|
|
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements DropItem, Query{ |
|
237
|
|
|
use IfExistsTrait, NameTrait, QueryTrait; |
|
238
|
|
|
|
|
239
|
|
|
/** @inheritdoc */ |
|
240
|
|
|
protected function getSQL():array{ |
|
241
|
|
|
return $this->dialect->dropTable($this->name, $this->ifExists); |
|
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
})->name($tablename); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
}; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @return \chillerlan\Database\Query\Insert |
|
252
|
|
|
*/ |
|
253
|
|
|
public function insert():Insert{ |
|
254
|
|
|
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Insert, BindValues, MultiQuery{ |
|
255
|
|
|
use MultiQueryTrait, OnConflictTrait{ |
|
256
|
|
|
name as into; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** @inheritdoc */ |
|
260
|
|
|
protected function getSQL():array{ |
|
261
|
|
|
|
|
262
|
|
|
if(empty($this->bindValues)){ |
|
263
|
|
|
throw new QueryException('no values given'); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return $this->dialect->insert($this->name, array_keys($this->multi ? $this->bindValues[0] : $this->bindValues), $this->on_conflict, $this->conflict_target); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** @inheritdoc */ |
|
270
|
|
|
public function values(iterable $values):Insert{ |
|
271
|
|
|
|
|
272
|
|
|
if($values instanceof ResultInterface){ |
|
273
|
|
|
$this->bindValues = $values->__toArray(); |
|
274
|
|
|
|
|
275
|
|
|
return $this; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
foreach($values as $key => $value){ |
|
279
|
|
|
$this->addBindValue($key, $value); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
return $this; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
}; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @return \chillerlan\Database\Query\Select |
|
290
|
|
|
*/ |
|
291
|
|
|
public function select():Select{ |
|
292
|
|
|
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Select, Where, BindValues, Query{ |
|
293
|
|
|
use QueryTrait, WhereTrait; |
|
294
|
|
|
|
|
295
|
|
|
protected bool $distinct = false; |
|
296
|
|
|
protected array $cols = []; |
|
297
|
|
|
protected array $from = []; |
|
298
|
|
|
protected array $orderby = []; |
|
299
|
|
|
protected array $groupby = []; |
|
300
|
|
|
|
|
301
|
|
|
/** @inheritdoc */ |
|
302
|
|
|
protected function getSQL():array{ |
|
303
|
|
|
|
|
304
|
|
|
if(empty($this->from)){ |
|
305
|
|
|
throw new QueryException('no FROM expression specified'); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
return $this->dialect->select($this->cols, $this->from, $this->_getWhere(), $this->limit, $this->offset, $this->distinct, $this->groupby, $this->orderby); |
|
|
|
|
|
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** @inheritdoc */ |
|
312
|
|
|
public function distinct():Select{ |
|
313
|
|
|
$this->distinct = true; |
|
314
|
|
|
|
|
315
|
|
|
return $this; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** @inheritdoc */ |
|
319
|
|
|
public function cols(array $expressions):Select{ |
|
320
|
|
|
$this->cols = $this->dialect->cols($expressions); |
|
321
|
|
|
|
|
322
|
|
|
return $this; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** @inheritdoc */ |
|
326
|
|
|
public function from(array $expressions):Select{ |
|
327
|
|
|
$this->from = $this->dialect->from($expressions); |
|
328
|
|
|
|
|
329
|
|
|
return $this; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** @inheritdoc */ |
|
333
|
|
|
public function orderBy(array $expressions):Select{ |
|
334
|
|
|
$this->orderby = $this->dialect->orderby($expressions); |
|
335
|
|
|
|
|
336
|
|
|
return $this; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** @inheritdoc */ |
|
340
|
|
|
public function groupBy(array $expressions):Select{ |
|
341
|
|
|
|
|
342
|
|
|
foreach($expressions as $expression){ |
|
343
|
|
|
$this->groupby[] = $this->dialect->quote($expression); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
return $this; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** @inheritdoc */ |
|
350
|
|
|
public function count():int{ |
|
351
|
|
|
|
|
352
|
|
|
if(empty($this->from)){ |
|
353
|
|
|
throw new QueryException('no FROM expression specified'); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
$sql = $this->dialect->selectCount($this->from, $this->_getWhere(), $this->distinct, $this->groupby); |
|
357
|
|
|
$result = $this->db->prepared(implode(' ', $sql), $this->bindValues); |
|
358
|
|
|
|
|
359
|
|
|
if($result instanceof ResultInterface && $result->length > 0){ |
|
360
|
|
|
return (int)$result[0]->count; |
|
|
|
|
|
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
return -1; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
}; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* @return \chillerlan\Database\Query\Show |
|
371
|
|
|
*/ |
|
372
|
|
|
public function show():Show{ |
|
373
|
|
|
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Show{ |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* @param string $name |
|
377
|
|
|
* |
|
378
|
|
|
* @return mixed |
|
379
|
|
|
* @throws \chillerlan\Database\Query\QueryException |
|
380
|
|
|
*/ |
|
381
|
|
|
public function __get(string $name){ |
|
382
|
|
|
$name = strtolower($name); |
|
383
|
|
|
|
|
384
|
|
|
if(in_array($name, ['create'], true)){ |
|
385
|
|
|
return call_user_func([$this, $name]); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
throw new QueryException('invalid statement'); |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
/** @inheritdoc */ |
|
392
|
|
|
public function databases():ShowItem{ |
|
393
|
|
|
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements ShowItem, Query{ |
|
394
|
|
|
use QueryTrait; |
|
395
|
|
|
|
|
396
|
|
|
/** @inheritdoc */ |
|
397
|
|
|
protected function getSQL():array{ |
|
398
|
|
|
return $this->dialect->showDatabases(); // @todo? WHERE |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
}; |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
/** @inheritdoc */ |
|
405
|
|
|
public function tables(string $from = null):ShowItem{ |
|
406
|
|
|
|
|
407
|
|
|
$showTables = new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements ShowItem, Where, Query{ |
|
408
|
|
|
use QueryTrait, WhereTrait, NameTrait{ |
|
409
|
|
|
name as from; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
protected ?string $pattern = null; |
|
413
|
|
|
|
|
414
|
|
|
/** @inheritdoc */ |
|
415
|
|
|
protected function getSQL():array{ |
|
416
|
|
|
return $this->dialect->showTables($this->name, $this->pattern, $this->_getWhere()); |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** @inheritdoc */ |
|
420
|
|
|
public function pattern(string $pattern):ShowItem{ |
|
421
|
|
|
$pattern = trim($pattern); |
|
422
|
|
|
|
|
423
|
|
|
if(!empty($pattern)){ |
|
424
|
|
|
$this->pattern = $pattern; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
return $this; |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
}; |
|
431
|
|
|
|
|
432
|
|
|
if(!empty($from)){ |
|
433
|
|
|
$showTables->from($from); |
|
|
|
|
|
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
return $showTables; |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** @inheritdoc */ |
|
440
|
|
|
public function create():ShowCreate{ |
|
441
|
|
|
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements ShowCreate{ |
|
442
|
|
|
|
|
443
|
|
|
/** @inheritdoc */ |
|
444
|
|
|
public function table(string $tablename):ShowItem{ |
|
445
|
|
|
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements ShowItem, Query{ |
|
446
|
|
|
use QueryTrait, NameTrait; |
|
447
|
|
|
|
|
448
|
|
|
/** @inheritdoc */ |
|
449
|
|
|
protected function getSQL():array{ |
|
450
|
|
|
return $this->dialect->showCreateTable($this->name); |
|
|
|
|
|
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
})->name($tablename); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
}; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
}; |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
/** |
|
463
|
|
|
* @return \chillerlan\Database\Query\Truncate |
|
464
|
|
|
*/ |
|
465
|
|
|
public function truncate():Truncate{ |
|
466
|
|
|
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Truncate{ |
|
467
|
|
|
|
|
468
|
|
|
/** @inheritdoc */ |
|
469
|
|
|
public function table(string $table):Truncate{ |
|
470
|
|
|
return (new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Truncate, Query{ |
|
471
|
|
|
use QueryTrait, NameTrait { |
|
472
|
|
|
name as table; |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
/** @inheritdoc */ |
|
476
|
|
|
protected function getSQL():array{ |
|
477
|
|
|
return $this->dialect->truncate($this->name); |
|
|
|
|
|
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
})->table($table); |
|
481
|
|
|
} |
|
482
|
|
|
|
|
483
|
|
|
}; |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
/** |
|
487
|
|
|
* @return \chillerlan\Database\Query\Update |
|
488
|
|
|
*/ |
|
489
|
|
|
public function update():Update{ |
|
490
|
|
|
|
|
491
|
|
|
return new class($this->db, $this->dialect, $this->logger) extends StatementAbstract implements Update, Where, BindValues, MultiQuery{ |
|
492
|
|
|
use WhereTrait, MultiQueryTrait, NameTrait { |
|
493
|
|
|
name as table; |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
protected array $set = []; |
|
497
|
|
|
|
|
498
|
|
|
/** @inheritdoc */ |
|
499
|
|
|
protected function getSQL():array{ |
|
500
|
|
|
|
|
501
|
|
|
if(empty($this->set)){ |
|
502
|
|
|
throw new QueryException('no fields to update specified'); |
|
503
|
|
|
} |
|
504
|
|
|
|
|
505
|
|
|
return $this->dialect->update($this->name, $this->set, $this->_getWhere()); |
|
|
|
|
|
|
506
|
|
|
} |
|
507
|
|
|
|
|
508
|
|
|
/** @inheritdoc */ |
|
509
|
|
|
public function set(array $set, bool $bind = null):Update{ |
|
510
|
|
|
|
|
511
|
|
|
foreach($set as $k => $v){ |
|
512
|
|
|
|
|
513
|
|
|
if($v instanceof Statement){ |
|
514
|
|
|
$this->set[] = $this->dialect->quote($k).' = ('.$v->sql().')'; |
|
|
|
|
|
|
515
|
|
|
$this->bindValues = array_merge($this->bindValues, $v->bindValues()); |
|
|
|
|
|
|
516
|
|
|
} |
|
517
|
|
|
elseif(is_array($v)){ |
|
518
|
|
|
// @todo: [expr, bindval1, bindval2, ...] |
|
519
|
|
|
} |
|
520
|
|
|
else{ |
|
521
|
|
|
if($bind === false){ |
|
522
|
|
|
// here be dragons |
|
523
|
|
|
$this->set[] = is_int($k) |
|
524
|
|
|
? $this->dialect->quote($v).' = ?' |
|
525
|
|
|
: $this->dialect->quote($k).' = '.$v; //$this->db->escape($v) |
|
526
|
|
|
} |
|
527
|
|
|
else{ |
|
528
|
|
|
$this->set[] = $this->dialect->quote($k).' = ?'; |
|
529
|
|
|
$this->addBindValue($k, is_bool($v) ? (int)$v : $v);// avoid errors with PDO firebird & mysql |
|
530
|
|
|
} |
|
531
|
|
|
} |
|
532
|
|
|
} |
|
533
|
|
|
|
|
534
|
|
|
return $this; |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
}; |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
} |
|
541
|
|
|
|