Query Value-s can be used along with query string templates. Query string templates is a special sort of query string that contains ? sign. ? will be subsituted by CDRS driver with query Values.
For instance:
const insert_numbers_query: &'static str = "INSERT INTO my.numbers (my_int, my_bigint) VALUES (?, ?)";
let values = query_values!(1 as i32, 1 as i64);
session.query_with_values(insert_numbers_query, values).unwrap();insert_numbers_query is a typical query template. session::query_with_values method provides an API for using such query strings along with query values.
There is full list of CDRSSession methods that allow using values and query templates:
-
exec_with_values- executes previously preared query with provided values (see example and/or Preparing and Executing section); -
query_with_params_tw- immediately executes a query using provided values (see example)
There are two type of query values supported by CDRS:
- simple
Value-s may be imagined as a tuple of actual values. This values will be inserted instead of a?that has the same index number as aValuewithin a tuple. To easy createValue-s CDRS providesquery_values!macro:
#[macro_use]
extern crate cdrs;
let values = query_values!(1 as i32, 1 as i64);Value-s with names may be imagined as aMapthat links a table column name with a value that should be inserted in a column. It means thatValue-s with maps should not necesarily have the same order as a corresponded?in a query template:
#[macro_use]
extern crate cdrs;
//...
const insert_numbers_query: &'static str = "INSERT INTO my.numbers (my_int, my_bigint) VALUES (?, ?)";
let values = query_values!(my_bigint => 1 as i64, my_int => 1 as i64);
session.query_with_values(insert_numbers_query, values).unwrap();What kind of values can be used as query_values! arguments? All types that have implementations of Into<Bytes> trait.
For Rust structs represented by Cassandra User Defined types #[derive(IntoCDRSValue)] can be used for recurcive implementation. See CRUD example.
-
Cassandra official docs - User Defined Types http://cassandra.apache.org/doc/4.0/cql/types.html#grammar-token-user_defined_type.
-
Datastax - User Defined Types https://docs.datastax.com/en/cql/3.3/cql/cql_using/useCreateUDT.html.
-
ScyllaDB - User Defined Types https://docs.scylladb.com/getting-started/types/