PreparedQueryAs

Struct PreparedQueryAs 

Source
pub struct PreparedQueryAs<R, F>
where F: for<'q> FnMut(QA<'q, R>, &str) -> QA<'q, R>,
{ /* 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 implements FromRow
  • F - 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>
where for<'row> R: FromRow<'row, MySqlRow> + Send + Unpin, F: for<'q> FnMut(QA<'q, R>, &str) -> QA<'q, R>,

Source

pub fn new<T>(template: T, binder: F) -> Result<Self>
where T: Into<String>,

Creates a new PreparedQueryAs from an SQL template and binder function.

§Arguments
  • template - SQL query template with named placeholders
  • binder - 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,
    }
)?;
Source

pub async fn fetch_all<'e, E>(&mut self, executor: E) -> Result<Vec<R>>
where E: Executor<'e, Database = MySql>,

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());
Source

pub async fn fetch_one<'e, E>(&mut self, executor: E) -> Result<R>
where E: Executor<'e, Database = MySql>,

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);
Source

pub async fn fetch_optional<'e, E>(&mut self, executor: E) -> Result<Option<R>>
where E: Executor<'e, Database = MySql>,

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>

§

impl<R, F> Send for PreparedQueryAs<R, F>
where F: Send, R: Send,

§

impl<R, F> Sync for PreparedQueryAs<R, F>
where F: Sync, R: Sync,

§

impl<R, F> Unpin for PreparedQueryAs<R, F>
where F: Unpin, R: Unpin,

§

impl<R, F> UnwindSafe for PreparedQueryAs<R, F>
where F: UnwindSafe, R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more