Struct QueryBuilder

Source
pub struct QueryBuilder<'q, T, DB: Database> {
    pub conditions: Vec<String>,
    pub arguments: DB::Arguments<'q>,
    /* private fields */
}

Fields§

§conditions: Vec<String>§arguments: DB::Arguments<'q>

Implementations§

Source§

impl<T> QueryBuilder<'_, T, Postgres>
where T: Default + Serialize,

Source

pub fn new() -> Self

Source§

impl<'q, T, DB> QueryBuilder<'q, T, DB>
where T: Default + Serialize, DB: Database, String: for<'a> Encode<'a, DB> + Type<DB>,

Adds search functionality to the query by creating LIKE conditions for specified columns.

§Arguments
  • params - Query parameters containing search text and columns to search in
§Details
  • Only searches in columns that are both specified and considered safe
  • Creates case-insensitive LIKE conditions with wildcards
  • Multiple search columns are combined with OR operators
  • Empty search text or no valid columns results in no conditions being added
§Returns

Returns self for method chaining

§Example
use sqlx::Postgres;
use serde::{Serialize};
use sqlx_paginated::{QueryBuilder, QueryParamsBuilder};

#[derive(Serialize, Default)]
struct UserExample {
    name: String
}

let initial_params = QueryParamsBuilder::<UserExample>::new()
        .with_search("john", vec!["name", "email"])
        .build();
let query_builder = QueryBuilder::<UserExample, Postgres>::new()
    .with_search(&initial_params)
    .build();
Source

pub fn with_filters(self, params: &'q QueryParams<'_, T>) -> Self

Adds equality filters to the query based on provided key-value pairs.

§Arguments
  • params - Query parameters containing filters as key-value pairs
§Details
  • Only applies filters for columns that exist and are considered safe
  • Automatically handles type casting based on the database dialect
  • Skips invalid columns with a warning when tracing is enabled
  • Null or empty values are ignored
§Returns

Returns self for method chaining

§Example
use sqlx::Postgres;
use serde::{Serialize};
use sqlx_paginated::{QueryBuilder, QueryParamsBuilder};

#[derive(Serialize, Default)]
struct UserExample {
    name: String
}

let initial_params = QueryParamsBuilder::<UserExample>::new()
        .with_search("john", vec!["name", "email"])
        .build();

let query_builder = QueryBuilder::<UserExample, Postgres>::new()
    .with_filters(&initial_params)
    .build();
Source

pub fn with_date_range(self, params: &'q QueryParams<'_, T>) -> Self
where DateTime<Utc>: for<'a> Encode<'a, DB> + Type<DB>,

Adds date range conditions to the query for a specified date column.

§Arguments
  • params - Query parameters containing date range information
§Type Parameters

Requires DateTime<Utc> to be encodable for the target database

§Details
  • Adds >= condition for date_after if specified
  • Adds <= condition for date_before if specified
  • Only applies to columns that exist and are considered safe
  • Skips invalid date columns with a warning when tracing is enabled
§Returns

Returns self for method chaining

§Example
use sqlx::Postgres;
use serde::{Serialize};
use chrono::{DateTime};
use sqlx_paginated::{QueryBuilder, QueryParamsBuilder, QueryParams};

#[derive(Serialize, Default)]
struct UserExample {
    name: String
}

let initial_params = QueryParamsBuilder::<UserExample>::new()
        .with_date_range(None, Some(DateTime::parse_from_rfc3339("2024-12-31T23:59:59Z").unwrap().into()), Some("deleted_at"))
        .build();
let query_builder = QueryBuilder::<UserExample, Postgres>::new()
    .with_date_range(&initial_params)
    .build();
Source

pub fn with_condition( self, column: &str, condition: impl Into<String>, value: String, ) -> Self

Adds a custom condition for a specific column with a provided operator and value.

§Arguments
  • column - The column name to apply the condition to
  • condition - The operator or condition to use (e.g., “>”, “LIKE”, etc.)
  • value - The value to compare against
§Details
  • Only applies to columns that exist and are considered safe
  • Automatically handles parameter binding
  • Skips invalid columns with a warning when tracing is enabled
