Skip to main content

agdb/query/
insert_aliases_query.rs

1use crate::DbError;
2use crate::DbImpl;
3use crate::QueryIds;
4use crate::QueryMut;
5use crate::QueryResult;
6use crate::StorageData;
7
8/// Query to insert or update aliases of existing nodes.
9/// All `ids` must exist. None of the `aliases` can be empty.
10/// If there is an existing alias for any of the elements it
11/// will be overwritten with a new one.
12///
13/// NOTE: Setting `ids` to a search query will result in an error.
14///
15/// The result will contain number of aliases inserted/updated but no elements.
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
18#[cfg_attr(feature = "derive", derive(agdb::DbSerialize))]
19#[cfg_attr(feature = "api", derive(agdb::TypeDefImpl))]
20#[derive(Clone, Debug, PartialEq)]
21pub struct InsertAliasesQuery {
22    /// Ids to be aliased
23    pub ids: QueryIds,
24
25    /// Aliases to be inserted
26    pub aliases: Vec<String>,
27}
28
29impl QueryMut for InsertAliasesQuery {
30    fn process<Store: StorageData>(&self, db: &mut DbImpl<Store>) -> Result<QueryResult, DbError> {
31        let mut result = QueryResult::default();
32
33        match &self.ids {
34            QueryIds::Ids(ids) => {
35                if ids.len() != self.aliases.len() {
36                    return Err(DbError::from("Ids and aliases must have the same length"));
37                }
38
39                for (id, alias) in ids.iter().zip(&self.aliases) {
40                    if alias.is_empty() {
41                        return Err(DbError::from("Empty alias is not allowed"));
42                    }
43
44                    let db_id = db.db_id(id)?;
45                    db.insert_alias(db_id, alias)?;
46                    result.result += 1;
47                }
48            }
49            QueryIds::Search(_) => {
50                return Err(DbError::from(
51                    "Insert aliases query does not support search queries",
52                ));
53            }
54        }
55
56        Ok(result)
57    }
58}
59
60impl QueryMut for &InsertAliasesQuery {
61    fn process<Store: StorageData>(&self, db: &mut DbImpl<Store>) -> Result<QueryResult, DbError> {
62        (*self).process(db)
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use crate::Db;
70    use crate::DbId;
71    use crate::SearchQueryAlgorithm;
72    use crate::query::query_id::QueryId;
73    use crate::query::search_query::SearchQuery;
74    use crate::test_utilities::test_file::TestFile;
75
76    #[test]
77    fn invalid_query() {
78        let test_file = TestFile::new();
79        let mut db = Db::new(test_file.file_name()).unwrap();
80        let query = InsertAliasesQuery {
81            ids: QueryIds::Search(SearchQuery {
82                algorithm: SearchQueryAlgorithm::BreadthFirst,
83                origin: QueryId::Id(DbId(0)),
84                destination: QueryId::Id(DbId(0)),
85                limit: 0,
86                offset: 0,
87                order_by: vec![],
88                conditions: vec![],
89            }),
90            aliases: vec![],
91        };
92        assert_eq!(
93            query.process(&mut db).unwrap_err(),
94            DbError::from("Insert aliases query does not support search queries")
95        );
96    }
97}