SQL

Struct SQL 

Source
pub struct SQL<'a, V: SQLParam> {
    pub chunks: SmallVec<[SQLChunk<'a, V>; 8]>,
}
Expand description

SQL fragment builder with flat chunk storage.

Uses SmallVec<[SQLChunk; 8]> for inline storage of typical SQL fragments without heap allocation.

Fields§

§chunks: SmallVec<[SQLChunk<'a, V>; 8]>

Implementations§

Source§

impl<'a, V: SQLParam> SQL<'a, V>

Source

pub const fn empty() -> Self

Creates an empty SQL fragment

Source

pub fn token(t: Token) -> Self

Creates SQL with a single token

Source

pub fn with_capacity_chunks(capacity: usize) -> Self

Creates an empty SQL fragment with pre-allocated chunk capacity.

Source

pub fn ident(name: impl Into<Cow<'a, str>>) -> Self

Creates SQL with a quoted identifier

Source

pub fn raw(text: impl Into<Cow<'a, str>>) -> Self

Creates SQL with raw text (unquoted)

Source

pub fn param(value: impl Into<Cow<'a, V>>) -> Self

Creates SQL with a single parameter value

Source

pub fn bytes(bytes: impl Into<Cow<'a, [u8]>>) -> Self
where V: From<&'a [u8]> + From<Vec<u8>> + Into<Cow<'a, V>>,

Creates SQL with a binary parameter value (BLOB/bytea)

Prefer this over SQL::param(Vec<u8>) to avoid list semantics.

Source

pub fn placeholder(name: &'static str) -> Self

Creates SQL with a named placeholder (no value, for prepared statements)

Source

pub fn table(table: &'static dyn SQLTableInfo) -> Self

Creates SQL referencing a table

Source

pub fn column(column: &'static dyn SQLColumnInfo) -> Self

Creates SQL referencing a column

Source

pub fn func(name: &'static str, args: SQL<'a, V>) -> Self

Creates SQL for a function call: NAME(args) Subqueries are automatically wrapped in parentheses: NAME((SELECT …))

Source

pub fn append(self, other: impl Into<SQL<'a, V>>) -> Self

Append another SQL fragment (flat extend)

Source

pub fn push(self, chunk: impl Into<SQLChunk<'a, V>>) -> Self

Push a single chunk

Source

pub fn with_capacity(self, additional: usize) -> Self

Pre-allocates capacity for additional chunks

Source

pub fn join<T>(sqls: T, separator: Token) -> SQL<'a, V>
where T: IntoIterator, T::Item: ToSQL<'a, V>,

Joins multiple SQL fragments with a separator

Source

pub fn parens(self) -> Self

Wrap in parentheses: (self)

Source

pub fn is_subquery(&self) -> bool

Check if this SQL fragment is a subquery (starts with SELECT)

Source

pub fn alias(self, name: impl Into<Cow<'a, str>>) -> SQL<'a, V>

Creates an aliased version: self AS “name”

Source

pub fn param_list<I>(values: I) -> Self
where I: IntoIterator, I::Item: Into<Cow<'a, V>>,

Creates a comma-separated list of parameters

Source

pub fn assignments<I, T>(pairs: I) -> Self
where I: IntoIterator<Item = (&'static str, T)>, T: Into<Cow<'a, V>>,

Creates a comma-separated list of column assignments: “col” = ?

Source

pub fn into_owned(self) -> OwnedSQL<V>

Converts to owned version (consuming self to avoid clone)

Source

pub fn sql(&self) -> String

Returns the SQL string with dialect-appropriate placeholders Uses $1, $2, ... for PostgreSQL, ? for SQLite/MySQL

Source

pub fn write_to(&self, buf: &mut impl Write)

Write SQL to a buffer with dialect-appropriate placeholders Uses $1, $2, ... for PostgreSQL, ? or :name for SQLite, ? for MySQL Named placeholders use :name syntax only for SQLite; PostgreSQL always uses $N

Source

pub fn write_chunk_to( &self, buf: &mut impl Write, chunk: &SQLChunk<'a, V>, index: usize, )

Write a single chunk with pattern detection

Source

pub fn write_qualified_columns( &self, buf: &mut impl Write, table: &dyn SQLTableInfo, )

Write fully qualified columns

Source

pub fn params(&self) -> impl Iterator<Item = &V>

Returns an iterator over references to parameter values (avoids allocating a Vec - callers can collect if needed)

Source

pub fn bind<T: SQLParam + Into<V>>( self, params: impl IntoIterator<Item: Into<ParamBind<'a, T>>>, ) -> SQL<'a, V>

Bind named parameters

Trait Implementations§

Source§

impl<'a, V: SQLParam + 'a> AsRef<SQL<'a, V>> for SQL<'a, V>

Source§

fn as_ref(&self) -> &SQL<'a, V>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a, V, T, N, A> AsRef<SQL<'a, V>> for SQLExpr<'a, V, T, N, A>

Provides reference conversion to inner SQL.

§Example

fn takes_sql_ref<'a, V>(sql: &SQL<'a, V>) { ... }
let expr = eq(users.id, 42);
takes_sql_ref(expr.as_ref());
Source§

fn as_ref(&self) -> &SQL<'a, V>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a, V: Clone + SQLParam> Clone for SQL<'a, V>

Source§

fn clone(&self) -> SQL<'a, V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, V: Debug + SQLParam> Debug for SQL<'a, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, V: SQLParam> Default for SQL<'a, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, V: SQLParam + Display> Display for SQL<'a, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, V> Expr<'a, V> for SQL<'a, V>
where V: SQLParam + 'a,

Source§

type SQLType = Any

The SQL data type this expression evaluates to.
Source§

type Nullable = Null

Whether this expression can be NULL.
Source§

type Aggregate = Scalar

Whether this is an aggregate (COUNT, SUM) or scalar expression.
Source§

impl<'a, T, V> From<&T> for SQL<'a, V>
where T: ToSQL<'a, V>, V: SQLParam,

Source§

fn from(value: &T) -> Self

Converts to this type from the input type.
Source§

impl<'a, V: SQLParam + 'a> From<&'a str> for SQL<'a, V>

Source§

fn from(s: &'a str) -> Self

Converts to this type from the input type.
Source§

impl<'a, V: SQLParam> From<SQL<'a, V>> for OwnedSQL<V>

Source§

fn from(value: SQL<'a, V>) -> Self

Converts to this type from the input type.
Source§

impl<'a, V: SQLParam, T: DataType, N: Nullability, A: AggregateKind> From<SQLExpr<'a, V, T, N, A>> for SQL<'a, V>

Source§

fn from(expr: SQLExpr<'a, V, T, N, A>) -> Self

Converts to this type from the input type.
Source§

impl<'a, V: SQLParam> From<Token> for SQL<'a, V>

Source§

fn from(value: Token) -> Self

Converts to this type from the input type.
Source§

impl<'a, V: SQLParam, T> FromIterator<T> for SQL<'a, V>
where SQLChunk<'a, V>: From<T>,

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, V: SQLParam> IntoIterator for SQL<'a, V>

Source§

type Item = SQLChunk<'a, V>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<[SQLChunk<'a, V>; 8]>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, V: SQLParam + 'a> ToSQL<'a, V> for SQL<'a, V>

Source§

fn to_sql(&self) -> SQL<'a, V>

Source§

fn alias(&self, alias: &'static str) -> SQL<'a, V>

Auto Trait Implementations§

§

impl<'a, V> Freeze for SQL<'a, V>
where V: Freeze,

§

impl<'a, V> !RefUnwindSafe for SQL<'a, V>

§

impl<'a, V> Send for SQL<'a, V>
where V: Send + Sync,

§

impl<'a, V> Sync for SQL<'a, V>
where V: Sync,

§

impl<'a, V> Unpin for SQL<'a, V>
where V: Unpin,

§

impl<'a, V> !UnwindSafe for SQL<'a, V>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<'a, V, E> ExprExt<'a, V> for E
where V: SQLParam, E: Expr<'a, V>,

Source§

fn eq<R>(self, other: R) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where R: Expr<'a, V>, Self::SQLType: Compatible<R::SQLType>,

Equality comparison (=). Read more
Source§

fn ne<R>(self, other: R) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where R: Expr<'a, V>, Self::SQLType: Compatible<R::SQLType>,

Inequality comparison (<>). Read more
Source§

fn gt<R>(self, other: R) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where R: Expr<'a, V>, Self::SQLType: Compatible<R::SQLType>,

Greater-than comparison (>). Read more
Source§

fn ge<R>(self, other: R) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where R: Expr<'a, V>, Self::SQLType: Compatible<R::SQLType>,

Greater-than-or-equal comparison (>=). Read more
Source§

fn lt<R>(self, other: R) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where R: Expr<'a, V>, Self::SQLType: Compatible<R::SQLType>,

Less-than comparison (<). Read more
Source§

fn le<R>(self, other: R) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where R: Expr<'a, V>, Self::SQLType: Compatible<R::SQLType>,

Less-than-or-equal comparison (<=). Read more
Source§

fn like<R>(self, pattern: R) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where R: Expr<'a, V>, Self::SQLType: Textual, R::SQLType: Textual,

LIKE pattern matching. Read more
Source§

fn not_like<R>(self, pattern: R) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where R: Expr<'a, V>, Self::SQLType: Textual, R::SQLType: Textual,

NOT LIKE pattern matching. Read more
Source§

fn is_null(self) -> SQLExpr<'a, V, Bool, NonNull, Scalar>

IS NULL check. Read more
Source§

fn is_not_null(self) -> SQLExpr<'a, V, Bool, NonNull, Scalar>

IS NOT NULL check. Read more
Source§

fn between<L, H>(self, low: L, high: H) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where L: Expr<'a, V>, H: Expr<'a, V>, Self::SQLType: Compatible<L::SQLType> + Compatible<H::SQLType>,

BETWEEN comparison. Read more
Source§

fn not_between<L, H>( self, low: L, high: H, ) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where L: Expr<'a, V>, H: Expr<'a, V>, Self::SQLType: Compatible<L::SQLType> + Compatible<H::SQLType>,

NOT BETWEEN comparison. Read more
Source§

fn in_array<I, R>(self, values: I) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where I: IntoIterator<Item = R>, R: Expr<'a, V>, Self::SQLType: Compatible<R::SQLType>,

IN array check. Read more
Source§

fn not_in_array<I, R>(self, values: I) -> SQLExpr<'a, V, Bool, NonNull, Scalar>
where I: IntoIterator<Item = R>, R: Expr<'a, V>, Self::SQLType: Compatible<R::SQLType>,

NOT IN array check. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToCompactString for T
where T: Display,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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<'a, V, L, R> SQLComparable<'a, V, R> for L
where V: SQLParam + 'a, L: ToSQL<'a, V>, R: ToSQL<'a, V>,