@@ -204,4 +204,72 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
204
204
205
205
return str_replace ('{:_table_:} ' , $ data , $ sql );
206
206
}
207
+
208
+ /**
209
+ * Generates a platform-specific batch update string from the supplied data
210
+ */
211
+ protected function _deleteBatch (string $ table , array $ keys , array $ values ): string
212
+ {
213
+ $ sql = $ this ->QBOptions ['sql ' ] ?? '' ;
214
+
215
+ // if this is the first iteration of batch then we need to build skeleton sql
216
+ if ($ sql === '' ) {
217
+ $ constraints = $ this ->QBOptions ['constraints ' ] ?? [];
218
+
219
+ if ($ constraints === []) {
220
+ if ($ this ->db ->DBDebug ) {
221
+ throw new DatabaseException ('You must specify a constraint to match on for batch deletes. ' ); // @codeCoverageIgnore
222
+ }
223
+
224
+ return '' ; // @codeCoverageIgnore
225
+ }
226
+
227
+ $ alias = $ this ->QBOptions ['alias ' ] ?? '_u ' ;
228
+
229
+ $ sql = 'DELETE FROM ' . $ table . "\n" ;
230
+
231
+ if (current ($ constraints ) instanceof RawSql) {
232
+ if ($ this ->db ->DBDebug ) {
233
+ throw new DatabaseException ('You cannot use RawSql for constraint in SQLite. ' ); // @codeCoverageIgnore
234
+ }
235
+ }
236
+
237
+ if (is_string (current (array_keys ($ constraints )))) {
238
+ $ concat1 = implode (' || ' , array_keys ($ constraints ));
239
+ $ concat2 = implode (' || ' , array_values ($ constraints ));
240
+ } else {
241
+ $ concat1 = implode (' || ' , $ constraints );
242
+ $ concat2 = $ concat1 ;
243
+ }
244
+
245
+ $ sql .= "WHERE {$ concat1 } IN (SELECT {$ concat2 } FROM ( \n{:_table_:})) " ;
246
+
247
+ // where is not supported
248
+ if ($ this ->QBWhere !== []) {
249
+ if ($ this ->db ->DBDebug ) {
250
+ throw new DatabaseException ('You cannot use WHERE with SQLite. ' ); // @codeCoverageIgnore
251
+ }
252
+ }
253
+
254
+ $ this ->QBOptions ['sql ' ] = $ sql ;
255
+ }
256
+
257
+ if (isset ($ this ->QBOptions ['fromQuery ' ])) {
258
+ $ data = $ this ->QBOptions ['fromQuery ' ];
259
+ } else {
260
+ $ data = implode (
261
+ " UNION ALL \n" ,
262
+ array_map (
263
+ static fn ($ value ) => 'SELECT ' . implode (', ' , array_map (
264
+ static fn ($ key , $ index ) => $ index . ' ' . $ key ,
265
+ $ keys ,
266
+ $ value
267
+ )),
268
+ $ values
269
+ )
270
+ ) . "\n" ;
271
+ }
272
+
273
+ return str_replace ('{:_table_:} ' , $ data , $ sql );
274
+ }
207
275
}
0 commit comments