Reputation: 522
I'm using prepared statements in the Cassandra Datastax C++ Driver. How do I bind an integer-value to the "USING TTL ?" part of a prepared statement?
My statement would be something like
INSERT INTO table (column1, column2, column3) VALUES (?, ?, ?) USING TTL ?
In other words, If I'm using the position to bind to TTL, what is its position? (In this example, is it 4?) If I'm using bind by column name, what is its column name?
It looks like this can be done in CQL, but I couldn't find any documentation about the C++ driver API for doing this.
Upvotes: 7
Views: 1721
Reputation: 1
I have experimented with Cassandra C++ driver 2.11. I have found following
TTL can be bound by position only and we should use (cass_int32_t) data for binding TTL value
Example: cass_statement_bind_int32(statement, 2, (cass_int32_t)20);
cass_statement_bind_int32_by_name won't work
Ex: cass_statement_bind_int32_by_name(statement, 2, (cass_int32_t)20); //won't work
Upvotes: 0
Reputation: 12738
In Cassandra CQL 2.0 you can have:
Cassandra 1.2 doesn't allow you to use a bind marker for the TIMESTAMP and TTL properties of update statements, nor for the LIMIT property of SELECT statements. This is now fixed and you can for instance prepare statements like:
SELECT * FROM myTable LIMIT ?;
UPDATE myTable USING TTL ? SET v = 2 WHERE k = 'foo';
See their blog for more.
Edit:
I found this pdf and it tells you more:
Bound parameters:
The driver supports two kinds of bound parameters: by marker and by name. Binding parameters The ? marker is used to denote the bind variables in a query string. This is used for both regular and prepared parameterized queries. In addition to adding the bind marker to your query string, your application must also provide the number of bind variables to cass_statement_new() when constructing a new statement. If a query doesn’t require any bind variables then 0 can be used. The cass_statement_bind_*() functions are then used to bind values to the statement’s variables. Bind variables can be bound by the marker index or by name.
Bind by marker index example
CassString query = cass_string_init("SELECT * FROM table1 WHERE column1
= ?");
/* Create a statement with a single parameter */
CassStatement* statement = cass_statement_new(query, 1);
cass_statement_bind_string(statement, 0, cass_string_init("abc"));
/* Execute statement */
cass_statement_free(statement);
Bind by marker name example
Variables can only be bound by name for prepared statements. This limitation exists because query metadata provided by Cassandra is required to map the variable name to the variable’s marker index.
/* Prepare statement */
/* The prepared query allocates the correct number of parameters
automatically */
CassStatement* statement = cass_prepared_bind(prepared);
/* The parameter can now be bound by name */
cass_statement_bind_string_by_name(statement, "column1",
cass_string_init("abc"));
/* Execute statement */
cass_statement_free(statement);
To answer your question you can use bind by index (works at least for sure):
CassString query = cass_string_init("INSERT INTO table (column1, column2, column3) VALUES (?, ?, ?) USING TTL ?");
/* Create a statement with a single parameter */
CassStatement* statement = cass_statement_new(query, 4); // Bind 4 variables.
cass_statement_bind_string(statement, 0, cass_string_init("abc")); // Bind abc to first column.
cass_statement_bind_string(statement, 1, cass_string_init("bcd")); // Bind bcd to second column.
cass_statement_bind_string(statement, 2, cass_string_init("cde")); // Bind cde to third column.
cass_statement_bind_string(statement, 3, cass_string_init(50)); // Bind 50 to TTL.
/* Execute statement */
cass_statement_free(statement);
Edit:
See https://docs.datastax.com/en/cql/3.3/cql/cql_using/useExpireExample.html where you see that in case of INSERT we have USING TTL as last part of query, as seen above.
Upvotes: 6