How to bind a value to TTL in INSERT, Cassandra C++ driver

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.

Tags:

C++

Cassandra