Skip to content

Commit

Permalink
[db] Allow slice or array for DbKeyOrder #1271 (#1272)
Browse files Browse the repository at this point in the history
order_by from slice or array or vec
  • Loading branch information
michaelvlach authored Sep 16, 2024
1 parent 95626bf commit 94989a0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 43 deletions.
27 changes: 27 additions & 0 deletions agdb/src/db/db_key_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ pub enum DbKeyOrder {
Desc(DbValue),
}

pub struct DbKeyOrders(pub Vec<DbKeyOrder>);

impl From<Vec<DbKeyOrder>> for DbKeyOrders {
fn from(orders: Vec<DbKeyOrder>) -> Self {
Self(orders)
}
}

impl From<&[DbKeyOrder]> for DbKeyOrders {
fn from(orders: &[DbKeyOrder]) -> Self {
Self(orders.to_vec())
}
}

impl<const N: usize> From<[DbKeyOrder; N]> for DbKeyOrders {
fn from(orders: [DbKeyOrder; N]) -> Self {
Self(orders.to_vec())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -37,4 +57,11 @@ mod tests {
DbKeyOrder::Asc(DbValue::default())
);
}

#[test]
fn db_key_orders() {
let _orders = DbKeyOrders(vec![DbKeyOrder::Asc(1.into())]);
let _orders = DbKeyOrders::from([DbKeyOrder::Asc(1.into())].as_slice());
let _orders = DbKeyOrders::from([DbKeyOrder::Asc(1.into())]);
}
}
56 changes: 28 additions & 28 deletions agdb/src/query_builder/search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::where_::Where;
use crate::db::db_key_order::DbKeyOrders;
use crate::Comparison;
use crate::DbKeyOrder;
use crate::DbValue;
use crate::QueryCondition;
use crate::QueryConditionData;
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Search {
/// ```
/// use agdb::{QueryBuilder, DbKeyOrder};
///
/// QueryBuilder::search().elements().order_by(vec![DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().elements().order_by([DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().elements().offset(5);
/// QueryBuilder::search().elements().limit(10);
/// QueryBuilder::search().elements().where_();
Expand Down Expand Up @@ -134,7 +134,7 @@ impl Search {
///
/// QueryBuilder::search().from(1).query();
/// QueryBuilder::search().from(1).to(2);
/// QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().from(1).order_by([DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().from(1).offset(5);
/// QueryBuilder::search().from(1).limit(10);
/// QueryBuilder::search().from(1).where_();
Expand All @@ -159,7 +159,7 @@ impl Search {
/// ```
/// use agdb::{QueryBuilder, DbKeyOrder};
///
/// QueryBuilder::search().to(1).order_by(vec![DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().to(1).order_by([DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().to(1).offset(5);
/// QueryBuilder::search().to(1).limit(10);
/// QueryBuilder::search().to(1).where_();
Expand Down Expand Up @@ -187,7 +187,7 @@ impl SearchAlgorithm {
///
/// QueryBuilder::search().depth_first().from(1).query();
/// QueryBuilder::search().depth_first().from(1).to(2);
/// QueryBuilder::search().depth_first().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().depth_first().from(1).order_by([DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().depth_first().from(1).offset(5);
/// QueryBuilder::search().depth_first().from(1).limit(10);
/// QueryBuilder::search().depth_first().from(1).where_();
Expand All @@ -205,7 +205,7 @@ impl SearchAlgorithm {
/// ```
/// use agdb::{QueryBuilder, DbKeyOrder};
///
/// QueryBuilder::search().depth_first().to(1).order_by(vec![DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().depth_first().to(1).order_by([DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().depth_first().to(1).offset(5);
/// QueryBuilder::search().depth_first().to(1).limit(10);
/// QueryBuilder::search().depth_first().to(1).where_();
Expand Down Expand Up @@ -264,13 +264,13 @@ impl SearchFrom {
/// ```
/// use agdb::{QueryBuilder, DbKeyOrder};
///
/// QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]).query();
/// QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]).offset(10);
/// QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]).limit(5);
/// QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]).where_();
/// QueryBuilder::search().from(1).order_by([DbKeyOrder::Asc("k".into())]).query();
/// QueryBuilder::search().from(1).order_by([DbKeyOrder::Asc("k".into())]).offset(10);
/// QueryBuilder::search().from(1).order_by([DbKeyOrder::Asc("k".into())]).limit(5);
/// QueryBuilder::search().from(1).order_by([DbKeyOrder::Asc("k".into())]).where_();
/// ```
pub fn order_by(mut self, keys: Vec<DbKeyOrder>) -> SearchOrderBy {
self.0.order_by = keys;
pub fn order_by<T: Into<DbKeyOrders>>(mut self, keys: T) -> SearchOrderBy {
self.0.order_by = Into::<DbKeyOrders>::into(keys).0;

SearchOrderBy(self.0)
}
Expand All @@ -289,7 +289,7 @@ impl SearchFrom {
/// use agdb::{QueryBuilder, DbKeyOrder};
///
/// QueryBuilder::search().from(1).to(2).query();
/// QueryBuilder::search().from(1).to(2).order_by(vec![DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().from(1).to(2).order_by([DbKeyOrder::Asc("k".into())]);
/// QueryBuilder::search().from(1).to(2).offset(10);
/// QueryBuilder::search().from(1).to(2).limit(5);
/// QueryBuilder::search().from(1).to(2).where_();
Expand Down Expand Up @@ -337,8 +337,8 @@ impl SearchOrderBy {
/// ```
/// use agdb::{QueryBuilder, DbKeyOrder};
///
/// QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]).limit(10).query();
/// QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]).limit(10).where_();
/// QueryBuilder::search().from(1).order_by([DbKeyOrder::Asc("k".into())]).limit(10).query();
/// QueryBuilder::search().from(1).order_by([DbKeyOrder::Asc("k".into())]).limit(10).where_();
/// ```
pub fn limit(mut self, value: u64) -> SelectLimit {
self.0.limit = value;
Expand All @@ -357,9 +357,9 @@ impl SearchOrderBy {
/// ```
/// use agdb::{QueryBuilder, DbKeyOrder};
///
/// QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]).offset(10).query();
/// QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]).offset(10).limit(5);
/// QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]).offset(10).where_();
/// QueryBuilder::search().from(1).order_by([DbKeyOrder::Asc("k".into())]).offset(10).query();
/// QueryBuilder::search().from(1).order_by([DbKeyOrder::Asc("k".into())]).offset(10).limit(5);
/// QueryBuilder::search().from(1).order_by([DbKeyOrder::Asc("k".into())]).offset(10).where_();
/// ```
pub fn offset(mut self, value: u64) -> SelectOffset {
self.0.offset = value;
Expand Down Expand Up @@ -409,8 +409,8 @@ impl SearchTo {
/// ```
/// use agdb::{QueryBuilder, DbKeyOrder};
///
/// QueryBuilder::search().to(1).order_by(vec![DbKeyOrder::Asc("k".into())]).limit(10).query();
/// QueryBuilder::search().to(1).order_by(vec![DbKeyOrder::Asc("k".into())]).limit(10).where_();
/// QueryBuilder::search().to(1).order_by([DbKeyOrder::Asc("k".into())]).limit(10).query();
/// QueryBuilder::search().to(1).order_by([DbKeyOrder::Asc("k".into())]).limit(10).where_();
/// ```
pub fn limit(mut self, value: u64) -> SelectLimit {
self.0.limit = value;
Expand All @@ -429,9 +429,9 @@ impl SearchTo {
/// ```
/// use agdb::{QueryBuilder, DbKeyOrder};
///
/// QueryBuilder::search().to(1).order_by(vec![DbKeyOrder::Asc("k".into())]).offset(10).query();
/// QueryBuilder::search().to(1).order_by(vec![DbKeyOrder::Asc("k".into())]).offset(10).limit(5);
/// QueryBuilder::search().to(1).order_by(vec![DbKeyOrder::Asc("k".into())]).offset(10).where_();
/// QueryBuilder::search().to(1).order_by([DbKeyOrder::Asc("k".into())]).offset(10).query();
/// QueryBuilder::search().to(1).order_by([DbKeyOrder::Asc("k".into())]).offset(10).limit(5);
/// QueryBuilder::search().to(1).order_by([DbKeyOrder::Asc("k".into())]).offset(10).where_();
/// ```
pub fn offset(mut self, value: u64) -> SelectOffset {
self.0.offset = value;
Expand All @@ -450,12 +450,12 @@ impl SearchTo {
/// ```
/// use agdb::{QueryBuilder, DbKeyOrder};
///
/// QueryBuilder::search().to(1).order_by(vec![DbKeyOrder::Asc("k".into())]).offset(10).query();
/// QueryBuilder::search().to(1).order_by(vec![DbKeyOrder::Asc("k".into())]).offset(10).limit(5);
/// QueryBuilder::search().to(1).order_by(vec![DbKeyOrder::Asc("k".into())]).offset(10).where_();
/// QueryBuilder::search().to(1).order_by([DbKeyOrder::Asc("k".into())]).offset(10).query();
/// QueryBuilder::search().to(1).order_by([DbKeyOrder::Asc("k".into())]).offset(10).limit(5);
/// QueryBuilder::search().to(1).order_by([DbKeyOrder::Asc("k".into())]).offset(10).where_();
/// ```
pub fn order_by(mut self, keys: Vec<DbKeyOrder>) -> SearchOrderBy {
self.0.order_by = keys;
pub fn order_by<T: Into<DbKeyOrders>>(mut self, keys: T) -> SearchOrderBy {
self.0.order_by = Into::<DbKeyOrders>::into(keys).0;

SearchOrderBy(self.0)
}
Expand Down
6 changes: 3 additions & 3 deletions agdb/tests/search_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ fn search_from_ordered_by() {
.ids(
QueryBuilder::search()
.from("users")
.order_by(vec![
.order_by([
DbKeyOrder::Desc("age".into()),
DbKeyOrder::Asc("name".into()),
])
Expand Down Expand Up @@ -375,7 +375,7 @@ fn search_from_ordered_by_limit_offset() {
.ids(
QueryBuilder::search()
.from("users")
.order_by(vec![
.order_by([
DbKeyOrder::Desc("age".into()),
DbKeyOrder::Asc("name".into()),
])
Expand Down Expand Up @@ -443,7 +443,7 @@ fn search_to_ordered_by() {
.ids(
QueryBuilder::search()
.to("users")
.order_by(vec![
.order_by([
DbKeyOrder::Asc("age".into()),
DbKeyOrder::Desc("name".into()),
])
Expand Down
14 changes: 7 additions & 7 deletions agdb_server/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ QueryBuilder::search().depth_first().to(1).query(),
QueryBuilder::search().depth_first().from("a").query(),
QueryBuilder::search().elements().query(),
QueryBuilder::search().index("age").value(20).query(),
QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Desc("age".into()), DbKeyOrder::Asc("name".into())]).query(),
QueryBuilder::search().from(1).order_by([DbKeyOrder::Desc("age".into()), DbKeyOrder::Asc("name".into())]).query(),
QueryBuilder::search().from(1).offset(10).query(),
QueryBuilder::search().from(1).limit(5).query(),
QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Desc("k".into())]).offset(10).query(),
QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Desc("k".into())]).limit(5).query(),
QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Desc("k".into())]).offset(10).limit(5).query(),
QueryBuilder::search().from(1).order_by([DbKeyOrder::Desc("k".into())]).offset(10).query(),
QueryBuilder::search().from(1).order_by([DbKeyOrder::Desc("k".into())]).limit(5).query(),
QueryBuilder::search().from(1).order_by([DbKeyOrder::Desc("k".into())]).offset(10).limit(5).query(),
QueryBuilder::search().from(1).offset(10).limit(5).query(),
QueryBuilder::search().from(1).where_().distance(CountComparison::LessThan(3)).query(),
QueryBuilder::search().from(1).where_().edge().query(),
Expand All @@ -273,14 +273,14 @@ QueryBuilder::search().from(1).where_().node().or().edge().query(),
QueryBuilder::search().from(1).where_().node().and().distance(CountComparison::GreaterThanOrEqual(3)).query(),
QueryBuilder::search().from(1).where_().node().or().where_().edge().and().key("k").value(Comparison::Equal(1.into())).end_where().query(),
QueryBuilder::search().from(1).where_().node().or().where_().edge().and().key("k").value(Comparison::Contains(1.into())).end_where().query(),
QueryBuilder::search().from(1).where_().node().or().where_().edge().and().key("k").value(Comparison::Contains((vec![1, 2]).into())).end_where().query(),
QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Asc("k".into())]).where_().node().query(),
QueryBuilder::search().from(1).where_().node().or().where_().edge().and().key("k").value(Comparison::Contains(([1, 2]).into())).end_where().query(),
QueryBuilder::search().from(1).order_by([DbKeyOrder::Asc("k".into())]).where_().node().query(),
QueryBuilder::search().from(1).limit(1).where_().node().query(),
QueryBuilder::search().from(1).offset(1).where_().node().query(),
QueryBuilder::search().to(1).offset(1).query(),
QueryBuilder::search().to(1).limit(1).query(),
QueryBuilder::search().to(1).where_().node().query(),
QueryBuilder::search().to(1).order_by(vec![DbKeyOrder::Asc("k".into())]).where_().node().query()
QueryBuilder::search().to(1).order_by([DbKeyOrder::Asc("k".into())]).where_().node().query()
];

serde_json::to_writer_pretty(File::create("test_queries.json").unwrap(), &queries).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion agdb_web/pages/docs/references/efficient-agdb.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ fn liked_posts(db: &Db, offset: u64, limit: u64) -> Result<Vec<PostLiked>, Query
.ids(
QueryBuilder::search()
.from("posts")
.order_by(vec![DbKeyOrder::Desc("likes".into())])
.order_by([DbKeyOrder::Desc("likes".into())])
.offset(offset)
.limit(limit)
.where_()
Expand Down
8 changes: 4 additions & 4 deletions agdb_web/pages/docs/references/queries.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -993,12 +993,12 @@ QueryBuilder::search().depth_first().from("a").query();
QueryBuilder::search().elements().query();
QueryBuilder::search().index("age").value(20).query(); //index search
//limit, offset and order_by can be applied similarly to all the search variants except search index
QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Desc("age".into()), DbKeyOrder::Asc("name".into())]).query()
QueryBuilder::search().from(1).order_by([DbKeyOrder::Desc("age".into()), DbKeyOrder::Asc("name".into())]).query()
QueryBuilder::search().from(1).offset(10).query();
QueryBuilder::search().from(1).limit(5).query();
QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Desc("k".into())]).offset(10).query();
QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Desc("k".into())]).limit(5).query();
QueryBuilder::search().from(1).order_by(vec![DbKeyOrder::Desc("k".into())]).offset(10).limit(5).query();
QueryBuilder::search().from(1).order_by([DbKeyOrder::Desc("k".into())]).offset(10).query();
QueryBuilder::search().from(1).order_by([DbKeyOrder::Desc("k".into())]).limit(5).query();
QueryBuilder::search().from(1).order_by([DbKeyOrder::Desc("k".into())]).offset(10).limit(5).query();
QueryBuilder::search().from(1).offset(10).limit(5).query();
```

Expand Down

0 comments on commit 94989a0

Please sign in to comment.