1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//! Pagination types for query results.
//!
//! This module provides types for implementing both offset-based and cursor-based pagination.
//!
//! # Offset-Based Pagination (Skip/Take)
//!
//! Simple pagination using skip and take:
//!
//! ```rust
//! use prax_query::Pagination;
//!
//! // Skip 10, take 20
//! let pagination = Pagination::new()
//! .skip(10)
//! .take(20);
//!
//! assert_eq!(pagination.skip, Some(10));
//! assert_eq!(pagination.take, Some(20));
//! assert_eq!(pagination.to_sql(), "LIMIT 20 OFFSET 10");
//!
//! // First N records
//! let first_10 = Pagination::first(10);
//! assert_eq!(first_10.to_sql(), "LIMIT 10");
//!
//! // Page-based pagination (1-indexed)
//! let page_3 = Pagination::page(3, 25); // Page 3 with 25 items per page
//! assert_eq!(page_3.skip, Some(50)); // Skip first 2 pages (50 items)
//! assert_eq!(page_3.take, Some(25));
//! ```
//!
//! # Checking Pagination State
//!
//! ```rust
//! use prax_query::Pagination;
//!
//! let empty = Pagination::new();
//! assert!(empty.is_empty());
//!
//! let with_limit = Pagination::new().take(10);
//! assert!(!with_limit.is_empty());
//! ```
use serde::{Deserialize, Serialize};
use std::fmt::Write;
/// Pagination configuration for queries.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Pagination {
/// Number of records to skip.
pub skip: Option<u64>,
/// Maximum number of records to take.
pub take: Option<u64>,
/// Cursor for cursor-based pagination.
pub cursor: Option<Cursor>,
}
impl Pagination {
/// Create a new pagination with no limits.
pub fn new() -> Self {
Self::default()
}
/// Set the number of records to skip.
pub fn skip(mut self, skip: u64) -> Self {
self.skip = Some(skip);
self
}
/// Set the maximum number of records to take.
pub fn take(mut self, take: u64) -> Self {
self.take = Some(take);
self
}
/// Set cursor for cursor-based pagination.
pub fn cursor(mut self, cursor: Cursor) -> Self {
self.cursor = Some(cursor);
self
}
/// Check if pagination is specified.
pub fn is_empty(&self) -> bool {
self.skip.is_none() && self.take.is_none() && self.cursor.is_none()
}
/// Generate SQL LIMIT/OFFSET clause.
///
/// Optimized to avoid intermediate allocations by writing directly to a buffer.
pub fn to_sql(&self) -> String {
// Estimate capacity: "LIMIT " (6) + number (up to 20) + " OFFSET " (8) + number (up to 20)
let mut sql = String::with_capacity(54);
if let Some(take) = self.take {
let _ = write!(sql, "LIMIT {}", take);
}
if let Some(skip) = self.skip {
if !sql.is_empty() {
sql.push(' ');
}
let _ = write!(sql, "OFFSET {}", skip);
}
sql
}
/// Write SQL LIMIT/OFFSET clause directly to a buffer (zero allocation).
///
/// # Examples
///
/// ```rust
/// use prax_query::Pagination;
///
/// let pagination = Pagination::new().skip(10).take(20);
/// let mut buffer = String::with_capacity(64);
/// buffer.push_str("SELECT * FROM users ");
/// pagination.write_sql(&mut buffer);
/// assert!(buffer.ends_with("LIMIT 20 OFFSET 10"));
/// ```
#[inline]
pub fn write_sql(&self, buffer: &mut String) {
if let Some(take) = self.take {
let _ = write!(buffer, "LIMIT {}", take);
}
if let Some(skip) = self.skip {
if self.take.is_some() {
buffer.push(' ');
}
let _ = write!(buffer, "OFFSET {}", skip);
}
}
/// Get pagination for the first N records.
pub fn first(n: u64) -> Self {
Self::new().take(n)
}
/// Get pagination for a page (1-indexed).
pub fn page(page: u64, page_size: u64) -> Self {
let skip = (page.saturating_sub(1)) * page_size;
Self::new().skip(skip).take(page_size)
}
}
/// Cursor for cursor-based pagination.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Cursor {
/// The column to use for cursor.
pub column: String,
/// The cursor value.
pub value: CursorValue,
/// Direction of pagination.
pub direction: CursorDirection,
}
impl Cursor {
/// Create a new cursor.
pub fn new(column: impl Into<String>, value: CursorValue, direction: CursorDirection) -> Self {
Self {
column: column.into(),
value,
direction,
}
}
/// Create a cursor for fetching records after this value.
pub fn after(column: impl Into<String>, value: impl Into<CursorValue>) -> Self {
Self::new(column, value.into(), CursorDirection::After)
}
/// Create a cursor for fetching records before this value.
pub fn before(column: impl Into<String>, value: impl Into<CursorValue>) -> Self {
Self::new(column, value.into(), CursorDirection::Before)
}
/// Generate the WHERE clause for cursor-based pagination.
///
/// Optimized to write directly to a pre-sized buffer.
pub fn to_sql_condition(&self) -> String {
// Estimate: column + " " + op + " $cursor" = column.len() + 10
let mut sql = String::with_capacity(self.column.len() + 12);
sql.push_str(&self.column);
sql.push(' ');
sql.push_str(match self.direction {
CursorDirection::After => "> $cursor",
CursorDirection::Before => "< $cursor",
});
sql
}
/// Write the cursor condition directly to a buffer (zero allocation).
#[inline]
pub fn write_sql_condition(&self, buffer: &mut String) {
buffer.push_str(&self.column);
buffer.push(' ');
buffer.push_str(match self.direction {
CursorDirection::After => "> $cursor",
CursorDirection::Before => "< $cursor",
});
}
/// Get the operator for this cursor direction.
#[inline]
pub const fn operator(&self) -> &'static str {
match self.direction {
CursorDirection::After => ">",
CursorDirection::Before => "<",
}
}
}
/// Cursor value type.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CursorValue {
/// Integer cursor (e.g., auto-increment ID).
Int(i64),
/// String cursor (e.g., UUID).
String(String),
}
impl From<i32> for CursorValue {
fn from(v: i32) -> Self {
Self::Int(v as i64)
}
}
impl From<i64> for CursorValue {
fn from(v: i64) -> Self {
Self::Int(v)
}
}
impl From<String> for CursorValue {
fn from(v: String) -> Self {
Self::String(v)
}
}
impl From<&str> for CursorValue {
fn from(v: &str) -> Self {
Self::String(v.to_string())
}
}
/// Direction for cursor-based pagination.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CursorDirection {
/// Fetch records after the cursor.
After,
/// Fetch records before the cursor.
Before,
}
/// Result of a paginated query with metadata.
#[derive(Debug, Clone)]
pub struct PaginatedResult<T> {
/// The query results.
pub data: Vec<T>,
/// Whether there are more records after these.
pub has_next: bool,
/// Whether there are more records before these.
pub has_previous: bool,
/// The cursor for the next page (last item's cursor).
pub next_cursor: Option<CursorValue>,
/// The cursor for the previous page (first item's cursor).
pub previous_cursor: Option<CursorValue>,
/// Total count (if requested).
pub total_count: Option<u64>,
}
impl<T> PaginatedResult<T> {
/// Create a new paginated result.
pub fn new(data: Vec<T>) -> Self {
Self {
data,
has_next: false,
has_previous: false,
next_cursor: None,
previous_cursor: None,
total_count: None,
}
}
/// Set pagination metadata.
pub fn with_pagination(mut self, has_next: bool, has_previous: bool) -> Self {
self.has_next = has_next;
self.has_previous = has_previous;
self
}
/// Set total count.
pub fn with_total(mut self, total: u64) -> Self {
self.total_count = Some(total);
self
}
/// Set cursors.
pub fn with_cursors(
mut self,
next: Option<CursorValue>,
previous: Option<CursorValue>,
) -> Self {
self.next_cursor = next;
self.previous_cursor = previous;
self
}
/// Get the number of records in this result.
pub fn len(&self) -> usize {
self.data.len()
}
/// Check if the result is empty.
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
}
impl<T> IntoIterator for PaginatedResult<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pagination_skip_take() {
let pagination = Pagination::new().skip(10).take(20);
assert_eq!(pagination.to_sql(), "LIMIT 20 OFFSET 10");
}
#[test]
fn test_pagination_page() {
let pagination = Pagination::page(3, 10);
assert_eq!(pagination.skip, Some(20));
assert_eq!(pagination.take, Some(10));
}
#[test]
fn test_cursor_after() {
let cursor = Cursor::after("id", 100i64);
assert_eq!(cursor.to_sql_condition(), "id > $cursor");
}
#[test]
fn test_cursor_before() {
let cursor = Cursor::before("id", 100i64);
assert_eq!(cursor.to_sql_condition(), "id < $cursor");
}
#[test]
fn test_paginated_result() {
let result = PaginatedResult::new(vec![1, 2, 3])
.with_pagination(true, false)
.with_total(100);
assert_eq!(result.len(), 3);
assert!(result.has_next);
assert!(!result.has_previous);
assert_eq!(result.total_count, Some(100));
}
}