Insert Builder

The Insert DSL provides methods such as all, ifNotExists, withTTL and withTimestamp which will generate the corresponding CQL statement. It also allows partial column insertion with column and columns. Using cqlFor values can be provided to the actual statement in order to insert actual data.

Insert DSL

Given our SimpleTable definition we can insert data with different properties.

All

All will generate standard CQL insert from Table row definition

val i1 = {
  t1.insert
    .all
    .build
}
i1.cqlStatement
// res0: String = "INSERT INTO test_ks.test_table (intColumn,longColumn,stringColumn,timeUuidColumn) VALUES (:intColumn,:longColumn,:stringColumn,:timeUuidColumn)   "

Insert if not exists

val i2 = {
  t1.insert
    .all
    .ifNotExists
    .build
}
i2.cqlStatement
// res1: String = "INSERT INTO test_ks.test_table (intColumn,longColumn,stringColumn,timeUuidColumn) VALUES (:intColumn,:longColumn,:stringColumn,:timeUuidColumn) IF NOT EXISTS  "

Insert with TTL

val i3 = {
  t1.insert
    .all
    .withTTL('ttl)
    .build
}
i3.cqlStatement
// res2: String = "INSERT INTO test_ks.test_table (intColumn,longColumn,stringColumn,timeUuidColumn) VALUES (:intColumn,:longColumn,:stringColumn,:timeUuidColumn)  USING TTL :ttl "

Insert with only a subset of columns

val i4 = {
  t1.insert
    .column('stringColumn)
    .build
}
i4.cqlStatement
// res3: String = "INSERT INTO test_ks.test_table (stringColumn,intColumn) VALUES (:stringColumn,:intColumn)   "

Take note that since Table t1 has intColumn as partition key and is mandatory it is included.

Insert with subset of columns from a Case class

import shapeless.LabelledGeneric

case class SimpleTableSubset(
  stringColumn: String,
  timeUuidColumn: UUID @@ Type1
)

val generic = LabelledGeneric[SimpleTableSubset]

val i5 = {
  t1.insert
    .columns[generic.Repr]  
    .build
}

Again here, we should see intColumn because it is the partition key.

i5.cqlStatement
// res4: String = "INSERT INTO test_ks.test_table (stringColumn,timeUuidColumn,intColumn) VALUES (:stringColumn,:timeUuidColumn,:intColumn)   "
Providing values from a Tuple with cqlFor

The previous builders will not fill the statement with actual values; in order to do so we need to use cqlFor

val i6 = { 
  t1.insert
    .column('stringColumn)
    .build
    .fromHList
    .fromTuple[(String, Int)]
    .cqlFor(("Hello", 1))
}
i6
// res5: String = "INSERT INTO test_ks.test_table (stringColumn,intColumn) VALUES ('Hello',1)   "
Providing a TTL value
import shapeless._
import shapeless.syntax.singleton._
import spinoco.fs2.cassandra.CType.TTL

import scala.concurrent.duration._

val generic = LabelledGeneric[SimpleTable]

val i3ttl = i3.mapIn[(FiniteDuration, SimpleTable)] { case (dur, row) => ('ttl ->> tag[TTL](dur)) +: generic.to(row) }
i3ttl
// res6: spinoco.fs2.cassandra.Insert[(scala.concurrent.duration.FiniteDuration, SimpleTable),Option[shapeless.::[Int with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("intColumn")],Int],shapeless.::[Long with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("longColumn")],Long],shapeless.::[String with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("stringColumn")],String],shapeless.::[java.util.UUID with shapeless.tag.Tagged[spinoco.fs2.cassandra.CType.Type1] with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("timeUuidColumn")],java.util.UUID with shapeless.tag.Tagged[spinoco.fs2.cassandra.CType.Type1]],shapeless.HNil]]]]]] = Insert[INSERT INTO test_ks.test_table (intColumn,longColumn,stringColumn,timeUuidColumn...