pub struct Query { /* private fields */ }
Expand description
A Query
is a special kind of body that we submit to the query
function. It has the following fields:
find
is responsible for defining which elements of the query you want shown in the response, it is required. Argument is a vector with elements to be queried,vec!["a", "b", "c"]
. It is parsed as:find [a b c]
, qherea, b, c
are the elements defined inwhere
clause.where_clause
is responsible for defining which rules will be applied to filter elements, it is required. Argument is a vector with the strings containing the filtering function,vec!["a :db-key1 b", "a :db-key2 c", "a :db-key3 <some value>"]
. It is parsed as:where [ [a :db-key1 b] [a :db-key2 c] [a :db-key3 <some value>] ]
.args
is responsible for defining arguments to be replaced inwhere_clause
, optional. Argument is a vector with strings containing the matchesvec!["?n \"Ivan\" ?l \"Ivanov\"", "?n \"Petr\" ?l \"Petrov\""]
.order_by
is responsible for defining the order in which the response will be represented, optional. Argument is a vector with strings containing the element and how to order (:asc
or:desc
)vec!["time :desc", "device-id :asc"]
.limit
is responsible for defining the limit size of the response, optional. Argument is a usize.offset
is responsible for defining the offset of the response, optional. Argument is a usize.
Implementations§
Source§impl Query
impl Query
Sourcepub fn find(find: Vec<&str>) -> Result<Self, CruxError>
pub fn find(find: Vec<&str>) -> Result<Self, CruxError>
find
is the function responsible for defining the :find
key in the query.
Input should be the elements to be queried by the where_clause
.
Ex: vec!["time", "device-id", "temperature", "humidity"]
.
Becomes: :find [time, device-id, temperature, humidity]
.
Error cases:
- All elements should start with
?
, examplevec!["?p1", "?n", "?g"]
. If theey do not start the CruxError::QueryFormatError containingAll elements of find clause should start with '?', element '{}' doesn't conform
is thrown.
Sourcepub fn find_by_aggregates(find: Vec<Aggregate>) -> Result<Self, CruxError>
pub fn find_by_aggregates(find: Vec<Aggregate>) -> Result<Self, CruxError>
find_by_aggregates
is the function responsible for defining the :find
key in the query similar to find
.
However, it supports sending aggregates to :find
keys.
Input should be the elements to be queried by the where_clause
.
Ex: vec![(Sum("?heads"), Min("?heads"), Max("?heads"), Count("?heads"), CountDistinct("?heads")]
.
Becomes: :find [(sum ?heads) (min ?heads) (max ?heads) (count ?heads) (count-distinct ?heads)]
.
Error cases:
- All elements should start with
?
, examplevec!["(min ?heads)"]
. If theey do not start the CruxError::QueryFormatError containingAll elements of find clause should start with '?', element '{}' doesn't conform
is thrown.
Sourcepub fn where_clause(self, where_: Vec<&str>) -> Result<Self, CruxError>
pub fn where_clause(self, where_: Vec<&str>) -> Result<Self, CruxError>
where_clause
is the function responsible for defining the required :where
key in the query.
Input should be element1 :key element2
, element2
may have a modifier like #inst
. The order matters.
Ex: vec!["c :condition/time time", "c :condition/device-id device-id", "c :condition/temperature temperature", "c :condition/humidity humidity"]
.
Becomes:
:where [[c :condition/time time] [c :condition/device-id device-id] [c :condition/temperature temperature] [c :condition/humidity humidity]]
.
Error cases:
- All elements present in find clause should be present in where clause. If your find clause is
"?p", "?n", "?s"
, and your where clause is"?p1 :alpha ?n", "?p1 :beta true"
an errorNot all element of find,
“?p”, “?n”, “?s”, are present in the where clause, ?s is missing
is thrown.
Sourcepub fn args(self, args: Vec<&str>) -> Result<Self, CruxError>
pub fn args(self, args: Vec<&str>) -> Result<Self, CruxError>
args
is the function responsible for defining the optional :args
key in the query.
Input are elements you want to replace in the where_clause
, a good practice is to name them with ?
before.
Ex: vec!["?n \"Ivan\" ?l \"Ivanov\"", "?n \"Petr\" ?l \"Petrov\""]
.
Becomes: :args [{?n "Ivan" ?l "Ivanov"} {?n "Petr" ?l "Petrov"}]
.
Error cases:
- The first element of the argument key-value tuple should start with
?
. An inputvec!["n true"]
will return an errorAll elements should start with '?'
. - All arguments key should be present in the where clause. If the where clause
?p1 :name ?n", "?p1 :is-sql ?s", "?p1 :is-sql true"
and an args clausevec!["?s true ?x 1243"]
will return an errorAll elements should be present in where clause
.
Sourcepub fn order_by(self, order_by: Vec<&str>) -> Result<Self, CruxError>
pub fn order_by(self, order_by: Vec<&str>) -> Result<Self, CruxError>
order_by
is the function responsible for defining the optional :order-by
key in the query.
Input is the elements to be ordered by, the first element is the first order, the second is the further orthers. Allowed keys are :Asc
and :desc
.
Ex: vec!["time :desc", "device-id :asc"]
.
Becomes: :order-by [[time :desc] [device-id :asc]]
.
Error cases:
- The second element of each order clause should be
:asc
or:desc
, if different, like:eq
in"?p1 :asc", "?n :desc", "?s :eq"
, errorOrder element should be ':asc' or ':desc'
is thrown. - The first element of each order clause should be present in the find clause. If the order clause is
"?p1 :asc", "?n :desc", "?g :asc"
and the find clause is"?p1", "?n"
the errorAll elements to be ordered should be present in find clause, ?g not present
is thrown.
Sourcepub fn limit(self, limit: usize) -> Self
pub fn limit(self, limit: usize) -> Self
limit
is the function responsible for defining the optional :limit
key in the query.
Input is a usize with the query limit size.
.limit(5usize)
Becomes: :limit 5
.
Sourcepub fn offset(self, offset: usize) -> Self
pub fn offset(self, offset: usize) -> Self
offset
is the function responsible for defining the optional :offset
key in the query.
Input is a usize with the query offset.
.offset(5usize)
Becomes: :offset 5
.
Sourcepub fn with_full_results(self) -> Self
pub fn with_full_results(self) -> Self
with_full_results
adds :full-results? true
to the query map to easily retrieve the source documents relating to the entities in the result set.