Skip to content
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
7 changes: 7 additions & 0 deletions crates/bindings/src/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ mod tests {
assert!(q.sql().contains("<>"));
}

#[test]
fn test_filter_alias() {
let q = users().filter(|c| c.id.eq(5)).filter(|c| c.age.lt(30)).build();
let expected = r#"SELECT * FROM "users" WHERE (("users"."id" = 5) AND ("users"."age" < 30))"#;
assert_eq!(norm(q.sql()), norm(expected));
}

#[test]
fn test_or_comparison() {
let q = users()
Expand Down
16 changes: 16 additions & 0 deletions crates/bindings/src/query_builder/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ impl<L: HasCols> LeftSemiJoin<L> {
}
}

// Filter is an alias for where
pub fn filter<F>(self, f: F) -> Self
where
F: Fn(&L::Cols) -> BoolExpr<L>,
{
self.r#where(f)
}

pub fn build(self) -> Query<L> {
let where_clause = self
.where_expr
Expand Down Expand Up @@ -187,6 +195,14 @@ impl<R: HasCols, L: HasCols> RightSemiJoin<R, L> {
}
}

// Filter is an alias for where
pub fn filter<F>(self, f: F) -> Self
where
F: Fn(&R::Cols) -> BoolExpr<R>,
{
self.r#where(f)
}

pub fn build(self) -> Query<R> {
let mut where_parts = Vec::new();

Expand Down
16 changes: 16 additions & 0 deletions crates/bindings/src/query_builder/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ impl<T: HasCols> Table<T> {
expr,
}
}

// Filter is an alias for where
pub fn filter<F>(self, f: F) -> FromWhere<T>
where
F: Fn(&T::Cols) -> BoolExpr<T>,
{
self.r#where(f)
}
}

impl<T: HasCols> FromWhere<T> {
Expand All @@ -147,6 +155,14 @@ impl<T: HasCols> FromWhere<T> {
}
}

// Filter is an alias for where
pub fn filter<F>(self, f: F) -> Self
where
F: Fn(&T::Cols) -> BoolExpr<T>,
{
self.r#where(f)
}

pub fn build(self) -> Query<T> {
let sql = format!(
r#"SELECT * FROM "{}" WHERE {}"#,
Expand Down
4 changes: 2 additions & 2 deletions smoketests/tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,9 +741,9 @@ class QueryView(Smoketest):
fn offline_user_in_twienties(ctx: &ViewContext) -> Query<User> {
ctx.from
.person()
.r#where(|p| p.age.eq(20))
.filter(|p| p.age.eq(20))
.right_semijoin(ctx.from.user(), |p, u| p.identity.eq(u.identity))
.r#where(|u| u.online.eq(false))
.filter(|u| u.online.eq(false))
.build()
}

Expand Down
Loading