This repository was archived by the owner on Nov 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDeleteQuery.php
More file actions
326 lines (282 loc) · 6.41 KB
/
DeleteQuery.php
File metadata and controls
326 lines (282 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
namespace Selective\Database;
use PDO;
use PDOStatement;
use RuntimeException;
/**
* Delete Query.
*
* @see https://dev.mysql.com/doc/refman/5.7/en/delete.html
*/
final class DeleteQuery implements QueryInterface
{
/**
* @var PDO
*/
private PDO $pdo;
/**
* @var Quoter
*/
private Quoter $quoter;
/**
* @var string Table name
*/
private string $table;
/**
* @var string Priority modifier
*/
private string $priority;
/**
* Errors that occur while executing the DELETE statement are ignored.
*
* @var string Ignore modifier
*/
private string $ignore;
/**
* @var string Ignore modifier
*/
private string $quick;
/**
* @var Condition Where conditions
*/
private Condition $condition;
/**
* @var array Order by
*/
private array $orderBy = [];
/**
* @var int Row count
*/
private int $limit;
/**
* @var bool Truncate
*/
private bool $truncate = false;
/**
* Constructor.
*
* @param Connection $connection The connection
*/
public function __construct(Connection $connection)
{
$this->pdo = $connection->getPdo();
$this->quoter = $connection->getQuoter();
$this->condition = new Condition($connection, $this);
}
/**
* Priority modifier.
*
* If you specify LOW_PRIORITY, the server delays execution of the
* DELETE until no other clients are reading from the table.
*
* This affects only storage engines that use only table-level
* locking (such as MyISAM, MEMORY, and MERGE).
*
* @return self
*/
public function lowPriority(): self
{
$this->priority = 'LOW_PRIORITY';
return $this;
}
/**
* Ignore errors modifier.
*
* @return self
*/
public function ignore(): self
{
$this->ignore = 'IGNORE';
return $this;
}
/**
* Quick modifier.
*
* For MyISAM tables, if you use the QUICK modifier,
* the storage engine does not merge index leaves during delete,
* which may speed up some kinds of delete operations.
*
* @return self
*/
public function quick(): self
{
$this->quick = 'QUICK';
return $this;
}
/**
* Table name.
*
* @param string $table Table name
*
* @return self
*/
public function from(string $table): self
{
$this->table = $table;
return $this;
}
/**
* Where AND condition.
*
* @param array ...$conditions The condition (field, comparison, value)
* or (field, comparison, new RawExp('table.field'))
* or new RawExp('...')
*
* @return self
*/
public function where(...$conditions): self
{
$this->condition->where($conditions);
return $this;
}
/**
* Where OR condition.
*
* @param array ...$conditions The conditions (field, comparison, value)
* or (field, comparison, new RawExp('table.field'))
* or new RawExp('...')
*
* @return self
*/
public function orWhere(...$conditions): self
{
$this->condition->orWhere($conditions);
return $this;
}
/**
* Order by.
*
* @param array ...$fields Column name(s)
*
* @return self
*/
public function orderBy(...$fields): self
{
$this->orderBy = $fields;
return $this;
}
/**
* Limit the number of rows returned.
*
* @param int $rowCount Row count
*
* @return self
*/
public function limit(int $rowCount): self
{
$this->limit = $rowCount;
return $this;
}
/**
* Truncate the entire table.
*
* @return self
*/
public function truncate(): self
{
$this->truncate = true;
return $this;
}
/**
* Executes a prepared statement.
*
* @return bool
*/
public function execute(): bool
{
return $this->prepare()->execute();
}
/**
* Prepares a statement for execution and returns a statement object.
*
* @throws RuntimeException
*
* @return PDOStatement
*/
public function prepare(): PDOStatement
{
$statement = $this->pdo->prepare($this->build());
if (!$statement instanceof PDOStatement) {
throw new RuntimeException('The database statement could not be prepared.');
}
return $statement;
}
/**
* Build a SQL string.
*
* @return string SQL string
*/
public function build(): string
{
if ($this->truncate === true) {
return $this->getTruncateSql();
}
$sql = [];
$sql = $this->getDeleteSql($sql);
$sql = $this->condition->getWhereSql($sql);
$sql = $this->getOrderBySql($sql);
$sql = $this->getLimitSql($sql);
return trim(implode(' ', $sql)) . ';';
}
/**
* Get sql.
*
* @return string The sql
*/
private function getTruncateSql(): string
{
return 'TRUNCATE TABLE ' . $this->quoter->quoteName($this->table) . ';';
}
/**
* Get sql.
*
* @param array $sql The sql
*
* @return array The sql
*/
private function getDeleteSql(array $sql): array
{
$delete = 'DELETE';
if (!empty($this->priority)) {
$delete .= ' ' . $this->priority;
}
if (!empty($this->quick)) {
$delete .= ' ' . $this->quick;
}
if (!empty($this->ignore)) {
$delete .= ' ' . $this->ignore;
}
$sql[] = $delete . ' FROM ' . $this->quoter->quoteName($this->table);
return $sql;
}
/**
* Get sql.
*
* @param array $sql The sql
*
* @return array The sql
*/
private function getOrderBySql(array $sql): array
{
if (empty($this->orderBy)) {
return $sql;
}
$sql[] = 'ORDER BY ' . implode(', ', $this->quoter->quoteByFields($this->orderBy));
return $sql;
}
/**
* Get sql.
*
* @param array $sql The sql
*
* @return array The sql
*/
private function getLimitSql(array $sql): array
{
if (!isset($this->limit)) {
return $sql;
}
$sql[] = sprintf('LIMIT %s', $this->limit);
return $sql;
}
}