§Returns

Returns self for method chaining

§Example
use sqlx::Postgres;
use serde::{Serialize};
use sqlx_paginated::{QueryBuilder};

#[derive(Serialize, Default)]
struct UserExample {
    name: String
}

let query_builder = QueryBuilder::<UserExample, Postgres>::new()
    .with_condition("age", ">", "18".to_string())
    .build();
Source

pub fn with_raw_condition(self, condition: impl Into<String>) -> Self

Adds a raw SQL condition to the query without any safety checks.

§Arguments
  • condition - Raw SQL condition to add to the query
§Safety

This method bypasses column safety checks. Use with caution to prevent SQL injection.

§Returns

Returns self for method chaining

§Example
use sqlx::Postgres;
use serde::{Serialize};
use sqlx_paginated::{QueryBuilder};

#[derive(Serialize, Default)]
struct UserExample {
    name: String
}

let query_builder = QueryBuilder::<UserExample, Postgres>::new()
    .with_raw_condition("status != 'deleted'")
    .build();
Source

pub fn with_combined_conditions<F>(self, f: F) -> Self
where F: FnOnce(&mut QueryBuilder<'_, T, DB>),

Allows adding multiple conditions using a closure.

§Arguments
  • f - Closure that takes a mutable reference to the QueryBuilder
§Details

Useful for grouping multiple conditions that are logically related

§Returns

Returns self for method chaining

§Example
use sqlx::Postgres;
use serde::{Serialize};
use sqlx_paginated::{QueryBuilder};

#[derive(Serialize, Default)]
struct UserExample {
    name: String
}
let query_builder = QueryBuilder::<UserExample, Postgres>::new()
    .with_combined_conditions(|builder| {
        builder.conditions.push("status = 'active'".to_string());
        builder.conditions.push("age >= 18".to_string());
    })
    .build();
Source

pub fn disable_protection(self) -> Self

Disables column protection for this query builder instance.

§Safety

This removes all column safety checks. Use with caution as it may expose the application to SQL injection if used with untrusted input.

§Returns

Returns self for method chaining

§Example
use sqlx::Postgres;
use serde::{Serialize};
use sqlx_paginated::{QueryBuilder};

#[derive(Serialize, Default)]
struct UserExample {
    name: String
}

let query_builder = QueryBuilder::<UserExample, Postgres>::new()
    .disable_protection()
    .with_raw_condition("custom_column = 'value'")
    .build();
Source

pub fn build(self) -> (Vec<String>, DB::Arguments<'q>)

Builds the final query conditions and arguments.

§Returns

Returns a tuple containing:

  • Vec: List of SQL conditions
  • DB::Arguments: Database-specific arguments for parameter binding
§Example
use sqlx::Postgres;
use serde::{Serialize};
use sqlx_paginated::{QueryBuilder, QueryParamsBuilder};

#[derive(Serialize, Default)]
struct UserExample {
    name: String
}

let initial_params = QueryParamsBuilder::<UserExample>::new()
        .with_search("john", vec!["name", "email"])
        .build();
let (conditions, arguments) = QueryBuilder::<UserExample, Postgres>::new()
    .with_search(&initial_params)
    .build();

Trait Implementations§

Source§

impl<T> Default for QueryBuilder<'_, T, Postgres>
where T: Default + Serialize,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'q, T, DB> Freeze for QueryBuilder<'q, T, DB>
where <DB as Database>::Arguments<'q>: Freeze,

§

impl<'q, T, DB> !RefUnwindSafe for QueryBuilder<'q, T, DB>

§

impl<'q, T, DB> !Send for QueryBuilder<'q, T, DB>

§

impl<'q, T, DB> !Sync for QueryBuilder<'q, T, DB>

§

impl<'q, T, DB> Unpin for QueryBuilder<'q, T, DB>
where <DB as Database>::Arguments<'q>: Unpin,

§

impl<'q, T, DB> !UnwindSafe for QueryBuilder<'q, T, DB>

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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
Source§

impl<T> ErasedDestructor for T
where T: 'static,