use std::sync::Arc;
use crate::storage::query::unified::ExecutionError;
use super::super::super::store::UnifiedStore;
use super::super::execution::{execute_scan_query, execute_table_query};
use super::super::filters::{Filter, FilterAcceptor, WhereClause};
use super::super::types::QueryResult;
#[derive(Debug, Clone)]
pub struct TableQueryBuilder {
pub(crate) collection: String,
pub(crate) filters: Vec<Filter>,
pub(crate) order_by: Option<(String, SortOrder)>,
pub(crate) limit: Option<usize>,
pub(crate) offset: usize,
pub(crate) with_embeddings: bool,
}
#[derive(Debug, Clone, Copy)]
pub enum SortOrder {
Asc,
Desc,
}
impl TableQueryBuilder {
pub fn new(collection: impl Into<String>) -> Self {
Self {
collection: collection.into(),
filters: Vec::new(),
order_by: None,
limit: None,
offset: 0,
with_embeddings: false,
}
}
pub fn where_(self, field: impl Into<String>) -> WhereClause<Self> {
WhereClause::new(self, field.into())
}
pub fn order_by(mut self, field: impl Into<String>, order: SortOrder) -> Self {
self.order_by = Some((field.into(), order));
self
}
pub fn order_by_asc(self, field: impl Into<String>) -> Self {
self.order_by(field, SortOrder::Asc)
}
pub fn order_by_desc(self, field: impl Into<String>) -> Self {
self.order_by(field, SortOrder::Desc)
}
pub fn limit(mut self, n: usize) -> Self {
self.limit = Some(n);
self
}
pub fn offset(mut self, n: usize) -> Self {
self.offset = n;
self
}
pub fn with_embeddings(mut self) -> Self {
self.with_embeddings = true;
self
}
pub fn execute(self, store: &Arc<UnifiedStore>) -> Result<QueryResult, ExecutionError> {
execute_table_query(self, store)
}
}
impl FilterAcceptor for TableQueryBuilder {
fn add_filter(&mut self, filter: Filter) {
self.filters.push(filter);
}
}
#[derive(Debug, Clone)]
pub struct ScanQueryBuilder {
pub(crate) collection: String,
pub(crate) filters: Vec<Filter>,
pub(crate) limit: Option<usize>,
}
impl ScanQueryBuilder {
pub fn new(collection: impl Into<String>) -> Self {
Self {
collection: collection.into(),
filters: Vec::new(),
limit: None,
}
}
pub fn where_(self, field: impl Into<String>) -> WhereClause<Self> {
WhereClause::new(self, field.into())
}
pub fn limit(mut self, n: usize) -> Self {
self.limit = Some(n);
self
}
pub fn execute(self, store: &Arc<UnifiedStore>) -> Result<QueryResult, ExecutionError> {
execute_scan_query(self, store)
}
}
impl FilterAcceptor for ScanQueryBuilder {
fn add_filter(&mut self, filter: Filter) {
self.filters.push(filter);
}
}