21
21
import com .datastax .oss .driver .api .core .CqlSession ;
22
22
import com .datastax .oss .driver .api .core .PagingIterable ;
23
23
import com .datastax .oss .driver .api .core .cql .AsyncResultSet ;
24
+ import com .datastax .oss .driver .api .core .cql .BoundStatement ;
24
25
import com .datastax .oss .driver .api .core .cql .ResultSet ;
25
26
import com .datastax .oss .driver .api .core .cql .Row ;
26
27
import com .datastax .oss .driver .api .core .cql .SimpleStatement ;
@@ -174,6 +175,21 @@ public void should_delete_with_condition() {
174
175
assertThat (dao .findById (id )).isNull ();
175
176
}
176
177
178
+ @ Test
179
+ public void should_delete_with_condition_statement () {
180
+ UUID id = FLAMETHROWER .getId ();
181
+ assertThat (dao .findById (id )).isNotNull ();
182
+
183
+ BoundStatement bs = dao .deleteIfDescriptionMatchesStatement (id , "foo" );
184
+ ResultSet rs = SESSION_RULE .session ().execute (bs );
185
+ assertThat (rs .wasApplied ()).isFalse ();
186
+ assertThat (rs .one ().getString ("description" )).isEqualTo (FLAMETHROWER .getDescription ());
187
+
188
+ rs = dao .deleteIfDescriptionMatches (id , FLAMETHROWER .getDescription ());
189
+ assertThat (rs .wasApplied ()).isTrue ();
190
+ assertThat (dao .findById (id )).isNull ();
191
+ }
192
+
177
193
@ Test
178
194
public void should_delete_with_condition_asynchronously () {
179
195
UUID id = FLAMETHROWER .getId ();
@@ -198,6 +214,13 @@ public void should_delete_by_partition_key() {
198
214
assertThat (saleDao .all ().all ()).containsOnly (FLAMETHROWER_SALE_5 , MP3_DOWNLOAD_SALE_1 );
199
215
}
200
216
217
+ @ Test
218
+ public void should_delete_by_partition_key_statement () {
219
+ // should delete FLAMETHROWER_SALE_[1-4]
220
+ SESSION_RULE .session ().execute (saleDao .deleteByIdForDayStatement (FLAMETHROWER .getId (), DATE_1 ));
221
+ assertThat (saleDao .all ().all ()).containsOnly (FLAMETHROWER_SALE_5 , MP3_DOWNLOAD_SALE_1 );
222
+ }
223
+
201
224
@ Test
202
225
public void should_delete_by_partition_key_and_partial_clustering () {
203
226
// should delete FLAMETHROWER_SALE_{1,3,4]
@@ -206,6 +229,16 @@ public void should_delete_by_partition_key_and_partial_clustering() {
206
229
.containsOnly (FLAMETHROWER_SALE_2 , FLAMETHROWER_SALE_5 , MP3_DOWNLOAD_SALE_1 );
207
230
}
208
231
232
+ @ Test
233
+ public void should_delete_by_partition_key_and_partial_clustering_statement () {
234
+ // should delete FLAMETHROWER_SALE_{1,3,4]
235
+ SESSION_RULE
236
+ .session ()
237
+ .execute (saleDao .deleteByIdForCustomerStatement (FLAMETHROWER .getId (), DATE_1 , 1 ));
238
+ assertThat (saleDao .all ().all ())
239
+ .containsOnly (FLAMETHROWER_SALE_2 , FLAMETHROWER_SALE_5 , MP3_DOWNLOAD_SALE_1 );
240
+ }
241
+
209
242
@ Test
210
243
public void should_delete_by_primary_key_sales () {
211
244
// should delete FLAMETHROWER_SALE_2
@@ -220,6 +253,23 @@ public void should_delete_by_primary_key_sales() {
220
253
MP3_DOWNLOAD_SALE_1 );
221
254
}
222
255
256
+ @ Test
257
+ public void should_delete_by_primary_key_sales_statement () {
258
+ // should delete FLAMETHROWER_SALE_2
259
+ SESSION_RULE
260
+ .session ()
261
+ .execute (
262
+ saleDao .deleteByIdForCustomerAtTimeStatement (
263
+ FLAMETHROWER .getId (), DATE_1 , 2 , FLAMETHROWER_SALE_2 .getTs ()));
264
+ assertThat (saleDao .all ().all ())
265
+ .containsOnly (
266
+ FLAMETHROWER_SALE_1 ,
267
+ FLAMETHROWER_SALE_3 ,
268
+ FLAMETHROWER_SALE_4 ,
269
+ FLAMETHROWER_SALE_5 ,
270
+ MP3_DOWNLOAD_SALE_1 );
271
+ }
272
+
223
273
@ Test
224
274
public void should_delete_if_price_matches () {
225
275
ResultSet result =
@@ -238,6 +288,26 @@ public void should_delete_if_price_matches() {
238
288
assertThat (result .wasApplied ()).isTrue ();
239
289
}
240
290
291
+ @ Test
292
+ public void should_delete_if_price_matchesStatement () {
293
+ BoundStatement bs =
294
+ saleDao .deleteIfPriceMatchesStatement (
295
+ FLAMETHROWER .getId (), DATE_1 , 2 , FLAMETHROWER_SALE_2 .getTs (), 250.0 );
296
+ ResultSet result = SESSION_RULE .session ().execute (bs );
297
+
298
+ assertThat (result .wasApplied ()).isFalse ();
299
+ Row row = result .one ();
300
+ assertThat (row ).isNotNull ();
301
+ assertThat (row .getDouble ("price" )).isEqualTo (500.0 );
302
+
303
+ bs =
304
+ saleDao .deleteIfPriceMatchesStatement (
305
+ FLAMETHROWER .getId (), DATE_1 , 2 , FLAMETHROWER_SALE_2 .getTs (), 500.0 );
306
+ result = SESSION_RULE .session ().execute (bs );
307
+
308
+ assertThat (result .wasApplied ()).isTrue ();
309
+ }
310
+
241
311
@ Test
242
312
public void should_delete_if_exists_sales () {
243
313
assertThat (saleDao .deleteIfExists (FLAMETHROWER .getId (), DATE_1 , 2 , FLAMETHROWER_SALE_2 .getTs ()))
@@ -262,6 +332,24 @@ public void should_delete_within_time_range() {
262
332
FLAMETHROWER_SALE_2 , FLAMETHROWER_SALE_4 , FLAMETHROWER_SALE_5 , MP3_DOWNLOAD_SALE_1 );
263
333
}
264
334
335
+ @ Test
336
+ public void should_delete_within_time_range_statement () {
337
+ // should delete FLAMETHROWER_SALE_{1,3}, but not 4 because range ends before
338
+ SESSION_RULE
339
+ .session ()
340
+ .execute (
341
+ saleDao .deleteInTimeRangeStatement (
342
+ FLAMETHROWER .getId (),
343
+ DATE_1 ,
344
+ 1 ,
345
+ FLAMETHROWER_SALE_1 .getTs (),
346
+ Uuids .startOf (Uuids .unixTimestamp (FLAMETHROWER_SALE_4 .getTs ()) - 1000 )));
347
+
348
+ assertThat (saleDao .all ().all ())
349
+ .containsOnly (
350
+ FLAMETHROWER_SALE_2 , FLAMETHROWER_SALE_4 , FLAMETHROWER_SALE_5 , MP3_DOWNLOAD_SALE_1 );
351
+ }
352
+
265
353
@ Test
266
354
public void should_delete_if_price_matches_custom_where () {
267
355
ResultSet result =
@@ -280,6 +368,26 @@ public void should_delete_if_price_matches_custom_where() {
280
368
assertThat (result .wasApplied ()).isTrue ();
281
369
}
282
370
371
+ @ Test
372
+ public void should_delete_if_price_matches_custom_where_statement () {
373
+ BoundStatement bs =
374
+ saleDao .deleteCustomWhereCustomIfStatement (
375
+ 2 , FLAMETHROWER .getId (), DATE_1 , FLAMETHROWER_SALE_2 .getTs (), 250.0 );
376
+ ResultSet result = SESSION_RULE .session ().execute (bs );
377
+
378
+ assertThat (result .wasApplied ()).isFalse ();
379
+ Row row = result .one ();
380
+ assertThat (row ).isNotNull ();
381
+ assertThat (row .getDouble ("price" )).isEqualTo (500.0 );
382
+
383
+ bs =
384
+ saleDao .deleteCustomWhereCustomIfStatement (
385
+ 2 , FLAMETHROWER .getId (), DATE_1 , FLAMETHROWER_SALE_2 .getTs (), 500.0 );
386
+ result = SESSION_RULE .session ().execute (bs );
387
+
388
+ assertThat (result .wasApplied ()).isTrue ();
389
+ }
390
+
283
391
@ Mapper
284
392
public interface InventoryMapper {
285
393
@ DaoFactory
@@ -305,6 +413,9 @@ public interface ProductDao {
305
413
@ Delete (entityClass = Product .class , customIfClause = "description = :expectedDescription" )
306
414
ResultSet deleteIfDescriptionMatches (UUID productId , String expectedDescription );
307
415
416
+ @ Delete (entityClass = Product .class , customIfClause = "description = :expectedDescription" )
417
+ BoundStatement deleteIfDescriptionMatchesStatement (UUID productId , String expectedDescription );
418
+
308
419
@ Delete
309
420
CompletionStage <Void > deleteAsync (Product product );
310
421
@@ -335,25 +446,50 @@ public interface ProductSaleDao {
335
446
@ Delete (entityClass = ProductSale .class )
336
447
ResultSet deleteByIdForDay (UUID id , String day );
337
448
449
+ // delete all rows in partition
450
+ @ Delete (entityClass = ProductSale .class )
451
+ BoundStatement deleteByIdForDayStatement (UUID id , String day );
452
+
338
453
// delete by partition key and partial clustering key
339
454
@ Delete (entityClass = ProductSale .class )
340
455
ResultSet deleteByIdForCustomer (UUID id , String day , int customerId );
341
456
457
+ // delete by partition key and partial clustering key
458
+ @ Delete (entityClass = ProductSale .class )
459
+ BoundStatement deleteByIdForCustomerStatement (UUID id , String day , int customerId );
460
+
342
461
// delete row (full primary key)
343
462
@ Delete (entityClass = ProductSale .class )
344
463
ResultSet deleteByIdForCustomerAtTime (UUID id , String day , int customerId , UUID ts );
345
464
465
+ // delete row (full primary key)
466
+ @ Delete (entityClass = ProductSale .class )
467
+ BoundStatement deleteByIdForCustomerAtTimeStatement (
468
+ UUID id , String day , int customerId , UUID ts );
469
+
346
470
@ Delete (entityClass = ProductSale .class , customIfClause = "price = :expectedPrice" )
347
471
ResultSet deleteIfPriceMatches (
348
472
UUID id , String day , int customerId , UUID ts , double expectedPrice );
349
473
474
+ @ Delete (entityClass = ProductSale .class , customIfClause = "price = :expectedPrice" )
475
+ BoundStatement deleteIfPriceMatchesStatement (
476
+ UUID id , String day , int customerId , UUID ts , double expectedPrice );
477
+
350
478
@ Delete (
351
479
entityClass = ProductSale .class ,
352
480
customWhereClause =
353
481
"id = :id and day = :day and customer_id = :customerId and ts >= :startTs and ts < "
354
482
+ ":endTs" )
355
483
ResultSet deleteInTimeRange (UUID id , String day , int customerId , UUID startTs , UUID endTs );
356
484
485
+ @ Delete (
486
+ entityClass = ProductSale .class ,
487
+ customWhereClause =
488
+ "id = :id and day = :day and customer_id = :customerId and ts >= :startTs and ts < "
489
+ + ":endTs" )
490
+ BoundStatement deleteInTimeRangeStatement (
491
+ UUID id , String day , int customerId , UUID startTs , UUID endTs );
492
+
357
493
// transpose order of parameters so doesn't match primary key to ensure that works.
358
494
@ Delete (
359
495
entityClass = ProductSale .class ,
@@ -362,6 +498,14 @@ ResultSet deleteIfPriceMatches(
362
498
ResultSet deleteCustomWhereCustomIf (
363
499
int customerId , UUID id , String day , UUID ts , double expectedPrice );
364
500
501
+ // transpose order of parameters so doesn't match primary key to ensure that works.
502
+ @ Delete (
503
+ entityClass = ProductSale .class ,
504
+ customWhereClause = "id = :id and day = :day and customer_id = :customerId and ts = :ts" ,
505
+ customIfClause = "price = :expectedPrice" )
506
+ BoundStatement deleteCustomWhereCustomIfStatement (
507
+ int customerId , UUID id , String day , UUID ts , double expectedPrice );
508
+
365
509
@ Delete (entityClass = ProductSale .class , ifExists = true )
366
510
boolean deleteIfExists (UUID id , String day , int customerId , UUID ts );
367
511
0 commit comments