@@ -434,4 +434,81 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
434
434
435
435
return str_replace ('{:_table_:} ' , $ data , $ sql );
436
436
}
437
+
438
+ /**
439
+ * Generates a platform-specific batch update string from the supplied data
440
+ */
441
+ protected function _deleteBatch (string $ table , array $ keys , array $ values ): string
442
+ {
443
+ $ sql = $ this ->QBOptions ['sql ' ] ?? '' ;
444
+
445
+ // if this is the first iteration of batch then we need to build skeleton sql
446
+ if ($ sql === '' ) {
447
+ $ constraints = $ this ->QBOptions ['constraints ' ] ?? [];
448
+
449
+ if ($ constraints === []) {
450
+ if ($ this ->db ->DBDebug ) {
451
+ throw new DatabaseException ('You must specify a constraint to match on for batch deletes. ' ); // @codeCoverageIgnore
452
+ }
453
+
454
+ return '' ; // @codeCoverageIgnore
455
+ }
456
+
457
+ $ alias = $ this ->QBOptions ['alias ' ] ?? '_u ' ;
458
+
459
+ $ sql = 'DELETE ' . $ table . "\n" ;
460
+
461
+ $ sql .= "WHERE EXISTS (SELECT * FROM ( \n{:_table_:} " ;
462
+
463
+ $ sql .= ') ' . $ alias . "\n" ;
464
+
465
+ $ sql .= 'WHERE ' . implode (
466
+ ' AND ' ,
467
+ array_map (
468
+ static fn ($ key ) => ($ key instanceof RawSql ?
469
+ $ key :
470
+ $ table . '. ' . $ key . ' = ' . $ alias . '. ' . $ key ),
471
+ $ constraints
472
+ )
473
+ );
474
+
475
+ // convert binds in where
476
+ foreach ($ this ->QBWhere as $ key => $ where ) {
477
+ foreach ($ this ->binds as $ field => $ bind ) {
478
+ $ this ->QBWhere [$ key ]['condition ' ] = str_replace (': ' . $ field . ': ' , $ bind [0 ], $ where ['condition ' ]);
479
+ }
480
+ }
481
+
482
+ // remove database prefix from alias in where
483
+ $ sql .= ' ' . str_replace (
484
+ 'WHERE ' ,
485
+ 'AND ' ,
486
+ str_replace (
487
+ $ this ->db ->DBPrefix . trim ($ alias , $ this ->db ->escapeChar ),
488
+ trim ($ alias , $ this ->db ->escapeChar ),
489
+ $ this ->compileWhereHaving ('QBWhere ' )
490
+ )
491
+ ) . ') ' ;
492
+
493
+ $ this ->QBOptions ['sql ' ] = $ sql ;
494
+ }
495
+
496
+ if (isset ($ this ->QBOptions ['fromQuery ' ])) {
497
+ $ data = $ this ->QBOptions ['fromQuery ' ];
498
+ } else {
499
+ $ data = implode (
500
+ " FROM DUAL UNION ALL \n" ,
501
+ array_map (
502
+ static fn ($ value ) => 'SELECT ' . implode (', ' , array_map (
503
+ static fn ($ key , $ index ) => $ index . ' ' . $ key ,
504
+ $ keys ,
505
+ $ value
506
+ )),
507
+ $ values
508
+ )
509
+ ) . " FROM DUAL \n" ;
510
+ }
511
+
512
+ return str_replace ('{:_table_:} ' , $ data , $ sql );
513
+ }
437
514
}
0 commit comments