Skip to main content

PaginatedQueryBuilder

Struct PaginatedQueryBuilder 

Source
pub struct PaginatedQueryBuilder<'q, T, DB, A>
where DB: Database, T: for<'r> FromRow<'r, <DB as Database>::Row> + Send + Unpin,
{ /* private fields */ }

Implementations§

Source§

impl<'q, T, DB, A> PaginatedQueryBuilder<'q, T, DB, A>
where DB: Database, T: for<'r> FromRow<'r, <DB as Database>::Row> + Send + Unpin + Serialize + Default, A: 'q + IntoArguments<'q, DB> + Send, DB::Arguments<'q>: IntoArguments<'q, DB>, for<'c> &'c Pool<DB>: Executor<'c, Database = DB>, usize: ColumnIndex<<DB as Database>::Row>, i64: Type<DB> + for<'r> Decode<'r, DB> + Send + Unpin,

A builder for constructing and executing paginated queries.

This builder provides a fluent interface for creating paginated queries. For more examples explore examples/paginated_query_builder_advanced_examples.rs

§Type Parameters

  • 'q: The lifetime of the query and its arguments
  • T: The model type that the query will return
  • A: The type of the query arguments

§Generic Constraints

  • T: Must be deserializable from Postgres rows (FromRow), Send, and Unpin
  • A: Must be compatible with Postgres arguments and Send
Source

pub fn new<F>(query: QueryAs<'q, DB, T, A>, build_query_fn: F) -> Self
where F: for<'p> Fn(&'p QueryParams<'_, T>) -> (Vec<String>, DB::Arguments<'p>) + Send + Sync + 'static,

Creates a new PaginatedQueryBuilder with default settings.

§Arguments
  • query - The base query to paginate
  • build_query_fn - Function to build query conditions and arguments
§Default Settings
  • Totals calculation is enabled
  • Uses default query parameters
§Examples
use sqlx::{FromRow, Postgres};
use serde::{Serialize};
use sqlx_paginated::PaginatedQueryBuilder;

#[derive(Serialize, FromRow, Default)]
struct UserExample {
    name: String
}
let base_query = sqlx::query_as::<Postgres, UserExample>("SELECT * FROM users");
let builder = PaginatedQueryBuilder::new(base_query, |params| {
    sqlx_paginated::QueryBuilder::<UserExample, Postgres>::new()
        .with_search(params)
        .with_filters(params)
        .with_date_range(params)
        .build()
});
Source

pub fn with_query_builder<F>(self, build_query_fn: F) -> Self
where F: for<'p> Fn(&'p QueryParams<'_, T>) -> (Vec<String>, DB::Arguments<'p>) + Send + Sync + 'static,

Source

pub fn with_params(self, params: impl Into<QueryParams<'q, T>>) -> Self

Source

pub fn disable_totals_count(self) -> Self

Disables the calculation of total record count.

When disabled, the response will not include total count or total pages. This can improve query performance for large datasets where the total count is not needed.

§Returns

Returns self for method chaining

Source§

impl<'q, T, A> PaginatedQueryBuilder<'q, T, Postgres, A>
where T: for<'r> FromRow<'r, <Postgres as Database>::Row> + Send + Unpin + Serialize + Default, A: 'q + IntoArguments<'q, Postgres> + Send,

Source

pub fn new_with_defaults(query: QueryAs<'q, Postgres, T, A>) -> Self

Creates a new PaginatedQueryBuilder for PostgreSQL with default settings.

§Arguments
  • query - The base query to paginate
§Default Settings
  • Totals calculation is enabled
  • Uses default query parameters
  • Uses safe default query building function
§Examples
use sqlx::{FromRow, Postgres};
use serde::{Serialize};
use sqlx_paginated::PaginatedQueryBuilder;

#[derive(Serialize, FromRow, Default)]
struct UserExample {
    name: String
}
let base_query = sqlx::query_as::<Postgres, UserExample>("SELECT * FROM users");
let builder = PaginatedQueryBuilder::<UserExample, Postgres, _>::new_with_defaults(base_query);
Source

pub async fn fetch_paginated( self, pool: &PgPool, ) -> Result<PaginatedResponse<T>, Error>

Executes the paginated query and returns the results.

§Arguments
  • pool - PostgreSQL database connection pool
§Returns

Returns a Result containing a PaginatedResponse<T> with:

  • Records for the requested page
  • Optional Pagination information (if enabled)
  • Optional total count and total pages (if enabled)
§Errors

Returns sqlx::Error if the query execution fails

§Example
use sqlx::{FromRow, PgPool, Postgres};
use serde::Serialize;
use sqlx_paginated::{PaginatedQueryBuilder, QueryParamsBuilder};

#[derive(Serialize, FromRow, Default)]
struct User {
    id: i32,
    name: String,
}

let params = QueryParamsBuilder::<User>::new()
    .with_pagination(1, 10)
    .build();

let result = PaginatedQueryBuilder::<User, Postgres, _>::new_with_defaults(
    sqlx::query_as::<Postgres, User>("SELECT * FROM users")
)
.with_params(params)
.fetch_paginated(&pool)
.await?;

Auto Trait Implementations§

§

impl<'q, T, DB, A> Freeze for PaginatedQueryBuilder<'q, T, DB, A>
where A: Freeze,

§

impl<'q, T, DB, A> !RefUnwindSafe for PaginatedQueryBuilder<'q, T, DB, A>

§

impl<'q, T, DB, A> Send for PaginatedQueryBuilder<'q, T, DB, A>
where T: Sync, A: Send,

§

impl<'q, T, DB, A> Sync for PaginatedQueryBuilder<'q, T, DB, A>
where T: Sync, DB: Sync, A: Sync,

§

impl<'q, T, DB, A> Unpin for PaginatedQueryBuilder<'q, T, DB, A>
where DB: Unpin, A: Unpin,

§

impl<'q, T, DB, A> !UnwindSafe for PaginatedQueryBuilder<'q, T, DB, A>

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,