Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder<E>
where E: Entity,
{ /* private fields */ }
Expand description

Query Builder for constructing type-safe SQL queries

§Type Parameters

  • E - The Entity type this query is built for

§Example

use ormkit::query::{QueryBuilder, FilterOp, Order};

let query = QueryBuilder::<User>::new()
    .filter("email", FilterOp::Eq, "user@example.com")
    .order("created_at", Order::Desc)
    .limit(10)
    .build();

Implementations§

Source§

impl<E> QueryBuilder<E>
where E: Entity,

Source

pub fn new() -> Self

Create a new QueryBuilder for entity E

Source

pub fn select(self, column: impl Into<String>) -> Self

Add a SELECT clause

§Arguments
  • column - The column name to select
Source

pub fn select_all( self, columns: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Add multiple SELECT clauses

§Arguments
  • columns - Iterator of column names to select
Source

pub fn select_raw(self, expr: impl Into<String>) -> Self

Add a raw SELECT expression

§Arguments
  • expr - The raw SQL expression
§Warning

This bypasses type safety. Use with caution.

Source

pub fn filter( self, column: impl Into<String>, op: FilterOp, value: impl Into<String>, ) -> Self

Add a WHERE filter

§Arguments
  • column - The column name to filter on
  • op - The filter operator
  • value - The filter value
§Example
let query = QueryBuilder::<User>::new()
    .filter("age", FilterOp::Gt, 18);
Source

pub fn filter_in( self, column: impl Into<String>, op: FilterOp, values: Vec<impl Into<String>>, ) -> Self

Add a WHERE filter with multiple values (for IN/NOT IN)

§Arguments
  • column - The column name to filter on
  • op - The filter operator (should be In or NotIn)
  • values - The filter values
Source

pub fn filter_between( self, column: impl Into<String>, low: impl Into<String>, high: impl Into<String>, ) -> Self

Add a WHERE filter with a range (for BETWEEN)

§Arguments
  • column - The column name to filter on
  • low - The low value
  • high - The high value
Source

pub fn filter_raw(self, expr: impl Into<String>) -> Self

Add a raw WHERE filter

§Arguments
  • expr - The raw SQL expression
§Warning

This bypasses type safety. Use with caution and never interpolate user input directly.

Source

pub fn join( self, join_type: JoinType, table: impl Into<String>, on_column: impl Into<String>, on_foreign_column: impl Into<String>, ) -> Self

Add a JOIN clause

§Arguments
  • join_type - The type of join
  • table - The table to join
  • on_column - The column in the current table
  • on_foreign_column - The column in the joined table
Source

pub fn inner_join( self, table: impl Into<String>, on_column: impl Into<String>, on_foreign_column: impl Into<String>, ) -> Self

Add an INNER JOIN

§Arguments
  • table - The table to join
  • on_column - The column in the current table
  • on_foreign_column - The column in the joined table
Source

pub fn left_join( self, table: impl Into<String>, on_column: impl Into<String>, on_foreign_column: impl Into<String>, ) -> Self

Add a LEFT JOIN

§Arguments
  • table - The table to join
  • on_column - The column in the current table
  • on_foreign_column - The column in the joined table
Source

pub fn order(self, column: impl Into<String>, order: Order) -> Self

Add an ORDER BY clause

§Arguments
  • column - The column name to order by
  • order - The sort direction
Source

pub fn order_with_table( self, table: impl Into<String>, column: impl Into<String>, order: Order, ) -> Self

Add an ORDER BY clause with explicit table

§Arguments
  • table - The table name
  • column - The column name
  • order - The sort direction
Source

pub fn limit(self, limit: u64) -> Self

Add a LIMIT clause

§Arguments
  • limit - The maximum number of rows to return
Source

pub fn offset(self, offset: u64) -> Self

Add an OFFSET clause

§Arguments
  • offset - The number of rows to skip
Source

pub fn distinct(self) -> Self

Add DISTINCT to the SELECT

Source

pub fn build(self) -> BuiltQuery

Build the query

Returns a BuiltQuery that can be executed

Source

pub async fn fetch_all<T>(self, pool: &PgPool) -> Result<Vec<T>, Error>
where T: for<'r> FromRow<'r, PgRow> + Unpin + Send,

Build and execute the query, fetching all results

§Arguments
  • pool - The database connection pool
§Returns

A vector of results

Source

pub async fn fetch_one<T>(self, pool: &PgPool) -> Result<T, Error>
where T: for<'r> FromRow<'r, PgRow> + Unpin + Send,

Build and execute the query, fetching the first result

§Arguments
  • pool - The database connection pool
§Returns

The first result or an error

Source

pub async fn fetch_optional<T>(self, pool: &PgPool) -> Result<Option<T>, Error>
where T: for<'r> FromRow<'r, PgRow> + Unpin + Send,

Build and execute the query, fetching the first result or None

§Arguments
  • pool - The database connection pool
§Returns

An optional result

Trait Implementations§

Source§

impl<E> Clone for QueryBuilder<E>
where E: Entity + Clone,

Source§

fn clone(&self) -> QueryBuilder<E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E> Debug for QueryBuilder<E>
where E: Entity + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<E> Default for QueryBuilder<E>
where E: Entity,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<E> Freeze for QueryBuilder<E>

§

impl<E> RefUnwindSafe for QueryBuilder<E>
where E: RefUnwindSafe,

§

impl<E> Send for QueryBuilder<E>

§

impl<E> Sync for QueryBuilder<E>

§

impl<E> Unpin for QueryBuilder<E>
where E: Unpin,

§

impl<E> UnwindSafe for QueryBuilder<E>
where E: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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