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
impl FastSqlBuilder
Sourcepub fn new(db_type: DatabaseType) -> Self
pub fn new(db_type: DatabaseType) -> Self
Create a new builder with the specified database type.
Sourcepub fn with_capacity(db_type: DatabaseType, capacity: QueryCapacity) -> Self
pub fn with_capacity(db_type: DatabaseType, capacity: QueryCapacity) -> Self
Create a new builder with pre-allocated capacity.
Sourcepub fn postgres(capacity: QueryCapacity) -> Self
pub fn postgres(capacity: QueryCapacity) -> Self
Create a PostgreSQL builder with pre-allocated capacity.
Sourcepub fn mysql(capacity: QueryCapacity) -> Self
pub fn mysql(capacity: QueryCapacity) -> Self
Create a MySQL builder with pre-allocated capacity.
Sourcepub fn sqlite(capacity: QueryCapacity) -> Self
pub fn sqlite(capacity: QueryCapacity) -> Self
Create a SQLite builder with pre-allocated capacity.
Sourcepub fn push_str(&mut self, s: &str) -> &mut Self
pub fn push_str(&mut self, s: &str) -> &mut Self
Push a string slice directly (zero allocation).
Sourcepub fn bind(&mut self, value: impl Into<FilterValue>) -> &mut Self
pub fn bind(&mut self, value: impl Into<FilterValue>) -> &mut Self
Bind a parameter and append its placeholder.
Sourcepub fn push_bind(&mut self, s: &str, value: impl Into<FilterValue>) -> &mut Self
pub fn push_bind(&mut self, s: &str, value: impl Into<FilterValue>) -> &mut Self
Push a string and bind a value.
Sourcepub fn bind_in_clause(
&mut self,
values: impl IntoIterator<Item = FilterValue>,
) -> &mut Self
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);Sourcepub fn bind_in_slice<T: Into<FilterValue> + Clone>(
&mut self,
values: &[T],
) -> &mut Self
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);Sourcepub fn write_fmt(&mut self, args: Arguments<'_>) -> &mut Self
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.
Sourcepub fn push_identifier(&mut self, name: &str) -> &mut Self
pub fn push_identifier(&mut self, name: &str) -> &mut Self
Push an identifier, quoting if necessary.
Sourcepub fn bind_if(
&mut self,
condition: bool,
value: impl Into<FilterValue>,
) -> &mut Self
pub fn bind_if( &mut self, condition: bool, value: impl Into<FilterValue>, ) -> &mut Self
Bind conditionally.
Sourcepub fn params(&self) -> &[FilterValue]
pub fn params(&self) -> &[FilterValue]
Get the current parameters.
Sourcepub fn param_count(&self) -> usize
pub fn param_count(&self) -> usize
Get the number of parameters.
Sourcepub fn build(self) -> (String, Vec<FilterValue>)
pub fn build(self) -> (String, Vec<FilterValue>)
Build the final SQL string and parameters.
Trait Implementations§
Source§impl Clone for FastSqlBuilder
impl Clone for FastSqlBuilder
Source§fn clone(&self) -> FastSqlBuilder
fn clone(&self) -> FastSqlBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more