FastSqlBuilder

Struct FastSqlBuilder 

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

An optimized SQL builder that uses a single String buffer.

This builder is more efficient than Sql for complex queries because:

  • Uses a single pre-allocated String instead of Vec
  • Uses write! macro instead of format! + push
  • Provides batch placeholder generation for IN clauses

§Examples

use prax_query::sql::{FastSqlBuilder, DatabaseType, QueryCapacity};

// Simple query with pre-allocated capacity
let mut builder = FastSqlBuilder::with_capacity(
    DatabaseType::PostgreSQL,
    QueryCapacity::SimpleSelect
);
builder.push_str("SELECT * FROM users WHERE id = ");
builder.bind(42i64);
let (sql, params) = builder.build();
assert_eq!(sql, "SELECT * FROM users WHERE id = $1");

// Complex query with multiple bindings
let mut builder = FastSqlBuilder::with_capacity(
    DatabaseType::PostgreSQL,
    QueryCapacity::SelectWithFilters(3)
);
builder.push_str("SELECT * FROM users WHERE active = ");
builder.bind(true);
builder.push_str(" AND age > ");
builder.bind(18i64);
builder.push_str(" ORDER BY created_at LIMIT ");
builder.bind(10i64);
let (sql, _) = builder.build();
assert!(sql.contains("$1") && sql.contains("$2") && sql.contains("$3"));

Implementations§

Source§

impl FastSqlBuilder

Source

pub fn new(db_type: DatabaseType) -> Self

Create a new builder with the specified database type.

Source

pub fn with_capacity(db_type: DatabaseType, capacity: QueryCapacity) -> Self

Create a new builder with pre-allocated capacity.

Source

pub fn postgres(capacity: QueryCapacity) -> Self

Create a PostgreSQL builder with pre-allocated capacity.

Source

pub fn mysql(capacity: QueryCapacity) -> Self

Create a MySQL builder with pre-allocated capacity.

Source

pub fn sqlite(capacity: QueryCapacity) -> Self

Create a SQLite builder with pre-allocated capacity.

Source

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

Push a string slice directly (zero allocation).

Source

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

Push a single character.

Source

pub fn bind(&mut self, value: impl Into<FilterValue>) -> &mut Self

Bind a parameter and append its placeholder.

Source

pub fn push_bind(&mut self, s: &str, value: impl Into<FilterValue>) -> &mut Self

Push a string and bind a value.

Source

pub fn bind_in_clause( &mut self, values: impl IntoIterator<Item = FilterValue>, ) -> &mut Self

Generate placeholders for an IN clause efficiently.

This is much faster than calling bind() in a loop because it:

  • Uses pre-computed placeholder patterns for common sizes
  • Pre-calculates the total string length for larger sizes
  • Generates all placeholders in one pass
§Examples
use prax_query::sql::{FastSqlBuilder, DatabaseType, QueryCapacity};
use prax_query::filter::FilterValue;

let mut builder = FastSqlBuilder::postgres(QueryCapacity::Custom(128));
builder.push_str("SELECT * FROM users WHERE id IN (");

let values: Vec<FilterValue> = vec![1i64, 2, 3, 4, 5].into_iter()
    .map(FilterValue::Int)
    .collect();
builder.bind_in_clause(values);
builder.push_char(')');

let (sql, params) = builder.build();
assert_eq!(sql, "SELECT * FROM users WHERE id IN ($1, $2, $3, $4, $5)");
assert_eq!(params.len(), 5);
Source

pub fn bind_in_slice<T: Into<FilterValue> + Clone>( &mut self, values: &[T], ) -> &mut Self

Bind a slice of values for an IN clause without collecting.

This is more efficient than bind_in_clause when you already have a slice, as it avoids collecting into a Vec first.

§Examples
use prax_query::sql::{FastSqlBuilder, DatabaseType, QueryCapacity};

let mut builder = FastSqlBuilder::postgres(QueryCapacity::Custom(128));
builder.push_str("SELECT * FROM users WHERE id IN (");

let ids: &[i64] = &[1, 2, 3, 4, 5];
builder.bind_in_slice(ids);
builder.push_char(')');

let (sql, params) = builder.build();
assert_eq!(sql, "SELECT * FROM users WHERE id IN ($1, $2, $3, $4, $5)");
assert_eq!(params.len(), 5);
Source

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

Write formatted content using the write! macro.

This is more efficient than format!() + push_str() as it writes directly to the buffer without intermediate allocation.

Source

pub fn push_identifier(&mut self, name: &str) -> &mut Self

Push an identifier, quoting if necessary.

Source

pub fn push_if(&mut self, condition: bool, s: &str) -> &mut Self

Push conditionally.

Source

pub fn bind_if( &mut self, condition: bool, value: impl Into<FilterValue>, ) -> &mut Self

Bind conditionally.

Source

pub fn sql(&self) -> &str

Get the current SQL string.

Source

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

Get the current parameters.

Source

pub fn param_count(&self) -> usize

Get the number of parameters.

Source

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

Build the final SQL string and parameters.

Source

pub fn build_sql(self) -> String

Build and return only the SQL string.

Trait Implementations§

Source§

impl Clone for FastSqlBuilder

Source§

fn clone(&self) -> FastSqlBuilder

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FastSqlBuilder

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

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

§

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
§

impl<T> From<T> for T

§

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
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

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

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

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