From 0e94f3abeefbcd9964a6c32693bb51a8af04bf25 Mon Sep 17 00:00:00 2001 From: Michael Vlach Date: Sun, 9 Jun 2024 21:09:14 +0200 Subject: [PATCH 1/4] Update insert_nodes_test.rs --- agdb/tests/insert_nodes_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agdb/tests/insert_nodes_test.rs b/agdb/tests/insert_nodes_test.rs index 82940f90d..bb5eb5946 100644 --- a/agdb/tests/insert_nodes_test.rs +++ b/agdb/tests/insert_nodes_test.rs @@ -282,7 +282,7 @@ fn insert_nodes_aliases_values_mismatched_length() { .aliases(vec!["alias", "alias2"]) .values(vec![vec![("key", 1).into()]]) .query(), - "Values (1) and aliases (2) must have the same length", + "Aliases (2) and values (1) must have compatible lenghts (2 <= 1)", ); } From 9ecd4fa8016e063ac5d011a163df0e9c64f8edbb Mon Sep 17 00:00:00 2001 From: Michael Vlach Date: Sun, 9 Jun 2024 21:09:16 +0200 Subject: [PATCH 2/4] Update insert_nodes.rs --- agdb/src/query_builder/insert_nodes.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agdb/src/query_builder/insert_nodes.rs b/agdb/src/query_builder/insert_nodes.rs index aa1cd9525..529a82df0 100644 --- a/agdb/src/query_builder/insert_nodes.rs +++ b/agdb/src/query_builder/insert_nodes.rs @@ -111,7 +111,7 @@ impl InsertNodes { /// ``` /// use agdb::QueryBuilder; /// - /// QueryBuilder::insert().nodes().ids(1).query(); + /// QueryBuilder::insert().nodes().ids(1); /// QueryBuilder::insert().nodes().ids(1).aliases("a"); /// QueryBuilder::insert().nodes().ids(1).count(1); /// QueryBuilder::insert().nodes().ids(1).values(vec![vec![("k", 1).into()]]); From 280f680230c9cfb0cf869b53658cb9a15d579a17 Mon Sep 17 00:00:00 2001 From: Michael Vlach Date: Sun, 9 Jun 2024 21:09:18 +0200 Subject: [PATCH 3/4] Update insert_nodes_query.rs --- agdb/src/query/insert_nodes_query.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/agdb/src/query/insert_nodes_query.rs b/agdb/src/query/insert_nodes_query.rs index 85ec7c928..328a4b830 100644 --- a/agdb/src/query/insert_nodes_query.rs +++ b/agdb/src/query/insert_nodes_query.rs @@ -57,6 +57,16 @@ impl QueryMut for InsertNodesQuery { QueryValues::Multi(v) => v.iter().collect(), }; + if values.len() < self.aliases.len() { + return Err(QueryError::from(format!( + "Aliases ({}) and values ({}) must have compatible lenghts ({} <= {})", + self.aliases.len(), + values.len(), + self.aliases.len(), + values.len(), + ))); + } + let query_ids = match &self.ids { QueryIds::Ids(ids) => ids .iter() @@ -97,14 +107,6 @@ impl QueryMut for InsertNodesQuery { ids.push(*db_id); } } else { - if !self.aliases.is_empty() && values.len() != self.aliases.len() { - return Err(QueryError::from(format!( - "Values ({}) and aliases ({}) must have the same length", - values.len(), - self.aliases.len() - ))); - } - for (index, key_values) in values.iter().enumerate() { if let Some(alias) = self.aliases.get(index) { if let Ok(db_id) = db.db_id(&QueryId::Alias(alias.to_string())) { From 674227ec489bad065edc0eb19c287d02f05b1a33 Mon Sep 17 00:00:00 2001 From: Michael Vlach Date: Sat, 15 Jun 2024 07:29:50 +0200 Subject: [PATCH 4/4] add test & update docs --- agdb/tests/insert_nodes_test.rs | 17 +++++++++++++++++ docs/queries.md | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/agdb/tests/insert_nodes_test.rs b/agdb/tests/insert_nodes_test.rs index bb5eb5946..3ef20dd1f 100644 --- a/agdb/tests/insert_nodes_test.rs +++ b/agdb/tests/insert_nodes_test.rs @@ -390,3 +390,20 @@ fn insert_or_update_edge_id() { "The ids for insert or update must all refer to nodes - edge id '-3' found", ); } + +#[test] +fn insert_aliases_and_normal_nodes() { + let mut db = TestDb::new(); + db.exec_mut( + QueryBuilder::insert() + .nodes() + .aliases("users") + .values(vec![ + vec![], + vec![("name", "alice").into()], + vec![("name", "bob").into()], + ]) + .query(), + 3, + ); +} diff --git a/docs/queries.md b/docs/queries.md index 031a8750e..9eff2b2a6 100644 --- a/docs/queries.md +++ b/docs/queries.md @@ -493,7 +493,7 @@ QueryBuilder::insert().nodes().ids(QueryBuilder::search().from(1).query()).count -The `count` is the number of nodes to be inserted into the database. It can be omitted (left `0`) if either `values` or `aliases` (or both) are provided. If the `values` is [`QueryValues::Single`](#queryvalues) you must provide either `count` or `aliases`. It is not an error if the count is set to `0` but the query will be a no-op and return empty result. If both `values` [`QueryValues::Multi`](#queryvalues) and `aliases` are provided their lengths must match, otherwise it will result in a logic error. Empty aliases (`""`) are not allowed. The values can be inferred from user defined types if they implement `DbUserValue` trait (`#derive(agdb::UserValue)`). Both singular nad vectorized versions are supported. Optionally one can specify `ids` that facilitates insert-or-update semantics. The field can be a search sub-query. If the resulting list in `ids` is empty the query will insert nodes as normal. If the list is not empty all ids must exist and must refer to nodes and the query will perform update instead - both aliases (replacing existing ones if applicable) and values. +The `count` is the number of nodes to be inserted into the database. It can be omitted (left `0`) if either `values` or `aliases` (or both) are provided. If the `values` is [`QueryValues::Single`](#queryvalues) you must provide either `count` or `aliases`. It is not an error if the count is set to `0` but the query will be a no-op and return empty result. If both `values` [`QueryValues::Multi`](#queryvalues) and `aliases` are provided their lengths must be compatible (aliases <= values), otherwise it will result in a logic error. Empty aliases (`""`) are not allowed. The values can be inferred from user defined types if they implement `DbUserValue` trait (`#derive(agdb::UserValue)`). Both singular nad vectorized versions are supported. Optionally one can specify `ids` that facilitates insert-or-update semantics. The field can be a search sub-query. If the resulting list in `ids` is empty the query will insert nodes as normal. If the list is not empty all ids must exist and must refer to nodes and the query will perform update instead - both aliases (replacing existing ones if applicable) and values. If an alias already exists in the database its values will be amended (inserted or replaced) with the provided values. @@ -535,7 +535,7 @@ Inserts or updates key-value pairs (properties) of existing elements or insert n If an id is non-0 or an existing alias that element will be updated in the database with provided values. -If an id is `0` or an non-existent alias new element (node) will be inserted into the database. +If an id is `0` or an non-existent alias new element (node) will be inserted into the database with that alias. Note that this query is insert-or-update for both nodes and existing values. By inserting the same `key` its old value will be overwritten with the new one.