Search code examples
scalacassandraphantom-dsl

Scala Phantom Cassandra Conditional insert?


I am using Phantom library to insert rows from my Scala code into Cassandra.

So basically I create a class which extends the CassandraTable and then I get all the create, update methods from the phantom library.

One thing I want to know is how does it generate its INSERT statements. Does it generate a vanilla INSERT statement of a conditional update statement like INSERT IF NOT EXISTS.


Solution

  • Just log the query and the response would be obvious. By default the insert query does NOT contain ifNotExists. As of Phantom 2.5.0, tables auto-generate a store method so you can call:

    database.table.store(record).ifNotExists.future()
    

    Before phantom 2.5.0:

    def store(record: Record): InsertQuery.Default[Table, Record] = {
      insert.value(_.column1, record.value1)
        .value(_.column2, record.value2)
        ...
        .ifNotExists()
    }
    

    On a side note, all queries in phantom have a .qb.queryString accessor on them so it's pretty trivial to look inside.