Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[db] Add search() to select(), insert() and remove() #1244 #1284

Merged
merged 13 commits into from
Sep 25, 2024
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,12 @@ You can also search through the graph to get back only certain elements based on
let user: User = db
.exec(
QueryBuilder::select()
.values(User::db_keys())
.ids(
QueryBuilder::search()
.from("users")
.where_()
.key("name")
.value(Equal("Bob".into()))
.query(),
)
.elements::<User>()
.search()
.from("users")
.where_()
.key("name")
.value(Equal("Bob".into()))
.query(),
)?
.try_into()?;
Expand Down
27 changes: 27 additions & 0 deletions agdb/src/query/insert_values_query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::query::query_values::QueryValues;
use crate::query_builder::search::SearchQueryBuilder;
use crate::DbElement;
use crate::DbId;
use crate::DbImpl;
Expand All @@ -8,6 +9,7 @@ use crate::QueryId;
use crate::QueryIds;
use crate::QueryMut;
use crate::QueryResult;
use crate::SearchQuery;
use crate::StorageData;

/// Query to insert or update key-value pairs (properties)
Expand Down Expand Up @@ -150,3 +152,28 @@ fn insert_values_id<Store: StorageData>(
}
Ok(())
}

impl SearchQueryBuilder for InsertValuesQuery {
fn search_mut(&mut self) -> &mut SearchQuery {
if let QueryIds::Search(search) = &mut self.ids {
search
} else {
panic!("Expected search query");
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic]
fn missing_search() {
InsertValuesQuery {
values: QueryValues::Single(vec![]),
ids: QueryIds::Ids(vec![]),
}
.search_mut();
}
}
23 changes: 23 additions & 0 deletions agdb/src/query/remove_query.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::query_builder::search::SearchQueryBuilder;
use crate::DbImpl;
use crate::QueryError;
use crate::QueryIds;
use crate::QueryMut;
use crate::QueryResult;
use crate::SearchQuery;
use crate::StorageData;

/// Query to remove database elements (nodes & edges). It
Expand Down Expand Up @@ -53,3 +55,24 @@ impl QueryMut for &RemoveQuery {
(*self).process(db)
}
}

impl SearchQueryBuilder for RemoveQuery {
fn search_mut(&mut self) -> &mut SearchQuery {
if let QueryIds::Search(search) = &mut self.0 {
search
} else {
panic!("Expected search query");
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic]
fn missing_search() {
RemoveQuery(QueryIds::Ids(vec![])).search_mut();
}
}
27 changes: 27 additions & 0 deletions agdb/src/query/remove_values_query.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::query_builder::search::SearchQueryBuilder;
use crate::DbImpl;
use crate::QueryError;
use crate::QueryIds;
use crate::QueryMut;
use crate::QueryResult;
use crate::SearchQuery;
use crate::SelectValuesQuery;
use crate::StorageData;

Expand Down Expand Up @@ -49,3 +51,28 @@ impl QueryMut for &RemoveValuesQuery {
(*self).process(db)
}
}

impl SearchQueryBuilder for RemoveValuesQuery {
fn search_mut(&mut self) -> &mut SearchQuery {
if let QueryIds::Search(search) = &mut self.0.ids {
search
} else {
panic!("Expected search query");
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic]
fn missing_search() {
RemoveValuesQuery(SelectValuesQuery {
keys: vec![],
ids: QueryIds::Ids(vec![]),
})
.search_mut();
}
}
19 changes: 19 additions & 0 deletions agdb/src/query/search_query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::db::db_key_order::DbKeyOrder;
use crate::query_builder::search::SearchQueryBuilder;
use crate::DbElement;
use crate::DbId;
use crate::DbImpl;
Expand Down Expand Up @@ -212,6 +213,18 @@ impl SearchQuery {
(_, _) => ids[self.offset as usize..(self.offset + self.limit) as usize].to_vec(),
})
}

pub(crate) fn new() -> Self {
Self {
algorithm: SearchQueryAlgorithm::BreadthFirst,
origin: QueryId::Id(DbId(0)),
destination: QueryId::Id(DbId(0)),
limit: 0,
offset: 0,
order_by: vec![],
conditions: vec![],
}
}
}

impl Query for &SearchQuery {
Expand All @@ -220,6 +233,12 @@ impl Query for &SearchQuery {
}
}

