Delete Builder

The Delete builder DSL provides simple row delete and conditional methods onlyIfExists and onlyIf generating CQL statement for delete.

Delete DSL

Given our SimpleTable definition we will generate delete statement with the DSL.

Delete full row

Delete with row generate our basic CQL delete statement.

val d1 = {
  t1.delete
    .row
    .build
}
d1.cqlStatement
// res0: String = "DELETE FROM test_ks.test_table  WHERE intColumn = :intColumn "

Partition key is included by default by the builder when using row

Delete row with clustering key

To specify clustering key, simply use cluster('columnName)

val d1c = {
  t2.delete
    .row
    .cluster('longColumn)
    .build
}
d1c.cqlStatement
// res1: String = "DELETE FROM test_ks.test_table  WHERE intColumn = :intColumn AND longColumn = :longColumn "

Delete specific column

CQL also allows to only delete specific columns, for that column instead of row can be used.

First let’s create a new table with an optional column fields, those fields have to be declared as Option since this will effectively put null as their values.

case class SimpleTableOptional (
  intColumn: Int,
  longColumn: Long,
  stringColumn: Option[String],
  timeUuidColumn: Option[UUID @@ Type1]
)

val t3 = {
  ks.table[SimpleTableOptional]
    .partition('intColumn)
    .build("test_option_table")
}

Let’s use column('columnName) to generate delete statement with only those optional columns for the row.

val d2 = {
  t3.delete
    .column('stringColumn)
    .column('timeUuidColumn)
    .primary
    .build
}
d2.cqlStatement
// res2: String = "DELETE stringColumn,timeUuidColumn FROM test_ks.test_option_table  WHERE intColumn = :intColumn "

Delete with conditionals

Cassandra allow doing conditionals delete which translate to IF EXISTS and IF column = 'value' in CQL.

Delete if exists
val d1c = {
  t1.delete
    .row
    .onlyIfExists
    .build
}
d1c.cqlStatement
// res3: String = DELETE FROM test_ks.test_table  WHERE intColumn = :intColumn  IF EXISTS
Delete only if condition

Cassandra CQL supports conditional delete with value checking, the DSL provide onlyIf builder for that use case.

This example with check if stringColumn equals the string cats before deleting partition key 1.

OnlyIf requires 3 arguments;the column name, the value and a comparator provided by the Enumeration Comparison.

Let’s first import the Comparison object

import spinoco.fs2.cassandra.Comparison

Now we can build our onlyIf with Comparison.EQ since we want our column to equals the string "cats".

val d2c = {
  t1.delete
    .row
    .primary
    .onlyIf('stringColumn, "string_value", Comparison.EQ)
    .build
}
d2c.cqlStatement
// res4: String = DELETE FROM test_ks.test_table  WHERE intColumn = :intColumn IF stringColumn = :string_value
Providing values from a Tuple with cqlFor

The previous builder will not fill the statement with the actual "cats" value nor the partition key; in order to do so we need to use cqlFor.

val d3c = { 
  // Starting from previous statement
  d2c.fromHList
     .fromTuple[(String, Int)]
     .cqlFor(("cats", 1))
}

Finally, we should see what is expected as CQL statement.

d3c
// res5: String = DELETE FROM test_ks.test_table  WHERE intColumn = 1 IF stringColumn = 'cats'