pub struct PreparedQueryAs<R, F>{ /* private fields */ }Expand description
A prepared query builder that returns typed results from named placeholders.
PreparedQueryAs is similar to PreparedQuery but returns strongly-typed results
using SQLx’s FromRow trait. It supports fetch_all, fetch_one, and fetch_optional.
§Type Parameters
R- The result type that implementsFromRowF- A binder function that binds values to placeholders
§Examples
use sqlx::{MySqlPool, FromRow};
use sqlx_named_bind::PreparedQueryAs;
#[derive(FromRow)]
struct User {
id: i32,
name: String,
}
let user_id = 42;
let mut query = PreparedQueryAs::<User, _>::new(
"SELECT id, name FROM users WHERE id = :id",
|q, key| match key {
":id" => q.bind(user_id),
_ => q,
}
)?;
let user: User = query.fetch_one(&pool).await?;
println!("User: {} ({})", user.name, user.id);Implementations§
Source§impl<R, F> PreparedQueryAs<R, F>
impl<R, F> PreparedQueryAs<R, F>
Sourcepub fn new<T>(template: T, binder: F) -> Result<Self>
pub fn new<T>(template: T, binder: F) -> Result<Self>
Creates a new PreparedQueryAs from an SQL template and binder function.
§Arguments
template- SQL query template with named placeholdersbinder- Function that binds values to placeholders
§Errors
Returns an error if the SQL template cannot be parsed.
§Examples
use sqlx::FromRow;
use sqlx_named_bind::PreparedQueryAs;
#[derive(FromRow)]
struct User {
id: i32,
name: String,
}
let query = PreparedQueryAs::<User, _>::new(
"SELECT id, name FROM users WHERE id = :id",
|q, key| match key {
":id" => q.bind(42),
_ => q,
}
)?;Sourcepub async fn fetch_all<'e, E>(&mut self, executor: E) -> Result<Vec<R>>
pub async fn fetch_all<'e, E>(&mut self, executor: E) -> Result<Vec<R>>
Executes the query and returns all matching rows.
§Arguments
executor- Any SQLx executor (pool, transaction, etc.)
§Returns
Returns a vector of all rows matching the query.
§Errors
Returns an error if the query fails or if any row cannot be converted to type R.
§Examples
use sqlx::{MySqlPool, FromRow};
use sqlx_named_bind::PreparedQueryAs;
#[derive(FromRow)]
struct User {
id: i32,
name: String,
}
let mut query = PreparedQueryAs::<User, _>::new(
"SELECT id, name FROM users WHERE age > :min_age",
|q, key| match key {
":min_age" => q.bind(18),
_ => q,
}
)?;
let users: Vec<User> = query.fetch_all(&pool).await?;
println!("Found {} users", users.len());Sourcepub async fn fetch_one<'e, E>(&mut self, executor: E) -> Result<R>
pub async fn fetch_one<'e, E>(&mut self, executor: E) -> Result<R>
Executes the query and returns exactly one row.
§Arguments
executor- Any SQLx executor (pool, transaction, etc.)
§Returns
Returns the single row matching the query.
§Errors
Returns an error if:
- No rows are found
- More than one row is found
- The query fails
- The row cannot be converted to type
R
§Examples
use sqlx::{MySqlPool, FromRow};
use sqlx_named_bind::PreparedQueryAs;
#[derive(FromRow)]
struct User {
id: i32,
name: String,
}
let mut query = PreparedQueryAs::<User, _>::new(
"SELECT id, name FROM users WHERE id = :id",
|q, key| match key {
":id" => q.bind(42),
_ => q,
}
)?;
let user: User = query.fetch_one(&pool).await?;
println!("Found user: {}", user.name);Sourcepub async fn fetch_optional<'e, E>(&mut self, executor: E) -> Result<Option<R>>
pub async fn fetch_optional<'e, E>(&mut self, executor: E) -> Result<Option<R>>
Executes the query and returns at most one row.
§Arguments
executor- Any SQLx executor (pool, transaction, etc.)
§Returns
Returns Some(row) if exactly one row matches, None if no rows match.
§Errors
Returns an error if:
- More than one row is found
- The query fails
- The row cannot be converted to type
R
§Examples
use sqlx::{MySqlPool, FromRow};
use sqlx_named_bind::PreparedQueryAs;
#[derive(FromRow)]
struct User {
id: i32,
name: String,
}
let mut query = PreparedQueryAs::<User, _>::new(
"SELECT id, name FROM users WHERE email = :email",
|q, key| match key {
":email" => q.bind("user@example.com"),
_ => q,
}
)?;
match query.fetch_optional(&pool).await? {
Some(user) => println!("Found user: {}", user.name),
None => println!("User not found"),
}Auto Trait Implementations§
impl<R, F> Freeze for PreparedQueryAs<R, F>where
F: Freeze,
impl<R, F> RefUnwindSafe for PreparedQueryAs<R, F>where
F: RefUnwindSafe,
R: RefUnwindSafe,
impl<R, F> Send for PreparedQueryAs<R, F>
impl<R, F> Sync for PreparedQueryAs<R, F>
impl<R, F> Unpin for PreparedQueryAs<R, F>
impl<R, F> UnwindSafe for PreparedQueryAs<R, F>where
F: UnwindSafe,
R: 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