Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder {
    pub sql: String,
    pub params: Vec<Value>,
    pub param_index: usize,
    pub tables: Vec<String>,
    pub current_schema: String,
}
Expand description

Builder for generating parameterized PostgreSQL SQL queries.

Converts PostgREST AST types into safe, parameterized SQL queries. All user input is placed in parameters to prevent SQL injection.

§Examples

use postgrest_parser::{QueryBuilder, parse_query_string};

let params = parse_query_string("age=gte.18&status=eq.active").unwrap();
let mut builder = QueryBuilder::new();
let result = builder.build_select("users", &params).unwrap();

assert!(result.query.contains("SELECT"));
assert!(result.query.contains("WHERE"));
assert_eq!(result.params.len(), 2);

Fields§

§sql: String

The SQL query being built

§params: Vec<Value>

Parameter values for the query

§param_index: usize

Current parameter index (for $1, $2, etc.)

§tables: Vec<String>

Tables referenced in the query

§current_schema: String

Current schema being queried (for relation resolution)

Implementations§

Source§

impl QueryBuilder

Source

pub fn new() -> Self

Creates a new empty query builder.

Source

pub fn with_schema(self, schema: impl Into<String>) -> Self

Sets the current schema

Source

pub fn build_select( &mut self, table: &str, params: &ParsedParams, ) -> Result<QueryResult, SqlError>

Builds a SELECT query from parsed parameters.

§Examples
use postgrest_parser::{QueryBuilder, ParsedParams, parse_filter, LogicCondition};

let filter = parse_filter("age", "gte.21").unwrap();
let params = ParsedParams::new()
    .with_filters(vec![LogicCondition::Filter(filter)]);

let mut builder = QueryBuilder::new();
let result = builder.build_select("users", &params).unwrap();

assert!(result.query.contains("SELECT * FROM"));
assert!(result.query.contains("WHERE"));
Source

pub fn build_where_clause( &mut self, filters: &[LogicCondition], ) -> Result<(), SqlError>

Source§

impl QueryBuilder

Source

pub fn build_insert( &mut self, resolved_table: &ResolvedTable, params: &InsertParams, ) -> Result<QueryResult, SqlError>

Builds an INSERT query with schema-qualified table name

Source

pub fn build_update( &mut self, resolved_table: &ResolvedTable, params: &UpdateParams, ) -> Result<QueryResult, SqlError>

Builds an UPDATE query with schema-qualified table name and safety validation

Source

pub fn build_delete( &mut self, resolved_table: &ResolvedTable, params: &DeleteParams, ) -> Result<QueryResult, SqlError>

Builds a DELETE query with schema-qualified table name and safety validation

Source§

impl QueryBuilder

Source

pub fn build_rpc( &mut self, resolved_table: &ResolvedTable, params: &RpcParams, ) -> Result<QueryResult, SqlError>

Builds an RPC (function call) query with schema-qualified function name

Generates: SELECT * FROM “schema”.“function_name”(arg1 := $1, arg2 := $2) PostgREST allows filtering, ordering, and pagination of function results

Trait Implementations§

Source§

impl Default for QueryBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. 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, 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, 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.