RyanJ
RyanJ

Reputation: 131

Cassandra prepared statements with collections

I've started learning Cassandra in PHP using the DataStax PHP Driver and I have to set a value of a map in CQL using the prepared statements, the query is:

INSERT INTO users (name, attributes, mail, password) VALUES (?, ?, ?, ?);

The "attributes" filed is a MAP type so I tried to pass it as a string:

{'address':'address...','phone':'phone...'}

in PHP:

$attr = "{'address':'address...','phone':'phone...'}";
$statement = $session->prepare("INSERT INTO users (name, attributes, mail, password) VALUES (?, ?, ?, ?);");
$session->execute($statement, new Cassandra\ExecutionOptions(array('arguments' => array($name, $attr, $mail, $password))));

But I get the error "java.lang.IllegalArgumentException", which is the correct syntax for collections like maps or lists in prepared statements?

Upvotes: 1

Views: 1129

Answers (2)

jimstaffer
jimstaffer

Reputation: 23

In case it saves someone else some time...

My 'targets' column is a set, not a map - but hopefully, the same principle applies to any collection. I found that the code below works to delete a particular element of the 'targets' set in a batch statement:

$batch = new Cassandra\BatchStatement(Cassandra::BATCH_LOGGED);
$deleteTargetStmt = "DELETE targets[?] FROM $this->keyspace.user WHERE userid=?";
$targetPrepStmt = $this->session->prepare($deleteTargetStmt);
$batch->add($targetPrepStmt, array(new Cassandra\Timeuuid($targetId), new Cassandra\Uuid($userId)));
... other batch statement ommitted ...
$this->session->execute($batch);

In fact, this is effectively the same syntax as for a non-prepared statement. But it took me ages to find out that I had to call new Timeuuid and Uuid on add's arguments!

Upvotes: 1

Mahendra Singh
Mahendra Singh

Reputation: 38

First, create a map or list in php. Then provide the object of that map or list in a batchstatment. You can do one more; without creating a batchstatment. Write queries directly, and you can pass as you already passed .

Upvotes: 1

Related Questions