pub struct PreparedStatement { /* private fields */ }
Expand description
A prepared query statement for use in a QueryRequest
.
PreparedStatement encapsulates a prepared query statement. It includes state that can be sent to a server and executed without re-parsing the query.
The details of a prepared query are purposefully opaque, as its internal data and implementation may change over time.
PreparedStatement is only created by calling QueryRequest::execute()
followed by
QueryResult::prepared_statement()
.
The main purpose of a prepared statement is to parse a query for execution many times,
with different variables. Here is a simple example using QueryRequest
and PreparedStatement
to
insert a set of rows into a table (note: it may be more optimal to use WriteMultipleRequest
for this specific case, this example is just to show the use of variables in prepared statetments):
let handle = Handle::builder().build().await?;
let prep_result = QueryRequest::new(
"declare $id integer; $name string; insert into testusers(id, name) values($id, $name)",
)
.prepare_only()
.execute(&handle)
.await?;
let data = vec!["jane", "john", "jasper"];
let mut qreq = QueryRequest::new_prepared(&prep_result.prepared_statement());
for i in 0..data.len() {
let id = (i as i32) + 1;
qreq.set_variable("$id", &id)?;
qreq.set_variable("$name", &data[i])?;
let result = qreq.execute(&handle).await?;
println!("Insert result = {:?}", result);
}
Trait Implementations§
Source§impl Clone for PreparedStatement
impl Clone for PreparedStatement
Source§fn clone(&self) -> PreparedStatement
fn clone(&self) -> PreparedStatement
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more