move over to variable in Find and Order and simplify names

This commit is contained in:
Emily Toop 2018-03-23 16:19:14 +00:00
parent 06c2d3c179
commit 9c756e4f90

View file

@ -19,12 +19,6 @@ use mentat_query::{
Variable, Variable,
}; };
use edn::{
DateTime,
Utc,
Uuid,
};
use errors::{ use errors::{
Result, Result,
}; };
@ -138,6 +132,22 @@ pub struct WhereBuilder {
pub trait ClauseBuilder {} pub trait ClauseBuilder {}
pub trait ClauseAggregator {
fn add<T>(self, clause: T) -> Self where T: 'static + ClauseBuilder;
fn clause<F>(self, clause_fn: F) -> Self where F: 'static + FnOnce(WhereClauseBuilder) -> WhereClauseBuilder;
fn or<F>(self, or_fn: F) -> Self where F: 'static + FnOnce(OrClauseBuilder) -> OrClauseBuilder;
fn not<F>(self, not_fn: F) -> Self where F: 'static + FnOnce(NotClauseBuilder) -> NotClauseBuilder;
fn and<F>(self, and_fn: F) -> Self where F: 'static + FnOnce(AndClauseBuilder) -> AndClauseBuilder;
}
pub trait JoinClause {
fn join(self, var: Variable) -> Self;
}
#[derive(Default)] #[derive(Default)]
pub struct WhereClauseBuilder { pub struct WhereClauseBuilder {
entity: Option<EntityType>, entity: Option<EntityType>,
@ -181,23 +191,37 @@ pub struct OrderBuilder {
} }
impl QueryBuilder { impl QueryBuilder {
pub fn add_find<F>(mut self, builder_fn: F) -> Self where F: 'static + FnOnce(FindBuilder) -> FindBuilder { pub fn find<F>(mut self, builder_fn: F) -> Self where F: 'static + FnOnce(FindBuilder) -> FindBuilder {
self.find_builder = Some(builder_fn(FindBuilder::new())); self.find_builder = Some(builder_fn(FindBuilder::new()));
// self.find_builder = Some(builder);
self self
} }
pub fn add_where<F>(mut self, builder_fn: F) -> Self where F: 'static + FnOnce(WhereBuilder) -> WhereBuilder { pub fn find_scalar<F>(mut self, builder_fn: F) -> Self where F: 'static + FnOnce(FindBuilder) -> FindBuilder {
self.find_builder = Some(builder_fn(FindBuilder::with_type(FindType::Scalar)));
self
}
pub fn find_coll<F>(mut self, builder_fn: F) -> Self where F: 'static + FnOnce(FindBuilder) -> FindBuilder {
self.find_builder = Some(builder_fn(FindBuilder::with_type(FindType::Coll)));
self
}
pub fn find_tuple<F>(mut self, builder_fn: F) -> Self where F: 'static + FnOnce(FindBuilder) -> FindBuilder {
self.find_builder = Some(builder_fn(FindBuilder::with_type(FindType::Tuple)));
self
}
pub fn wheres<F>(mut self, builder_fn: F) -> Self where F: 'static + FnOnce(WhereBuilder) -> WhereBuilder {
self.where_builder = Some(builder_fn(WhereBuilder::default())); self.where_builder = Some(builder_fn(WhereBuilder::default()));
self self
} }
pub fn add_order<F>(mut self, builder_fn: F) -> Self where F: 'static + FnOnce(OrderBuilder) -> OrderBuilder { pub fn order_by<F>(mut self, builder_fn: F) -> Self where F: 'static + FnOnce(OrderBuilder) -> OrderBuilder {
self.order_builder = Some(builder_fn(OrderBuilder::default())); self.order_builder = Some(builder_fn(OrderBuilder::default()));
self self
} }
pub fn add_limit(mut self, limit: i32) -> Self { pub fn limit(mut self, limit: i32) -> Self {
self.limit = Some(limit); self.limit = Some(limit);
self self
} }
@ -228,32 +252,37 @@ impl FindBuilder {
self self
} }
pub fn add<'a, T>(mut self, var: T) -> Self where T: Into<&'a str> { pub fn var(mut self, var: Variable) -> Self {
self.vars.push(Variable::from_valid_name(var.into())); self.vars.push(var);
self self
} }
} }
impl WhereBuilder { impl ClauseAggregator for WhereBuilder {
pub fn add<T>(mut self, clause: T) -> Self where T: 'static + ClauseBuilder { fn add<T>(mut self, clause: T) -> Self where T: 'static + ClauseBuilder {
self.clauses.push(Box::new(clause)); self.clauses.push(Box::new(clause));
self self
} }
pub fn add_clause<F>(mut self, clause_fn: F) -> Self where F: 'static + FnOnce(WhereClauseBuilder) -> WhereClauseBuilder { fn clause<F>(mut self, clause_fn: F) -> Self where F: 'static + FnOnce(WhereClauseBuilder) -> WhereClauseBuilder {
self.clauses.push(Box::new(clause_fn(WhereClauseBuilder::default()))); self.clauses.push(Box::new(clause_fn(WhereClauseBuilder::default())));
self self
} }
pub fn add_or<F>(mut self, or_fn: F) -> Self where F: 'static + FnOnce(OrClauseBuilder) -> OrClauseBuilder { fn or<F>(mut self, or_fn: F) -> Self where F: 'static + FnOnce(OrClauseBuilder) -> OrClauseBuilder {
self.clauses.push(Box::new(or_fn(OrClauseBuilder::default()))); self.clauses.push(Box::new(or_fn(OrClauseBuilder::default())));
self self
} }
pub fn add_not<F>(mut self, not_fn: F) -> Self where F: 'static + FnOnce(NotClauseBuilder) -> NotClauseBuilder { fn not<F>(mut self, not_fn: F) -> Self where F: 'static + FnOnce(NotClauseBuilder) -> NotClauseBuilder {
self.clauses.push(Box::new(not_fn(NotClauseBuilder::default()))); self.clauses.push(Box::new(not_fn(NotClauseBuilder::default())));
self self
} }
fn and<F>(mut self, and_fn: F) -> Self where F: 'static + FnOnce(AndClauseBuilder) -> AndClauseBuilder {
self.clauses.push(Box::new(and_fn(AndClauseBuilder::default())));
self
}
} }
impl WhereClauseBuilder { impl WhereClauseBuilder {
@ -273,109 +302,113 @@ impl WhereClauseBuilder {
} }
} }
impl NotClauseBuilder { impl JoinClause for NotClauseBuilder {
pub fn add<T>(mut self, clause: T) -> Self where T: 'static + ClauseBuilder { fn join(mut self, var: Variable) -> Self {
self.join = Some(var);
self
}
}
impl ClauseAggregator for NotClauseBuilder {
fn add<T>(mut self, clause: T) -> Self where T: 'static + ClauseBuilder {
self.clauses.push(Box::new(clause)); self.clauses.push(Box::new(clause));
self self
} }
pub fn join(mut self, var: &str) -> Self { fn clause<F>(mut self, clause_fn: F) -> Self where F: 'static + FnOnce(WhereClauseBuilder) -> WhereClauseBuilder {
self.join = Some(Variable::from_valid_name(var));
self
}
pub fn add_clause<F>(mut self, clause_fn: F) -> Self where F: 'static + FnOnce(WhereClauseBuilder) -> WhereClauseBuilder {
self.clauses.push(Box::new(clause_fn(WhereClauseBuilder::default()))); self.clauses.push(Box::new(clause_fn(WhereClauseBuilder::default())));
self self
} }
pub fn add_or<F>(mut self, or_fn: F) -> Self where F: 'static + FnOnce(OrClauseBuilder) -> OrClauseBuilder { fn or<F>(mut self, or_fn: F) -> Self where F: 'static + FnOnce(OrClauseBuilder) -> OrClauseBuilder {
self.clauses.push(Box::new(or_fn(OrClauseBuilder::default()))); self.clauses.push(Box::new(or_fn(OrClauseBuilder::default())));
self self
} }
pub fn add_not<F>(mut self, not_fn: F) -> Self where F: 'static + FnOnce(NotClauseBuilder) -> NotClauseBuilder { fn not<F>(mut self, not_fn: F) -> Self where F: 'static + FnOnce(NotClauseBuilder) -> NotClauseBuilder {
self.clauses.push(Box::new(not_fn(NotClauseBuilder::default()))); self.clauses.push(Box::new(not_fn(NotClauseBuilder::default())));
self self
} }
pub fn add_and<F>(mut self, and_fn: F) -> Self where F: 'static + FnOnce(AndClauseBuilder) -> AndClauseBuilder { fn and<F>(mut self, and_fn: F) -> Self where F: 'static + FnOnce(AndClauseBuilder) -> AndClauseBuilder {
self.clauses.push(Box::new(and_fn(AndClauseBuilder::default()))); self.clauses.push(Box::new(and_fn(AndClauseBuilder::default())));
self self
} }
} }
impl OrClauseBuilder { impl JoinClause for OrClauseBuilder {
pub fn add<T>(mut self, clause: T) -> Self where T: 'static + ClauseBuilder { fn join(mut self, var: Variable) -> Self {
self.join = Some(var);
self
}
}
impl ClauseAggregator for OrClauseBuilder {
fn add<T>(mut self, clause: T) -> Self where T: 'static + ClauseBuilder {
self.clauses.push(Box::new(clause)); self.clauses.push(Box::new(clause));
self self
} }
pub fn join(mut self, var: &str) -> Self { fn clause<F>(mut self, clause_fn: F) -> Self where F: 'static + FnOnce(WhereClauseBuilder) -> WhereClauseBuilder {
self.join = Some(Variable::from_valid_name(var));
self
}
pub fn add_clause<F>(mut self, clause_fn: F) -> Self where F: 'static + FnOnce(WhereClauseBuilder) -> WhereClauseBuilder {
self.clauses.push(Box::new(clause_fn(WhereClauseBuilder::default()))); self.clauses.push(Box::new(clause_fn(WhereClauseBuilder::default())));
self self
} }
pub fn add_or<F>(mut self, or_fn: F) -> Self where F: 'static + FnOnce(OrClauseBuilder) -> OrClauseBuilder { fn or<F>(mut self, or_fn: F) -> Self where F: 'static + FnOnce(OrClauseBuilder) -> OrClauseBuilder {
self.clauses.push(Box::new(or_fn(OrClauseBuilder::default()))); self.clauses.push(Box::new(or_fn(OrClauseBuilder::default())));
self self
} }
pub fn add_not<F>(mut self, not_fn: F) -> Self where F: 'static + FnOnce(NotClauseBuilder) -> NotClauseBuilder { fn not<F>(mut self, not_fn: F) -> Self where F: 'static + FnOnce(NotClauseBuilder) -> NotClauseBuilder {
self.clauses.push(Box::new(not_fn(NotClauseBuilder::default()))); self.clauses.push(Box::new(not_fn(NotClauseBuilder::default())));
self self
} }
pub fn add_and<F>(mut self, and_fn: F) -> Self where F: 'static + FnOnce(AndClauseBuilder) -> AndClauseBuilder { fn and<F>(mut self, and_fn: F) -> Self where F: 'static + FnOnce(AndClauseBuilder) -> AndClauseBuilder {
self.clauses.push(Box::new(and_fn(AndClauseBuilder::default()))); self.clauses.push(Box::new(and_fn(AndClauseBuilder::default())));
self self
} }
} }
impl AndClauseBuilder { impl ClauseAggregator for AndClauseBuilder {
pub fn add<T>(mut self, clause: T) -> Self where T: 'static + ClauseBuilder { fn add<T>(mut self, clause: T) -> Self where T: 'static + ClauseBuilder {
self.clauses.push(Box::new(clause)); self.clauses.push(Box::new(clause));
self self
} }
pub fn add_clause<F>(mut self, clause_fn: F) -> Self where F: 'static + FnOnce(WhereClauseBuilder) -> WhereClauseBuilder { fn clause<F>(mut self, clause_fn: F) -> Self where F: 'static + FnOnce(WhereClauseBuilder) -> WhereClauseBuilder {
self.clauses.push(Box::new(clause_fn(WhereClauseBuilder::default()))); self.clauses.push(Box::new(clause_fn(WhereClauseBuilder::default())));
self self
} }
pub fn add_or<F>(mut self, or_fn: F) -> Self where F: 'static + FnOnce(OrClauseBuilder) -> OrClauseBuilder { fn or<F>(mut self, or_fn: F) -> Self where F: 'static + FnOnce(OrClauseBuilder) -> OrClauseBuilder {
self.clauses.push(Box::new(or_fn(OrClauseBuilder::default()))); self.clauses.push(Box::new(or_fn(OrClauseBuilder::default())));
self self
} }
pub fn add_not<F>(mut self, not_fn: F) -> Self where F: 'static + FnOnce(NotClauseBuilder) -> NotClauseBuilder { fn not<F>(mut self, not_fn: F) -> Self where F: 'static + FnOnce(NotClauseBuilder) -> NotClauseBuilder {
self.clauses.push(Box::new(not_fn(NotClauseBuilder::default()))); self.clauses.push(Box::new(not_fn(NotClauseBuilder::default())));
self self
} }
pub fn add_and<F>(mut self, and_fn: F) -> Self where F: 'static + FnOnce(AndClauseBuilder) -> AndClauseBuilder { fn and<F>(mut self, and_fn: F) -> Self where F: 'static + FnOnce(AndClauseBuilder) -> AndClauseBuilder {
self.clauses.push(Box::new(and_fn(AndClauseBuilder::default()))); self.clauses.push(Box::new(and_fn(AndClauseBuilder::default())));
self self
} }
} }
impl OrderBuilder { impl OrderBuilder {
pub fn add<'a, T>(self, var: T) -> Self where T: Into<&'a str> { pub fn add(self, var: Variable) -> Self {
self.add_ascending(var) self.ascending(var)
} }
pub fn add_ascending<'a, T>(mut self, var: T) -> Self where T: Into<&'a str> { pub fn ascending(mut self, var: Variable) -> Self {
self.orders.push((Variable::from_valid_name(var.into()), QueryOrder::Ascending)); self.orders.push((var, QueryOrder::Ascending));
self self
} }
pub fn add_descending<'a, T>(mut self, var: T) -> Self where T: Into<&'a str> { pub fn descending(mut self, var: Variable) -> Self {
self.orders.push((Variable::from_valid_name(var.into()), QueryOrder::Descending)); self.orders.push((var, QueryOrder::Descending));
self self
} }
} }
@ -394,9 +427,9 @@ mod test {
#[test] #[test]
// [:find ?x :where [?x :foo/bar "yyy"]] // [:find ?x :where [?x :foo/bar "yyy"]]
fn test_find_rel() { fn test_find_rel() {
let _ = query().add_find(|find| find.add("?x")) let _ = query().find(|find| find.var(var!(?x)))
.add_where(|w| { .wheres(|w| {
w.add_clause(|c| c.entity(var!(?x)) w.clause(|c| c.entity(var!(?x))
.attribute(kw!(:foo/bar)) .attribute(kw!(:foo/bar))
.value(QueryValueType::value("yyy"))) .value(QueryValueType::value("yyy")))
}).execute(); }).execute();
@ -406,9 +439,9 @@ mod test {
#[test] #[test]
// [:find ?x :where [?x _ "yyy"]] // [:find ?x :where [?x _ "yyy"]]
fn test_find_no_attribute() { fn test_find_no_attribute() {
let _ = query().add_find(|find| find.add("?x")) let _ = query().find(|find| find.var(var!(?x)))
.add_where(|w| { .wheres(|w| {
w.add_clause(|c| c.entity(var!(?x)) w.clause(|c| c.entity(var!(?x))
.value(QueryValueType::value("yyy"))) .value(QueryValueType::value("yyy")))
}).execute(); }).execute();
panic!("not complete"); panic!("not complete");
@ -417,9 +450,9 @@ mod test {
#[test] #[test]
// [:find ?x . :where [?x :foo/bar "yyy"]] // [:find ?x . :where [?x :foo/bar "yyy"]]
fn test_find_scalar() { fn test_find_scalar() {
let _ = query().add_find(|find| find.set_type(FindType::Scalar).add("?x")) let _ = query().find_scalar(|find| find.var(var!(?x)))
.add_where(|w| { .wheres(|w| {
w.add_clause(|c| c.entity(var!(?x)) w.clause(|c| c.entity(var!(?x))
.attribute(kw!(:foo/bar)) .attribute(kw!(:foo/bar))
.value(QueryValueType::value("yyy"))) .value(QueryValueType::value("yyy")))
}).execute(); }).execute();
@ -436,21 +469,21 @@ mod test {
// [?page :page/description ?description]] // [?page :page/description ?description]]
#[test] #[test]
fn test_find_or_join() { fn test_find_or_join() {
let _ = query().add_find(|find| find.add("?url").add("?description")) let _ = query().find(|find| find.var(var!(?url)).var(var!(?description)))
.add_where(|w| { .wheres(|w| {
w.add_or(|or| { w.or(|or| {
or.join("?page") or.join(var!(?page))
.add_clause(|c| c.entity(var!(?page)) .clause(|c| c.entity(var!(?page))
.attribute(kw!(:page/url)) .attribute(kw!(:page/url))
.value(QueryValueType::value("http://foo.com/"))) .value(QueryValueType::value("http://foo.com/")))
.add_clause(|c| c.entity(var!(?page)) .clause(|c| c.entity(var!(?page))
.attribute(kw!(:page/title)) .attribute(kw!(:page/title))
.value(QueryValueType::value("Foo"))) .value(QueryValueType::value("Foo")))
}) })
.add_clause(|c| c.entity(var!(?page)) .clause(|c| c.entity(var!(?page))
.attribute(kw!(:page/url)) .attribute(kw!(:page/url))
.value(var!(?url))) .value(var!(?url)))
.add_clause(|c| c.entity(var!(?page)) .clause(|c| c.entity(var!(?page))
.attribute(kw!(:page/description)) .attribute(kw!(:page/description))
.value(var!(?description))) .value(var!(?description)))
}) })
@ -462,13 +495,13 @@ mod test {
// [:find ?x :where [?x :foo/baz ?y] :limit 1000] // [:find ?x :where [?x :foo/baz ?y] :limit 1000]
#[test] #[test]
fn test_find_with_limit() { fn test_find_with_limit() {
let _ = query().add_find(|find| find.add("?x")) let _ = query().find(|find| find.var(var!(?x)))
.add_where(|w| { .wheres(|w| {
w.add_clause(|c| c.entity(var!(?x)) w.clause(|c| c.entity(var!(?x))
.attribute(kw!(:foo/baz)) .attribute(kw!(:foo/baz))
.value(var!(?y))) .value(var!(?y)))
}) })
.add_limit(1000) .limit(1000)
.execute(); .execute();
panic!("not complete"); panic!("not complete");
} }
@ -476,13 +509,13 @@ mod test {
// [:find ?x :where [?x :foo/baz ?y] :order ?y] // [:find ?x :where [?x :foo/baz ?y] :order ?y]
#[test] #[test]
fn test_find_with_default_order() { fn test_find_with_default_order() {
let _ = query().add_find(|find| find.add("?x")) let _ = query().find(|find| find.var(var!(?x)))
.add_where(|w| { .wheres(|w| {
w.add_clause(|c| c.entity(var!(?x)) w.clause(|c| c.entity(var!(?x))
.attribute(kw!(:foo/baz)) .attribute(kw!(:foo/baz))
.value(var!(?y))) .value(var!(?y)))
}) })
.add_order(|order| order.add("?y")) .order_by(|order| order.add(var!(?y)))
.execute(); .execute();
panic!("not complete"); panic!("not complete");
} }
@ -490,13 +523,13 @@ mod test {
// [:find ?x :where [?x :foo/bar ?y] :order (desc ?y)] // [:find ?x :where [?x :foo/bar ?y] :order (desc ?y)]
#[test] #[test]
fn test_find_with_desc_order() { fn test_find_with_desc_order() {
let _ = query().add_find(|find| find.add("?x")) let _ = query().find(|find| find.var(var!(?x)))
.add_where(|w| { .wheres(|w| {
w.add_clause(|c| c.entity(var!(?x)) w.clause(|c| c.entity(var!(?x))
.attribute(kw!(:foo/bar)) .attribute(kw!(:foo/bar))
.value(var!(?y))) .value(var!(?y)))
}) })
.add_order(|order| order.add_descending("?y")) .order_by(|order| order.descending(var!(?y)))
.execute(); .execute();
panic!("not complete"); panic!("not complete");
} }
@ -504,13 +537,13 @@ mod test {
// [:find ?x :where [?x :foo/baz ?y] :order (desc ?y) (asc ?x)] // [:find ?x :where [?x :foo/baz ?y] :order (desc ?y) (asc ?x)]
#[test] #[test]
fn test_find_with_multiple_orders() { fn test_find_with_multiple_orders() {
let _ = query().add_find(|find| find.add("?x")) let _ = query().find(|find| find.var(var!(?x)))
.add_where(|w| { .wheres(|w| {
w.add_clause(|c| c.entity(var!(?x)) w.clause(|c| c.entity(var!(?x))
.attribute(kw!(:foo/baz)) .attribute(kw!(:foo/baz))
.value(var!(?y))) .value(var!(?y)))
}) })
.add_order(|order| order.add_descending("?y").add_ascending("?x")) .order_by(|order| order.descending(var!(?y)).ascending(var!(?x)))
.execute(); .execute();
panic!("not complete"); panic!("not complete");
} }
@ -518,12 +551,12 @@ mod test {
// [:find ?x . :where [?x :foo/bar ?y] [(!= ?y 12)]] // [:find ?x . :where [?x :foo/bar ?y] [(!= ?y 12)]]
#[test] #[test]
fn test_find_with_predicate() { fn test_find_with_predicate() {
let _ = query().add_find(|find| find.set_type(FindType::Scalar).add("?x")) let _ = query().find(|find| find.set_type(FindType::Scalar).var(var!(?x)))
.add_where(|w| { .wheres(|w| {
w.add_clause(|c| c.entity(var!(?x)) w.clause(|c| c.entity(var!(?x))
.attribute(kw!(:foo/bar)) .attribute(kw!(:foo/bar))
.value(var!(?y))) .value(var!(?y)))
.add_clause(|c| c.entity("!=") .clause(|c| c.entity("!=")
.attribute(var!(?y)) .attribute(var!(?y))
.value(QueryValueType::value(12))) .value(QueryValueType::value(12)))
}) })