Delete
Deletes are executed with a query and optional columns, both of which are represented via case classes.
For deletes that will delete the entire row:
scala> case class Query(s: String)
defined class Query
scala> val deleteStatement = sSession.deleteRow("mytable", Query("some str"))
deleteStatement: com.weather.scalacass.scsession.SCDeleteStatement = SCDeleteStatement(DELETE FROM mykeyspace.mytable WHERE s=some str)
scala> deleteStatement.execute()
res2: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
For deletes that only delete certain columns of that row, specify the columns as a case class. However, you will not actually use an instance of the case class in the statement, just pass it in as type parameter:
scala> case class ColumnsToRemove(i: Int)
defined class ColumnsToRemove
scala> val deleteColumnsStatement = sSession.delete[ColumnsToRemove]("mytable", Query("some str"))
deleteColumnsStatement: com.weather.scalacass.scsession.SCDeleteStatement = SCDeleteStatement(DELETE i FROM mykeyspace.mytable WHERE s=some str)
scala> deleteColumnsStatement.execute()
res3: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
If Statment
You can use case classes to model If statements. For now, only equivalency is possible. This means that the values
in the if statement are translated to an =
comparison:
scala> case class If(l: Long)
defined class If
scala> val deleteWithIf = deleteStatement.`if`(If(5678L))
deleteWithIf: com.weather.scalacass.scsession.SCDeleteStatement = SCDeleteStatement(DELETE FROM mykeyspace.mytable WHERE s=some str IF l=5678)
scala> deleteWithIf.execute()
res4: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: false, Columns[[applied](boolean)]])
You can just specify IF EXISTS
as well:
scala> val deleteWithIfExists = deleteStatement.ifExists
deleteWithIfExists: com.weather.scalacass.scsession.SCDeleteStatement = SCDeleteStatement(DELETE FROM mykeyspace.mytable WHERE s=some str IF EXISTS)
scala> deleteWithIfExists.execute()
res5: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: false, Columns[[applied](boolean)]])
You can remove any if clause:
scala> val deleteWithoutIf = deleteWithIf.noConditional
deleteWithoutIf: com.weather.scalacass.scsession.SCDeleteStatement = SCDeleteStatement(DELETE FROM mykeyspace.mytable WHERE s=some str)
scala> deleteWithoutIf.execute()
res6: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
Timestamp
You can specify a timestamp:
scala> val timestampDelete = deleteStatement.usingTimestamp(System.currentTimeMillis)
timestampDelete: com.weather.scalacass.scsession.SCDeleteStatement = SCDeleteStatement(DELETE FROM mykeyspace.mytable USING TIMESTAMP 1532318847804 WHERE s=some str)
scala> timestampDelete.execute()
res7: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
or use shorthand for current time:
scala> val timestampNowDelete = deleteStatement.usingTimestampNow
timestampNowDelete: com.weather.scalacass.scsession.SCDeleteStatement = SCDeleteStatement(DELETE FROM mykeyspace.mytable USING TIMESTAMP 1532318847970 WHERE s=some str)
scala> timestampNowDelete.execute()
res8: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
and finally, remove a timestamp from the statement:
scala> val noTimestampDelete = timestampDelete.noTimestamp
noTimestampDelete: com.weather.scalacass.scsession.SCDeleteStatement = SCDeleteStatement(DELETE FROM mykeyspace.mytable WHERE s=some str)
scala> noTimestampDelete.execute()
res9: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])