Skip to main content

reddb_server/storage/unified/dsl/builders/
table.rs

1//! Table and scan query builders
2//!
3//! Builders for table/collection queries and full scans.
4
5use std::sync::Arc;
6
7use crate::storage::query::unified::ExecutionError;
8
9use super::super::super::store::UnifiedStore;
10use super::super::execution::{execute_scan_query, execute_table_query};
11use super::super::filters::{Filter, FilterAcceptor, WhereClause};
12use super::super::types::QueryResult;
13
14/// Builder for table/collection queries
15#[derive(Debug, Clone)]
16pub struct TableQueryBuilder {
17    pub(crate) collection: String,
18    pub(crate) filters: Vec<Filter>,
19    pub(crate) order_by: Option<(String, SortOrder)>,
20    pub(crate) limit: Option<usize>,
21    pub(crate) offset: usize,
22    pub(crate) with_embeddings: bool,
23}
24
25#[derive(Debug, Clone, Copy)]
26pub enum SortOrder {
27    Asc,
28    Desc,
29}
30
31impl TableQueryBuilder {
32    pub fn new(collection: impl Into<String>) -> Self {
33        Self {
34            collection: collection.into(),
35            filters: Vec::new(),
36            order_by: None,
37            limit: None,
38            offset: 0,
39            with_embeddings: false,
40        }
41    }
42
43    /// Add a filter condition
44    pub fn where_(self, field: impl Into<String>) -> WhereClause<Self> {
45        WhereClause::new(self, field.into())
46    }
47
48    /// Order results by field
49    pub fn order_by(mut self, field: impl Into<String>, order: SortOrder) -> Self {
50        self.order_by = Some((field.into(), order));
51        self
52    }
53
54    /// Shorthand for ascending order
55    pub fn order_by_asc(self, field: impl Into<String>) -> Self {
56        self.order_by(field, SortOrder::Asc)
57    }
58
59    /// Shorthand for descending order
60    pub fn order_by_desc(self, field: impl Into<String>) -> Self {
61        self.order_by(field, SortOrder::Desc)
62    }
63
64    /// Limit results
65    pub fn limit(mut self, n: usize) -> Self {
66        self.limit = Some(n);
67        self
68    }
69
70    /// Skip first N results
71    pub fn offset(mut self, n: usize) -> Self {
72        self.offset = n;
73        self
74    }
75
76    /// Include embeddings in results
77    pub fn with_embeddings(mut self) -> Self {
78        self.with_embeddings = true;
79        self
80    }
81
82    /// Execute the query
83    pub fn execute(self, store: &Arc<UnifiedStore>) -> Result<QueryResult, ExecutionError> {
84        execute_table_query(self, store)
85    }
86}
87
88impl FilterAcceptor for TableQueryBuilder {
89    fn add_filter(&mut self, filter: Filter) {
90        self.filters.push(filter);
91    }
92}
93
94/// Builder for full collection scans
95#[derive(Debug, Clone)]
96pub struct ScanQueryBuilder {
97    pub(crate) collection: String,
98    pub(crate) filters: Vec<Filter>,
99    pub(crate) limit: Option<usize>,
100}
101
102impl ScanQueryBuilder {
103    pub fn new(collection: impl Into<String>) -> Self {
104        Self {
105            collection: collection.into(),
106            filters: Vec::new(),
107            limit: None,
108        }
109    }
110
111    /// Add a filter condition
112    pub fn where_(self, field: impl Into<String>) -> WhereClause<Self> {
113        WhereClause::new(self, field.into())
114    }
115
116    /// Limit results
117    pub fn limit(mut self, n: usize) -> Self {
118        self.limit = Some(n);
119        self
120    }
121
122    /// Execute the query
123    pub fn execute(self, store: &Arc<UnifiedStore>) -> Result<QueryResult, ExecutionError> {
124        execute_scan_query(self, store)
125    }
126}
127
128impl FilterAcceptor for ScanQueryBuilder {
129    fn add_filter(&mut self, filter: Filter) {
130        self.filters.push(filter);
131    }
132}