From 3830737f3f7ebd3bdd15e8ca62fbc27eb57d77d2 Mon Sep 17 00:00:00 2001 From: Julien Nicoulaud <julien.nicoulaud@gmail.com> Date: Mon, 25 Oct 2021 18:56:06 +0200 Subject: [PATCH] fix bug in Condition::add where Condition negation is ignored --- src/query/condition.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/query/condition.rs b/src/query/condition.rs index 6539181ff..58b528819 100644 --- a/src/query/condition.rs +++ b/src/query/condition.rs @@ -46,6 +46,24 @@ impl Condition { /// /// If it's an [`Condition::any`], it will be separated from the others by an `" OR "` in the query. If it's /// an [`Condition::all`], it will be separated by an `" AND "`. + /// + /// ``` + /// use sea_query::{*, tests_cfg::*}; + /// + /// let statement = sea_query::Query::select() + /// .column(Glyph::Id) + /// .from(Glyph::Table) + /// .cond_where( + /// Cond::all() + /// .add(Expr::col(Glyph::Aspect).eq(0).into_condition().not()) + /// .add(Expr::col(Glyph::Id).eq(0).into_condition().not()), + /// ) + /// .to_string(PostgresQueryBuilder); + /// assert_eq!( + /// statement, + /// r#"SELECT "id" FROM "glyph" WHERE (NOT ("aspect" = 0)) AND (NOT ("id" = 0))"# + /// ); + /// ``` #[allow(clippy::should_implement_trait)] pub fn add<C>(mut self, condition: C) -> Self where @@ -58,7 +76,7 @@ impl Condition { return self; } // Skip the junction if there is only one. - if c.conditions.len() == 1 { + if c.conditions.len() == 1 && !c.negate { expr = c.conditions.pop().unwrap(); } }