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] Remove restriction on matching aliases and values #1111 #1114

Merged
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
18 changes: 10 additions & 8 deletions agdb/src/query/insert_nodes_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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())) {
Expand Down
2 changes: 1 addition & 1 deletion agdb/src/query_builder/insert_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()]]);
Expand Down
19 changes: 18 additions & 1 deletion agdb/tests/insert_nodes_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
);
}

Expand Down Expand Up @@ -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,
);
}
4 changes: 2 additions & 2 deletions docs/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ QueryBuilder::insert().nodes().ids(QueryBuilder::search().from(1).query()).count

</td></tr></table>

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.

Expand Down Expand Up @@ -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.

Expand Down