Unfortunately, I don't this this is supported by the CQL syntax. When I try:
"Invalid set literal for value: bind variables are not supported inside collection literals"
I think you need to dynamically build a query with the correct number of "?" in the set/map literal. For example, a two items update would need to have a set of two items: "UPDATE examples.test_set SET value = value + {?, ?} WHERE key = ?", then use multiple calls to "cass_statement_bind_*()".
#include <stdio.h>
#include <cassandra.h>
/* Schema:
* CREATE KEYSPACE examples WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 2 };
* CREATE TABLE test_set(key int PRIMARY KEY, value set<text>);
*
* Precondition:
* INSERT INTO test_set (key, value) VALUES (1, {'item1', 'item2'});
*/
int main() {
/* Setup and connect to cluster */
CassCluster* cluster = cass_cluster_new();
CassSession* session = cass_session_new();
/* Add contact points */
cass_cluster_set_contact_points(cluster, "127.0.0.1,127.0.0.2,127.0.0.3");
CassFuture* connect_future = cass_session_connect(session, cluster);
if (cass_future_error_code(connect_future) == CASS_OK) {
/* These don't work */
/*CassString query = cass_string_init("UPDATE examples.test_set SET value = value + {?} WHERE key = ?");*/
CassString query = cass_string_init("UPDATE examples.test_set SET value = value + ? WHERE key = ?");
CassStatement* statement = cass_statement_new(query, 2);
CassCollection* coll = cass_collection_new(CASS_COLLECTION_TYPE_SET, 2);
cass_statement_bind_int32(statement, 0, 1);
cass_collection_append_string(coll, cass_string_init("item3"));
cass_collection_append_string(coll, cass_string_init("item4"));
cass_statement_bind_collection(statement, 1, coll);
cass_collection_free(coll);
CassFuture* future = cass_session_execute(session, statement);
if (cass_future_error_code(future) == CASS_OK) {
printf("Success!\n");
} else {
CassString message = cass_future_error_message(future);
fprintf(stderr, "Error running query: %.*s\n", (int)message.length, message.data);
}
cass_statement_free(statement);
} else {
CassString message = cass_future_error_message(connect_future);
fprintf(stderr, "Error connectin: %.*s\n", (int)message.length, message.data);
}
cass_future_free(connect_future);
cass_session_free(session);
cass_cluster_free(cluster);
return 0;
}