@@ -408,4 +408,85 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
408
408
409
409
return str_replace ('{:_table_:} ' , $ data , $ sql );
410
410
}
411
+
412
+ /**
413
+ * Generates a platform-specific batch update string from the supplied data
414
+ */
415
+ protected function _deleteBatch (string $ table , array $ keys , array $ values ): string
416
+ {
417
+ $ sql = $ this ->QBOptions ['sql ' ] ?? '' ;
418
+
419
+ // if this is the first iteration of batch then we need to build skeleton sql
420
+ if ($ sql === '' ) {
421
+ $ constraints = $ this ->QBOptions ['constraints ' ] ?? [];
422
+
423
+ if ($ constraints === []) {
424
+ if ($ this ->db ->DBDebug ) {
425
+ throw new DatabaseException ('You must specify a constraint to match on for batch deletes. ' ); // @codeCoverageIgnore
426
+ }
427
+
428
+ return '' ; // @codeCoverageIgnore
429
+ }
430
+
431
+ $ updateFields = $ this ->QBOptions ['updateFields ' ] ??
432
+ $ this ->updateFields ($ keys , false , $ constraints )->QBOptions ['updateFields ' ] ??
433
+ [];
434
+
435
+ $ alias = $ this ->QBOptions ['alias ' ] ?? '_u ' ;
436
+
437
+ $ sql = 'DELETE FROM ' . $ table . "\n" ;
438
+
439
+ $ sql .= "USING ( \n{:_table_:} " ;
440
+
441
+ $ sql .= ') ' . $ alias . "\n" ;
442
+
443
+ $ sql .= 'WHERE ' . implode (
444
+ ' AND ' ,
445
+ array_map (
446
+ static fn ($ key ) => ($ key instanceof RawSql ?
447
+ $ key :
448
+ $ table . '. ' . $ key . ' = ' . $ alias . '. ' . $ key ),
449
+ $ constraints
450
+ )
451
+ );
452
+
453
+ // convert binds in where
454
+ foreach ($ this ->QBWhere as $ key => $ where ) {
455
+ foreach ($ this ->binds as $ field => $ bind ) {
456
+ $ this ->QBWhere [$ key ]['condition ' ] = str_replace (': ' . $ field . ': ' , $ bind [0 ], $ where ['condition ' ]);
457
+ }
458
+ }
459
+
460
+ // remove database prefix from alias in where
461
+ $ sql .= ' ' . str_replace (
462
+ 'WHERE ' ,
463
+ ' AND ' ,
464
+ str_replace (
465
+ $ this ->db ->DBPrefix . trim ($ alias , $ this ->db ->escapeChar ),
466
+ trim ($ alias , $ this ->db ->escapeChar ),
467
+ $ this ->compileWhereHaving ('QBWhere ' )
468
+ )
469
+ );
470
+
471
+ $ this ->QBOptions ['sql ' ] = $ sql ;
472
+ }
473
+
474
+ if (isset ($ this ->QBOptions ['fromQuery ' ])) {
475
+ $ data = $ this ->QBOptions ['fromQuery ' ];
476
+ } else {
477
+ $ data = implode (
478
+ " UNION ALL \n" ,
479
+ array_map (
480
+ static fn ($ value ) => 'SELECT ' . implode (', ' , array_map (
481
+ static fn ($ key , $ index ) => $ index . ' ' . $ key ,
482
+ $ keys ,
483
+ $ value
484
+ )),
485
+ $ values
486
+ )
487
+ ) . "\n" ;
488
+ }
489
+
490
+ return str_replace ('{:_table_:} ' , $ data , $ sql );
491
+ }
411
492
}
0 commit comments