reddb_server/storage/unified/dsl/builders/
table.rs1use 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#[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 pub fn where_(self, field: impl Into<String>) -> WhereClause<Self> {
45 WhereClause::new(self, field.into())
46 }
47
48 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 pub fn order_by_asc(self, field: impl Into<String>) -> Self {
56 self.order_by(field, SortOrder::Asc)
57 }
58
59 pub fn order_by_desc(self, field: impl Into<String>) -> Self {
61 self.order_by(field, SortOrder::Desc)
62 }
63
64 pub fn limit(mut self, n: usize) -> Self {
66 self.limit = Some(n);
67 self
68 }
69
70 pub fn offset(mut self, n: usize) -> Self {
72 self.offset = n;
73 self
74 }
75
76 pub fn with_embeddings(mut self) -> Self {
78 self.with_embeddings = true;
79 self
80 }
81
82 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#[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 pub fn where_(self, field: impl Into<String>) -> WhereClause<Self> {
113 WhereClause::new(self, field.into())
114 }
115
116 pub fn limit(mut self, n: usize) -> Self {
118 self.limit = Some(n);
119 self
120 }
121
122 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}