|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license http://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yii\db; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\base\Component; |
|
12
|
|
|
use yii\base\NotSupportedException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Command represents a SQL statement to be executed against a database. |
|
16
|
|
|
* |
|
17
|
|
|
* A command object is usually created by calling [[Connection::createCommand()]]. |
|
18
|
|
|
* The SQL statement it represents can be set via the [[sql]] property. |
|
19
|
|
|
* |
|
20
|
|
|
* To execute a non-query SQL (such as INSERT, DELETE, UPDATE), call [[execute()]]. |
|
21
|
|
|
* To execute a SQL statement that returns a result data set (such as SELECT), |
|
22
|
|
|
* use [[queryAll()]], [[queryOne()]], [[queryColumn()]], [[queryScalar()]], or [[query()]]. |
|
23
|
|
|
* |
|
24
|
|
|
* For example, |
|
25
|
|
|
* |
|
26
|
|
|
* ```php |
|
27
|
|
|
* $users = $connection->createCommand('SELECT * FROM user')->queryAll(); |
|
28
|
|
|
* ``` |
|
29
|
|
|
* |
|
30
|
|
|
* Command supports SQL statement preparation and parameter binding. |
|
31
|
|
|
* Call [[bindValue()]] to bind a value to a SQL parameter; |
|
32
|
|
|
* Call [[bindParam()]] to bind a PHP variable to a SQL parameter. |
|
33
|
|
|
* When binding a parameter, the SQL statement is automatically prepared. |
|
34
|
|
|
* You may also call [[prepare()]] explicitly to prepare a SQL statement. |
|
35
|
|
|
* |
|
36
|
|
|
* Command also supports building SQL statements by providing methods such as [[insert()]], |
|
37
|
|
|
* [[update()]], etc. For example, the following code will create and execute an INSERT SQL statement: |
|
38
|
|
|
* |
|
39
|
|
|
* ```php |
|
40
|
|
|
* $connection->createCommand()->insert('user', [ |
|
41
|
|
|
* 'name' => 'Sam', |
|
42
|
|
|
* 'age' => 30, |
|
43
|
|
|
* ])->execute(); |
|
44
|
|
|
* ``` |
|
45
|
|
|
* |
|
46
|
|
|
* To build SELECT SQL statements, please use [[Query]] instead. |
|
47
|
|
|
* |
|
48
|
|
|
* For more details and usage information on Command, see the [guide article on Database Access Objects](guide:db-dao). |
|
49
|
|
|
* |
|
50
|
|
|
* @property string $rawSql The raw SQL with parameter values inserted into the corresponding placeholders in |
|
51
|
|
|
* [[sql]]. |
|
52
|
|
|
* @property string $sql The SQL statement to be executed. |
|
53
|
|
|
* |
|
54
|
|
|
* @author Qiang Xue <[email protected]> |
|
55
|
|
|
* @since 2.0 |
|
56
|
|
|
*/ |
|
57
|
|
|
class Command extends Component |
|
58
|
|
|
{ |
|
59
|
|
|
/** |
|
60
|
|
|
* @var Connection the DB connection that this command is associated with |
|
61
|
|
|
*/ |
|
62
|
|
|
public $db; |
|
63
|
|
|
/** |
|
64
|
|
|
* @var \PDOStatement the PDOStatement object that this command is associated with |
|
65
|
|
|
*/ |
|
66
|
|
|
public $pdoStatement; |
|
67
|
|
|
/** |
|
68
|
|
|
* @var int the default fetch mode for this command. |
|
69
|
|
|
* @see http://www.php.net/manual/en/pdostatement.setfetchmode.php |
|
70
|
|
|
*/ |
|
71
|
|
|
public $fetchMode = \PDO::FETCH_ASSOC; |
|
72
|
|
|
/** |
|
73
|
|
|
* @var array the parameters (name => value) that are bound to the current PDO statement. |
|
74
|
|
|
* This property is maintained by methods such as [[bindValue()]]. It is mainly provided for logging purpose |
|
75
|
|
|
* and is used to generate [[rawSql]]. Do not modify it directly. |
|
76
|
|
|
*/ |
|
77
|
|
|
public $params = []; |
|
78
|
|
|
/** |
|
79
|
|
|
* @var int the default number of seconds that query results can remain valid in cache. |
|
80
|
|
|
* Use 0 to indicate that the cached data will never expire. And use a negative number to indicate |
|
81
|
|
|
* query cache should not be used. |
|
82
|
|
|
* @see cache() |
|
83
|
|
|
*/ |
|
84
|
|
|
public $queryCacheDuration; |
|
85
|
|
|
/** |
|
86
|
|
|
* @var \yii\caching\Dependency the dependency to be associated with the cached query result for this command |
|
87
|
|
|
* @see cache() |
|
88
|
|
|
*/ |
|
89
|
|
|
public $queryCacheDependency; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @var array pending parameters to be bound to the current PDO statement. |
|
93
|
|
|
*/ |
|
94
|
|
|
private $_pendingParams = []; |
|
95
|
|
|
/** |
|
96
|
|
|
* @var string the SQL statement that this command represents |
|
97
|
|
|
*/ |
|
98
|
|
|
private $_sql; |
|
99
|
|
|
/** |
|
100
|
|
|
* @var string name of the table, which schema, should be refreshed after command execution. |
|
101
|
|
|
*/ |
|
102
|
|
|
private $_refreshTableName; |
|
103
|
|
|
/** |
|
104
|
|
|
* @var string|false|null the isolation level to use for this transaction. |
|
105
|
|
|
* See [[Transaction::begin()]] for details. |
|
106
|
|
|
*/ |
|
107
|
|
|
private $_isolationLevel = false; |
|
108
|
|
|
/** |
|
109
|
|
|
* @var callable a callable (e.g. anonymous function) that is called when [[\yii\db\Exception]] is thrown |
|
110
|
|
|
* when executing the command. |
|
111
|
|
|
*/ |
|
112
|
|
|
private $_retryHandler; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Enables query cache for this command. |
|
116
|
|
|
* @param int $duration the number of seconds that query result of this command can remain valid in the cache. |
|
117
|
|
|
* If this is not set, the value of [[Connection::queryCacheDuration]] will be used instead. |
|
118
|
|
|
* Use 0 to indicate that the cached data will never expire. |
|
119
|
|
|
* @param \yii\caching\Dependency $dependency the cache dependency associated with the cached query result. |
|
120
|
|
|
* @return $this the command object itself |
|
121
|
|
|
*/ |
|
122
|
6 |
|
public function cache($duration = null, $dependency = null) |
|
123
|
|
|
{ |
|
124
|
6 |
|
$this->queryCacheDuration = $duration === null ? $this->db->queryCacheDuration : $duration; |
|
125
|
6 |
|
$this->queryCacheDependency = $dependency; |
|
126
|
6 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Disables query cache for this command. |
|
131
|
|
|
* @return $this the command object itself |
|
132
|
|
|
*/ |
|
133
|
3 |
|
public function noCache() |
|
134
|
|
|
{ |
|
135
|
3 |
|
$this->queryCacheDuration = -1; |
|
136
|
3 |
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns the SQL statement for this command. |
|
141
|
|
|
* @return string the SQL statement to be executed |
|
142
|
|
|
*/ |
|
143
|
1282 |
|
public function getSql() |
|
144
|
|
|
{ |
|
145
|
1282 |
|
return $this->_sql; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Specifies the SQL statement to be executed. The SQL statement will be quoted using [[Connection::quoteSql()]]. |
|
150
|
|
|
* The previous SQL (if any) will be discarded, and [[params]] will be cleared as well. See [[reset()]] |
|
151
|
|
|
* for details. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $sql the SQL statement to be set. |
|
154
|
|
|
* @return $this this command instance |
|
155
|
|
|
* @see reset() |
|
156
|
|
|
* @see cancel() |
|
157
|
|
|
*/ |
|
158
|
1303 |
|
public function setSql($sql) |
|
159
|
|
|
{ |
|
160
|
1303 |
|
if ($sql !== $this->_sql) { |
|
161
|
1303 |
|
$this->cancel(); |
|
162
|
1303 |
|
$this->reset(); |
|
163
|
1303 |
|
$this->_sql = $this->db->quoteSql($sql); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
1303 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Specifies the SQL statement to be executed. The SQL statement will not be modified in any way. |
|
171
|
|
|
* The previous SQL (if any) will be discarded, and [[params]] will be cleared as well. See [[reset()]] |
|
172
|
|
|
* for details. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $sql the SQL statement to be set. |
|
175
|
|
|
* @return $this this command instance |
|
176
|
|
|
* @since 2.0.13 |
|
177
|
|
|
* @see reset() |
|
178
|
|
|
* @see cancel() |
|
179
|
|
|
*/ |
|
180
|
22 |
|
public function setRawSql($sql) |
|
181
|
|
|
{ |
|
182
|
22 |
|
if ($sql !== $this->_sql) { |
|
183
|
22 |
|
$this->cancel(); |
|
184
|
22 |
|
$this->reset(); |
|
185
|
22 |
|
$this->_sql = $sql; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
22 |
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Returns the raw SQL by inserting parameter values into the corresponding placeholders in [[sql]]. |
|
193
|
|
|
* Note that the return value of this method should mainly be used for logging purpose. |
|
194
|
|
|
* It is likely that this method returns an invalid SQL due to improper replacement of parameter placeholders. |
|
195
|
|
|
* @return string the raw SQL with parameter values inserted into the corresponding placeholders in [[sql]]. |
|
196
|
|
|
*/ |
|
197
|
1285 |
|
public function getRawSql() |
|
198
|
|
|
{ |
|
199
|
1285 |
|
if (empty($this->params)) { |
|
200
|
1124 |
|
return $this->_sql; |
|
201
|
|
|
} |
|
202
|
982 |
|
$params = []; |
|
203
|
982 |
|
foreach ($this->params as $name => $value) { |
|
204
|
982 |
|
if (is_string($name) && strncmp(':', $name, 1)) { |
|
205
|
15 |
|
$name = ':' . $name; |
|
206
|
|
|
} |
|
207
|
982 |
|
if (is_string($value)) { |
|
208
|
792 |
|
$params[$name] = $this->db->quoteValue($value); |
|
209
|
785 |
|
} elseif (is_bool($value)) { |
|
210
|
25 |
|
$params[$name] = ($value ? 'TRUE' : 'FALSE'); |
|
211
|
778 |
|
} elseif ($value === null) { |
|
212
|
309 |
|
$params[$name] = 'NULL'; |
|
213
|
722 |
|
} elseif ((!is_object($value) && !is_resource($value)) || $value instanceof Expression) { |
|
214
|
982 |
|
$params[$name] = $value; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
982 |
|
if (!isset($params[1])) { |
|
218
|
982 |
|
return strtr($this->_sql, $params); |
|
219
|
|
|
} |
|
220
|
|
|
$sql = ''; |
|
221
|
|
|
foreach (explode('?', $this->_sql) as $i => $part) { |
|
222
|
|
|
$sql .= (isset($params[$i]) ? $params[$i] : '') . $part; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $sql; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Prepares the SQL statement to be executed. |
|
230
|
|
|
* For complex SQL statement that is to be executed multiple times, |
|
231
|
|
|
* this may improve performance. |
|
232
|
|
|
* For SQL statement with binding parameters, this method is invoked |
|
233
|
|
|
* automatically. |
|
234
|
|
|
* @param bool $forRead whether this method is called for a read query. If null, it means |
|
235
|
|
|
* the SQL statement should be used to determine whether it is for read or write. |
|
236
|
|
|
* @throws Exception if there is any DB error |
|
237
|
|
|
*/ |
|
238
|
1270 |
|
public function prepare($forRead = null) |
|
239
|
|
|
{ |
|
240
|
1270 |
|
if ($this->pdoStatement) { |
|
241
|
56 |
|
$this->bindPendingParams(); |
|
242
|
56 |
|
return; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
1270 |
|
$sql = $this->getSql(); |
|
246
|
|
|
|
|
247
|
1270 |
|
if ($this->db->getTransaction()) { |
|
248
|
|
|
// master is in a transaction. use the same connection. |
|
249
|
21 |
|
$forRead = false; |
|
250
|
|
|
} |
|
251
|
1270 |
|
if ($forRead || $forRead === null && $this->db->getSchema()->isReadQuery($sql)) { |
|
252
|
1225 |
|
$pdo = $this->db->getSlavePdo(); |
|
253
|
|
|
} else { |
|
254
|
665 |
|
$pdo = $this->db->getMasterPdo(); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
try { |
|
258
|
1270 |
|
$this->pdoStatement = $pdo->prepare($sql); |
|
259
|
1269 |
|
$this->bindPendingParams(); |
|
260
|
4 |
|
} catch (\Exception $e) { |
|
261
|
4 |
|
$message = $e->getMessage() . "\nFailed to prepare SQL: $sql"; |
|
262
|
4 |
|
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; |
|
263
|
4 |
|
throw new Exception($message, $errorInfo, (int) $e->getCode(), $e); |
|
264
|
|
|
} |
|
265
|
1269 |
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Cancels the execution of the SQL statement. |
|
269
|
|
|
* This method mainly sets [[pdoStatement]] to be null. |
|
270
|
|
|
*/ |
|
271
|
1303 |
|
public function cancel() |
|
272
|
|
|
{ |
|
273
|
1303 |
|
$this->pdoStatement = null; |
|
274
|
1303 |
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Binds a parameter to the SQL statement to be executed. |
|
278
|
|
|
* @param string|int $name parameter identifier. For a prepared statement |
|
279
|
|
|
* using named placeholders, this will be a parameter name of |
|
280
|
|
|
* the form `:name`. For a prepared statement using question mark |
|
281
|
|
|
* placeholders, this will be the 1-indexed position of the parameter. |
|
282
|
|
|
* @param mixed $value the PHP variable to bind to the SQL statement parameter (passed by reference) |
|
283
|
|
|
* @param int $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
|
284
|
|
|
* @param int $length length of the data type |
|
285
|
|
|
* @param mixed $driverOptions the driver-specific options |
|
286
|
|
|
* @return $this the current command being executed |
|
287
|
|
|
* @see http://www.php.net/manual/en/function.PDOStatement-bindParam.php |
|
288
|
|
|
*/ |
|
289
|
3 |
|
public function bindParam($name, &$value, $dataType = null, $length = null, $driverOptions = null) |
|
290
|
|
|
{ |
|
291
|
3 |
|
$this->prepare(); |
|
292
|
|
|
|
|
293
|
3 |
|
if ($dataType === null) { |
|
294
|
3 |
|
$dataType = $this->db->getSchema()->getPdoType($value); |
|
295
|
|
|
} |
|
296
|
3 |
|
if ($length === null) { |
|
297
|
3 |
|
$this->pdoStatement->bindParam($name, $value, $dataType); |
|
298
|
|
|
} elseif ($driverOptions === null) { |
|
299
|
|
|
$this->pdoStatement->bindParam($name, $value, $dataType, $length); |
|
300
|
|
|
} else { |
|
301
|
|
|
$this->pdoStatement->bindParam($name, $value, $dataType, $length, $driverOptions); |
|
302
|
|
|
} |
|
303
|
3 |
|
$this->params[$name] = &$value; |
|
304
|
|
|
|
|
305
|
3 |
|
return $this; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Binds pending parameters that were registered via [[bindValue()]] and [[bindValues()]]. |
|
310
|
|
|
* Note that this method requires an active [[pdoStatement]]. |
|
311
|
|
|
*/ |
|
312
|
1269 |
|
protected function bindPendingParams() |
|
313
|
|
|
{ |
|
314
|
1269 |
|
foreach ($this->_pendingParams as $name => $value) { |
|
315
|
963 |
|
$this->pdoStatement->bindValue($name, $value[0], $value[1]); |
|
316
|
|
|
} |
|
317
|
1269 |
|
$this->_pendingParams = []; |
|
318
|
1269 |
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Binds a value to a parameter. |
|
322
|
|
|
* @param string|int $name Parameter identifier. For a prepared statement |
|
323
|
|
|
* using named placeholders, this will be a parameter name of |
|
324
|
|
|
* the form `:name`. For a prepared statement using question mark |
|
325
|
|
|
* placeholders, this will be the 1-indexed position of the parameter. |
|
326
|
|
|
* @param mixed $value The value to bind to the parameter |
|
327
|
|
|
* @param int $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
|
328
|
|
|
* @return $this the current command being executed |
|
329
|
|
|
* @see http://www.php.net/manual/en/function.PDOStatement-bindValue.php |
|
330
|
|
|
*/ |
|
331
|
6 |
|
public function bindValue($name, $value, $dataType = null) |
|
332
|
|
|
{ |
|
333
|
6 |
|
if ($dataType === null) { |
|
334
|
6 |
|
$dataType = $this->db->getSchema()->getPdoType($value); |
|
335
|
|
|
} |
|
336
|
6 |
|
$this->_pendingParams[$name] = [$value, $dataType]; |
|
337
|
6 |
|
$this->params[$name] = $value; |
|
338
|
|
|
|
|
339
|
6 |
|
return $this; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Binds a list of values to the corresponding parameters. |
|
344
|
|
|
* This is similar to [[bindValue()]] except that it binds multiple values at a time. |
|
345
|
|
|
* Note that the SQL data type of each value is determined by its PHP type. |
|
346
|
|
|
* @param array $values the values to be bound. This must be given in terms of an associative |
|
347
|
|
|
* array with array keys being the parameter names, and array values the corresponding parameter values, |
|
348
|
|
|
* e.g. `[':name' => 'John', ':age' => 25]`. By default, the PDO type of each value is determined |
|
349
|
|
|
* by its PHP type. You may explicitly specify the PDO type by using a [[yii\db\PdoValue]] class: `new PdoValue(value, type)`, |
|
350
|
|
|
* e.g. `[':name' => 'John', ':profile' => new PdoValue($profile, \PDO::PARAM_LOB)]`. |
|
351
|
|
|
* @return $this the current command being executed |
|
352
|
|
|
*/ |
|
353
|
1303 |
|
public function bindValues($values) |
|
354
|
|
|
{ |
|
355
|
1303 |
|
if (empty($values)) { |
|
356
|
1145 |
|
return $this; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
982 |
|
$schema = $this->db->getSchema(); |
|
360
|
982 |
|
foreach ($values as $name => $value) { |
|
361
|
982 |
|
if ($value instanceof PdoValue) { |
|
362
|
90 |
|
$this->_pendingParams[$name] = [$value->getValue(), $value->getType()]; |
|
363
|
90 |
|
$this->params[$name] = $value->getValue(); |
|
364
|
|
|
} else { |
|
365
|
982 |
|
$type = $schema->getPdoType($value); |
|
366
|
982 |
|
$this->_pendingParams[$name] = [$value, $type]; |
|
367
|
982 |
|
$this->params[$name] = $value; |
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
982 |
|
return $this; |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Executes the SQL statement and returns query result. |
|
376
|
|
|
* This method is for executing a SQL query that returns result set, such as `SELECT`. |
|
377
|
|
|
* @return DataReader the reader object for fetching the query result |
|
378
|
|
|
* @throws Exception execution failed |
|
379
|
|
|
*/ |
|
380
|
9 |
|
public function query() |
|
381
|
|
|
{ |
|
382
|
9 |
|
return $this->queryInternal(''); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* Executes the SQL statement and returns ALL rows at once. |
|
387
|
|
|
* @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
|
388
|
|
|
* for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
|
389
|
|
|
* @return array all rows of the query result. Each array element is an array representing a row of data. |
|
390
|
|
|
* An empty array is returned if the query results in nothing. |
|
391
|
|
|
* @throws Exception execution failed |
|
392
|
|
|
*/ |
|
393
|
1104 |
|
public function queryAll($fetchMode = null) |
|
394
|
|
|
{ |
|
395
|
1104 |
|
return $this->queryInternal('fetchAll', $fetchMode); |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* Executes the SQL statement and returns the first row of the result. |
|
400
|
|
|
* This method is best used when only the first row of result is needed for a query. |
|
401
|
|
|
* @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://php.net/manual/en/pdostatement.setfetchmode.php) |
|
402
|
|
|
* for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
|
403
|
|
|
* @return array|false the first row (in terms of an array) of the query result. False is returned if the query |
|
404
|
|
|
* results in nothing. |
|
405
|
|
|
* @throws Exception execution failed |
|
406
|
|
|
*/ |
|
407
|
457 |
|
public function queryOne($fetchMode = null) |
|
408
|
|
|
{ |
|
409
|
457 |
|
return $this->queryInternal('fetch', $fetchMode); |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** |
|
413
|
|
|
* Executes the SQL statement and returns the value of the first column in the first row of data. |
|
414
|
|
|
* This method is best used when only a single value is needed for a query. |
|
415
|
|
|
* @return string|null|false the value of the first column in the first row of the query result. |
|
416
|
|
|
* False is returned if there is no value. |
|
417
|
|
|
* @throws Exception execution failed |
|
418
|
|
|
*/ |
|
419
|
302 |
|
public function queryScalar() |
|
420
|
|
|
{ |
|
421
|
302 |
|
$result = $this->queryInternal('fetchColumn', 0); |
|
422
|
299 |
|
if (is_resource($result) && get_resource_type($result) === 'stream') { |
|
423
|
18 |
|
return stream_get_contents($result); |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
292 |
|
return $result; |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
/** |
|
430
|
|
|
* Executes the SQL statement and returns the first column of the result. |
|
431
|
|
|
* This method is best used when only the first column of result (i.e. the first element in each row) |
|
432
|
|
|
* is needed for a query. |
|
433
|
|
|
* @return array the first column of the query result. Empty array is returned if the query results in nothing. |
|
434
|
|
|
* @throws Exception execution failed |
|
435
|
|
|
*/ |
|
436
|
83 |
|
public function queryColumn() |
|
437
|
|
|
{ |
|
438
|
83 |
|
return $this->queryInternal('fetchAll', \PDO::FETCH_COLUMN); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* Creates an INSERT command. |
|
443
|
|
|
* |
|
444
|
|
|
* For example, |
|
445
|
|
|
* |
|
446
|
|
|
* ```php |
|
447
|
|
|
* $connection->createCommand()->insert('user', [ |
|
448
|
|
|
* 'name' => 'Sam', |
|
449
|
|
|
* 'age' => 30, |
|
450
|
|
|
* ])->execute(); |
|
451
|
|
|
* ``` |
|
452
|
|
|
* |
|
453
|
|
|
* The method will properly escape the column names, and bind the values to be inserted. |
|
454
|
|
|
* |
|
455
|
|
|
* Note that the created command is not executed until [[execute()]] is called. |
|
456
|
|
|
* |
|
457
|
|
|
* @param string $table the table that new rows will be inserted into. |
|
458
|
|
|
* @param array|\yii\db\Query $columns the column data (name => value) to be inserted into the table or instance |
|
459
|
|
|
* of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement. |
|
460
|
|
|
* Passing of [[yii\db\Query|Query]] is available since version 2.0.11. |
|
461
|
|
|
* @return $this the command object itself |
|
462
|
|
|
*/ |
|
463
|
446 |
|
public function insert($table, $columns) |
|
464
|
|
|
{ |
|
465
|
446 |
|
$params = []; |
|
466
|
446 |
|
$sql = $this->db->getQueryBuilder()->insert($table, $columns, $params); |
|
467
|
|
|
|
|
468
|
437 |
|
return $this->setSql($sql)->bindValues($params); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Creates a batch INSERT command. |
|
473
|
|
|
* |
|
474
|
|
|
* For example, |
|
475
|
|
|
* |
|
476
|
|
|
* ```php |
|
477
|
|
|
* $connection->createCommand()->batchInsert('user', ['name', 'age'], [ |
|
478
|
|
|
* ['Tom', 30], |
|
479
|
|
|
* ['Jane', 20], |
|
480
|
|
|
* ['Linda', 25], |
|
481
|
|
|
* ])->execute(); |
|
482
|
|
|
* ``` |
|
483
|
|
|
* |
|
484
|
|
|
* The method will properly escape the column names, and quote the values to be inserted. |
|
485
|
|
|
* |
|
486
|
|
|
* Note that the values in each row must match the corresponding column names. |
|
487
|
|
|
* |
|
488
|
|
|
* Also note that the created command is not executed until [[execute()]] is called. |
|
489
|
|
|
* |
|
490
|
|
|
* @param string $table the table that new rows will be inserted into. |
|
491
|
|
|
* @param array $columns the column names |
|
492
|
|
|
* @param array|\Generator $rows the rows to be batch inserted into the table |
|
493
|
|
|
* @return $this the command object itself |
|
494
|
|
|
*/ |
|
495
|
22 |
|
public function batchInsert($table, $columns, $rows) |
|
496
|
|
|
{ |
|
497
|
22 |
|
$table = $this->db->quoteSql($table); |
|
498
|
|
|
$columns = array_map(function ($column) { |
|
499
|
22 |
|
return $this->db->quoteSql($column); |
|
500
|
22 |
|
}, $columns); |
|
501
|
|
|
|
|
502
|
22 |
|
$sql = $this->db->getQueryBuilder()->batchInsert($table, $columns, $rows); |
|
503
|
|
|
|
|
504
|
22 |
|
$this->setRawSql($sql); |
|
505
|
|
|
|
|
506
|
22 |
|
return $this; |
|
507
|
|
|
} |
|
508
|
|
|
|
|
509
|
|
|
/** |
|
510
|
|
|
* Creates a command to insert rows into a database table if |
|
511
|
|
|
* they do not already exist (matching unique constraints), |
|
512
|
|
|
* or update them if they do. |
|
513
|
|
|
* |
|
514
|
|
|
* For example, |
|
515
|
|
|
* |
|
516
|
|
|
* ```php |
|
517
|
|
|
* $sql = $queryBuilder->upsert('pages', [ |
|
518
|
|
|
* 'name' => 'Front page', |
|
519
|
|
|
* 'url' => 'http://example.com/', // url is unique |
|
520
|
|
|
* 'visits' => 0, |
|
521
|
|
|
* ], [ |
|
522
|
|
|
* 'visits' => new \yii\db\Expression('visits + 1'), |
|
523
|
|
|
* ], $params); |
|
524
|
|
|
* ``` |
|
525
|
|
|
* |
|
526
|
|
|
* The method will properly escape the table and column names. |
|
527
|
|
|
* |
|
528
|
|
|
* @param string $table the table that new rows will be inserted into/updated in. |
|
529
|
|
|
* @param array|Query $insertColumns the column data (name => value) to be inserted into the table or instance |
|
530
|
|
|
* of [[Query]] to perform `INSERT INTO ... SELECT` SQL statement. |
|
531
|
|
|
* @param array|bool $updateColumns the column data (name => value) to be updated if they already exist. |
|
532
|
|
|
* If `true` is passed, the column data will be updated to match the insert column data. |
|
533
|
|
|
* If `false` is passed, no update will be performed if the column data already exists. |
|
534
|
|
|
* @param array $params the parameters to be bound to the command. |
|
535
|
|
|
* @return $this the command object itself. |
|
536
|
|
|
* @since 2.0.14 |
|
537
|
|
|
*/ |
|
538
|
33 |
|
public function upsert($table, $insertColumns, $updateColumns = true, $params = []) |
|
539
|
|
|
{ |
|
540
|
33 |
|
$sql = $this->db->getQueryBuilder()->upsert($table, $insertColumns, $updateColumns, $params); |
|
541
|
|
|
|
|
542
|
33 |
|
return $this->setSql($sql)->bindValues($params); |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
/** |
|
546
|
|
|
* Creates an UPDATE command. |
|
547
|
|
|
* |
|
548
|
|
|
* For example, |
|
549
|
|
|
* |
|
550
|
|
|
* ```php |
|
551
|
|
|
* $connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute(); |
|
552
|
|
|
* ``` |
|
553
|
|
|
* |
|
554
|
|
|
* or with using parameter binding for the condition: |
|
555
|
|
|
* |
|
556
|
|
|
* ```php |
|
557
|
|
|
* $minAge = 30; |
|
558
|
|
|
* $connection->createCommand()->update('user', ['status' => 1], 'age > :minAge', [':minAge' => $minAge])->execute(); |
|
559
|
|
|
* ``` |
|
560
|
|
|
* |
|
561
|
|
|
* The method will properly escape the column names and bind the values to be updated. |
|
562
|
|
|
* |
|
563
|
|
|
* Note that the created command is not executed until [[execute()]] is called. |
|
564
|
|
|
* |
|
565
|
|
|
* @param string $table the table to be updated. |
|
566
|
|
|
* @param array $columns the column data (name => value) to be updated. |
|
567
|
|
|
* @param string|array $condition the condition that will be put in the WHERE part. Please |
|
568
|
|
|
* refer to [[Query::where()]] on how to specify condition. |
|
569
|
|
|
* @param array $params the parameters to be bound to the command |
|
570
|
|
|
* @return $this the command object itself |
|
571
|
|
|
*/ |
|
572
|
109 |
|
public function update($table, $columns, $condition = '', $params = []) |
|
573
|
|
|
{ |
|
574
|
109 |
|
$sql = $this->db->getQueryBuilder()->update($table, $columns, $condition, $params); |
|
575
|
|
|
|
|
576
|
109 |
|
return $this->setSql($sql)->bindValues($params); |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
/** |
|
580
|
|
|
* Creates a DELETE command. |
|
581
|
|
|
* |
|
582
|
|
|
* For example, |
|
583
|
|
|
* |
|
584
|
|
|
* ```php |
|
585
|
|
|
* $connection->createCommand()->delete('user', 'status = 0')->execute(); |
|
586
|
|
|
* ``` |
|
587
|
|
|
* |
|
588
|
|
|
* or with using parameter binding for the condition: |
|
589
|
|
|
* |
|
590
|
|
|
* ```php |
|
591
|
|
|
* $status = 0; |
|
592
|
|
|
* $connection->createCommand()->delete('user', 'status = :status', [':status' => $status])->execute(); |
|
593
|
|
|
* ``` |
|
594
|
|
|
* |
|
595
|
|
|
* The method will properly escape the table and column names. |
|
596
|
|
|
* |
|
597
|
|
|
* Note that the created command is not executed until [[execute()]] is called. |
|
598
|
|
|
* |
|
599
|
|
|
* @param string $table the table where the data will be deleted from. |
|
600
|
|
|
* @param string|array $condition the condition that will be put in the WHERE part. Please |
|
601
|
|
|
* refer to [[Query::where()]] on how to specify condition. |
|
602
|
|
|
* @param array $params the parameters to be bound to the command |
|
603
|
|
|
* @return $this the command object itself |
|
604
|
|
|
*/ |
|
605
|
350 |
|
public function delete($table, $condition = '', $params = []) |
|
606
|
|
|
{ |
|
607
|
350 |
|
$sql = $this->db->getQueryBuilder()->delete($table, $condition, $params); |
|
608
|
|
|
|
|
609
|
350 |
|
return $this->setSql($sql)->bindValues($params); |
|
610
|
|
|
} |
|
611
|
|
|
|
|
612
|
|
|
/** |
|
613
|
|
|
* Creates a SQL command for creating a new DB table. |
|
614
|
|
|
* |
|
615
|
|
|
* The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
|
616
|
|
|
* where name stands for a column name which will be properly quoted by the method, and definition |
|
617
|
|
|
* stands for the column type which can contain an abstract DB type. |
|
618
|
|
|
* The method [[QueryBuilder::getColumnType()]] will be called |
|
619
|
|
|
* to convert the abstract column types to physical ones. For example, `string` will be converted |
|
620
|
|
|
* as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
|
621
|
|
|
* |
|
622
|
|
|
* If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
|
623
|
|
|
* inserted into the generated SQL. |
|
624
|
|
|
* |
|
625
|
|
|
* @param string $table the name of the table to be created. The name will be properly quoted by the method. |
|
626
|
|
|
* @param array $columns the columns (name => definition) in the new table. |
|
627
|
|
|
* @param string $options additional SQL fragment that will be appended to the generated SQL. |
|
628
|
|
|
* @return $this the command object itself |
|
629
|
|
|
*/ |
|
630
|
131 |
|
public function createTable($table, $columns, $options = null) |
|
631
|
|
|
{ |
|
632
|
131 |
|
$sql = $this->db->getQueryBuilder()->createTable($table, $columns, $options); |
|
633
|
|
|
|
|
634
|
131 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
635
|
|
|
} |
|
636
|
|
|
|
|
637
|
|
|
/** |
|
638
|
|
|
* Creates a SQL command for renaming a DB table. |
|
639
|
|
|
* @param string $table the table to be renamed. The name will be properly quoted by the method. |
|
640
|
|
|
* @param string $newName the new table name. The name will be properly quoted by the method. |
|
641
|
|
|
* @return $this the command object itself |
|
642
|
|
|
*/ |
|
643
|
3 |
|
public function renameTable($table, $newName) |
|
644
|
|
|
{ |
|
645
|
3 |
|
$sql = $this->db->getQueryBuilder()->renameTable($table, $newName); |
|
646
|
|
|
|
|
647
|
3 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
648
|
|
|
} |
|
649
|
|
|
|
|
650
|
|
|
/** |
|
651
|
|
|
* Creates a SQL command for dropping a DB table. |
|
652
|
|
|
* @param string $table the table to be dropped. The name will be properly quoted by the method. |
|
653
|
|
|
* @return $this the command object itself |
|
654
|
|
|
*/ |
|
655
|
39 |
|
public function dropTable($table) |
|
656
|
|
|
{ |
|
657
|
39 |
|
$sql = $this->db->getQueryBuilder()->dropTable($table); |
|
658
|
|
|
|
|
659
|
39 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
660
|
|
|
} |
|
661
|
|
|
|
|
662
|
|
|
/** |
|
663
|
|
|
* Creates a SQL command for truncating a DB table. |
|
664
|
|
|
* @param string $table the table to be truncated. The name will be properly quoted by the method. |
|
665
|
|
|
* @return $this the command object itself |
|
666
|
|
|
*/ |
|
667
|
13 |
|
public function truncateTable($table) |
|
668
|
|
|
{ |
|
669
|
13 |
|
$sql = $this->db->getQueryBuilder()->truncateTable($table); |
|
670
|
|
|
|
|
671
|
13 |
|
return $this->setSql($sql); |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
/** |
|
675
|
|
|
* Creates a SQL command for adding a new DB column. |
|
676
|
|
|
* @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
|
677
|
|
|
* @param string $column the name of the new column. The name will be properly quoted by the method. |
|
678
|
|
|
* @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called |
|
679
|
|
|
* to convert the give column type to the physical one. For example, `string` will be converted |
|
680
|
|
|
* as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
|
681
|
|
|
* @return $this the command object itself |
|
682
|
|
|
*/ |
|
683
|
4 |
|
public function addColumn($table, $column, $type) |
|
684
|
|
|
{ |
|
685
|
4 |
|
$sql = $this->db->getQueryBuilder()->addColumn($table, $column, $type); |
|
686
|
|
|
|
|
687
|
4 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
688
|
|
|
} |
|
689
|
|
|
|
|
690
|
|
|
/** |
|
691
|
|
|
* Creates a SQL command for dropping a DB column. |
|
692
|
|
|
* @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
|
693
|
|
|
* @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
|
694
|
|
|
* @return $this the command object itself |
|
695
|
|
|
*/ |
|
696
|
|
|
public function dropColumn($table, $column) |
|
697
|
|
|
{ |
|
698
|
|
|
$sql = $this->db->getQueryBuilder()->dropColumn($table, $column); |
|
699
|
|
|
|
|
700
|
|
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
701
|
|
|
} |
|
702
|
|
|
|
|
703
|
|
|
/** |
|
704
|
|
|
* Creates a SQL command for renaming a column. |
|
705
|
|
|
* @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
|
706
|
|
|
* @param string $oldName the old name of the column. The name will be properly quoted by the method. |
|
707
|
|
|
* @param string $newName the new name of the column. The name will be properly quoted by the method. |
|
708
|
|
|
* @return $this the command object itself |
|
709
|
|
|
*/ |
|
710
|
|
|
public function renameColumn($table, $oldName, $newName) |
|
711
|
|
|
{ |
|
712
|
|
|
$sql = $this->db->getQueryBuilder()->renameColumn($table, $oldName, $newName); |
|
713
|
|
|
|
|
714
|
|
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
715
|
|
|
} |
|
716
|
|
|
|
|
717
|
|
|
/** |
|
718
|
|
|
* Creates a SQL command for changing the definition of a column. |
|
719
|
|
|
* @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
|
720
|
|
|
* @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
|
721
|
|
|
* @param string $type the column type. [[\yii\db\QueryBuilder::getColumnType()]] will be called |
|
722
|
|
|
* to convert the give column type to the physical one. For example, `string` will be converted |
|
723
|
|
|
* as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. |
|
724
|
|
|
* @return $this the command object itself |
|
725
|
|
|
*/ |
|
726
|
2 |
|
public function alterColumn($table, $column, $type) |
|
727
|
|
|
{ |
|
728
|
2 |
|
$sql = $this->db->getQueryBuilder()->alterColumn($table, $column, $type); |
|
729
|
|
|
|
|
730
|
2 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
731
|
|
|
} |
|
732
|
|
|
|
|
733
|
|
|
/** |
|
734
|
|
|
* Creates a SQL command for adding a primary key constraint to an existing table. |
|
735
|
|
|
* The method will properly quote the table and column names. |
|
736
|
|
|
* @param string $name the name of the primary key constraint. |
|
737
|
|
|
* @param string $table the table that the primary key constraint will be added to. |
|
738
|
|
|
* @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
|
739
|
|
|
* @return $this the command object itself. |
|
740
|
|
|
*/ |
|
741
|
2 |
|
public function addPrimaryKey($name, $table, $columns) |
|
742
|
|
|
{ |
|
743
|
2 |
|
$sql = $this->db->getQueryBuilder()->addPrimaryKey($name, $table, $columns); |
|
744
|
|
|
|
|
745
|
2 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
746
|
|
|
} |
|
747
|
|
|
|
|
748
|
|
|
/** |
|
749
|
|
|
* Creates a SQL command for removing a primary key constraint to an existing table. |
|
750
|
|
|
* @param string $name the name of the primary key constraint to be removed. |
|
751
|
|
|
* @param string $table the table that the primary key constraint will be removed from. |
|
752
|
|
|
* @return $this the command object itself |
|
753
|
|
|
*/ |
|
754
|
2 |
|
public function dropPrimaryKey($name, $table) |
|
755
|
|
|
{ |
|
756
|
2 |
|
$sql = $this->db->getQueryBuilder()->dropPrimaryKey($name, $table); |
|
757
|
|
|
|
|
758
|
2 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
759
|
|
|
} |
|
760
|
|
|
|
|
761
|
|
|
/** |
|
762
|
|
|
* Creates a SQL command for adding a foreign key constraint to an existing table. |
|
763
|
|
|
* The method will properly quote the table and column names. |
|
764
|
|
|
* @param string $name the name of the foreign key constraint. |
|
765
|
|
|
* @param string $table the table that the foreign key constraint will be added to. |
|
766
|
|
|
* @param string|array $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas. |
|
767
|
|
|
* @param string $refTable the table that the foreign key references to. |
|
768
|
|
|
* @param string|array $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas. |
|
769
|
|
|
* @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
|
770
|
|
|
* @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
|
771
|
|
|
* @return $this the command object itself |
|
772
|
|
|
*/ |
|
773
|
4 |
|
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
|
774
|
|
|
{ |
|
775
|
4 |
|
$sql = $this->db->getQueryBuilder()->addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update); |
|
776
|
|
|
|
|
777
|
4 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
778
|
|
|
} |
|
779
|
|
|
|
|
780
|
|
|
/** |
|
781
|
|
|
* Creates a SQL command for dropping a foreign key constraint. |
|
782
|
|
|
* @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
|
783
|
|
|
* @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
|
784
|
|
|
* @return $this the command object itself |
|
785
|
|
|
*/ |
|
786
|
4 |
|
public function dropForeignKey($name, $table) |
|
787
|
|
|
{ |
|
788
|
4 |
|
$sql = $this->db->getQueryBuilder()->dropForeignKey($name, $table); |
|
789
|
|
|
|
|
790
|
4 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
791
|
|
|
} |
|
792
|
|
|
|
|
793
|
|
|
/** |
|
794
|
|
|
* Creates a SQL command for creating a new index. |
|
795
|
|
|
* @param string $name the name of the index. The name will be properly quoted by the method. |
|
796
|
|
|
* @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
|
797
|
|
|
* @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, please separate them |
|
798
|
|
|
* by commas. The column names will be properly quoted by the method. |
|
799
|
|
|
* @param bool $unique whether to add UNIQUE constraint on the created index. |
|
800
|
|
|
* @return $this the command object itself |
|
801
|
|
|
*/ |
|
802
|
12 |
|
public function createIndex($name, $table, $columns, $unique = false) |
|
803
|
|
|
{ |
|
804
|
12 |
|
$sql = $this->db->getQueryBuilder()->createIndex($name, $table, $columns, $unique); |
|
805
|
|
|
|
|
806
|
12 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
807
|
|
|
} |
|
808
|
|
|
|
|
809
|
|
|
/** |
|
810
|
|
|
* Creates a SQL command for dropping an index. |
|
811
|
|
|
* @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
|
812
|
|
|
* @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
|
813
|
|
|
* @return $this the command object itself |
|
814
|
|
|
*/ |
|
815
|
3 |
|
public function dropIndex($name, $table) |
|
816
|
|
|
{ |
|
817
|
3 |
|
$sql = $this->db->getQueryBuilder()->dropIndex($name, $table); |
|
818
|
|
|
|
|
819
|
3 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
820
|
|
|
} |
|
821
|
|
|
|
|
822
|
|
|
/** |
|
823
|
|
|
* Creates a SQL command for adding an unique constraint to an existing table. |
|
824
|
|
|
* @param string $name the name of the unique constraint. |
|
825
|
|
|
* The name will be properly quoted by the method. |
|
826
|
|
|
* @param string $table the table that the unique constraint will be added to. |
|
827
|
|
|
* The name will be properly quoted by the method. |
|
828
|
|
|
* @param string|array $columns the name of the column to that the constraint will be added on. |
|
829
|
|
|
* If there are multiple columns, separate them with commas. |
|
830
|
|
|
* The name will be properly quoted by the method. |
|
831
|
|
|
* @return $this the command object itself. |
|
832
|
|
|
* @since 2.0.13 |
|
833
|
|
|
*/ |
|
834
|
2 |
|
public function addUnique($name, $table, $columns) |
|
835
|
|
|
{ |
|
836
|
2 |
|
$sql = $this->db->getQueryBuilder()->addUnique($name, $table, $columns); |
|
837
|
|
|
|
|
838
|
2 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
839
|
|
|
} |
|
840
|
|
|
|
|
841
|
|
|
/** |
|
842
|
|
|
* Creates a SQL command for dropping an unique constraint. |
|
843
|
|
|
* @param string $name the name of the unique constraint to be dropped. |
|
844
|
|
|
* The name will be properly quoted by the method. |
|
845
|
|
|
* @param string $table the table whose unique constraint is to be dropped. |
|
846
|
|
|
* The name will be properly quoted by the method. |
|
847
|
|
|
* @return $this the command object itself. |
|
848
|
|
|
* @since 2.0.13 |
|
849
|
|
|
*/ |
|
850
|
2 |
|
public function dropUnique($name, $table) |
|
851
|
|
|
{ |
|
852
|
2 |
|
$sql = $this->db->getQueryBuilder()->dropUnique($name, $table); |
|
853
|
|
|
|
|
854
|
2 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
855
|
|
|
} |
|
856
|
|
|
|
|
857
|
|
|
/** |
|
858
|
|
|
* Creates a SQL command for adding a check constraint to an existing table. |
|
859
|
|
|
* @param string $name the name of the check constraint. |
|
860
|
|
|
* The name will be properly quoted by the method. |
|
861
|
|
|
* @param string $table the table that the check constraint will be added to. |
|
862
|
|
|
* The name will be properly quoted by the method. |
|
863
|
|
|
* @param string $expression the SQL of the `CHECK` constraint. |
|
864
|
|
|
* @return $this the command object itself. |
|
865
|
|
|
* @since 2.0.13 |
|
866
|
|
|
*/ |
|
867
|
1 |
|
public function addCheck($name, $table, $expression) |
|
868
|
|
|
{ |
|
869
|
1 |
|
$sql = $this->db->getQueryBuilder()->addCheck($name, $table, $expression); |
|
870
|
|
|
|
|
871
|
1 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
872
|
|
|
} |
|
873
|
|
|
|
|
874
|
|
|
/** |
|
875
|
|
|
* Creates a SQL command for dropping a check constraint. |
|
876
|
|
|
* @param string $name the name of the check constraint to be dropped. |
|
877
|
|
|
* The name will be properly quoted by the method. |
|
878
|
|
|
* @param string $table the table whose check constraint is to be dropped. |
|
879
|
|
|
* The name will be properly quoted by the method. |
|
880
|
|
|
* @return $this the command object itself. |
|
881
|
|
|
* @since 2.0.13 |
|
882
|
|
|
*/ |
|
883
|
1 |
|
public function dropCheck($name, $table) |
|
884
|
|
|
{ |
|
885
|
1 |
|
$sql = $this->db->getQueryBuilder()->dropCheck($name, $table); |
|
886
|
|
|
|
|
887
|
1 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
888
|
|
|
} |
|
889
|
|
|
|
|
890
|
|
|
/** |
|
891
|
|
|
* Creates a SQL command for adding a default value constraint to an existing table. |
|
892
|
|
|
* @param string $name the name of the default value constraint. |
|
893
|
|
|
* The name will be properly quoted by the method. |
|
894
|
|
|
* @param string $table the table that the default value constraint will be added to. |
|
895
|
|
|
* The name will be properly quoted by the method. |
|
896
|
|
|
* @param string $column the name of the column to that the constraint will be added on. |
|
897
|
|
|
* The name will be properly quoted by the method. |
|
898
|
|
|
* @param mixed $value default value. |
|
899
|
|
|
* @return $this the command object itself. |
|
900
|
|
|
* @since 2.0.13 |
|
901
|
|
|
*/ |
|
902
|
|
|
public function addDefaultValue($name, $table, $column, $value) |
|
903
|
|
|
{ |
|
904
|
|
|
$sql = $this->db->getQueryBuilder()->addDefaultValue($name, $table, $column, $value); |
|
905
|
|
|
|
|
906
|
|
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
907
|
|
|
} |
|
908
|
|
|
|
|
909
|
|
|
/** |
|
910
|
|
|
* Creates a SQL command for dropping a default value constraint. |
|
911
|
|
|
* @param string $name the name of the default value constraint to be dropped. |
|
912
|
|
|
* The name will be properly quoted by the method. |
|
913
|
|
|
* @param string $table the table whose default value constraint is to be dropped. |
|
914
|
|
|
* The name will be properly quoted by the method. |
|
915
|
|
|
* @return $this the command object itself. |
|
916
|
|
|
* @since 2.0.13 |
|
917
|
|
|
*/ |
|
918
|
|
|
public function dropDefaultValue($name, $table) |
|
919
|
|
|
{ |
|
920
|
|
|
$sql = $this->db->getQueryBuilder()->dropDefaultValue($name, $table); |
|
921
|
|
|
|
|
922
|
|
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
923
|
|
|
} |
|
924
|
|
|
|
|
925
|
|
|
/** |
|
926
|
|
|
* Creates a SQL command for resetting the sequence value of a table's primary key. |
|
927
|
|
|
* The sequence will be reset such that the primary key of the next new row inserted |
|
928
|
|
|
* will have the specified value or 1. |
|
929
|
|
|
* @param string $table the name of the table whose primary key sequence will be reset |
|
930
|
|
|
* @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
|
931
|
|
|
* the next new row's primary key will have a value 1. |
|
932
|
|
|
* @return $this the command object itself |
|
933
|
|
|
* @throws NotSupportedException if this is not supported by the underlying DBMS |
|
934
|
|
|
*/ |
|
935
|
16 |
|
public function resetSequence($table, $value = null) |
|
936
|
|
|
{ |
|
937
|
16 |
|
$sql = $this->db->getQueryBuilder()->resetSequence($table, $value); |
|
938
|
|
|
|
|
939
|
16 |
|
return $this->setSql($sql); |
|
940
|
|
|
} |
|
941
|
|
|
|
|
942
|
|
|
/** |
|
943
|
|
|
* Builds a SQL command for enabling or disabling integrity check. |
|
944
|
|
|
* @param bool $check whether to turn on or off the integrity check. |
|
945
|
|
|
* @param string $schema the schema name of the tables. Defaults to empty string, meaning the current |
|
946
|
|
|
* or default schema. |
|
947
|
|
|
* @param string $table the table name. |
|
948
|
|
|
* @return $this the command object itself |
|
949
|
|
|
* @throws NotSupportedException if this is not supported by the underlying DBMS |
|
950
|
|
|
*/ |
|
951
|
4 |
|
public function checkIntegrity($check = true, $schema = '', $table = '') |
|
952
|
|
|
{ |
|
953
|
4 |
|
$sql = $this->db->getQueryBuilder()->checkIntegrity($check, $schema, $table); |
|
954
|
|
|
|
|
955
|
4 |
|
return $this->setSql($sql); |
|
956
|
|
|
} |
|
957
|
|
|
|
|
958
|
|
|
/** |
|
959
|
|
|
* Builds a SQL command for adding comment to column. |
|
960
|
|
|
* |
|
961
|
|
|
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
|
962
|
|
|
* @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
|
963
|
|
|
* @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
|
964
|
|
|
* @return $this the command object itself |
|
965
|
|
|
* @since 2.0.8 |
|
966
|
|
|
*/ |
|
967
|
2 |
|
public function addCommentOnColumn($table, $column, $comment) |
|
968
|
|
|
{ |
|
969
|
2 |
|
$sql = $this->db->getQueryBuilder()->addCommentOnColumn($table, $column, $comment); |
|
970
|
|
|
|
|
971
|
2 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
972
|
|
|
} |
|
973
|
|
|
|
|
974
|
|
|
/** |
|
975
|
|
|
* Builds a SQL command for adding comment to table. |
|
976
|
|
|
* |
|
977
|
|
|
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
|
978
|
|
|
* @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
|
979
|
|
|
* @return $this the command object itself |
|
980
|
|
|
* @since 2.0.8 |
|
981
|
|
|
*/ |
|
982
|
|
|
public function addCommentOnTable($table, $comment) |
|
983
|
|
|
{ |
|
984
|
|
|
$sql = $this->db->getQueryBuilder()->addCommentOnTable($table, $comment); |
|
985
|
|
|
|
|
986
|
|
|
return $this->setSql($sql); |
|
987
|
|
|
} |
|
988
|
|
|
|
|
989
|
|
|
/** |
|
990
|
|
|
* Builds a SQL command for dropping comment from column. |
|
991
|
|
|
* |
|
992
|
|
|
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
|
993
|
|
|
* @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
|
994
|
|
|
* @return $this the command object itself |
|
995
|
|
|
* @since 2.0.8 |
|
996
|
|
|
*/ |
|
997
|
2 |
|
public function dropCommentFromColumn($table, $column) |
|
998
|
|
|
{ |
|
999
|
2 |
|
$sql = $this->db->getQueryBuilder()->dropCommentFromColumn($table, $column); |
|
1000
|
|
|
|
|
1001
|
2 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($table); |
|
1002
|
|
|
} |
|
1003
|
|
|
|
|
1004
|
|
|
/** |
|
1005
|
|
|
* Builds a SQL command for dropping comment from table. |
|
1006
|
|
|
* |
|
1007
|
|
|
* @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
|
1008
|
|
|
* @return $this the command object itself |
|
1009
|
|
|
* @since 2.0.8 |
|
1010
|
|
|
*/ |
|
1011
|
|
|
public function dropCommentFromTable($table) |
|
1012
|
|
|
{ |
|
1013
|
|
|
$sql = $this->db->getQueryBuilder()->dropCommentFromTable($table); |
|
1014
|
|
|
|
|
1015
|
|
|
return $this->setSql($sql); |
|
1016
|
|
|
} |
|
1017
|
|
|
|
|
1018
|
|
|
/** |
|
1019
|
|
|
* Creates a SQL View. |
|
1020
|
|
|
* |
|
1021
|
|
|
* @param string $viewName the name of the view to be created. |
|
1022
|
|
|
* @param string|Query $subquery the select statement which defines the view. |
|
1023
|
|
|
* This can be either a string or a [[Query]] object. |
|
1024
|
|
|
* @return $this the command object itself. |
|
1025
|
|
|
* @since 2.0.14 |
|
1026
|
|
|
*/ |
|
1027
|
3 |
|
public function createView($viewName, $subquery) |
|
1028
|
|
|
{ |
|
1029
|
3 |
|
$sql = $this->db->getQueryBuilder()->createView($viewName, $subquery); |
|
1030
|
|
|
|
|
1031
|
3 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($viewName); |
|
1032
|
|
|
} |
|
1033
|
|
|
|
|
1034
|
|
|
/** |
|
1035
|
|
|
* Drops a SQL View. |
|
1036
|
|
|
* |
|
1037
|
|
|
* @param string $viewName the name of the view to be dropped. |
|
1038
|
|
|
* @return $this the command object itself. |
|
1039
|
|
|
* @since 2.0.14 |
|
1040
|
|
|
*/ |
|
1041
|
3 |
|
public function dropView($viewName) |
|
1042
|
|
|
{ |
|
1043
|
3 |
|
$sql = $this->db->getQueryBuilder()->dropView($viewName); |
|
1044
|
|
|
|
|
1045
|
3 |
|
return $this->setSql($sql)->requireTableSchemaRefresh($viewName); |
|
1046
|
|
|
} |
|
1047
|
|
|
|
|
1048
|
|
|
/** |
|
1049
|
|
|
* Executes the SQL statement. |
|
1050
|
|
|
* This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs. |
|
1051
|
|
|
* No result set will be returned. |
|
1052
|
|
|
* @return int number of rows affected by the execution. |
|
1053
|
|
|
* @throws Exception execution failed |
|
1054
|
|
|
*/ |
|
1055
|
651 |
|
public function execute() |
|
1056
|
|
|
{ |
|
1057
|
651 |
|
$sql = $this->getSql(); |
|
1058
|
651 |
|
list($profile, $rawSql) = $this->logQuery(__METHOD__); |
|
1059
|
|
|
|
|
1060
|
651 |
|
if ($sql == '') { |
|
1061
|
6 |
|
return 0; |
|
1062
|
|
|
} |
|
1063
|
|
|
|
|
1064
|
648 |
|
$this->prepare(false); |
|
1065
|
|
|
|
|
1066
|
|
|
try { |
|
1067
|
648 |
|
$profile and Yii::beginProfile($rawSql, __METHOD__); |
|
1068
|
|
|
|
|
1069
|
648 |
|
$this->internalExecute($rawSql); |
|
1070
|
645 |
|
$n = $this->pdoStatement->rowCount(); |
|
1071
|
|
|
|
|
1072
|
645 |
|
$profile and Yii::endProfile($rawSql, __METHOD__); |
|
1073
|
|
|
|
|
1074
|
645 |
|
$this->refreshTableSchema(); |
|
1075
|
|
|
|
|
1076
|
645 |
|
return $n; |
|
1077
|
18 |
|
} catch (Exception $e) { |
|
1078
|
18 |
|
$profile and Yii::endProfile($rawSql, __METHOD__); |
|
1079
|
18 |
|
throw $e; |
|
1080
|
|
|
} |
|
1081
|
|
|
} |
|
1082
|
|
|
|
|
1083
|
|
|
/** |
|
1084
|
|
|
* Logs the current database query if query logging is enabled and returns |
|
1085
|
|
|
* the profiling token if profiling is enabled. |
|
1086
|
|
|
* @param string $category the log category. |
|
1087
|
|
|
* @return array array of two elements, the first is boolean of whether profiling is enabled or not. |
|
1088
|
|
|
* The second is the rawSql if it has been created. |
|
1089
|
|
|
*/ |
|
1090
|
1267 |
|
private function logQuery($category) |
|
1091
|
|
|
{ |
|
1092
|
1267 |
|
if ($this->db->enableLogging) { |
|
1093
|
1267 |
|
$rawSql = $this->getRawSql(); |
|
1094
|
1267 |
|
Yii::info($rawSql, $category); |
|
1095
|
|
|
} |
|
1096
|
1267 |
|
if (!$this->db->enableProfiling) { |
|
1097
|
7 |
|
return [false, isset($rawSql) ? $rawSql : null]; |
|
1098
|
|
|
} |
|
1099
|
|
|
|
|
1100
|
1267 |
|
return [true, isset($rawSql) ? $rawSql : $this->getRawSql()]; |
|
1101
|
|
|
} |
|
1102
|
|
|
|
|
1103
|
|
|
/** |
|
1104
|
|
|
* Performs the actual DB query of a SQL statement. |
|
1105
|
|
|
* @param string $method method of PDOStatement to be called |
|
1106
|
|
|
* @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) |
|
1107
|
|
|
* for valid fetch modes. If this parameter is null, the value set in [[fetchMode]] will be used. |
|
1108
|
|
|
* @return mixed the method execution result |
|
1109
|
|
|
* @throws Exception if the query causes any problem |
|
1110
|
|
|
* @since 2.0.1 this method is protected (was private before). |
|
1111
|
|
|
*/ |
|
1112
|
1226 |
|
protected function queryInternal($method, $fetchMode = null) |
|
1113
|
|
|
{ |
|
1114
|
1226 |
|
list($profile, $rawSql) = $this->logQuery('yii\db\Command::query'); |
|
1115
|
|
|
|
|
1116
|
1226 |
|
if ($method !== '') { |
|
1117
|
1223 |
|
$info = $this->db->getQueryCacheInfo($this->queryCacheDuration, $this->queryCacheDependency); |
|
1118
|
1223 |
|
if (is_array($info)) { |
|
1119
|
|
|
/* @var $cache \yii\caching\CacheInterface */ |
|
1120
|
6 |
|
$cache = $info[0]; |
|
1121
|
|
|
$cacheKey = [ |
|
1122
|
6 |
|
__CLASS__, |
|
1123
|
6 |
|
$method, |
|
1124
|
6 |
|
$fetchMode, |
|
1125
|
6 |
|
$this->db->dsn, |
|
1126
|
6 |
|
$this->db->username, |
|
1127
|
6 |
|
$rawSql ?: $rawSql = $this->getRawSql(), |
|
1128
|
|
|
]; |
|
1129
|
6 |
|
$result = $cache->get($cacheKey); |
|
1130
|
6 |
|
if (is_array($result) && isset($result[0])) { |
|
1131
|
6 |
|
Yii::debug('Query result served from cache', 'yii\db\Command::query'); |
|
1132
|
6 |
|
return $result[0]; |
|
1133
|
|
|
} |
|
1134
|
|
|
} |
|
1135
|
|
|
} |
|
1136
|
|
|
|
|
1137
|
1226 |
|
$this->prepare(true); |
|
1138
|
|
|
|
|
1139
|
|
|
try { |
|
1140
|
1225 |
|
$profile and Yii::beginProfile($rawSql, 'yii\db\Command::query'); |
|
1141
|
|
|
|
|
1142
|
1225 |
|
$this->internalExecute($rawSql); |
|
1143
|
|
|
|
|
1144
|
1222 |
|
if ($method === '') { |
|
1145
|
9 |
|
$result = new DataReader($this); |
|
1146
|
|
|
} else { |
|
1147
|
1219 |
|
if ($fetchMode === null) { |
|
1148
|
1123 |
|
$fetchMode = $this->fetchMode; |
|
1149
|
|
|
} |
|
1150
|
1219 |
|
$result = call_user_func_array([$this->pdoStatement, $method], (array) $fetchMode); |
|
1151
|
1219 |
|
$this->pdoStatement->closeCursor(); |
|
1152
|
|
|
} |
|
1153
|
|
|
|
|
1154
|
1222 |
|
$profile and Yii::endProfile($rawSql, 'yii\db\Command::query'); |
|
1155
|
22 |
|
} catch (Exception $e) { |
|
1156
|
22 |
|
$profile and Yii::endProfile($rawSql, 'yii\db\Command::query'); |
|
1157
|
22 |
|
throw $e; |
|
1158
|
|
|
} |
|
1159
|
|
|
|
|
1160
|
1222 |
|
if (isset($cache, $cacheKey, $info)) { |
|
1161
|
6 |
|
$cache->set($cacheKey, [$result], $info[1], $info[2]); |
|
1162
|
6 |
|
Yii::debug('Saved query result in cache', 'yii\db\Command::query'); |
|
1163
|
|
|
} |
|
1164
|
|
|
|
|
1165
|
1222 |
|
return $result; |
|
1166
|
|
|
} |
|
1167
|
|
|
|
|
1168
|
|
|
/** |
|
1169
|
|
|
* Marks a specified table schema to be refreshed after command execution. |
|
1170
|
|
|
* @param string $name name of the table, which schema should be refreshed. |
|
1171
|
|
|
* @return $this this command instance |
|
1172
|
|
|
* @since 2.0.6 |
|
1173
|
|
|
*/ |
|
1174
|
140 |
|
protected function requireTableSchemaRefresh($name) |
|
1175
|
|
|
{ |
|
1176
|
140 |
|
$this->_refreshTableName = $name; |
|
1177
|
140 |
|
return $this; |
|
1178
|
|
|
} |
|
1179
|
|
|
|
|
1180
|
|
|
/** |
|
1181
|
|
|
* Refreshes table schema, which was marked by [[requireTableSchemaRefresh()]]. |
|
1182
|
|
|
* @since 2.0.6 |
|
1183
|
|
|
*/ |
|
1184
|
645 |
|
protected function refreshTableSchema() |
|
1185
|
|
|
{ |
|
1186
|
645 |
|
if ($this->_refreshTableName !== null) { |
|
1187
|
140 |
|
$this->db->getSchema()->refreshTableSchema($this->_refreshTableName); |
|
1188
|
|
|
} |
|
1189
|
645 |
|
} |
|
1190
|
|
|
|
|
1191
|
|
|
/** |
|
1192
|
|
|
* Marks the command to be executed in transaction. |
|
1193
|
|
|
* @param string|null $isolationLevel The isolation level to use for this transaction. |
|
1194
|
|
|
* See [[Transaction::begin()]] for details. |
|
1195
|
|
|
* @return $this this command instance. |
|
1196
|
|
|
* @since 2.0.14 |
|
1197
|
|
|
*/ |
|
1198
|
3 |
|
protected function requireTransaction($isolationLevel = null) |
|
1199
|
|
|
{ |
|
1200
|
3 |
|
$this->_isolationLevel = $isolationLevel; |
|
1201
|
3 |
|
return $this; |
|
1202
|
|
|
} |
|
1203
|
|
|
|
|
1204
|
|
|
/** |
|
1205
|
|
|
* Sets a callable (e.g. anonymous function) that is called when [[Exception]] is thrown |
|
1206
|
|
|
* when executing the command. The signature of the callable should be: |
|
1207
|
|
|
* |
|
1208
|
|
|
* ```php |
|
1209
|
|
|
* function (\yii\db\Exception $e, $attempt) |
|
1210
|
|
|
* { |
|
1211
|
|
|
* // return true or false (whether to retry the command or rethrow $e) |
|
1212
|
|
|
* } |
|
1213
|
|
|
* ``` |
|
1214
|
|
|
* |
|
1215
|
|
|
* The callable will recieve a database exception thrown and a current attempt |
|
1216
|
|
|
* (to execute the command) number starting from 1. |
|
1217
|
|
|
* |
|
1218
|
|
|
* @param callable $handler a PHP callback to handle database exceptions. |
|
1219
|
|
|
* @return $this this command instance. |
|
1220
|
|
|
* @since 2.0.14 |
|
1221
|
|
|
*/ |
|
1222
|
3 |
|
protected function setRetryHandler(callable $handler) |
|
1223
|
|
|
{ |
|
1224
|
3 |
|
$this->_retryHandler = $handler; |
|
1225
|
3 |
|
return $this; |
|
1226
|
|
|
} |
|
1227
|
|
|
|
|
1228
|
|
|
/** |
|
1229
|
|
|
* Executes a prepared statement. |
|
1230
|
|
|
* |
|
1231
|
|
|
* It's a wrapper around [[\PDOStatement::execute()]] to support transactions |
|
1232
|
|
|
* and retry handlers. |
|
1233
|
|
|
* |
|
1234
|
|
|
* @param string|null $rawSql the rawSql if it has been created. |
|
1235
|
|
|
* @throws Exception if execution failed. |
|
1236
|
|
|
* @since 2.0.14 |
|
1237
|
|
|
*/ |
|
1238
|
1266 |
|
protected function internalExecute($rawSql) |
|
1239
|
|
|
{ |
|
1240
|
1266 |
|
$attempt = 0; |
|
1241
|
1266 |
|
while (true) { |
|
1242
|
|
|
try { |
|
1243
|
|
|
if ( |
|
1244
|
1266 |
|
++$attempt === 1 |
|
1245
|
1266 |
|
&& $this->_isolationLevel !== false |
|
1246
|
1266 |
|
&& $this->db->getTransaction() === null |
|
1247
|
|
|
) { |
|
1248
|
3 |
|
$this->db->transaction(function () use ($rawSql) { |
|
1249
|
3 |
|
$this->internalExecute($rawSql); |
|
1250
|
3 |
|
}, $this->_isolationLevel); |
|
1251
|
|
|
} else { |
|
1252
|
1266 |
|
$this->pdoStatement->execute(); |
|
1253
|
|
|
} |
|
1254
|
1263 |
|
break; |
|
1255
|
37 |
|
} catch (\Exception $e) { |
|
1256
|
37 |
|
$rawSql = $rawSql ?: $this->getRawSql(); |
|
1257
|
37 |
|
$e = $this->db->getSchema()->convertException($e, $rawSql); |
|
1258
|
37 |
|
if ($this->_retryHandler === null || !call_user_func($this->_retryHandler, $e, $attempt)) { |
|
1259
|
37 |
|
throw $e; |
|
1260
|
|
|
} |
|
1261
|
|
|
} |
|
1262
|
|
|
} |
|
1263
|
1263 |
|
} |
|
1264
|
|
|
|
|
1265
|
|
|
/** |
|
1266
|
|
|
* Resets command properties to their initial state. |
|
1267
|
|
|
* |
|
1268
|
|
|
* @since 2.0.13 |
|
1269
|
|
|
*/ |
|
1270
|
1303 |
|
protected function reset() |
|
1271
|
|
|
{ |
|
1272
|
1303 |
|
$this->_sql = null; |
|
1273
|
1303 |
|
$this->_pendingParams = []; |
|
1274
|
1303 |
|
$this->params = []; |
|
1275
|
1303 |
|
$this->_refreshTableName = null; |
|
1276
|
1303 |
|
$this->_isolationLevel = false; |
|
1277
|
1303 |
|
$this->_retryHandler = null; |
|
1278
|
1303 |
|
} |
|
1279
|
|
|
} |
|
1280
|
|
|
|