Skip to main content

SqlBuilder

Struct SqlBuilder 

Source
pub struct SqlBuilder { /* private fields */ }
Expand description

Accumulates SQL text and bind parameters into a parameterized query.

Constructed via SqlBuilder::new(), populated with push* methods, and finalised with build() which returns (String, Vec<SqlParam>).

§Invariants

  • push_param always appends $N where N = params.len() after the push.
  • Identifiers are always double-quote escaped via push_ident / push_qi.
  • Literals are single-quote escaped via push_literal.

Implementations§

Source§

impl SqlBuilder

Source

pub fn new() -> Self

Create an empty builder.

Source

pub fn with_sql(sql: impl Into<String>) -> Self

Create a builder pre-loaded with the given SQL text.

Source

pub fn push(&mut self, s: &str)

Append raw SQL text (no escaping).

Source

pub fn push_char(&mut self, c: char)

Append a single character.

Source

pub fn push_ident(&mut self, ident: &str)

Append a double-quoted SQL identifier.

Internal double-quotes are doubled per the SQL standard.

§SQL Example
-- push_ident("user\"name") produces:
"user""name"
Source

pub fn push_qi(&mut self, qi: &QualifiedIdentifier)

Append a schema-qualified identifier ("schema"."name").

If the schema is empty, only the name is emitted.

§SQL Example
-- push_qi(QI { schema: "public", name: "users" }) produces:
"public"."users"
Source

pub fn push_literal(&mut self, s: &str)

Append a single-quoted SQL literal.

Single-quotes are doubled. If the value contains a backslash, the PostgreSQL E-string syntax (E'...') is used so that \\ is treated as a literal backslash regardless of standard_conforming_strings.

§SQL Example
-- push_literal("it's") produces:
'it''s'
-- push_literal("back\\slash") produces:
E'back\\slash'
Source

pub fn push_param(&mut self, param: SqlParam)

Append a bind-parameter placeholder ($N) and store the value.

The placeholder index is self.params.len() + 1 (1-based).

Source

pub fn param_count(&self) -> usize

Current number of bind parameters.

Source

pub fn is_empty(&self) -> bool

Whether the builder is empty (no SQL text).

Source

pub fn sql_len(&self) -> usize

Current length of the SQL text buffer.

Source

pub fn push_separated<T, F>(&mut self, sep: &str, items: &[T], f: F)
where F: Fn(&mut SqlBuilder, &T),

Append items separated by sep, where each item is written by the callback f.

§Behaviour

Does nothing if items is empty. Does not emit a trailing separator.

Source

pub fn push_builder(&mut self, other: &SqlBuilder)

Merge another builder’s SQL and params into this one.

The merged builder’s $N placeholders are rewritten to continue from this builder’s current param count.

Source

pub fn build(self) -> (String, Vec<SqlParam>)

Consume the builder and return the SQL string and parameters.

Source

pub fn sql(&self) -> &str

Borrow the current SQL text (for debugging / assertions).

Source

pub fn params(&self) -> &[SqlParam]

Borrow the current parameters (for debugging / assertions).

Trait Implementations§

Source§

impl Clone for SqlBuilder

Source§

fn clone(&self) -> SqlBuilder

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 Debug for SqlBuilder

Source§

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

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

impl Default for SqlBuilder

Source§

fn default() -> Self

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

Auto Trait Implementations§

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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, 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<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