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

[query] Add insert node query builder #397 #398

Merged
merged 4 commits into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/agdb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod db_value;

mod db_float;

use crate::Query;
use crate::query::Query;
use crate::QueryError;
use crate::QueryResult;
use crate::Transaction;
Expand Down
14 changes: 14 additions & 0 deletions src/agdb/db/db_key_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
use crate::DbKey;
use crate::DbValue;

#[derive(Clone)]
pub struct DbKeyValue {
pub key: DbKey,
pub value: DbValue,
}

impl<K, T> From<(K, T)> for DbKeyValue
where
K: Into<DbKey>,
T: Into<DbValue>,
{
fn from(value: (K, T)) -> Self {
DbKeyValue {
key: value.0.into(),
value: value.1.into(),
}
}
}
2 changes: 1 addition & 1 deletion src/agdb/db/db_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::utilities::serialize::Serialize;
use crate::utilities::stable_hash::StableHash;
use crate::DbError;

#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum DbValue {
Bytes(Vec<u8>),
Int(i64),
Expand Down
7 changes: 5 additions & 2 deletions src/agdb/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod db;
mod graph;
mod graph_search;
mod query;
mod query_builder;
mod storage;
mod transaction;
mod utilities;
Expand All @@ -14,9 +15,11 @@ mod test_utilities;
pub use db::db_error::DbError;
pub use db::db_index::DbIndex;
pub use db::db_key::DbKey;
pub use db::db_key_value::DbKeyValue;
pub use db::db_value::DbValue;
pub use db::Db;
pub use query::query_error::QueryError;
pub use query::query_result::QueryResult;
pub use query::Query;
pub use query::QueryError;
pub use query::QueryResult;
pub use query_builder::QueryBuilder;
pub use transaction::Transaction;
51 changes: 19 additions & 32 deletions src/agdb/query.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
mod comparison_operator;
mod condition;
mod direction;
mod distance_condition;
mod edge_count_condition;
mod insert_aliases_query;
mod insert_edges_query;
mod insert_nodes_query;
mod insert_query;
mod key_value_condition;
mod query_error;
mod query_id;
mod query_ids;
mod query_result;
mod query_values;
mod search_query;
mod select_query;
pub mod comparison_operator;
pub mod condition;
pub mod direction;
pub mod distance_condition;
pub mod edge_count_condition;
pub mod insert_aliases_query;
pub mod insert_edges_query;
pub mod insert_nodes_query;
pub mod insert_query;
pub mod key_value_condition;
pub mod query_error;
pub mod query_id;
pub mod query_ids;
pub mod query_result;
pub mod query_values;
pub mod search_query;
pub mod select_query;

use self::insert_aliases_query::InsertAliasQuery;
use self::insert_edges_query::InsertEdgesQuery;
use self::insert_nodes_query::InsertNodeQuery;
use self::insert_nodes_query::InsertNodesQuery;
use self::insert_query::InsertQuery;
use self::query_ids::QueryIds;
use self::query_values::QueryValues;
use self::search_query::SearchQuery;
use self::select_query::SelectQuery;
pub use query_error::QueryError;
pub use query_result::QueryResult;

