pub struct PostgresWhereGenerator { /* private fields */ }Expand description
PostgreSQL WHERE clause generator.
Converts WhereClause AST to PostgreSQL SQL with parameterized queries.
§Interior Mutability Pattern
This struct uses Cell<usize> for the parameter counter. This is safe because:
-
Single-threaded usage: Each WHERE generator is created for a single query execution and isn’t shared across async tasks.
-
Reset per call: The counter is reset at the start of
generate(), ensuring no state leakage between calls. -
Performance: Avoids mutex overhead for a simple counter that needs frequent updates.
§If Shared Across Tasks
If this generator were Arc-shared across multiple async tasks, replace
Cell<usize> with AtomicUsize to prevent data races:
// Instead of: Cell<usize>
// Use: AtomicUsize
param_counter: std::sync::atomic::AtomicUsize::new(0),
// Then use compare_and_swap or fetch_add operations§Indexed Column Optimization
When an IndexedColumnsCache is provided, the generator checks if nested paths
have corresponding indexed columns. If found, it uses the indexed column directly
instead of JSONB extraction, enabling index usage.
For example, with items__product__category__code indexed:
- Without cache:
data->'items'->'product'->'category'->>'code' = $1 - With cache:
items__product__category__code = $1
§Example
use fraiseql_core::db::postgres::PostgresWhereGenerator;
use fraiseql_core::db::{WhereClause, WhereOperator};
use serde_json::json;
let generator = PostgresWhereGenerator::new();
let clause = WhereClause::Field {
path: vec!["email".to_string()],
operator: WhereOperator::Icontains,
value: json!("example.com"),
};
let (sql, params) = generator.generate(&clause).expect("Failed to generate SQL");
// sql: "data->>'email' ILIKE '%' || $1 || '%'"
// params: ["example.com"]Implementations§
Source§impl PostgresWhereGenerator
impl PostgresWhereGenerator
Sourcepub fn with_indexed_columns(indexed_columns: Arc<HashSet<String>>) -> Self
pub fn with_indexed_columns(indexed_columns: Arc<HashSet<String>>) -> Self
Create new PostgreSQL WHERE generator with indexed columns for a view.
When indexed columns are provided, the generator will use them instead of JSONB extraction for nested paths that have corresponding indexed columns.
§Arguments
indexed_columns- Set of indexed column names for the current view
§Example
use fraiseql_core::db::postgres::PostgresWhereGenerator;
use std::collections::HashSet;
use std::sync::Arc;
let mut columns = HashSet::new();
columns.insert("items__product__category__code".to_string());
let generator = PostgresWhereGenerator::with_indexed_columns(Arc::new(columns));