Skip to content

Commit

Permalink
[db] Return QueryResult from Query::process #628 (#632)
Browse files Browse the repository at this point in the history
update query and querymut
  • Loading branch information
michaelvlach authored Jul 8, 2023
1 parent f9f9c32 commit d92e465
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/agdb/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use crate::QueryError;
use crate::QueryResult;

pub trait Query {
fn process(&self, db: &Db, result: &mut QueryResult) -> Result<(), QueryError>;
fn process(&self, db: &Db) -> Result<QueryResult, QueryError>;
}

pub trait QueryMut {
fn process(&self, db: &mut Db, result: &mut QueryResult) -> Result<(), QueryError>;
fn process(&self, db: &mut Db) -> Result<QueryResult, QueryError>;
}
43 changes: 27 additions & 16 deletions src/agdb/query/insert_aliases_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,35 @@ pub struct InsertAliasesQuery {
}

impl QueryMut for InsertAliasesQuery {
fn process(&self, db: &mut Db, result: &mut QueryResult) -> Result<(), QueryError> {
if let QueryIds::Ids(ids) = &self.ids {
if ids.len() != self.aliases.len() {
return Err(QueryError::from(
"Ids and aliases must have the same length",
));
}
fn process(&self, db: &mut Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

for (id, alias) in ids.iter().zip(&self.aliases) {
if alias.is_empty() {
return Err(QueryError::from("Empty alias is not allowed"));
match &self.ids {
QueryIds::Ids(ids) => {
if ids.len() != self.aliases.len() {
return Err(QueryError::from(
"Ids and aliases must have the same length",
));
}

let db_id = db.db_id(id)?;
db.insert_alias(db_id, alias)?;
result.result += 1;
for (id, alias) in ids.iter().zip(&self.aliases) {
if alias.is_empty() {
return Err(QueryError::from("Empty alias is not allowed"));
}

let db_id = db.db_id(id)?;
db.insert_alias(db_id, alias)?;
result.result += 1;
}
}
QueryIds::Search(_) => {
return Err(QueryError::from(
"Insert aliases query does not support search queries",
));
}
}

Ok(())
Ok(result)
}
}

Expand All @@ -46,7 +55,6 @@ mod tests {
fn invalid_query() {
let test_file = TestFile::new();
let mut db = Db::new(test_file.file_name()).unwrap();
let mut result = QueryResult::default();
let query = InsertAliasesQuery {
ids: QueryIds::Search(SearchQuery {
algorithm: SearchQueryAlgorithm::BreadthFirst,
Expand All @@ -59,6 +67,9 @@ mod tests {
}),
aliases: vec![],
};
assert_eq!(query.process(&mut db, &mut result), Ok(()));
assert_eq!(
query.process(&mut db).unwrap_err(),
QueryError::from("Insert aliases query does not support search queries")
);
}
}
6 changes: 4 additions & 2 deletions src/agdb/query/insert_edges_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ pub struct InsertEdgesQuery {
}

impl QueryMut for InsertEdgesQuery {
fn process(&self, db: &mut Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &mut Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

let from = Self::db_ids(&self.from, db)?;
let to: Vec<DbId> = Self::db_ids(&self.to, db)?;

Expand All @@ -32,7 +34,7 @@ impl QueryMut for InsertEdgesQuery {
.map(|id| DbElement { id, values: vec![] })
.collect();

Ok(())
Ok(result)
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/agdb/query/insert_nodes_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub struct InsertNodesQuery {
}

impl QueryMut for InsertNodesQuery {
fn process(&self, db: &mut Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &mut Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();
let mut ids = vec![];
let count = std::cmp::max(self.count, self.aliases.len() as u64);
let values = match &self.values {
Expand All @@ -39,6 +40,6 @@ impl QueryMut for InsertNodesQuery {
.map(|id| DbElement { id, values: vec![] })
.collect();

Ok(())
Ok(result)
}
}
6 changes: 4 additions & 2 deletions src/agdb/query/insert_values_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ pub struct InsertValuesQuery {
}

impl QueryMut for InsertValuesQuery {
fn process(&self, db: &mut Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &mut Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

match &self.ids {
QueryIds::Ids(ids) => match &self.values {
QueryValues::Single(values) => {
Expand Down Expand Up @@ -65,6 +67,6 @@ impl QueryMut for InsertValuesQuery {
}
}

Ok(())
Ok(result)
}
}
6 changes: 4 additions & 2 deletions src/agdb/query/remove_aliases_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use crate::QueryResult;
pub struct RemoveAliasesQuery(pub Vec<String>);

impl QueryMut for RemoveAliasesQuery {
fn process(&self, db: &mut Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &mut Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

for alias in &self.0 {
if db.remove_alias(alias)? {
result.result -= 1;
}
}

Ok(())
Ok(result)
}
}
6 changes: 4 additions & 2 deletions src/agdb/query/remove_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::QueryResult;
pub struct RemoveQuery(pub QueryIds);

impl QueryMut for RemoveQuery {
fn process(&self, db: &mut Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &mut Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

match &self.0 {
QueryIds::Ids(ids) => {
for id in ids {
Expand All @@ -25,6 +27,6 @@ impl QueryMut for RemoveQuery {
}
}

Ok(())
Ok(result)
}
}
6 changes: 4 additions & 2 deletions src/agdb/query/remove_values_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::QueryResult;
pub struct RemoveValuesQuery(pub SelectValuesQuery);

impl QueryMut for RemoveValuesQuery {
fn process(&self, db: &mut Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &mut Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

match &self.0.ids {
QueryIds::Ids(ids) => {
for id in ids {
Expand All @@ -23,6 +25,6 @@ impl QueryMut for RemoveValuesQuery {
}
}

Ok(())
Ok(result)
}
}
6 changes: 4 additions & 2 deletions src/agdb/query/search_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ pub struct SearchQuery {
}

impl Query for SearchQuery {
fn process(&self, db: &Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

for id in self.search(db)? {
result.elements.push(DbElement { id, values: vec![] });
}

result.result = result.elements.len() as i64;

Ok(())
Ok(result)
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/agdb/query/select_aliases_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use crate::QueryResult;
pub struct SelectAliasesQuery(pub QueryIds);

impl Query for SelectAliasesQuery {
fn process(&self, db: &Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

match &self.0 {
QueryIds::Ids(ids) => {
result.elements.reserve(ids.len());
Expand Down Expand Up @@ -42,6 +44,6 @@ impl Query for SelectAliasesQuery {
}
}

Ok(())
Ok(result)
}
}
6 changes: 4 additions & 2 deletions src/agdb/query/select_all_aliases_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::QueryResult;
pub struct SelectAllAliases {}

impl Query for SelectAllAliases {
fn process(&self, db: &Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

let mut aliases = db.aliases();
aliases.sort();
result.elements.reserve(aliases.len());
Expand All @@ -20,6 +22,6 @@ impl Query for SelectAllAliases {
});
}

Ok(())
Ok(result)
}
}
6 changes: 4 additions & 2 deletions src/agdb/query/select_key_count_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::QueryResult;
pub struct SelectKeyCountQuery(pub QueryIds);

impl Query for SelectKeyCountQuery {
fn process(&self, db: &Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

let db_ids = match &self.0 {
QueryIds::Ids(ids) => {
let mut db_ids = vec![];
Expand All @@ -33,6 +35,6 @@ impl Query for SelectKeyCountQuery {
});
}

Ok(())
Ok(result)
}
}
6 changes: 4 additions & 2 deletions src/agdb/query/select_keys_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::QueryResult;
pub struct SelectKeysQuery(pub QueryIds);

impl Query for SelectKeysQuery {
fn process(&self, db: &Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

let db_ids = match &self.0 {
QueryIds::Ids(ids) => {
let mut db_ids = vec![];
Expand All @@ -33,6 +35,6 @@ impl Query for SelectKeysQuery {
});
}

Ok(())
Ok(result)
}
}
7 changes: 5 additions & 2 deletions src/agdb/query/select_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::QueryResult;
pub struct SelectQuery(pub QueryIds);

impl Query for SelectQuery {
fn process(&self, db: &Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

let db_ids = match &self.0 {
QueryIds::Ids(ids) => {
let mut db_ids = vec![];
Expand All @@ -32,6 +34,7 @@ impl Query for SelectQuery {
values: db.values(db_id)?,
});
}
Ok(())

Ok(result)
}
}
6 changes: 4 additions & 2 deletions src/agdb/query/select_values_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub struct SelectValuesQuery {
}

impl Query for SelectValuesQuery {
fn process(&self, db: &Db, result: &mut QueryResult) -> Result<(), QueryError> {
fn process(&self, db: &Db) -> Result<QueryResult, QueryError> {
let mut result = QueryResult::default();

let (db_ids, is_search) = match &self.ids {
QueryIds::Ids(ids) => {
let mut db_ids = vec![];
Expand Down Expand Up @@ -47,6 +49,6 @@ impl Query for SelectValuesQuery {
result.elements.push(DbElement { id: db_id, values });
}

Ok(())
Ok(result)
}
}
9 changes: 1 addition & 8 deletions src/agdb/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ impl<'a> Transaction<'a> {
}

pub fn exec<T: Query>(&self, query: &T) -> Result<QueryResult, QueryError> {
let mut result = QueryResult {
result: 0,
elements: vec![],
};

query.process(self.db, &mut result)?;

Ok(result)
query.process(self.db)
}
}
9 changes: 1 addition & 8 deletions src/agdb/transaction_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ impl<'a> TransactionMut<'a> {
}

pub fn exec_mut<T: QueryMut>(&mut self, query: &T) -> Result<QueryResult, QueryError> {
let mut result = QueryResult {
result: 0,
elements: vec![],
};

query.process(self.db, &mut result)?;

Ok(result)
query.process(self.db)
}

pub(crate) fn commit(self) -> Result<(), QueryError> {
Expand Down

0 comments on commit d92e465

Please sign in to comment.