Skip to main content

toasty_core/driver/operation/
query_pk.rs

1use super::Operation;
2use crate::{
3    schema::db::{ColumnId, IndexId, TableId},
4    stmt,
5};
6
7/// Describes how results from a [`QueryPk`] operation should be bounded.
8#[derive(Debug, Clone)]
9pub enum QueryPkLimit {
10    /// Cursor-based (keyset) pagination. Returns `page_size` items resuming
11    /// after `after`. `after = None` means the first page.
12    Cursor {
13        /// Maximum number of items to return per page.
14        page_size: i64,
15        /// Serialized key of the last item from the previous page, or `None`
16        /// for the first page.
17        after: Option<stmt::Value>,
18    },
19    /// Hard-limit with optional client-side skip. Returns up to `limit` items
20    /// after discarding the first `offset`.
21    Offset {
22        /// Maximum number of items to return.
23        limit: i64,
24        /// Number of leading items to skip before returning results.
25        offset: Option<i64>,
26    },
27}
28
29/// Queries a table by primary key (or secondary index) with optional filtering,
30/// ordering, and pagination.
31///
32/// This is the primary read operation for key-value drivers. The driver applies
33/// `pk_filter` against the index, then applies the optional post-`filter`, and
34/// returns up to `limit` rows in the requested `order`.
35///
36/// # Examples
37///
38/// ```ignore
39/// use toasty_core::driver::operation::{QueryPk, Operation};
40///
41/// let op = QueryPk {
42///     table: table_id,
43///     index: None, // query the primary key
44///     select: vec![col_a, col_b],
45///     pk_filter: pk_expr,
46///     filter: None,
47///     limit: None,
48///     order: None,
49/// };
50/// let operation: Operation = op.into();
51/// ```
52#[derive(Debug, Clone)]
53pub struct QueryPk {
54    /// The table to query.
55    pub table: TableId,
56
57    /// Index to query. `None` means the primary key; `Some(id)` means a
58    /// secondary index.
59    pub index: Option<IndexId>,
60
61    /// Which columns to include in the returned rows.
62    pub select: Vec<ColumnId>,
63
64    /// Filter expression applied against the index key columns.
65    pub pk_filter: stmt::Expr,
66
67    /// Optional post-filter applied to rows after the index scan, before
68    /// returning results to the caller.
69    pub filter: Option<stmt::Expr>,
70
71    /// Limit and pagination bounds for this query. `None` means unbounded.
72    pub limit: Option<QueryPkLimit>,
73
74    /// Sort key ordering direction for tables with a composite primary key.
75    /// `None` uses the driver's default ordering.
76    pub order: Option<stmt::Direction>,
77}
78
79impl From<QueryPk> for Operation {
80    fn from(value: QueryPk) -> Self {
81        Self::QueryPk(value)
82    }
83}