pub struct SqlWriter<'d> { /* private fields */ }Expand description
The SQL buffer, the bound arguments, the placeholder counter and the dialect, together.
bob passes start int down the tree and every caller adds len(args) by hand
before recursing — SelectQuery.WriteSQL does it more than fifteen times.
Here the counter lives next to the arguments and only
push_arg touches it, so sub-queries and nested expressions
re-index correctly with no bookkeeping at the call site.
Implementations§
Source§impl<'d> SqlWriter<'d>
impl<'d> SqlWriter<'d>
Sourcepub fn with_start(dialect: &'d dyn Dialect, start: usize) -> Self
pub fn with_start(dialect: &'d dyn Dialect, start: usize) -> Self
A writer numbering placeholders from start.
Used to splice a query into one that already has arguments — bob’s
BuildN.
§Panics
If start is 0. Placeholders are 1-based in every supported dialect.
Sourcepub fn arg_position(&self) -> usize
pub fn arg_position(&self) -> usize
The position the next push_arg will use (1-based).
Sourcepub fn record_error(&mut self, e: Error)
pub fn record_error(&mut self, e: Error)
Record a failure.
The first one wins: it is the one with the most context, and a later
failure is usually a consequence of it. Rendering continues either way —
the partial SQL is still useful in a debug print, and
finish is what refuses to hand it over.
Sourcepub fn push_arg(&mut self, v: impl ToValue)
pub fn push_arg(&mut self, v: impl ToValue)
Bind v and write its placeholder.
The single point where the placeholder counter advances.
Sourcepub fn push_named_arg(&mut self, name: &str)
pub fn push_named_arg(&mut self, name: &str)
Write a named argument’s placeholder.
Named arguments exist to prepare a statement whose values are supplied at bind time, so nothing is added to the argument list and the positional counter does not move.
Records Error::NoNamedArgs if the dialect has no named-argument
syntax.
Sourcepub fn push_quoted<S: AsRef<str>>(&mut self, parts: &[S])
pub fn push_quoted<S: AsRef<str>>(&mut self, parts: &[S])
Write a dotted, quoted identifier: ["users", "id"] becomes
"users"."id".
Empty parts are skipped, so a caller can pass an unset qualifier without
branching. Generic over AsRef<str> so that a clause can hand over its
stored [Cow<'static, str>] directly.
Sourcepub fn write_expr<E: Expression + ?Sized>(&mut self, e: &E)
pub fn write_expr<E: Expression + ?Sized>(&mut self, e: &E)
Render a nested expression. Replaces bob’s Express.
Sourcepub fn write_if<E: Expression + ?Sized>(
&mut self,
cond: bool,
prefix: &str,
e: &E,
suffix: &str,
)
pub fn write_if<E: Expression + ?Sized>( &mut self, cond: bool, prefix: &str, e: &E, suffix: &str, )
Render prefix, the expression, then suffix — but only if cond.
Replaces bob’s ExpressIf.
When cond is false nothing at all is written, affixes included, and no
argument is consumed.
Sourcepub fn write_if_some<E: Expression + ?Sized>(
&mut self,
e: Option<&E>,
prefix: &str,
suffix: &str,
)
pub fn write_if_some<E: Expression + ?Sized>( &mut self, e: Option<&E>, prefix: &str, suffix: &str, )
write_if for an optional clause, which is how most
clauses are stored.
Sourcepub fn write_slice<E: Expression>(
&mut self,
items: &[E],
prefix: &str,
sep: &str,
suffix: &str,
)
pub fn write_slice<E: Expression>( &mut self, items: &[E], prefix: &str, sep: &str, suffix: &str, )
Render a slice joined by sep and wrapped in prefix/suffix, writing
nothing at all when the slice is empty. Replaces bob’s ExpressSlice.
The empty case is the load-bearing part: it is how a clause omits itself, keyword and all.
Sourcepub fn write_iter<E, I>(
&mut self,
items: I,
prefix: &str,
sep: &str,
suffix: &str,
)where
E: Expression,
I: IntoIterator<Item = E>,
pub fn write_iter<E, I>(
&mut self,
items: I,
prefix: &str,
sep: &str,
suffix: &str,
)where
E: Expression,
I: IntoIterator<Item = E>,
write_slice for anything iterable, so a clause can
map over its own storage without collecting into a Vec first.
Sourcepub fn write_with_dialect<E: Expression + ?Sized>(
&mut self,
dialect: &dyn Dialect,
e: &E,
)
pub fn write_with_dialect<E: Expression + ?Sized>( &mut self, dialect: &dyn Dialect, e: &E, )
Render a nested expression under a different dialect, keeping one shared argument list, placeholder counter and error slot.
This is how a sub-query built for one dialect embeds in a query built for
another — bob’s BaseQuery.WriteSQL ignores the dialect handed to it and
uses its own.
Trait Implementations§
Source§impl Write for SqlWriter<'_>
write! into the SQL buffer, for the rare fragment that is easier formatted
than pushed.
impl Write for SqlWriter<'_>
write! into the SQL buffer, for the rare fragment that is easier formatted
than pushed.