/
Command.php
291 lines (260 loc) · 11 KB
/
Command.php
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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\db;
use Craft;
use craft\helpers\Db;
use craft\helpers\StringHelper;
use DateTime;
use yii\db\Query as YiiQuery;
/**
* @inheritdoc
* @property Connection $db Connection the DB connection that this command is associated with.
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class Command extends \yii\db\Command
{
/**
* @inheritdoc
*
* If the table contains `dateCreated`, `dateUpdated`, and/or `uid` columns, those values will be included
* automatically, if not already set.
*/
public function insert($table, $columns): Command
{
if (!isset($columns['dateCreated']) && $this->db->columnExists($table, 'dateCreated')) {
$now = Db::prepareDateForDb(new DateTime());
$columns['dateCreated'] = $now;
}
if (!isset($columns['dateUpdated']) && $this->db->columnExists($table, 'dateUpdated')) {
$columns['dateUpdated'] = $now ?? Db::prepareDateForDb(new DateTime());
}
if (!isset($columns['uid']) && $this->db->columnExists($table, 'uid')) {
$columns['uid'] = StringHelper::UUID();
}
return parent::insert($table, $columns);
}
/**
* @inheritdoc
*
* If the table contains `dateCreated`, `dateUpdated`, and/or `uid` columns, those values will be included
* automatically, if not already set.
*/
public function batchInsert($table, $columns, $rows): Command
{
if (empty($rows)) {
return $this;
}
if (!in_array('dateCreated', $columns) && $this->db->columnExists($table, 'dateCreated')) {
$columns[] = 'dateCreated';
$now = Db::prepareDateForDb(new DateTime());
foreach ($rows as &$row) {
$row[] = $now;
}
}
if (!in_array('dateUpdated', $columns) && $this->db->columnExists($table, 'dateUpdated')) {
$columns[] = 'dateUpdated';
$now = $now ?? Db::prepareDateForDb(new DateTime());
foreach ($rows as &$row) {
$row[] = $now;
}
}
if (!in_array('uid', $columns) && $this->db->columnExists($table, 'uid')) {
$columns[] = 'uid';
foreach ($rows as &$row) {
$row[] = StringHelper::UUID();
}
}
return parent::batchInsert($table, $columns, $rows);
}
/**
* @inheritdoc
*
* If the table contains `dateCreated`, `dateUpdated`, and/or `uid` columns, those values will be included
* for new rows automatically, if not already set.
*
* @param string $table the table that new rows will be inserted into/updated in.
* @param array|YiiQuery $insertColumns the column data (name => value) to be inserted into the table or instance
* of [[YiiQuery]] to perform `INSERT INTO ... SELECT` SQL statement.
* @param array|bool $updateColumns the column data (name => value) to be updated if they already exist.
* If `true` is passed, the column data will be updated to match the insert column data.
* If `false` is passed, no update will be performed if the column data already exists.
* @param array $params the parameters to be bound to the command.
* @param bool $updateTimestamp Whether the `dateUpdated` column should be updated for existing rows, if the table has one.
* @return $this the command object itself.
*/
public function upsert($table, $insertColumns, $updateColumns = true, $params = [], bool $updateTimestamp = true): Command
{
if (is_array($insertColumns)) {
if (!isset($insertColumns['dateCreated']) && $this->db->columnExists($table, 'dateCreated')) {
$now = Db::prepareDateForDb(new DateTime());
$insertColumns['dateCreated'] = $now;
}
if (!isset($insertColumns['dateUpdated']) && $this->db->columnExists($table, 'dateUpdated')) {
$now = $now ?? Db::prepareDateForDb(new DateTime());
$insertColumns['dateUpdated'] = $now;
}
if (!isset($insertColumns['uid']) && $this->db->columnExists($table, 'uid')) {
$insertColumns['uid'] = StringHelper::UUID();
}
if (
$updateColumns !== false &&
!isset($updateColumns['dateUpdated']) &&
$this->_updateTimestamp($updateTimestamp, $table)
) {
if ($updateColumns === true) {
$updateColumns = array_merge($insertColumns);
unset($updateColumns['dateCreated'], $updateColumns['uid']);
}
$updateColumns['dateUpdated'] = $now ?? Db::prepareDateForDb(new DateTime());
}
}
return parent::upsert($table, $insertColumns, $updateColumns, $params);
}
/**
* @inheritdoc
* @param string $table The table to be updated.
* @param array $columns The column data (name => value) to be updated.
* @param string|array $condition The condition that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify condition.
* @param array $params The parameters to be bound to the command.
* @param bool $updateTimestamp Whether the `dateUpdated` column should be updated, if the table has one.
* @return static The command object itself.
*/
public function update($table, $columns, $condition = '', $params = [], bool $updateTimestamp = true): Command
{
if (!isset($columns['dateUpdated']) && $this->_updateTimestamp($updateTimestamp, $table)) {
$columns['dateUpdated'] = Db::prepareDateForDb(new DateTime());
}
return parent::update($table, $columns, $condition, $params);
}
/**
* Returns whether a table’s `dateUpdated` column should be updated.
*
* @param bool $updateTimestamp
* @param string $table
* @return bool
*/
private function _updateTimestamp(bool $updateTimestamp, string $table): bool
{
return $updateTimestamp && $this->db->columnExists($table, 'dateUpdated');
}
/**
* Creates a DELETE command that will only delete duplicate rows from a table.
*
* For example,
*
* ```php
* $connection->createCommand()->deleteDuplicates('user', ['email'])->execute();
* ```
*
* The method will properly escape the table and column names.
*
* Note that the created command is not executed until [[execute()]] is called.
*
* @param string $table The table where the data will be deleted from
* @param string[] $columns The column names that contain duplicate data
* @param string $pk The primary key column name
* @return $this the command object itself
* @since 3.5.2
*/
public function deleteDuplicates(string $table, array $columns, string $pk = 'id'): self
{
$sql = $this->db->getQueryBuilder()->deleteDuplicates($table, $columns, $pk);
return $this->setSql($sql);
}
/**
* Creates a SQL statement for replacing some text with other text in a given table column.
*
* @param string $table The table to be updated.
* @param string $column The column to be searched.
* @param string $find The text to be searched for.
* @param string $replace The replacement text.
* @param array|string $condition The condition that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify condition.
* @param array $params The parameters to be bound to the command.
* @return Command The command object itself.
*/
public function replace(string $table, string $column, string $find, string $replace, array|string $condition = '', array $params = []): Command
{
$sql = $this->db->getQueryBuilder()->replace($table, $column, $find, $replace, $condition, $params);
return $this->setSql($sql)->bindValues($params);
}
/**
* Creates a SQL statement for dropping a DB table, if it exists.
*
* @param string $table The table to be dropped. The name will be properly quoted by the method.
* @return Command the command object itself
*/
public function dropTableIfExists(string $table): Command
{
$sql = $this->db->getQueryBuilder()->dropTableIfExists($table);
return $this->setSql($sql);
}
/**
* Creates a SQL statement for renaming a DB sequence.
*
* @param string $oldName the sequence to be renamed. The name will be properly quoted by the method.
* @param string $newName the new sequence name. The name will be properly quoted by the method.
* @return Command the command object itself
*/
public function renameSequence(string $oldName, string $newName): Command
{
$sql = $this->db->getQueryBuilder()->renameSequence($oldName, $newName);
return $this->setSql($sql);
}
/**
* Creates a SQL statement for soft-deleting a row.
*
* @param string $table The table to be updated.
* @param array|string $condition The condition that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify condition.
* @param array $params The parameters to be bound to the command.
* @return static The command object itself.
* @since 3.1.0
*/
public function softDelete(string $table, array|string $condition = '', array $params = []): Command
{
return $this->update($table, [
'dateDeleted' => Db::prepareDateForDb(new DateTime()),
], $condition, $params, false);
}
/**
* Creates a SQL statement for restoring a soft-deleted row.
*
* @param string $table The table to be updated.
* @param array|string $condition The condition that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify condition.
* @param array $params The parameters to be bound to the command.
* @return static The command object itself.
* @since 3.1.0
*/
public function restore(string $table, array|string $condition = '', array $params = []): Command
{
return $this->update($table, [
'dateDeleted' => null,
], $condition, $params, false);
}
/**
* Logs the current database query if query logging is enabled and returns
* the profiling token if profiling is enabled.
* @param string $category the log category.
* @return array array of two elements, the first is boolean of whether profiling is enabled or not.
* The second is the rawSql if it has been created.
*/
protected function logQuery($category): array
{
if ($this->db->enableLogging) {
$rawSql = $this->getRawSql();
Craft::debug("SQL query:\n" . $rawSql, $category);
}
if (!$this->db->enableProfiling) {
return [false, isset($rawSql) ? $rawSql : null];
}
return [true, isset($rawSql) ? $rawSql : $this->getRawSql()];
}
}