From dd06043f6a7b4cbd898fc6f9586fb77d97f19c2c Mon Sep 17 00:00:00 2001 From: Julien Nicoulaud 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 | 34 +++++++++++++++------- src/table/column.rs | 66 +++++++++++++++++++++--------------------- 2 files changed, 57 insertions(+), 43 deletions(-) diff --git a/src/query/condition.rs b/src/query/condition.rs index 6539181ff..541ecaae9 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(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(); } } @@ -73,7 +91,7 @@ impl Condition { /// # Examples /// /// ``` - /// use sea_query::{*, tests_cfg::*}; + /// use sea_query::{tests_cfg::*, *}; /// /// let query = Query::select() /// .column(Glyph::Image) @@ -81,7 +99,7 @@ impl Condition { /// .cond_where( /// Cond::all() /// .add_option(Some(Expr::tbl(Glyph::Table, Glyph::Image).like("A%"))) - /// .add_option(None::) + /// .add_option(None::), /// ) /// .to_owned(); /// @@ -189,7 +207,7 @@ impl Condition { /// # More Examples /// /// ``` - /// use sea_query::{*, tests_cfg::*}; + /// use sea_query::{tests_cfg::*, *}; /// /// let query = Query::select() /// .column(Glyph::Id) @@ -199,13 +217,9 @@ impl Condition { /// Cond::all() /// .not() /// .add(Expr::val(1).eq(1)) - /// .add(Expr::val(2).eq(2)) - /// ) - /// .add( - /// Cond::any() - /// .add(Expr::val(3).eq(3)) - /// .add(Expr::val(4).eq(4)) + /// .add(Expr::val(2).eq(2)), /// ) + /// .add(Cond::any().add(Expr::val(3).eq(3)).add(Expr::val(4).eq(4))), /// ) /// .to_owned(); /// diff --git a/src/table/column.rs b/src/table/column.rs index 0ff42b68d..28647b835 100644 --- a/src/table/column.rs +++ b/src/table/column.rs @@ -256,40 +256,40 @@ impl ColumnDef { /// Set column type as interval type with optional fields and precision. Postgres only /// /// ``` - /// use sea_query::{*, tests_cfg::*}; + /// use sea_query::{tests_cfg::*, *}; /// assert_eq!( - /// Table::create() - /// .table(Glyph::Table) - /// .col( - /// ColumnDef::new(Alias::new("I1")) - /// .interval(None, None) - /// .not_null() - /// ) - /// .col( - /// ColumnDef::new(Alias::new("I2")) - /// .interval(Some(IntervalField::YearToMonth), None) - /// .not_null() - /// ) - /// .col( - /// ColumnDef::new(Alias::new("I3")) - /// .interval(None, Some(42)) - /// .not_null() - /// ) - /// .col( - /// ColumnDef::new(Alias::new("I4")) - /// .interval(Some(IntervalField::Hour), Some(43)) - /// .not_null() - /// ) - /// .to_string(PostgresQueryBuilder), - /// vec![ - /// r#"CREATE TABLE "glyph" ("#, - /// r#""I1" interval NOT NULL,"#, - /// r#""I2" interval YEAR TO MONTH NOT NULL,"#, - /// r#""I3" interval(42) NOT NULL,"#, - /// r#""I4" interval HOUR(43) NOT NULL"#, - /// r#")"#, - /// ] - /// .join(" ") + /// Table::create() + /// .table(Glyph::Table) + /// .col( + /// ColumnDef::new(Alias::new("I1")) + /// .interval(None, None) + /// .not_null() + /// ) + /// .col( + /// ColumnDef::new(Alias::new("I2")) + /// .interval(Some(IntervalField::YearToMonth), None) + /// .not_null() + /// ) + /// .col( + /// ColumnDef::new(Alias::new("I3")) + /// .interval(None, Some(42)) + /// .not_null() + /// ) + /// .col( + /// ColumnDef::new(Alias::new("I4")) + /// .interval(Some(IntervalField::Hour), Some(43)) + /// .not_null() + /// ) + /// .to_string(PostgresQueryBuilder), + /// vec![ + /// r#"CREATE TABLE "glyph" ("#, + /// r#""I1" interval NOT NULL,"#, + /// r#""I2" interval YEAR TO MONTH NOT NULL,"#, + /// r#""I3" interval(42) NOT NULL,"#, + /// r#""I4" interval HOUR(43) NOT NULL"#, + /// r#")"#, + /// ] + /// .join(" ") /// ); /// ``` #[cfg(feature = "backend-postgres")]