Update
Use case classes to model both the query and new value for updates:
scala> case class Query(s: String)
defined class Query
scala> case class NewValue(i: Int, l: Long)
defined class NewValue
scala> val updateStatement = sSession.update("mytable", NewValue(1234, 5678L), Query("some str"))
updateStatement: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str)
scala> updateStatement.execute()
res2: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
Add/Subtract
There is a special class available to specify that you would like to either add or subtract elements
to a Casssandra collection. Namely, UpdateBehavior.Add
and UpdateBehavior.Subtract
.
scala> import com.weather.scalacass.syntax._
import com.weather.scalacass.syntax._
scala> case class NewValueList(li: UpdateBehavior[List, String])
defined class NewValueList
scala> val updateStatementAdd = sSession.update("mytable", NewValueList(UpdateBehavior.Add(List("a", "b", "c"))), Query("some str"))
updateStatementAdd: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable SET li=li+[a, b, c] WHERE s=some str)
scala> updateStatementAdd.execute()
res3: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
scala> sSession.selectOneStar("mytable", Query("some str")).execute()
res4: com.weather.scalacass.Result[Option[com.datastax.driver.core.Row]] = Right(Some(Row[some str, 1234, 5678, [a, b, c]]))
scala> val updateStatementSubtract = sSession.update("mytable", NewValueList(UpdateBehavior.Subtract(List("a", "b"))), Query("some str"))
updateStatementSubtract: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable SET li=li-[a, b] WHERE s=some str)
scala> updateStatementSubtract.execute()
res5: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
scala> sSession.selectOneStar("mytable", Query("some str")).execute()
res6: com.weather.scalacass.Result[Option[com.datastax.driver.core.Row]] = Right(Some(Row[some str, 1234, 5678, [c]]))
For parity, there is also UpdateBehavior.Replace
, but simply using a class directly will act in the same way.
Using UpdateBehavior.Replace
:
scala> val updateStatementReplace = sSession.update("mytable", NewValueList(UpdateBehavior.Replace(List("d", "e", "f"))), Query("some str"))
updateStatementReplace: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable SET li=[d, e, f] WHERE s=some str)
scala> updateStatementReplace.execute()
res7: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
scala> sSession.selectOneStar("mytable", Query("some str")).execute()
res8: com.weather.scalacass.Result[Option[com.datastax.driver.core.Row]] = Right(Some(Row[some str, 1234, 5678, [d, e, f]]))
Using regular List
:
scala> case class NewValueListRegular(li: List[String])
defined class NewValueListRegular
scala> val updateStatementRegular = sSession.update("mytable", NewValueListRegular(List("g", "h", "i")), Query("some str"))
updateStatementRegular: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable SET li=[g, h, i] WHERE s=some str)
scala> updateStatementRegular.execute()
res9: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
scala> sSession.selectOneStar("mytable", Query("some str")).execute()
res10: com.weather.scalacass.Result[Option[com.datastax.driver.core.Row]] = Right(Some(Row[some str, 1234, 5678, [g, h, i]]))
If Statment
You can use case classes to model If statements. For now, only equivalency is possible, meaning values
in the if statement are translated to an =
comparison. If you need a different comparison operation,
see raw statements:
scala> case class If(l: Long)
defined class If
scala> val updateWithIf = updateStatement.`if`(If(5678L))
updateWithIf: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str IF l=5678)
scala> updateWithIf.execute()
res11: 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 updateWithIfExists = updateStatement.ifExists
updateWithIfExists: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str IF EXISTS)
scala> updateWithIfExists.execute()
res12: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: false, Columns[[applied](boolean)]])
You can remove any if clause:
scala> val updateWithoutIf = updateWithIf.noConditional
updateWithoutIf: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str)
scala> updateWithoutIf.execute()
res13: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
TTL
you can add a TTL:
scala> val ttlUpdate = updateStatement.usingTTL(12345)
ttlUpdate: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable USING TTL 12345 SET i=1234, l=5678 WHERE s=some str)
scala> ttlUpdate.execute()
res14: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
and remove the TTL:
scala> val noTtlUpdate = ttlUpdate.noTTL
noTtlUpdate: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str)
scala> noTtlUpdate.execute()
res15: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
Timestamp
You can specify a timestamp:
scala> val timestampUpdate = updateStatement.usingTimestamp(System.currentTimeMillis)
timestampUpdate: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable USING TIMESTAMP 1532318896842 SET i=1234, l=5678 WHERE s=some str)
scala> timestampUpdate.execute()
res16: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
or use shorthand for current time:
scala> val timestampNowUpdate = updateStatement.usingTimestampNow
timestampNowUpdate: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable USING TIMESTAMP 1532318897009 SET i=1234, l=5678 WHERE s=some str)
scala> timestampNowUpdate.execute()
res17: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
and finally, remove a timestamp from the statement:
scala> val noTimestampUpdate = timestampUpdate.noTimestamp
noTimestampUpdate: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str)
scala> noTimestampUpdate.execute()
res18: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])