pub struct PaginatedQueryBuilder<'q, T, A>{ /* private fields */ }
Implementations§
Source§impl<'q, T, A> PaginatedQueryBuilder<'q, T, A>
A builder for constructing and executing paginated queries.
impl<'q, T, A> PaginatedQueryBuilder<'q, T, A>
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 argumentsT
: The model type that the query will returnA
: The type of the query arguments
§Generic Constraints
T
: Must be deserializable from Postgres rows (FromRow
),Send
, andUnpin
A
: Must be compatible with Postgres arguments andSend
(Attention: Only Pool<Postgres>
is supported at the moment)
Sourcepub fn new(query: QueryAs<'q, Postgres, T, A>) -> Self
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);
pub fn with_query_builder( self, build_query_fn: fn(&QueryParams<'_, T>) -> (Vec<String>, PgArguments), ) -> Self
pub fn with_params(self, params: impl Into<QueryParams<'q, T>>) -> Self
Sourcepub fn disable_totals_count(self) -> Self
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
Sourcepub async fn fetch_paginated(
self,
pool: &Pool<Postgres>,
) -> Result<PaginatedResponse<T>, Error>
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: OnlyPool<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>
impl<'q, T, A> Sync for PaginatedQueryBuilder<'q, T, A>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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