1010use Throwable ;
1111use Yiisoft \Db \Exception \Exception ;
1212use Yiisoft \Db \Expression \Expression ;
13- use Yiisoft \Db \Profiler \ProfilerAwareTrait ;
1413use Yiisoft \Db \Profiler \Context \CommandContext ;
14+ use Yiisoft \Db \Profiler \ProfilerAwareTrait ;
1515use Yiisoft \Db \Query \Data \DataReaderInterface ;
1616use Yiisoft \Db \Query \QueryInterface ;
17+ use Yiisoft \Db \QueryBuilder \QueryBuilderInterface ;
1718
1819use function current ;
1920use function explode ;
7172 */
7273abstract class AbstractCommand implements CommandInterface
7374{
75+ use LoggerAwareTrait;
76+ use ProfilerAwareTrait;
77+
7478 /**
7579 * Command in this query mode returns count of affected rows.
7680 *
@@ -103,9 +107,6 @@ abstract class AbstractCommand implements CommandInterface
103107 */
104108 protected const QUERY_MODE_CURSOR = 16 ;
105109
106- use LoggerAwareTrait;
107- use ProfilerAwareTrait;
108-
109110 /**
110111 * @var string|null Transaction isolation level.
111112 */
@@ -128,31 +129,31 @@ abstract class AbstractCommand implements CommandInterface
128129
129130 public function addCheck (string $ table , string $ name , string $ expression ): static
130131 {
131- $ sql = $ this ->queryBuilder ()->addCheck ($ table , $ name , $ expression );
132+ $ sql = $ this ->getQueryBuilder ()->addCheck ($ table , $ name , $ expression );
132133 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
133134 }
134135
135136 public function addColumn (string $ table , string $ column , string $ type ): static
136137 {
137- $ sql = $ this ->queryBuilder ()->addColumn ($ table , $ column , $ type );
138+ $ sql = $ this ->getQueryBuilder ()->addColumn ($ table , $ column , $ type );
138139 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
139140 }
140141
141142 public function addCommentOnColumn (string $ table , string $ column , string $ comment ): static
142143 {
143- $ sql = $ this ->queryBuilder ()->addCommentOnColumn ($ table , $ column , $ comment );
144+ $ sql = $ this ->getQueryBuilder ()->addCommentOnColumn ($ table , $ column , $ comment );
144145 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
145146 }
146147
147148 public function addCommentOnTable (string $ table , string $ comment ): static
148149 {
149- $ sql = $ this ->queryBuilder ()->addCommentOnTable ($ table , $ comment );
150+ $ sql = $ this ->getQueryBuilder ()->addCommentOnTable ($ table , $ comment );
150151 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
151152 }
152153
153154 public function addDefaultValue (string $ table , string $ name , string $ column , mixed $ value ): static
154155 {
155- $ sql = $ this ->queryBuilder ()->addDefaultValue ($ table , $ name , $ column , $ value );
156+ $ sql = $ this ->getQueryBuilder ()->addDefaultValue ($ table , $ name , $ column , $ value );
156157 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
157158 }
158159
@@ -165,7 +166,7 @@ public function addForeignKey(
165166 string $ delete = null ,
166167 string $ update = null
167168 ): static {
168- $ sql = $ this ->queryBuilder ()->addForeignKey (
169+ $ sql = $ this ->getQueryBuilder ()->addForeignKey (
169170 $ table ,
170171 $ name ,
171172 $ columns ,
@@ -179,35 +180,35 @@ public function addForeignKey(
179180
180181 public function addPrimaryKey (string $ table , string $ name , array |string $ columns ): static
181182 {
182- $ sql = $ this ->queryBuilder ()->addPrimaryKey ($ table , $ name , $ columns );
183+ $ sql = $ this ->getQueryBuilder ()->addPrimaryKey ($ table , $ name , $ columns );
183184 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
184185 }
185186
186187 public function addUnique (string $ table , string $ name , array |string $ columns ): static
187188 {
188- $ sql = $ this ->queryBuilder ()->addUnique ($ table , $ name , $ columns );
189+ $ sql = $ this ->getQueryBuilder ()->addUnique ($ table , $ name , $ columns );
189190 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
190191 }
191192
192193 public function alterColumn (string $ table , string $ column , string $ type ): static
193194 {
194- $ sql = $ this ->queryBuilder ()->alterColumn ($ table , $ column , $ type );
195+ $ sql = $ this ->getQueryBuilder ()->alterColumn ($ table , $ column , $ type );
195196 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
196197 }
197198
198199 public function batchInsert (string $ table , array $ columns , iterable $ rows ): static
199200 {
200- $ table = $ this ->queryBuilder ()->quoter ()->quoteSql ($ table );
201+ $ table = $ this ->getQueryBuilder ()->quoter ()->quoteSql ($ table );
201202
202203 /** @psalm-var string[] $columns */
203204 foreach ($ columns as &$ column ) {
204- $ column = $ this ->queryBuilder ()->quoter ()->quoteSql ($ column );
205+ $ column = $ this ->getQueryBuilder ()->quoter ()->quoteSql ($ column );
205206 }
206207
207208 unset($ column );
208209
209210 $ params = [];
210- $ sql = $ this ->queryBuilder ()->batchInsert ($ table , $ columns , $ rows , $ params );
211+ $ sql = $ this ->getQueryBuilder ()->batchInsert ($ table , $ columns , $ rows , $ params );
211212
212213 $ this ->setRawSql ($ sql );
213214 $ this ->bindValues ($ params );
@@ -221,7 +222,7 @@ abstract public function bindValues(array $values): static;
221222
222223 public function checkIntegrity (string $ schema , string $ table , bool $ check = true ): static
223224 {
224- $ sql = $ this ->queryBuilder ()->checkIntegrity ($ schema , $ table , $ check );
225+ $ sql = $ this ->getQueryBuilder ()->checkIntegrity ($ schema , $ table , $ check );
225226 return $ this ->setSql ($ sql );
226227 }
227228
@@ -232,91 +233,91 @@ public function createIndex(
232233 string $ indexType = null ,
233234 string $ indexMethod = null
234235 ): static {
235- $ sql = $ this ->queryBuilder ()->createIndex ($ table , $ name , $ columns , $ indexType , $ indexMethod );
236+ $ sql = $ this ->getQueryBuilder ()->createIndex ($ table , $ name , $ columns , $ indexType , $ indexMethod );
236237 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
237238 }
238239
239240 public function createTable (string $ table , array $ columns , string $ options = null ): static
240241 {
241- $ sql = $ this ->queryBuilder ()->createTable ($ table , $ columns , $ options );
242+ $ sql = $ this ->getQueryBuilder ()->createTable ($ table , $ columns , $ options );
242243 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
243244 }
244245
245246 public function createView (string $ viewName , QueryInterface |string $ subQuery ): static
246247 {
247- $ sql = $ this ->queryBuilder ()->createView ($ viewName , $ subQuery );
248+ $ sql = $ this ->getQueryBuilder ()->createView ($ viewName , $ subQuery );
248249 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ viewName );
249250 }
250251
251252 public function delete (string $ table , array |string $ condition = '' , array $ params = []): static
252253 {
253- $ sql = $ this ->queryBuilder ()->delete ($ table , $ condition , $ params );
254+ $ sql = $ this ->getQueryBuilder ()->delete ($ table , $ condition , $ params );
254255 return $ this ->setSql ($ sql )->bindValues ($ params );
255256 }
256257
257258 public function dropCheck (string $ table , string $ name ): static
258259 {
259- $ sql = $ this ->queryBuilder ()->dropCheck ($ table , $ name );
260+ $ sql = $ this ->getQueryBuilder ()->dropCheck ($ table , $ name );
260261 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
261262 }
262263
263264 public function dropColumn (string $ table , string $ column ): static
264265 {
265- $ sql = $ this ->queryBuilder ()->dropColumn ($ table , $ column );
266+ $ sql = $ this ->getQueryBuilder ()->dropColumn ($ table , $ column );
266267 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
267268 }
268269
269270 public function dropCommentFromColumn (string $ table , string $ column ): static
270271 {
271- $ sql = $ this ->queryBuilder ()->dropCommentFromColumn ($ table , $ column );
272+ $ sql = $ this ->getQueryBuilder ()->dropCommentFromColumn ($ table , $ column );
272273 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
273274 }
274275
275276 public function dropCommentFromTable (string $ table ): static
276277 {
277- $ sql = $ this ->queryBuilder ()->dropCommentFromTable ($ table );
278+ $ sql = $ this ->getQueryBuilder ()->dropCommentFromTable ($ table );
278279 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
279280 }
280281
281282 public function dropDefaultValue (string $ table , string $ name ): static
282283 {
283- $ sql = $ this ->queryBuilder ()->dropDefaultValue ($ table , $ name );
284+ $ sql = $ this ->getQueryBuilder ()->dropDefaultValue ($ table , $ name );
284285 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
285286 }
286287
287288 public function dropForeignKey (string $ table , string $ name ): static
288289 {
289- $ sql = $ this ->queryBuilder ()->dropForeignKey ($ table , $ name );
290+ $ sql = $ this ->getQueryBuilder ()->dropForeignKey ($ table , $ name );
290291 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
291292 }
292293
293294 public function dropIndex (string $ table , string $ name ): static
294295 {
295- $ sql = $ this ->queryBuilder ()->dropIndex ($ table , $ name );
296+ $ sql = $ this ->getQueryBuilder ()->dropIndex ($ table , $ name );
296297 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
297298 }
298299
299300 public function dropPrimaryKey (string $ table , string $ name ): static
300301 {
301- $ sql = $ this ->queryBuilder ()->dropPrimaryKey ($ table , $ name );
302+ $ sql = $ this ->getQueryBuilder ()->dropPrimaryKey ($ table , $ name );
302303 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
303304 }
304305
305306 public function dropTable (string $ table ): static
306307 {
307- $ sql = $ this ->queryBuilder ()->dropTable ($ table );
308+ $ sql = $ this ->getQueryBuilder ()->dropTable ($ table );
308309 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
309310 }
310311
311312 public function dropUnique (string $ table , string $ name ): static
312313 {
313- $ sql = $ this ->queryBuilder ()->dropUnique ($ table , $ name );
314+ $ sql = $ this ->getQueryBuilder ()->dropUnique ($ table , $ name );
314315 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
315316 }
316317
317318 public function dropView (string $ viewName ): static
318319 {
319- $ sql = $ this ->queryBuilder ()->dropView ($ viewName );
320+ $ sql = $ this ->getQueryBuilder ()->dropView ($ viewName );
320321 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ viewName );
321322 }
322323
@@ -357,7 +358,7 @@ public function getRawSql(): string
357358
358359 if (is_string ($ value )) {
359360 /** @psalm-var mixed */
360- $ params [$ name ] = $ this ->queryBuilder ()->quoter ()->quoteValue ($ value );
361+ $ params [$ name ] = $ this ->getQueryBuilder ()->quoter ()->quoteValue ($ value );
361362 } elseif (is_bool ($ value )) {
362363 /** @psalm-var string */
363364 $ params [$ name ] = $ value ? 'TRUE ' : 'FALSE ' ;
@@ -394,15 +395,15 @@ public function getSql(): string
394395 public function insert (string $ table , QueryInterface |array $ columns ): static
395396 {
396397 $ params = [];
397- $ sql = $ this ->queryBuilder ()->insert ($ table , $ columns , $ params );
398+ $ sql = $ this ->getQueryBuilder ()->insert ($ table , $ columns , $ params );
398399 return $ this ->setSql ($ sql )->bindValues ($ params );
399400 }
400401
401402 public function insertWithReturningPks (string $ table , array $ columns ): bool |array
402403 {
403404 $ params = [];
404405
405- $ sql = $ this ->queryBuilder ()->insertWithReturningPks ($ table , $ columns , $ params );
406+ $ sql = $ this ->getQueryBuilder ()->insertWithReturningPks ($ table , $ columns , $ params );
406407
407408 $ this ->setSql ($ sql )->bindValues ($ params );
408409
@@ -474,19 +475,19 @@ public function queryScalar(): bool|string|null|int|float
474475
475476 public function renameColumn (string $ table , string $ oldName , string $ newName ): static
476477 {
477- $ sql = $ this ->queryBuilder ()->renameColumn ($ table , $ oldName , $ newName );
478+ $ sql = $ this ->getQueryBuilder ()->renameColumn ($ table , $ oldName , $ newName );
478479 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
479480 }
480481
481482 public function renameTable (string $ table , string $ newName ): static
482483 {
483- $ sql = $ this ->queryBuilder ()->renameTable ($ table , $ newName );
484+ $ sql = $ this ->getQueryBuilder ()->renameTable ($ table , $ newName );
484485 return $ this ->setSql ($ sql )->requireTableSchemaRefresh ($ table );
485486 }
486487
487488 public function resetSequence (string $ table , int |string $ value = null ): static
488489 {
489- $ sql = $ this ->queryBuilder ()->resetSequence ($ table , $ value );
490+ $ sql = $ this ->getQueryBuilder ()->resetSequence ($ table , $ value );
490491 return $ this ->setSql ($ sql );
491492 }
492493
@@ -505,7 +506,7 @@ public function setSql(string $sql): static
505506 {
506507 $ this ->cancel ();
507508 $ this ->reset ();
508- $ this ->sql = $ this ->queryBuilder ()->quoter ()->quoteSql ($ sql );
509+ $ this ->sql = $ this ->getQueryBuilder ()->quoter ()->quoteSql ($ sql );
509510 return $ this ;
510511 }
511512
@@ -517,13 +518,13 @@ public function setRetryHandler(Closure|null $handler): static
517518
518519 public function truncateTable (string $ table ): static
519520 {
520- $ sql = $ this ->queryBuilder ()->truncateTable ($ table );
521+ $ sql = $ this ->getQueryBuilder ()->truncateTable ($ table );
521522 return $ this ->setSql ($ sql );
522523 }
523524
524525 public function update (string $ table , array $ columns , array |string $ condition = '' , array $ params = []): static
525526 {
526- $ sql = $ this ->queryBuilder ()->update ($ table , $ columns , $ condition , $ params );
527+ $ sql = $ this ->getQueryBuilder ()->update ($ table , $ columns , $ condition , $ params );
527528 return $ this ->setSql ($ sql )->bindValues ($ params );
528529 }
529530
@@ -533,10 +534,15 @@ public function upsert(
533534 bool |array $ updateColumns = true ,
534535 array $ params = []
535536 ): static {
536- $ sql = $ this ->queryBuilder ()->upsert ($ table , $ insertColumns , $ updateColumns , $ params );
537+ $ sql = $ this ->getQueryBuilder ()->upsert ($ table , $ insertColumns , $ updateColumns , $ params );
537538 return $ this ->setSql ($ sql )->bindValues ($ params );
538539 }
539540
541+ /**
542+ * @return QueryBuilderInterface The query builder instance.
543+ */
544+ abstract protected function getQueryBuilder (): QueryBuilderInterface ;
545+
540546 /**
541547 * Returns the query result.
542548 *
0 commit comments