Skip to content

Commit

Permalink
[db] Allow aliases as slice or array #1273 (#1274)
Browse files Browse the repository at this point in the history
aliases from array or slice
  • Loading branch information
michaelvlach authored Sep 16, 2024
1 parent 94989a0 commit 5299e09
Show file tree
Hide file tree
Showing 33 changed files with 114 additions and 113 deletions.
2 changes: 1 addition & 1 deletion agdb/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ mod tests {
let queries: Vec<QueryType> = vec![
QueryBuilder::insert().nodes().count(2).query().into(),
QueryBuilder::insert()
.aliases(vec!["node1", "node2"])
.aliases(["node1", "node2"])
.ids(vec![1, 2])
.query()
.into(),
Expand Down
39 changes: 33 additions & 6 deletions agdb/src/query/query_aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
/// [`QueryBuilder`].
pub struct QueryAliases(pub Vec<String>);

impl From<Vec<String>> for QueryAliases {
fn from(value: Vec<String>) -> Self {
QueryAliases(value)
impl<T: Into<String>> From<Vec<T>> for QueryAliases {
fn from(value: Vec<T>) -> Self {
QueryAliases(value.into_iter().map(|v| v.into()).collect())
}
}

impl From<Vec<&str>> for QueryAliases {
fn from(value: Vec<&str>) -> Self {
QueryAliases(value.iter().map(|v| v.to_string()).collect())
impl<T: Into<String> + Clone> From<&[T]> for QueryAliases {
fn from(value: &[T]) -> Self {
QueryAliases(value.iter().map(|v| v.clone().into()).collect())
}
}

impl<T: Into<String>, const N: usize> From<[T; N]> for QueryAliases {
fn from(value: [T; N]) -> Self {
QueryAliases(value.into_iter().map(|v| v.into()).collect())
}
}

Expand All @@ -26,3 +32,24 @@ impl From<String> for QueryAliases {
QueryAliases(vec![value])
}
}

impl From<&String> for QueryAliases {
fn from(value: &String) -> Self {
QueryAliases(vec![value.clone()])
}
}

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

#[test]
fn query_aliases() {
let _aliases = QueryAliases::from(vec!["a".to_string()]);
let _aliases = QueryAliases::from(["a", "b"]);
let _aliases = QueryAliases::from(["a", "b"].as_slice());
let _aliases = QueryAliases::from("a");
let _aliases = QueryAliases::from("a".to_string());
let _aliases = QueryAliases::from(&"a".to_string());
}
}
4 changes: 2 additions & 2 deletions agdb/src/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl QueryBuilder {
/// QueryBuilder::insert().nodes();
/// QueryBuilder::insert().edges();
/// QueryBuilder::insert().aliases("a");
/// QueryBuilder::insert().aliases(vec!["a", "b"]);
/// QueryBuilder::insert().aliases(["a", "b"]);
/// QueryBuilder::insert().element(&MyValue { db_id: Some(DbId(1)), key: "a".to_string(), });
/// QueryBuilder::insert().elements(&[MyValue { db_id: Some(DbId(1)), key: "a".to_string(), }]);
/// QueryBuilder::insert().index("k");
Expand All @@ -77,7 +77,7 @@ impl QueryBuilder {
/// QueryBuilder::remove().ids(QueryBuilder::search().from(1).query());
/// QueryBuilder::remove().index("k");
/// QueryBuilder::remove().aliases("a");
/// QueryBuilder::remove().aliases(vec!["a", "b"]);
/// QueryBuilder::remove().aliases(["a", "b"]);
/// QueryBuilder::remove().values(vec!["k".into()]);
/// ```
pub fn remove() -> Remove {
Expand Down
4 changes: 2 additions & 2 deletions agdb/src/query_builder/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Insert {
/// use agdb::QueryBuilder;
///
/// QueryBuilder::insert().aliases("a").ids(1);
/// QueryBuilder::insert().aliases(vec!["a", "b"]).ids(vec![1, 2]);
/// QueryBuilder::insert().aliases(["a", "b"]).ids(vec![1, 2]);
/// ```
pub fn aliases<T: Into<QueryAliases>>(self, names: T) -> InsertAliases {
InsertAliases(InsertAliasesQuery {
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Insert {
///
/// QueryBuilder::insert().nodes().count(1);
/// QueryBuilder::insert().nodes().aliases("a");
/// QueryBuilder::insert().nodes().aliases(vec!["a", "b"]);
/// QueryBuilder::insert().nodes().aliases(["a", "b"]);
/// QueryBuilder::insert().nodes().ids(1);
/// QueryBuilder::insert().nodes().ids(vec![1, 2]);
/// QueryBuilder::insert().nodes().ids("a");
Expand Down
6 changes: 3 additions & 3 deletions agdb/tests/db_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn data_persistence() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias", "alias2"])
.aliases(["alias", "alias2"])
.values_uniform(values.clone())
.query(),
)
Expand Down Expand Up @@ -185,7 +185,7 @@ fn data_remove_persistence() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias", "alias2"])
.aliases(["alias", "alias2"])
.values_uniform(vec![("key", 100).into()])
.query(),
)
Expand Down Expand Up @@ -493,7 +493,7 @@ fn queries_as_reference() {

let query = QueryBuilder::insert()
.nodes()
.aliases(vec!["root", "users"])
.aliases(["root", "users"])
.query();
db.exec_mut(&query).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion agdb/tests/efficient_agdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn create_db() -> Result<Arc<RwLock<Db>>, QueryError> {
t.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["root", "users", "posts"])
.aliases(["root", "users", "posts"])
.query(),
)?;
t.exec_mut(
Expand Down
6 changes: 3 additions & 3 deletions agdb/tests/insert_aliases_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn insert_aliases_of() {
db.exec_mut(QueryBuilder::insert().nodes().count(2).query(), 2);
db.exec_mut(
QueryBuilder::insert()
.aliases(vec![String::from("alias"), String::from("alias2")])
.aliases([String::from("alias"), String::from("alias2")])
.ids(vec![1, 2])
.query(),
2,
Expand All @@ -25,7 +25,7 @@ fn insert_aliases_of_alias() {
db.exec_mut(QueryBuilder::insert().nodes().count(1).query(), 1);
db.exec_mut(
QueryBuilder::insert()
.aliases(vec!["alias1", "alias2"])
.aliases(["alias1", "alias2"])
.ids(vec![QueryId::from("alias"), 2.into()])
.query(),
2,
Expand All @@ -41,7 +41,7 @@ fn insert_aliases_rollback() {
|t| -> Result<(), QueryError> {
t.exec_mut(
QueryBuilder::insert()
.aliases(vec!["alias1", "alias2"])
.aliases(["alias1", "alias2"])
.ids(vec![QueryId::from("alias"), 2.into()])
.query(),
)?;
Expand Down
18 changes: 9 additions & 9 deletions agdb/tests/insert_edges_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn insert_edges_from_to() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2", "alias3", "alias4"])
.aliases(["alias1", "alias2", "alias3", "alias4"])
.query(),
4,
);
Expand All @@ -59,7 +59,7 @@ fn insert_edges_from_to_each() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2", "alias3"])
.aliases(["alias1", "alias2", "alias3"])
.query(),
3,
);
Expand All @@ -79,7 +79,7 @@ fn insert_edges_from_to_values() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2", "alias3", "alias4"])
.aliases(["alias1", "alias2", "alias3", "alias4"])
.query(),
4,
);
Expand Down Expand Up @@ -120,7 +120,7 @@ fn insert_edges_from_to_each_values() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2", "alias3", "alias4"])
.aliases(["alias1", "alias2", "alias3", "alias4"])
.query(),
4,
);
Expand Down Expand Up @@ -176,7 +176,7 @@ fn insert_edges_from_to_each_values_uniform() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2", "alias3", "alias4"])
.aliases(["alias1", "alias2", "alias3", "alias4"])
.query(),
4,
);
Expand Down Expand Up @@ -227,7 +227,7 @@ fn insert_edges_from_to_values_bad_length() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2", "alias3", "alias4"])
.aliases(["alias1", "alias2", "alias3", "alias4"])
.query(),
4,
);
Expand All @@ -248,7 +248,7 @@ fn insert_edges_from_to_values_each_bad_length() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2", "alias3", "alias4"])
.aliases(["alias1", "alias2", "alias3", "alias4"])
.query(),
4,
);
Expand All @@ -270,7 +270,7 @@ fn insert_edges_from_to_values_asymmetric() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2", "alias3"])
.aliases(["alias1", "alias2", "alias3"])
.query(),
3,
);
Expand Down Expand Up @@ -311,7 +311,7 @@ fn insert_edges_from_to_values_uniform() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2", "alias3"])
.aliases(["alias1", "alias2", "alias3"])
.query(),
3,
);
Expand Down
12 changes: 6 additions & 6 deletions agdb/tests/insert_nodes_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn insert_nodes_aliases() {
db.exec_mut_ids(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2"])
.aliases(["alias1", "alias2"])
.query(),
&[1, 2],
);
Expand All @@ -59,7 +59,7 @@ fn insert_nodes_aliases_values() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2"])
.aliases(["alias1", "alias2"])
.values(vec![
vec![("key", "value").into(), ("key2", "value2").into()],
vec![("key", "value3").into()],
Expand Down Expand Up @@ -95,7 +95,7 @@ fn insert_nodes_aliases_values_rollback() {
.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2"])
.aliases(["alias1", "alias2"])
.values(vec![
vec![("key", "value").into(), ("key2", "value2").into()],
vec![("key", "value3").into()],
Expand Down Expand Up @@ -143,7 +143,7 @@ fn insert_nodes_aliases_values_uniform() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias1", "alias2"])
.aliases(["alias1", "alias2"])
.values_uniform(vec![("key", "value").into(), ("key2", "value2").into()])
.query(),
2,
Expand Down Expand Up @@ -243,7 +243,7 @@ fn insert_nodes_existing_aliases_values() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["new_alias", "alias", "alias3"])
.aliases(["new_alias", "alias", "alias3"])
.values(vec![
vec![("some_key", "value").into()],
vec![("key", 10).into(), ("new_key", 100).into()],
Expand Down Expand Up @@ -279,7 +279,7 @@ fn insert_nodes_aliases_values_mismatched_length() {
db.exec_mut_error(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias", "alias2"])
.aliases(["alias", "alias2"])
.values(vec![vec![("key", 1).into()]])
.query(),
"Aliases (2) and values (1) must have compatible lenghts (2 <= 1)",
Expand Down
2 changes: 1 addition & 1 deletion agdb/tests/insert_values_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn insert_values_uniform_ids() {
db.exec_mut_ids(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias", "alias2"])
.aliases(["alias", "alias2"])
.query(),
&[1, 2],
);
Expand Down
14 changes: 4 additions & 10 deletions agdb/tests/remove_aliases_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ fn remove_aliases() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias", "alias2"])
.aliases(["alias", "alias2"])
.query(),
2,
);
db.exec_mut(
QueryBuilder::remove()
.aliases(vec!["alias", "alias2"])
.query(),
QueryBuilder::remove().aliases(["alias", "alias2"]).query(),
-2,
);
}
Expand All @@ -29,18 +27,14 @@ fn remove_aliases_rollback() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias", "alias2"])
.aliases(["alias", "alias2"])
.query(),
2,
);

db.transaction_mut_error(
|t| -> Result<QueryResult, QueryError> {
t.exec_mut(
QueryBuilder::remove()
.aliases(vec!["alias", "alias2"])
.query(),
)?;
t.exec_mut(QueryBuilder::remove().aliases(["alias", "alias2"]).query())?;
t.exec(QueryBuilder::select().ids("alias2").query())
},
"Alias 'alias2' not found".into(),
Expand Down
2 changes: 1 addition & 1 deletion agdb/tests/remove_nodes_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn remove_nodes() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias", "alias2"])
.aliases(["alias", "alias2"])
.query(),
2,
);
Expand Down
4 changes: 2 additions & 2 deletions agdb/tests/remove_values_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn remove_values_ids() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias", "alias2"])
.aliases(["alias", "alias2"])
.values_uniform(vec![("key1", "value1").into()])
.query(),
2,
Expand Down Expand Up @@ -102,7 +102,7 @@ fn remove_missing_key() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["alias", "alias2"])
.aliases(["alias", "alias2"])
.values_uniform(vec![("key1", "value1").into(), ("key2", 100).into()])
.query(),
2,
Expand Down
4 changes: 2 additions & 2 deletions agdb/tests/search_where_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn create_db() -> TestDb {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["root", "users", "docs"])
.aliases(["root", "users", "docs"])
.query(),
3,
);
Expand Down Expand Up @@ -477,7 +477,7 @@ fn search_from_to_where_filter() {
db.exec_mut(
QueryBuilder::insert()
.nodes()
.aliases(vec!["start", "end"])
.aliases(["start", "end"])
.query(),
2,
);
Expand Down
Loading

0 comments on commit 5299e09

Please sign in to comment.