entity/ent/query/
mod.rs

1use crate::{DatabaseError, DatabaseResult, Ent, Id, WeakDatabaseRc};
2use derive_more::{Constructor, IntoIterator};
3use std::{fmt::Debug, iter::Extend};
4
5mod filter;
6pub use filter::*;
7
8mod predicate;
9pub use predicate::*;
10
11/// Represents a query interface for some ent
12pub trait EntQuery: Sized {
13    type Output;
14
15    /// Executes the query against the provided database, returning the query's
16    /// output as the result
17    fn execute_with_db(self, db: WeakDatabaseRc) -> DatabaseResult<Self::Output>;
18
19    /// Executes the query against the global database, returning the query's
20    /// output as the result
21    fn execute(self) -> DatabaseResult<Self::Output> {
22        Self::execute_with_db(self, crate::global::db())
23    }
24}
25
26/// Represents a generic query to find ents within some database
27#[derive(Constructor, IntoIterator, Clone, Debug, Default)]
28pub struct Query(Vec<Filter>);
29
30impl EntQuery for Query {
31    type Output = Vec<Box<dyn Ent>>;
32
33    #[inline]
34    fn execute_with_db(self, db: WeakDatabaseRc) -> DatabaseResult<Self::Output> {
35        let database = WeakDatabaseRc::upgrade(&db).ok_or(DatabaseError::Disconnected)?;
36        database.find_all(self)
37    }
38}
39
40impl Extend<Filter> for Query {
41    /// Extends the query's filters with the contents of the iterator
42    fn extend<T: IntoIterator<Item = Filter>>(&mut self, iter: T) {
43        self.0.extend(iter)
44    }
45}
46
47impl Query {
48    /// Consumes query, producing a new query with the additional filter
49    /// added to the end of the filters to be applied
50    pub fn chain(self, filter: Filter) -> Self {
51        let mut filters = self.0;
52        filters.push(filter);
53
54        Query::new(filters)
55    }
56
57    /// Updates the query by adding an additional filter to the end
58    pub fn add_filter(&mut self, filter: Filter) -> &mut Self {
59        self.0.push(filter);
60        self
61    }
62
63    pub fn where_id<P: Into<TypedPredicate<Id>>>(self, p: P) -> Self {
64        self.chain(Filter::where_id(p))
65    }
66
67    pub fn where_type<P: Into<TypedPredicate<String>>>(self, p: P) -> Self {
68        self.chain(Filter::where_type(p))
69    }
70
71    pub fn where_created<P: Into<TypedPredicate<u64>>>(self, p: P) -> Self {
72        self.chain(Filter::where_created(p))
73    }
74
75    pub fn where_last_updated<P: Into<TypedPredicate<u64>>>(self, p: P) -> Self {
76        self.chain(Filter::where_last_updated(p))
77    }
78
79    pub fn where_field<S: Into<String>, P: Into<Predicate>>(self, name: S, p: P) -> Self {
80        self.chain(Filter::where_field(name, p))
81    }
82
83    pub fn where_edge<S: Into<String>, F: Into<Filter>>(self, name: S, filter: F) -> Self {
84        self.chain(Filter::where_edge(name, filter))
85    }
86
87    pub fn where_into_edge<S: Into<String>>(self, name: S) -> Self {
88        self.chain(Filter::where_into_edge(name))
89    }
90}