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 conversion from QueryResult to QueryIds #609 #617

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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ let users = db.exec_mut(&QueryBuilder::insert()
vec![("username", "Bob").into(), ("joined", 2015).into()],
vec![("username", "John").into()]])
.query())?;
db.exec_mut(&QueryBuilder::insert().edges().from("users").to(users.ids()).query())?;
db.exec_mut(&QueryBuilder::insert().edges().from("users").to(&users).query())?;
```

This code creates a database called `user_db.agdb` with a simple graph of 4 nodes. The first node is aliased `users` and 3 user nodes for Alice, Bob and John are then connected with edges to the `users` node. The arbitrary `username` and sometimes `joined` properties are attached to the user nodes.

You can select the graph elements (both nodes & edges) with their ids to get them back with their associated data (key-value properties):

```Rust
let user_elements = db.exec(&QueryBuilder::select().ids(users.ids()).query())?;
let user_elements = db.exec(&QueryBuilder::select().ids(users).query())?;
println!("{:?}", user_elements);
// QueryResult {
// result: 3,
Expand Down
14 changes: 13 additions & 1 deletion src/agdb/query/query_ids.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::query_id::QueryId;
use super::search_query::SearchQuery;
use crate::DbId;
use crate::{DbId, QueryResult};

#[derive(Debug, Clone, PartialEq)]
pub enum QueryIds {
Expand Down Expand Up @@ -71,6 +71,18 @@ impl From<String> for QueryIds {
}
}

impl From<QueryResult> for QueryIds {
fn from(value: QueryResult) -> Self {
Self::from(&value)
}
}

impl From<&QueryResult> for QueryIds {
fn from(value: &QueryResult) -> Self {
QueryIds::Ids(value.ids())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion tests/db_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn optimize_on_drop() {
.query(),
)
.unwrap();
db.exec_mut(&QueryBuilder::remove().ids(result.ids()).query())
db.exec_mut(&QueryBuilder::remove().ids(result).query())
.unwrap();
db_file_size = std::fs::File::open(test_file.file_name())
.unwrap()
Expand Down
6 changes: 2 additions & 4 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ fn quickstart() {
&QueryBuilder::insert()
.edges()
.from("users")
.to(users.ids())
.to(&users)
.query(),
)
.unwrap();

let user_elements = db
.exec(&QueryBuilder::select().ids(users.ids()).query())
.unwrap();
let user_elements = db.exec(&QueryBuilder::select().ids(users).query()).unwrap();

println!("{:?}", user_elements);
// QueryResult {
Expand Down
6 changes: 1 addition & 5 deletions tests/insert_edges_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,7 @@ fn insert_edges_from_to_inserted_nodes() {
let to = db.exec_mut_result(QueryBuilder::insert().nodes().count(2).query());

db.exec_mut_ids(
QueryBuilder::insert()
.edges()
.from(from.ids())
.to(to.ids())
.query(),
QueryBuilder::insert().edges().from(from).to(to).query(),
&[-5, -6],
);
}
4 changes: 2 additions & 2 deletions tests/search_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ fn search_from_ordered_by() {
QueryBuilder::insert()
.edges()
.from("users")
.to(users.ids())
.to(users)
.query(),
10,
);
Expand Down Expand Up @@ -377,7 +377,7 @@ fn search_to_ordered_by() {
db.exec_mut(
QueryBuilder::insert()
.edges()
.from(users.ids())
.from(users)
.to("users")
.query(),
10,
Expand Down
4 changes: 2 additions & 2 deletions tests/search_where_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn create_db() -> TestDb {
QueryBuilder::insert()
.edges()
.from("docs")
.to(docs.ids())
.to(&docs)
.query(),
3,
);
Expand Down Expand Up @@ -102,7 +102,7 @@ fn create_db() -> TestDb {
QueryBuilder::insert()
.edges()
.from("users")
.to(users.ids())
.to(&users)
.query(),
5,
);
Expand Down