Skip to main content

SqlWriter

Struct SqlWriter 

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

Source

pub fn new(dialect: &'d dyn Dialect) -> Self

A writer numbering placeholders from 1.

Source

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.

Source

pub fn dialect(&self) -> &'d dyn Dialect

The dialect this writer renders for.

Source

pub fn sql(&self) -> &str

The SQL written so far.

Source

pub fn args(&self) -> &[Value]

The arguments bound so far, in placeholder order.

Source

pub fn arg_position(&self) -> usize

The position the next push_arg will use (1-based).

Source

pub fn error(&self) -> Option<&Error>

The first recorded failure, if any.

Source

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.

Source

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

Append raw SQL.

Source

pub fn push_arg(&mut self, v: impl ToValue)

Bind v and write its placeholder.

The single point where the placeholder counter advances.

Source

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.

Source

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.

Source

pub fn write_expr<E: Expression + ?Sized>(&mut self, e: &E)

Render a nested expression. Replaces bob’s Express.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn finish(self) -> Result<(String, Vec<Value>)>

Consume the writer, yielding the SQL and its arguments — or the recorded failure.

Source

pub fn into_parts(self) -> (String, Vec<Value>, Option<Error>)

Consume the writer, yielding everything including any recorded failure.

For a debug print that wants the partial SQL as well as the reason.

Trait Implementations§

Source§

impl<'d> Debug for SqlWriter<'d>

Source§

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

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

impl Write for SqlWriter<'_>

write! into the SQL buffer, for the rare fragment that is easier formatted than pushed.

Source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · Source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more

Auto Trait Implementations§

§

impl<'d> !RefUnwindSafe for SqlWriter<'d>

§

impl<'d> !UnwindSafe for SqlWriter<'d>

§

impl<'d> Freeze for SqlWriter<'d>

§

impl<'d> Send for SqlWriter<'d>

§

impl<'d> Sync for SqlWriter<'d>

§

impl<'d> Unpin for SqlWriter<'d>

§

impl<'d> UnsafeUnpin for SqlWriter<'d>

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, 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, 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.