pub struct PreparedQuery<F> { /* private fields */ }Expand description
A prepared query builder that supports named placeholders.
PreparedQuery allows you to use named placeholders (:name) in your SQL templates
instead of positional placeholders (?). It avoids self-referential lifetime issues
by storing the SQL template, placeholder order, and binder function separately,
and constructing the actual Query on each execution.
§Type Parameters
F- A binder function that binds values to placeholders. Must work with any lifetime'q.
§Examples
use sqlx::MySqlPool;
use sqlx_named_bind::PreparedQuery;
let user_id = 42;
let name = "John Doe";
let mut query = PreparedQuery::new(
"INSERT INTO users (user_id, name) VALUES (:user_id, :name)",
|q, key| match key {
":user_id" => q.bind(user_id),
":name" => q.bind(name),
_ => q,
}
)?;
let result = query.execute(&pool).await?;
println!("Inserted {} rows", result.rows_affected());§Using with Transactions
use sqlx::{MySqlPool, Transaction, MySql};
use sqlx_named_bind::PreparedQuery;
let mut tx: Transaction<MySql> = pool.begin().await?;
let mut query = PreparedQuery::new(
"UPDATE users SET name = :name WHERE user_id = :user_id",
|q, key| match key {
":user_id" => q.bind(vec![1, 2, 3]),
":name" => q.bind("Jane Doe"),
_ => q,
}
)?;
query.execute(&mut *tx).await?;
tx.commit().await?;Implementations§
Source§impl<F> PreparedQuery<F>
impl<F> PreparedQuery<F>
Sourcepub fn new<T>(template: T, binder: F) -> Result<Self>
pub fn new<T>(template: T, binder: F) -> Result<Self>
Creates a new PreparedQuery from an SQL template and binder function.
The SQL template can contain named placeholders in the format :name.
The binder function will be called for each placeholder in the order they appear.
§Arguments
template- SQL query template with named placeholders (e.g.,:user_id)binder- Function that binds values to placeholders based on their names
§Errors
Returns an error if the SQL template cannot be parsed (invalid regex pattern).
§Examples
use sqlx_named_bind::PreparedQuery;
let query = PreparedQuery::new(
"SELECT * FROM users WHERE id = :id",
|q, key| match key {
":id" => q.bind(42),
_ => q,
}
)?;Sourcepub async fn execute<'e, E>(&mut self, executor: E) -> Result<MySqlQueryResult>
pub async fn execute<'e, E>(&mut self, executor: E) -> Result<MySqlQueryResult>
Executes the prepared query using the provided executor.
This method constructs a fresh Query on each call, avoiding self-referential
lifetime issues. It works with any SQLx Executor implementation, including
MySqlPool, Transaction, and others.
§Arguments
executor- Any SQLx executor (pool, transaction, etc.)
§Returns
Returns the MySQL query result containing information about affected rows, last insert ID, etc.
§Errors
Returns an error if the database query fails.
§Examples
use sqlx::MySqlPool;
use sqlx_named_bind::PreparedQuery;
let mut query = PreparedQuery::new(
"DELETE FROM users WHERE id = :id",
|q, key| match key {
":id" => q.bind(42),
_ => q,
}
)?;
let result = query.execute(&pool).await?;
println!("Deleted {} rows", result.rows_affected());Auto Trait Implementations§
impl<F> Freeze for PreparedQuery<F>where
F: Freeze,
impl<F> RefUnwindSafe for PreparedQuery<F>where
F: RefUnwindSafe,
impl<F> Send for PreparedQuery<F>where
F: Send,
impl<F> Sync for PreparedQuery<F>where
F: Sync,
impl<F> Unpin for PreparedQuery<F>where
F: Unpin,
impl<F> UnwindSafe for PreparedQuery<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more