pub enum Query {
Insert(InsertQuery),
InsertAliases(InsertAliasQuery),
InsertEdges(InsertEdgesQuery),
InsertNodes(InsertNodeQuery),
InsertNodes(InsertNodesQuery),
Remove(SelectQuery),
Search(SearchQuery),
Select(SelectQuery),
Expand All @@ -40,13 +37,3 @@ pub enum Query {
SelectKeyCount(QueryIds),
SelectKeys(QueryIds),
}

impl Default for Query {
fn default() -> Self {
Query::InsertNodes(InsertNodeQuery {
count: 0,
values: QueryValues::None,
aliases: vec![],
})
}
}
4 changes: 2 additions & 2 deletions src/agdb/query/insert_nodes_query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::query_values::QueryValues;

pub struct InsertNodeQuery {
pub struct InsertNodesQuery {
pub count: u64,
pub values: QueryValues,
pub aliases: Vec<String>,
pub alias: String,
}
14 changes: 14 additions & 0 deletions src/agdb/query_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mod insert;
mod insert_node;
mod insert_node_alias;
mod insert_node_values;

use self::insert::InsertBuilder;

pub struct QueryBuilder {}

impl QueryBuilder {
pub fn insert() -> InsertBuilder {
InsertBuilder {}
}
}
16 changes: 16 additions & 0 deletions src/agdb/query_builder/insert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::query::insert_nodes_query::InsertNodesQuery;
use crate::query::query_values::QueryValues;

use super::insert_node::InsertNodeBuilder;

pub struct InsertBuilder {}

impl InsertBuilder {
pub fn node(self) -> InsertNodeBuilder {
InsertNodeBuilder(InsertNodesQuery {
count: 1,
values: QueryValues::None,
alias: String::new(),
})
}
}
26 changes: 26 additions & 0 deletions src/agdb/query_builder/insert_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use super::insert_node_alias::InsertNodeAlias;
use super::insert_node_values::InsertNodeValues;
use crate::db::db_key_value::DbKeyValue;
use crate::query::insert_nodes_query::InsertNodesQuery;
use crate::query::query_values::QueryValues;
use crate::Query;

pub struct InsertNodeBuilder(pub InsertNodesQuery);

impl InsertNodeBuilder {
pub fn alias(mut self, name: &str) -> InsertNodeAlias {
self.0.alias = name.to_string();

InsertNodeAlias(self.0)
}

pub fn values(mut self, key_values: &[DbKeyValue]) -> InsertNodeValues {
self.0.values = QueryValues::Single(key_values.to_vec());

InsertNodeValues(self.0)
}

pub fn query(self) -> Query {
Query::InsertNodes(self.0)
}
}
19 changes: 19 additions & 0 deletions src/agdb/query_builder/insert_node_alias.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::insert_node_values::InsertNodeValues;
use crate::query::insert_nodes_query::InsertNodesQuery;
use crate::query::query_values::QueryValues;
use crate::DbKeyValue;
use crate::Query;

pub struct InsertNodeAlias(pub InsertNodesQuery);

impl InsertNodeAlias {
pub fn query(self) -> Query {
Query::InsertNodes(self.0)
}

pub fn values(mut self, key_values: &[DbKeyValue]) -> InsertNodeValues {
self.0.values = QueryValues::Single(key_values.to_vec());

InsertNodeValues(self.0)
}
}
10 changes: 10 additions & 0 deletions src/agdb/query_builder/insert_node_values.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::query::insert_nodes_query::InsertNodesQuery;
use crate::Query;

pub struct InsertNodeValues(pub InsertNodesQuery);

impl InsertNodeValues {
pub fn query(self) -> Query {
Query::InsertNodes(self.0)
}
}
2 changes: 1 addition & 1 deletion src/agdb/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Query;
use crate::query::Query;
use crate::QueryError;
use crate::QueryResult;

Expand Down
7 changes: 3 additions & 4 deletions tests/db_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use agdb::Db;
use agdb::DbError;
use agdb::DbKey;
use agdb::DbValue;
use agdb::Query;
use agdb::QueryBuilder;
use agdb::QueryError;
use agdb::QueryResult;

#[test]
fn public_types() {
let _db = Db::default();
let _query = Query::default();
let _query_error = QueryError::default();
let _result = QueryResult::default();
let _db_error = DbError::from("");
Expand All @@ -20,6 +19,6 @@ fn public_types() {
#[test]
fn exec_takes_query_returns_query_result() {
let db = Db::default();
let query = Query::default();
let _result: Result<QueryResult, QueryError> = db.exec(query);
let query = QueryBuilder::insert().node().query();
let _result = db.exec(query);
}
28 changes: 25 additions & 3 deletions tests/query_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
use agdb::Query;
use agdb::QueryBuilder;

#[test]
fn query_is_public_type() {
let _query = Query::default();
fn insert_node() {
let _query = QueryBuilder::insert().node().query();
}

#[test]
fn insert_node_alias() {
let _query = QueryBuilder::insert().node().alias("alias").query();
}

#[test]
fn insert_node_values() {
let _query = QueryBuilder::insert()
.node()
.values(&[("key", "value").into()])
.query();
}

#[test]
fn insert_node_alias_values() {
let _query = QueryBuilder::insert()
.node()
.alias("alias")
.values(&[("key", "value").into()])
.query();
}
17 changes: 3 additions & 14 deletions tests/transaction_test.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
use agdb::Db;
use agdb::Query;
use agdb::QueryBuilder;
use agdb::QueryError;
use agdb::QueryResult;
use agdb::Transaction;

#[test]
fn transaction_is_public_type() {
let _transaction = Transaction::default();
}

#[test]
fn create_transaction_from_db() {
let db = Db::default();
let _transaction: Transaction = db.transaction();
}

#[test]
fn exec_takes_query_returns_query_result() {
let db = Db::default();
let query = Query::default();
let transaction = db.transaction();
let _result: Result<QueryResult, QueryError> = transaction.exec(query);
let query = QueryBuilder::insert().node().query();
let _result = transaction.exec(query);
}

#[test]
Expand Down