I am trying to bind a Scala List after the "IN" keyword in a CQL WHERE clause.
I tried this
val session: com.datastax.driver.core.Session = ...
val deleteStatement = session.prepare(s"DELETE FROM table_name WHERE id IN ?;")
def deleteById(ids: List[String]): Try[Boolean] = {
val boundStatement = new BoundStatement(deleteStatement)
boundStatement.bind(ids)
session.execute(boundStatement).wasApplied()
}
But "boundStatement.bind" throws
Codec not found for requested operation: [list <-> scala.collection.immutable.$colon$colon]
How do I bind this list?
CodePudding user response:
Since the datastax library is written in Java, the BoundStatement is expecting a Java collection. You can explicitly convert your Scala collection to a Java collection like this:
boundStatement.bind(new java.util.ArrayList[String](ids.asJava))
Note that an implicit conversion does not work here, since the bind() method accepts parameters of type java.lang.Object rather than something more concrete.
CodePudding user response:
You should also be able to do:
boundStatement.bind().setList(0, ids.asJava)
Or:
boundStatement.bind().setList(0, ids.asJava, classOf[String])