impl SearchQueryBuilder for SearchQuery {
fn search_mut(&mut self) -> &mut SearchQuery {
self
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
23 changes: 23 additions & 0 deletions agdb/src/query/select_aliases_query.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::query_builder::search::SearchQueryBuilder;
use crate::DbElement;
use crate::DbImpl;
use crate::Query;
use crate::QueryError;
use crate::QueryId;
use crate::QueryIds;
use crate::QueryResult;
use crate::SearchQuery;
use crate::StorageData;

/// Query to select aliases of given ids. All of the ids
Expand Down Expand Up @@ -69,3 +71,24 @@ impl Query for &SelectAliasesQuery {
(*self).process(db)
}
}

impl SearchQueryBuilder for SelectAliasesQuery {
fn search_mut(&mut self) -> &mut SearchQuery {
if let QueryIds::Search(search) = &mut self.0 {
search
} else {
panic!("Expected search query");
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic]
fn missing_search() {
SelectAliasesQuery(QueryIds::Ids(vec![])).search_mut();
}
}
28 changes: 28 additions & 0 deletions agdb/src/query/select_edge_count_query.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::query_builder::search::SearchQueryBuilder;
use crate::DbElement;
use crate::DbImpl;
use crate::Query;
use crate::QueryError;
use crate::QueryIds;
use crate::QueryResult;
use crate::SearchQuery;
use crate::StorageData;

/// Query to select number of edges of given node ids.
Expand Down Expand Up @@ -72,3 +74,29 @@ impl Query for &SelectEdgeCountQuery {
(*self).process(db)
}
}

impl SearchQueryBuilder for SelectEdgeCountQuery {
fn search_mut(&mut self) -> &mut SearchQuery {
if let QueryIds::Search(search) = &mut self.ids {
search
} else {
panic!("Expected search query");
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic]
fn missing_search() {
SelectEdgeCountQuery {
ids: QueryIds::Ids(vec![]),
from: false,
to: false,
}
.search_mut();
}
}
23 changes: 23 additions & 0 deletions agdb/src/query/select_key_count_query.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::query_builder::search::SearchQueryBuilder;
use crate::DbElement;
use crate::DbImpl;
use crate::Query;
use crate::QueryError;
use crate::QueryIds;
use crate::QueryResult;
use crate::SearchQuery;
use crate::StorageData;

/// Query to select number of properties (key count) of
Expand Down Expand Up @@ -55,3 +57,24 @@ impl Query for &SelectKeyCountQuery {
(*self).process(db)
}
}

impl SearchQueryBuilder for SelectKeyCountQuery {
fn search_mut(&mut self) -> &mut SearchQuery {
if let QueryIds::Search(search) = &mut self.0 {
search
} else {
panic!("Expected search query");
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic]
fn missing_search() {
SelectKeyCountQuery(QueryIds::Ids(vec![])).search_mut();
}
}
23 changes: 23 additions & 0 deletions agdb/src/query/select_keys_query.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::query_builder::search::SearchQueryBuilder;
use crate::DbElement;
use crate::DbImpl;
use crate::Query;
use crate::QueryError;
use crate::QueryIds;
use crate::QueryResult;
use crate::SearchQuery;
use crate::StorageData;

/// Query to select only property keys of given ids. All
Expand Down Expand Up @@ -54,3 +56,24 @@ impl Query for &SelectKeysQuery {
(*self).process(db)
}
}

impl SearchQueryBuilder for SelectKeysQuery {
fn search_mut(&mut self) -> &mut SearchQuery {
if let QueryIds::Search(search) = &mut self.0 {
search
} else {
panic!("Expected search query");
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic]
fn missing_search() {
SelectKeysQuery(QueryIds::Ids(vec![])).search_mut();
}
}
27 changes: 27 additions & 0 deletions agdb/src/query/select_values_query.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::query_builder::search::SearchQueryBuilder;
use crate::DbElement;
use crate::DbImpl;
use crate::DbValue;
use crate::Query;
use crate::QueryError;
use crate::QueryIds;
use crate::QueryResult;
use crate::SearchQuery;
use crate::StorageData;

/// Query to select elements with only certain properties of
Expand Down Expand Up @@ -76,3 +78,28 @@ impl Query for &SelectValuesQuery {
(*self).process(db)
}
}

impl SearchQueryBuilder for SelectValuesQuery {
fn search_mut(&mut self) -> &mut SearchQuery {
if let QueryIds::Search(search) = &mut self.ids {
search
} else {
panic!("Expected search query");
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic]
fn missing_search() {
SelectValuesQuery {
keys: vec![],
ids: QueryIds::Ids(vec![]),
}
.search_mut();
}
}
Loading