Struct PaginatedQueryBuilder

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

Implementations§

Source§

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

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

(Attention: Only Pool<Postgres> is supported at the moment)

Source

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

Creates a new PaginatedQueryBuilder 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::<_, UserExample>("SELECT * FROM users");
let builder = PaginatedQueryBuilder::new(base_query);
Source

pub fn with_query_builder( self, build_query_fn: fn(&QueryParams<'_, T>) -> (Vec<String>, PgArguments), ) -> Self

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

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

Executes the paginated query and returns the results.

§Arguments
  • pool - Database connection pool (Attention: Only Pool<Postgres> is supported at the moment)
§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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<'q, T, A> !UnwindSafe for PaginatedQueryBuilder<'q, T, 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,