You can create unprepared statements in several ways:
use scylla::statement::unprepared::Statement;// From a stringlet statement = Statement::new("SELECT * FROM ks.table");// From a Stringlet query_string = String::from("SELECT * FROM ks.table");let statement = Statement::from(query_string);// From a string slicelet statement: Statement = "SELECT * FROM ks.table".into();
Fetches a single page of results with manual control over pagination:
use scylla::statement::unprepared::Statement;use scylla::response::PagingState;use std::ops::ControlFlow;let paged_query = Statement::new("SELECT a, b, c FROM examples_ks.select_paging") .with_page_size(6);let mut paging_state = PagingState::start();loop { let (res, paging_state_response) = session .query_single_page(paged_query.clone(), &[], paging_state) .await?; let res = res.into_rows_result()?; println!("Fetched {} rows", res.rows_num()); match paging_state_response.into_paging_control_flow() { ControlFlow::Break(()) => { // No more pages to fetch break; } ControlFlow::Continue(new_paging_state) => { // Continue with next page paging_state = new_paging_state; } }}
Returns an async iterator that automatically fetches all pages:
use futures::stream::StreamExt;let mut rows_stream = session .query_iter("SELECT a, b, c FROM examples_ks.basic", &[]) .await? .rows_stream::<(i32, i32, String)>()?;while let Some(next_row_res) = rows_stream.next().await { let (a, b, c) = next_row_res?; println!("a, b, c: {}, {}, {}", a, b, c);}
Control how many rows are returned per page (default: 5000):
let mut statement = Statement::new("SELECT * FROM ks.table");statement.set_page_size(1000);// Or use the builder patternlet statement = Statement::new("SELECT * FROM ks.table") .with_page_size(1000);
use scylla::statement::Consistency;let mut statement = Statement::new("SELECT * FROM ks.table");statement.set_consistency(Consistency::Quorum);// Or unset to use profile defaultstatement.unset_consistency();
Set serial consistency for lightweight transactions:
use scylla::statement::SerialConsistency;let mut statement = Statement::new( "UPDATE ks.table SET value = ? WHERE id = ? IF value = ?");statement.set_serial_consistency(Some(SerialConsistency::LocalSerial));
When using query_unpaged() or query_iter() with non-empty values, the driver must prepare the statement internally, resulting in 2 round trips instead of 1. For better performance, use execute_unpaged() or execute_iter() with a manually prepared statement.