pub struct PaginatedQueryBuilder<'q, T, DB, A>{ /* 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.
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 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, andUnpinA: Must be compatible with Postgres arguments andSend
Sourcepub fn new<F>(query: QueryAs<'q, DB, T, A>, build_query_fn: F) -> Self
pub fn new<F>(query: QueryAs<'q, DB, T, A>, build_query_fn: F) -> Self
Creates a new PaginatedQueryBuilder with default settings.
§Arguments
query- The base query to paginatebuild_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()
});pub fn with_query_builder<F>(self, build_query_fn: F) -> 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
Source§impl<'q, T, A> PaginatedQueryBuilder<'q, T, Postgres, A>
impl<'q, T, A> PaginatedQueryBuilder<'q, T, Postgres, A>
Sourcepub fn new_with_defaults(query: QueryAs<'q, Postgres, T, A>) -> Self
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);Sourcepub async fn fetch_paginated(
self,
pool: &PgPool,
) -> Result<PaginatedResponse<T>, Error>
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>
impl<'q, T, DB, A> Sync for PaginatedQueryBuilder<'q, T, DB, A>
impl<'q, T, DB, A> Unpin for PaginatedQueryBuilder<'q, T, DB, A>
impl<'q, T, DB, A> !UnwindSafe for PaginatedQueryBuilder<'q, T, DB, 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