Skip to main content

PostgresWhereGenerator

Struct PostgresWhereGenerator 

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

  1. Single-threaded usage: Each WHERE generator is created for a single query execution and isn’t shared across async tasks.

  2. Reset per call: The counter is reset at the start of generate(), ensuring no state leakage between calls.

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

Source

pub fn new() -> Self

Create new PostgreSQL WHERE generator.

Source

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

pub fn generate(&self, clause: &WhereClause) -> Result<(String, Vec<Value>)>

Generate SQL WHERE clause and parameters.

§Arguments
  • clause - WHERE clause AST
§Returns

Returns tuple of (SQL string, parameter values).

§Errors

Returns FraiseQLError::Validation if clause is invalid.

Trait Implementations§

Source§

impl Default for PostgresWhereGenerator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl ExtendedOperatorHandler for PostgresWhereGenerator

Source§

fn generate_extended_sql( &self, operator: &ExtendedOperator, field_sql: &str, params: &mut Vec<Value>, ) -> Result<String>

Generate database-specific SQL for an extended operator. Read more

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

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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