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_paramalways appends$Nwhere 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
impl SqlBuilder
Sourcepub fn with_sql(sql: impl Into<String>) -> Self
pub fn with_sql(sql: impl Into<String>) -> Self
Create a builder pre-loaded with the given SQL text.
Sourcepub fn push_ident(&mut self, ident: &str)
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"Sourcepub fn push_qi(&mut self, qi: &QualifiedIdentifier)
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"Sourcepub fn push_literal(&mut self, s: &str)
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'Sourcepub fn push_param(&mut self, param: SqlParam)
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).
Sourcepub fn param_count(&self) -> usize
pub fn param_count(&self) -> usize
Current number of bind parameters.
Sourcepub fn push_separated<T, F>(&mut self, sep: &str, items: &[T], f: F)
pub fn push_separated<T, F>(&mut self, sep: &str, items: &[T], f: F)
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.
Sourcepub fn push_builder(&mut self, other: &SqlBuilder)
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.
Trait Implementations§
Source§impl Clone for SqlBuilder
impl Clone for SqlBuilder
Source§fn clone(&self) -> SqlBuilder
fn clone(&self) -> SqlBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more