Skip to main content

SqlBuilder

Struct SqlBuilder 

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

Builds parameterized SQL queries safely.

All user-supplied values go through DuckDB’s parameter binding (? placeholders), never through string interpolation. Methods return &mut Self for chaining.

Implementations§

Source§

impl SqlBuilder

Source

pub fn new(table: &str) -> Self

Create a builder targeting the given table or view.

Source

pub fn select(&mut self, cols: &[&str]) -> &mut Self

Set the columns to select (replaces the default *).

Source

pub fn distinct(&mut self) -> &mut Self

Add DISTINCT to the SELECT clause.

Source

pub fn join(&mut self, clause: &str) -> &mut Self

Add a JOIN clause.

The clause should be a full JOIN expression, e.g. "JOIN sets s ON cards.setCode = s.code".

Source

pub fn where_clause(&mut self, condition: &str, params: &[&str]) -> &mut Self

Add a WHERE condition with ? placeholders for each param.

The caller provides a condition using ? for each parameter value. Parameters are appended in order.

Source

pub fn where_like(&mut self, column: &str, value: &str) -> &mut Self

Add a case-insensitive LIKE condition.

Generates: LOWER({column}) LIKE LOWER(?)

Source

pub fn where_in(&mut self, column: &str, values: &[&str]) -> &mut Self

Add an IN condition with parameterized values.

Empty values list produces FALSE.

Source

pub fn where_eq(&mut self, column: &str, value: &str) -> &mut Self

Add an equality condition: {column} = ?.

Source

pub fn where_gte(&mut self, column: &str, value: &str) -> &mut Self

Add a greater-than-or-equal condition: {column} >= ?.

Source

pub fn where_lte(&mut self, column: &str, value: &str) -> &mut Self

Add a less-than-or-equal condition: {column} <= ?.

Source

pub fn where_regex(&mut self, column: &str, pattern: &str) -> &mut Self

Add a regex match condition using DuckDB’s regexp_matches.

Generates: regexp_matches({column}, ?)

Source

pub fn where_fuzzy( &mut self, column: &str, value: &str, threshold: f64, ) -> &mut Self

Add a fuzzy string match condition using Jaro-Winkler similarity.

Generates: jaro_winkler_similarity({column}, ?) > {threshold}

The threshold must be between 0.0 and 1.0 (inclusive).

Source

pub fn where_or(&mut self, conditions: &[(&str, &str)]) -> &mut Self

Add OR-combined conditions.

Each condition is a (sql_fragment, param_value) tuple where the fragment uses ? as a placeholder.

§Example
use mtgjson_sdk::SqlBuilder;
let mut builder = SqlBuilder::new("cards");
builder.where_or(&[("name = ?", "Bolt"), ("name = ?", "Counter")]);
// -> WHERE (name = ? OR name = ?)
Source

pub fn group_by(&mut self, cols: &[&str]) -> &mut Self

Add GROUP BY columns.

Source

pub fn having(&mut self, condition: &str, params: &[&str]) -> &mut Self

Add a HAVING condition with ? placeholders.

Source

pub fn order_by(&mut self, clauses: &[&str]) -> &mut Self

Add ORDER BY clauses (e.g. "name ASC", "price DESC").

Source

pub fn limit(&mut self, n: usize) -> &mut Self

Set the maximum number of rows to return.

Source

pub fn offset(&mut self, n: usize) -> &mut Self

Set the number of rows to skip before returning results.

Source

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

Build the final SQL string and parameter list.

Returns a tuple of (sql_string, params_list) ready for execution.

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. 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
Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,