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
26 changes: 25 additions & 1 deletion pqb/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,22 @@ impl Expr {
self.binary(BinaryOp::IsNot, right)
}

/// Express a `IS DISTINCT FROM` expression.
pub fn is_distinct_from<R>(self, right: R) -> Expr
where
R: Into<Expr>,
{
self.binary(BinaryOp::IsDistinctFrom, right)
}

/// Express a `IS NOT DISTINCT FROM` expression.
pub fn is_not_distinct_from<R>(self, right: R) -> Expr
where
R: Into<Expr>,
{
self.binary(BinaryOp::IsNotDistinctFrom, right)
}

/// Express a `IN` expression.
pub fn is_in<V, I>(self, v: I) -> Expr
where
Expand Down Expand Up @@ -479,6 +495,8 @@ pub enum BinaryOp {
NotLike,
Is,
IsNot,
IsDistinctFrom,
IsNotDistinctFrom,
In,
NotIn,
LShift,
Expand Down Expand Up @@ -645,6 +663,8 @@ fn write_binary_op<W: SqlWriter>(w: &mut W, op: &BinaryOp) {
BinaryOp::NotLike => "NOT LIKE",
BinaryOp::Is => "IS",
BinaryOp::IsNot => "IS NOT",
BinaryOp::IsDistinctFrom => "IS DISTINCT FROM",
BinaryOp::IsNotDistinctFrom => "IS NOT DISTINCT FROM",
BinaryOp::In => "IN",
BinaryOp::NotIn => "NOT IN",
BinaryOp::Between => "BETWEEN",
Expand Down Expand Up @@ -740,6 +760,7 @@ fn well_known_high_precedence(expr: &Expr, outer_op: &Operator) -> bool {
|| outer_op.is_between()
|| outer_op.is_in()
|| outer_op.is_like()
|| outer_op.is_is()
|| outer_op.is_logical();
}

Expand Down Expand Up @@ -789,7 +810,10 @@ impl Operator {
fn is_is(&self) -> bool {
matches!(
self,
Operator::Binary(BinaryOp::Is) | Operator::Binary(BinaryOp::IsNot)
Operator::Binary(BinaryOp::Is)
| Operator::Binary(BinaryOp::IsNot)
| Operator::Binary(BinaryOp::IsDistinctFrom)
| Operator::Binary(BinaryOp::IsNotDistinctFrom)
)
}

Expand Down
52 changes: 52 additions & 0 deletions pqb/tests/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,55 @@ fn select_range_ops() {
@r#"SELECT * FROM "ranges" WHERE "r1" @> "r2" AND "r1" <@ "r2" AND "r1" && "r2" AND "r1" << "r2" AND "r1" >> "r2" AND "r1" &< "r2" AND "r1" &> "r2" AND "r1" -|- "r2""#
);
}

#[test]
fn select_is_distinct_from() {
let left = Expr::column("c1");
let right = Expr::column("c2");

assert_snapshot!(
Select::new()
.expr(Expr::asterisk())
.from("t")
.and_where(left.clone().is_distinct_from(right.clone()))
.and_where(left.clone().is_not_distinct_from(right.clone()))
.to_sql()
.validate(),
@r#"SELECT * FROM "t" WHERE "c1" IS DISTINCT FROM "c2" AND "c1" IS NOT DISTINCT FROM "c2""#
);

assert_snapshot!(
Select::new()
.expr(Expr::asterisk())
.from("t")
.and_where(left.clone().add(Expr::value(1)).is_distinct_from(right.clone().add(Expr::value(2))))
.to_sql()
.validate(),
@r#"SELECT * FROM "t" WHERE "c1" + 1 IS DISTINCT FROM "c2" + 2"#
);
}

#[test]
fn select_is_null() {
let c1 = Expr::column("c1");

assert_snapshot!(
Select::new()
.expr(Expr::asterisk())
.from("t")
.and_where(c1.clone().add(Expr::value(1)).is_null())
.to_sql()
.validate(),
@r#"SELECT * FROM "t" WHERE "c1" + 1 IS NULL"#
);

assert_snapshot!(
Select::new()
.expr(Expr::asterisk())
.from("t")
.and_where(c1.clone().add(Expr::value(1)).is_not_null())
.to_sql()
.validate(),
@r#"SELECT * FROM "t" WHERE "c1" + 1 IS NOT NULL"#
);
}