Skip to content

Commit

Permalink
fix bug in Condition::add where Condition negation is ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoulaj authored and tyt2y3 committed Oct 26, 2021
1 parent 5b3a4e6 commit dd06043
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 43 deletions.
34 changes: 24 additions & 10 deletions src/query/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
Expand All @@ -73,15 +91,15 @@ impl Condition {
/// # Examples
///
/// ```
/// use sea_query::{*, tests_cfg::*};
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .column(Glyph::Image)
/// .from(Glyph::Table)
/// .cond_where(
/// Cond::all()
/// .add_option(Some(Expr::tbl(Glyph::Table, Glyph::Image).like("A%")))
/// .add_option(None::<SimpleExpr>)
/// .add_option(None::<SimpleExpr>),
/// )
/// .to_owned();
///
Expand Down Expand Up @@ -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)
Expand All @@ -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();
///
Expand Down
66 changes: 33 additions & 33 deletions src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit dd06043

Please sign in to comment.