From d99ea4b343a765b511a260d3f0f2c4bf9704afb2 Mon Sep 17 00:00:00 2001 From: Marlon Sousa Date: Mon, 4 Oct 2021 15:19:04 -0300 Subject: [PATCH 1/3] Change returning clause to accept a list of columns instead of a full select expression --- src/backend/postgres/query.rs | 32 ++++++++++++++++++++++---------- src/backend/query_builder.rs | 2 +- src/query/delete.rs | 16 +++++++++------- src/query/insert.rs | 16 +++++++++------- src/query/mod.rs | 2 ++ src/query/returning.rs | 17 +++++++++++++++++ src/query/update.rs | 16 +++++++++------- 7 files changed, 69 insertions(+), 32 deletions(-) create mode 100644 src/query/returning.rs diff --git a/src/backend/postgres/query.rs b/src/backend/postgres/query.rs index 0d4ccdf32..d659e28cb 100644 --- a/src/backend/postgres/query.rs +++ b/src/backend/postgres/query.rs @@ -8,19 +8,31 @@ impl QueryBuilder for PostgresQueryBuilder { fn prepare_returning( &self, - returning: &[SelectExpr], + returning: &Returning, sql: &mut SqlWriter, collector: &mut dyn FnMut(Value), ) { - if !returning.is_empty() { - write!(sql, " RETURNING ").unwrap(); - returning.iter().fold(true, |first, expr| { - if !first { - write!(sql, ", ").unwrap() - } - self.prepare_select_expr(expr, sql, collector); - false - }); + match returning { + Returning::All => write!(sql, " RETURNING *").unwrap(), + Returning::Collumns(cols) => { + write!(sql, " RETURNING ").unwrap(); + cols.into_iter().fold(true, |first, column_ref| { + if !first { + write!(sql, ", ").unwrap() + } + match column_ref { + ColumnRef::Column(column) => column.prepare(sql, self.quote()), + ColumnRef::TableColumn(table, column) => { + table.prepare(sql, self.quote()); + write!(sql, ".").unwrap(); + column.prepare(sql, self.quote()); + } + }; + false + }); + } + Returning::PrimaryKey => write!(sql, " RETURNING *").unwrap(), + Returning::Nothing => return, } } diff --git a/src/backend/query_builder.rs b/src/backend/query_builder.rs index 3c7a01467..5a1842481 100644 --- a/src/backend/query_builder.rs +++ b/src/backend/query_builder.rs @@ -736,7 +736,7 @@ pub trait QueryBuilder: QuotedBuilder { /// Hook to insert "RETURNING" statements. fn prepare_returning( &self, - _returning: &[SelectExpr], + returning: &Returning, _sql: &mut SqlWriter, _collector: &mut dyn FnMut(Value), ) { diff --git a/src/query/delete.rs b/src/query/delete.rs index d8c83e2e0..b63254a25 100644 --- a/src/query/delete.rs +++ b/src/query/delete.rs @@ -4,7 +4,7 @@ use crate::{ query::{condition::*, OrderedStatement}, types::*, value::*, - Query, QueryStatementBuilder, SelectExpr, SelectStatement, + ColumnRef, QueryStatementBuilder, Returning, }; /// Delete existing rows from the table @@ -39,7 +39,7 @@ pub struct DeleteStatement { pub(crate) wherei: ConditionHolder, pub(crate) orders: Vec, pub(crate) limit: Option, - pub(crate) returning: Vec, + pub(crate) returning: Returning, } impl Default for DeleteStatement { @@ -56,7 +56,7 @@ impl DeleteStatement { wherei: ConditionHolder::new(), orders: Vec::new(), limit: None, - returning: Vec::new(), + returning: Returning::default(), } } @@ -108,7 +108,7 @@ impl DeleteStatement { /// let query = Query::delete() /// .from_table(Glyph::Table) /// .and_where(Expr::col(Glyph::Id).eq(1)) - /// .returning(Query::select().column(Glyph::Id).take()) + /// .returning(Returning::Collumns(vec![Glyph::Id.into_column_ref()])) /// .to_owned(); /// /// assert_eq!( @@ -124,8 +124,8 @@ impl DeleteStatement { /// r#"DELETE FROM `glyph` WHERE `id` = 1"# /// ); /// ``` - pub fn returning(&mut self, select: SelectStatement) -> &mut Self { - self.returning = select.selects; + pub fn returning(&mut self, returning_cols: Returning) -> &mut Self { + self.returning = returning_cols; self } @@ -158,7 +158,9 @@ impl DeleteStatement { where C: IntoIden, { - self.returning(Query::select().column(col.into_iden()).take()) + self.returning(Returning::Collumns(vec![ColumnRef::Column( + col.into_iden(), + )])) } } diff --git a/src/query/insert.rs b/src/query/insert.rs index 39903b953..493e0f765 100644 --- a/src/query/insert.rs +++ b/src/query/insert.rs @@ -1,6 +1,6 @@ use crate::{ - backend::QueryBuilder, error::*, prepare::*, types::*, value::*, Expr, Query, - QueryStatementBuilder, SelectExpr, SelectStatement, SimpleExpr, + backend::QueryBuilder, error::*, prepare::*, types::*, value::*, Expr, QueryStatementBuilder, + Returning, SimpleExpr, }; /// Insert any new rows into an existing table @@ -35,7 +35,7 @@ pub struct InsertStatement { pub(crate) table: Option>, pub(crate) columns: Vec, pub(crate) values: Vec>, - pub(crate) returning: Vec, + pub(crate) returning: Returning, } impl InsertStatement { @@ -188,7 +188,7 @@ impl InsertStatement { /// .into_table(Glyph::Table) /// .columns(vec![Glyph::Image]) /// .values_panic(vec!["12A".into()]) - /// .returning(Query::select().column(Glyph::Id).take()) + /// .returning(Returning::Collumns(vec![Glyph::Id.into_column_ref()])) /// .to_owned(); /// /// assert_eq!( @@ -204,8 +204,8 @@ impl InsertStatement { /// "INSERT INTO `glyph` (`image`) VALUES ('12A')" /// ); /// ``` - pub fn returning(&mut self, select: SelectStatement) -> &mut Self { - self.returning = select.selects; + pub fn returning(&mut self, returning: Returning) -> &mut Self { + self.returning = returning; self } @@ -239,7 +239,9 @@ impl InsertStatement { where C: IntoIden, { - self.returning(Query::select().column(col.into_iden()).take()) + self.returning(Returning::Collumns(vec![ColumnRef::Column( + col.into_iden(), + )])) } } diff --git a/src/query/mod.rs b/src/query/mod.rs index c30ccc77a..591bddb97 100644 --- a/src/query/mod.rs +++ b/src/query/mod.rs @@ -11,6 +11,7 @@ mod condition; mod delete; mod insert; mod ordered; +mod returning; mod select; mod shim; mod traits; @@ -20,6 +21,7 @@ pub use condition::*; pub use delete::*; pub use insert::*; pub use ordered::*; +pub use returning::*; pub use select::*; pub use traits::*; pub use update::*; diff --git a/src/query/returning.rs b/src/query/returning.rs new file mode 100644 index 000000000..5274e9070 --- /dev/null +++ b/src/query/returning.rs @@ -0,0 +1,17 @@ +use std::default::Default; + +use crate::ColumnRef; + +#[derive(Clone, Debug)] +pub enum Returning { + All, + Collumns(Vec), + Nothing, + PrimaryKey, +} + +impl Default for Returning { + fn default() -> Self { + Self::Nothing + } +} diff --git a/src/query/update.rs b/src/query/update.rs index 5c8af7495..49236c21d 100644 --- a/src/query/update.rs +++ b/src/query/update.rs @@ -5,7 +5,7 @@ use crate::{ query::{condition::*, OrderedStatement}, types::*, value::*, - Query, QueryStatementBuilder, SelectExpr, SelectStatement, + QueryStatementBuilder, Returning, }; /// Update existing rows in the table @@ -44,7 +44,7 @@ pub struct UpdateStatement { pub(crate) wherei: ConditionHolder, pub(crate) orders: Vec, pub(crate) limit: Option, - pub(crate) returning: Vec, + pub(crate) returning: Returning, } impl Default for UpdateStatement { @@ -62,7 +62,7 @@ impl UpdateStatement { wherei: ConditionHolder::new(), orders: Vec::new(), limit: None, - returning: Vec::new(), + returning: Returning::default(), } } @@ -233,7 +233,7 @@ impl UpdateStatement { /// .value(Glyph::Aspect, 2.1345.into()) /// .value(Glyph::Image, "235m".into()) /// .and_where(Expr::col(Glyph::Id).eq(1)) - /// .returning(Query::select().column(Glyph::Id).take()) + /// .returning(Returning::Collumns(vec![Glyph::Id.into_column_ref()])) /// .to_owned(); /// /// assert_eq!( @@ -249,8 +249,8 @@ impl UpdateStatement { /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1"# /// ); /// ``` - pub fn returning(&mut self, select: SelectStatement) -> &mut Self { - self.returning = select.selects; + pub fn returning(&mut self, returning_cols: Returning) -> &mut Self { + self.returning = returning_cols; self } @@ -286,7 +286,9 @@ impl UpdateStatement { where C: IntoIden, { - self.returning(Query::select().column(col.into_iden()).take()) + self.returning(Returning::Collumns(vec![ColumnRef::Column( + col.into_iden(), + )])) } } From 31a68ea9aef8a91557909dd3e7fc99da1d30a72e Mon Sep 17 00:00:00 2001 From: Marlon Sousa Date: Tue, 5 Oct 2021 09:41:33 -0300 Subject: [PATCH 2/3] extended returning support --- .github/workflows/rust.yml | 12 ++- Cargo.toml | 3 +- src/backend/postgres/query.rs | 30 ------ src/backend/query_builder.rs | 31 ++++++ src/query/delete.rs | 18 ++-- src/query/insert.rs | 18 ++-- src/query/returning.rs | 16 ++- src/query/update.rs | 18 ++-- tests/mysql/query.rs | 196 ++++++++++++++++++++++++++++++++++ tests/postgres/query.rs | 92 ++++++++++++++++ tests/sqlite/query.rs | 196 ++++++++++++++++++++++++++++++++++ 11 files changed, 565 insertions(+), 65 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 1bda4c021..4272b182f 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -22,7 +22,7 @@ jobs: toolchain: stable components: clippy override: true - + - uses: actions-rs/clippy-check@v1 with: token: ${{ secrets.GITHUB_TOKEN }} @@ -92,6 +92,16 @@ jobs: command: test args: --all-features + - uses: actions-rs/cargo@v1 + with: + command: test + args: --no-default-features --features="backend-mysql" --test test-mysql + + - uses: actions-rs/cargo@v1 + with: + command: test + args: --no-default-features --features="backend-sqlite" --test test-sqlite + derive-test: name: Derive Tests runs-on: ubuntu-20.04 diff --git a/Cargo.toml b/Cargo.toml index a1a714f95..f7af98d0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,7 @@ uuid = { version = "^0", optional = true } [features] backend-mysql = [] -backend-postgres = [] +backend-postgres = ["with-returning"] backend-sqlite = [] default = ["derive", "backend-mysql", "backend-postgres", "backend-sqlite"] derive = ["sea-query-derive"] @@ -67,6 +67,7 @@ with-json = ["serde_json"] with-rust_decimal = ["rust_decimal"] with-bigdecimal = ["bigdecimal"] with-uuid = ["uuid"] +with-returning = [] [[test]] name = "test-derive" diff --git a/src/backend/postgres/query.rs b/src/backend/postgres/query.rs index d659e28cb..ac54723e6 100644 --- a/src/backend/postgres/query.rs +++ b/src/backend/postgres/query.rs @@ -6,36 +6,6 @@ impl QueryBuilder for PostgresQueryBuilder { ("$", true) } - fn prepare_returning( - &self, - returning: &Returning, - sql: &mut SqlWriter, - collector: &mut dyn FnMut(Value), - ) { - match returning { - Returning::All => write!(sql, " RETURNING *").unwrap(), - Returning::Collumns(cols) => { - write!(sql, " RETURNING ").unwrap(); - cols.into_iter().fold(true, |first, column_ref| { - if !first { - write!(sql, ", ").unwrap() - } - match column_ref { - ColumnRef::Column(column) => column.prepare(sql, self.quote()), - ColumnRef::TableColumn(table, column) => { - table.prepare(sql, self.quote()); - write!(sql, ".").unwrap(); - column.prepare(sql, self.quote()); - } - }; - false - }); - } - Returning::PrimaryKey => write!(sql, " RETURNING *").unwrap(), - Returning::Nothing => return, - } - } - fn if_null_function(&self) -> &str { "COALESCE" } diff --git a/src/backend/query_builder.rs b/src/backend/query_builder.rs index 5a1842481..853504218 100644 --- a/src/backend/query_builder.rs +++ b/src/backend/query_builder.rs @@ -734,6 +734,7 @@ pub trait QueryBuilder: QuotedBuilder { #[doc(hidden)] /// Hook to insert "RETURNING" statements. + #[cfg(not(feature = "with-returning"))] fn prepare_returning( &self, returning: &Returning, @@ -742,6 +743,36 @@ pub trait QueryBuilder: QuotedBuilder { ) { } + #[cfg(feature = "with-returning")] + fn prepare_returning( + &self, + returning: &Returning, + sql: &mut SqlWriter, + _collector: &mut dyn FnMut(Value), + ) { + match returning { + Returning::All => write!(sql, " RETURNING *").unwrap(), + Returning::Columns(cols) => { + write!(sql, " RETURNING ").unwrap(); + cols.into_iter().fold(true, |first, column_ref| { + if !first { + write!(sql, ", ").unwrap() + } + match column_ref { + ColumnRef::Column(column) => column.prepare(sql, self.quote()), + ColumnRef::TableColumn(table, column) => { + table.prepare(sql, self.quote()); + write!(sql, ".").unwrap(); + column.prepare(sql, self.quote()); + } + }; + false + }); + } + Returning::Nothing => return, + } + } + #[doc(hidden)] /// Translate a condition to a "WHERE" clause. fn prepare_condition( diff --git a/src/query/delete.rs b/src/query/delete.rs index b63254a25..5d9953e96 100644 --- a/src/query/delete.rs +++ b/src/query/delete.rs @@ -100,7 +100,7 @@ impl DeleteStatement { self } - /// RETURNING expressions. Postgres only. + /// RETURNING expressions. Enabled by default for postgres and under the feature with-returning for the remaining back ends. /// /// ``` /// use sea_query::{tests_cfg::*, *}; @@ -108,12 +108,12 @@ impl DeleteStatement { /// let query = Query::delete() /// .from_table(Glyph::Table) /// .and_where(Expr::col(Glyph::Id).eq(1)) - /// .returning(Returning::Collumns(vec![Glyph::Id.into_column_ref()])) + /// .returning(Returning::Columns(vec![Glyph::Id.into_column_ref()])) /// .to_owned(); /// /// assert_eq!( /// query.to_string(MysqlQueryBuilder), - /// r#"DELETE FROM `glyph` WHERE `id` = 1"# + /// r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING `id`"# /// ); /// assert_eq!( /// query.to_string(PostgresQueryBuilder), @@ -121,7 +121,7 @@ impl DeleteStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// r#"DELETE FROM `glyph` WHERE `id` = 1"# + /// r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING `id`"# /// ); /// ``` pub fn returning(&mut self, returning_cols: Returning) -> &mut Self { @@ -129,7 +129,7 @@ impl DeleteStatement { self } - /// RETURNING a column after delete. Postgres only. + /// RETURNING a column after delete. Enabled by default for postgres and under the feature with-returning for the remaining back ends. /// Wrapper over [`DeleteStatement::returning()`]. /// /// ``` @@ -143,7 +143,7 @@ impl DeleteStatement { /// /// assert_eq!( /// query.to_string(MysqlQueryBuilder), - /// r#"DELETE FROM `glyph` WHERE `id` = 1"# + /// r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING `id`"# /// ); /// assert_eq!( /// query.to_string(PostgresQueryBuilder), @@ -151,16 +151,14 @@ impl DeleteStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// r#"DELETE FROM `glyph` WHERE `id` = 1"# + /// r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING `id`"# /// ); /// ``` pub fn returning_col(&mut self, col: C) -> &mut Self where C: IntoIden, { - self.returning(Returning::Collumns(vec![ColumnRef::Column( - col.into_iden(), - )])) + self.returning(Returning::Columns(vec![ColumnRef::Column(col.into_iden())])) } } diff --git a/src/query/insert.rs b/src/query/insert.rs index 493e0f765..896bff066 100644 --- a/src/query/insert.rs +++ b/src/query/insert.rs @@ -179,7 +179,7 @@ impl InsertStatement { self.exprs(values).unwrap() } - /// RETURNING expressions. Postgres only. + /// RETURNING expressions. Enabled by default for postgres and under the feature with-returning for the remaining back ends. /// /// ``` /// use sea_query::{tests_cfg::*, *}; @@ -188,12 +188,12 @@ impl InsertStatement { /// .into_table(Glyph::Table) /// .columns(vec![Glyph::Image]) /// .values_panic(vec!["12A".into()]) - /// .returning(Returning::Collumns(vec![Glyph::Id.into_column_ref()])) + /// .returning(Returning::Columns(vec![Glyph::Id.into_column_ref()])) /// .to_owned(); /// /// assert_eq!( /// query.to_string(MysqlQueryBuilder), - /// "INSERT INTO `glyph` (`image`) VALUES ('12A')" + /// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING `id`" /// ); /// assert_eq!( /// query.to_string(PostgresQueryBuilder), @@ -201,7 +201,7 @@ impl InsertStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// "INSERT INTO `glyph` (`image`) VALUES ('12A')" + /// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING `id`" /// ); /// ``` pub fn returning(&mut self, returning: Returning) -> &mut Self { @@ -209,7 +209,7 @@ impl InsertStatement { self } - /// RETURNING a column after insertion. Postgres only. This is equivalent to MySQL's LAST_INSERT_ID. + /// RETURNING a column after insertion. Enabled by default for postgres and under the feature with-returning for the remaining back ends. /// Wrapper over [`InsertStatement::returning()`]. /// /// ``` @@ -224,7 +224,7 @@ impl InsertStatement { /// /// assert_eq!( /// query.to_string(MysqlQueryBuilder), - /// "INSERT INTO `glyph` (`image`) VALUES ('12A')" + /// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING `id`" /// ); /// assert_eq!( /// query.to_string(PostgresQueryBuilder), @@ -232,16 +232,14 @@ impl InsertStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// "INSERT INTO `glyph` (`image`) VALUES ('12A')" + /// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING `id`" /// ); /// ``` pub fn returning_col(&mut self, col: C) -> &mut Self where C: IntoIden, { - self.returning(Returning::Collumns(vec![ColumnRef::Column( - col.into_iden(), - )])) + self.returning(Returning::Columns(vec![ColumnRef::Column(col.into_iden())])) } } diff --git a/src/query/returning.rs b/src/query/returning.rs index 5274e9070..9f3fc4fc2 100644 --- a/src/query/returning.rs +++ b/src/query/returning.rs @@ -1,13 +1,23 @@ use std::default::Default; -use crate::ColumnRef; +use crate::{ColumnRef, IntoColumnRef}; #[derive(Clone, Debug)] pub enum Returning { All, - Collumns(Vec), + Columns(Vec), Nothing, - PrimaryKey, +} + +impl Returning { + pub fn cols(cols: I) -> Self + where + T: IntoColumnRef, + I: IntoIterator, + { + let cols: Vec<_> = cols.into_iter().map(|c| c.into_column_ref()).collect(); + Self::Columns(cols) + } } impl Default for Returning { diff --git a/src/query/update.rs b/src/query/update.rs index 49236c21d..c43c6dcad 100644 --- a/src/query/update.rs +++ b/src/query/update.rs @@ -223,7 +223,7 @@ impl UpdateStatement { self } - /// RETURNING expressions. Postgres only. + /// RETURNING expressions. Enabled by default for postgres and under the feature with-returning for the remaining back ends. /// /// ``` /// use sea_query::{tests_cfg::*, *}; @@ -233,12 +233,12 @@ impl UpdateStatement { /// .value(Glyph::Aspect, 2.1345.into()) /// .value(Glyph::Image, "235m".into()) /// .and_where(Expr::col(Glyph::Id).eq(1)) - /// .returning(Returning::Collumns(vec![Glyph::Id.into_column_ref()])) + /// .returning(Returning::Columns(vec![Glyph::Id.into_column_ref()])) /// .to_owned(); /// /// assert_eq!( /// query.to_string(MysqlQueryBuilder), - /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1"# + /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1 RETURNING `id`"# /// ); /// assert_eq!( /// query.to_string(PostgresQueryBuilder), @@ -246,7 +246,7 @@ impl UpdateStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1"# + /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1 RETURNING `id`"# /// ); /// ``` pub fn returning(&mut self, returning_cols: Returning) -> &mut Self { @@ -254,7 +254,7 @@ impl UpdateStatement { self } - /// RETURNING a column after update. Postgres only. + /// RETURNING a column after update. Enabled by default for postgres and under the feature with-returning for the remaining back ends. /// Wrapper over [`UpdateStatement::returning()`]. /// /// ``` @@ -271,7 +271,7 @@ impl UpdateStatement { /// /// assert_eq!( /// query.to_string(MysqlQueryBuilder), - /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1"# + /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1 RETURNING `id`"# /// ); /// assert_eq!( /// query.to_string(PostgresQueryBuilder), @@ -279,16 +279,14 @@ impl UpdateStatement { /// ); /// assert_eq!( /// query.to_string(SqliteQueryBuilder), - /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1"# + /// r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m' WHERE `id` = 1 RETURNING `id`"# /// ); /// ``` pub fn returning_col(&mut self, col: C) -> &mut Self where C: IntoIden, { - self.returning(Returning::Collumns(vec![ColumnRef::Column( - col.into_iden(), - )])) + self.returning(Returning::Columns(vec![ColumnRef::Column(col.into_iden())])) } } diff --git a/tests/mysql/query.rs b/tests/mysql/query.rs index 525c9dacc..702c2ddda 100644 --- a/tests/mysql/query.rs +++ b/tests/mysql/query.rs @@ -836,6 +836,78 @@ fn insert_5() { ); } +#[test] +#[cfg(feature = "with-returning")] +#[allow(clippy::approx_constant)] +fn insert_returning_all_columns() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns(vec![Glyph::Image, Glyph::Aspect,]) + .values_panic(vec![ + "04108048005887010020060000204E0180400400".into(), + 3.1415.into(), + ]) + .returning(Returning::All) + .to_string(MysqlQueryBuilder), + r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415) RETURNING *"# + ); +} + +#[test] +#[cfg(feature = "with-returning")] +#[allow(clippy::approx_constant)] +fn insert_returning_specific_columns() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns(vec![Glyph::Image, Glyph::Aspect,]) + .values_panic(vec![ + "04108048005887010020060000204E0180400400".into(), + 3.1415.into(), + ]) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) + .to_string(MysqlQueryBuilder), + r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415) RETURNING `id`, `image`"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +#[allow(clippy::approx_constant)] +fn insert_returning_all_columns_with_returning_disabled() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns(vec![Glyph::Image, Glyph::Aspect,]) + .values_panic(vec![ + "04108048005887010020060000204E0180400400".into(), + 3.1415.into(), + ]) + .returning(Returning::All) + .to_string(MysqlQueryBuilder), + r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415)"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +#[allow(clippy::approx_constant)] +fn insert_returning_specific_columns_with_returning_disabled() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns(vec![Glyph::Image, Glyph::Aspect,]) + .values_panic(vec![ + "04108048005887010020060000204E0180400400".into(), + 3.1415.into(), + ]) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) + .to_string(MysqlQueryBuilder), + r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415)"# + ); +} + #[test] fn update_1() { assert_eq!( @@ -870,6 +942,78 @@ fn update_3() { ); } +#[test] +#[cfg(feature = "with-returning")] +fn update_returning_all_columns() { + assert_eq!( + Query::update() + .table(Glyph::Table) + .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) + .values(vec![( + Glyph::Image, + "24B0E11951B03B07F8300FD003983F03F0780060".into() + ),]) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::All) + .to_string(MysqlQueryBuilder), + r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1 RETURNING *"# + ); +} + +#[test] +#[cfg(feature = "with-returning")] +fn update_returning_specified_columns() { + assert_eq!( + Query::update() + .table(Glyph::Table) + .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) + .values(vec![( + Glyph::Image, + "24B0E11951B03B07F8300FD003983F03F0780060".into() + ),]) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image])) + .to_string(MysqlQueryBuilder), + r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1 RETURNING `id`, `image`"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +fn update_returning_all_columns_with_returning_disabled() { + assert_eq!( + Query::update() + .table(Glyph::Table) + .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) + .values(vec![( + Glyph::Image, + "24B0E11951B03B07F8300FD003983F03F0780060".into() + ),]) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::All) + .to_string(MysqlQueryBuilder), + r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +fn update_returning_specified_columns_with_returning_disabled() { + assert_eq!( + Query::update() + .table(Glyph::Table) + .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) + .values(vec![( + Glyph::Image, + "24B0E11951B03B07F8300FD003983F03F0780060".into() + ),]) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image])) + .to_string(MysqlQueryBuilder), + r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1"# + ); +} + #[test] fn delete_1() { assert_eq!( @@ -882,3 +1026,55 @@ fn delete_1() { "DELETE FROM `glyph` WHERE `id` = 1 ORDER BY `id` ASC LIMIT 1" ); } + +#[test] +#[cfg(feature = "with-returning")] +fn delete_returning_all_columns() { + assert_eq!( + Query::delete() + .from_table(Glyph::Table) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::All) + .to_string(MysqlQueryBuilder), + r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING *"# + ); +} + +#[test] +#[cfg(feature = "with-returning")] +fn delete_returning_specific_columns() { + assert_eq!( + Query::delete() + .from_table(Glyph::Table) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) + .to_string(MysqlQueryBuilder), + r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING `id`, `image`"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +fn delete_returning_all_columns_with_returning_disabled() { + assert_eq!( + Query::delete() + .from_table(Glyph::Table) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::All) + .to_string(MysqlQueryBuilder), + r#"DELETE FROM `glyph` WHERE `id` = 1"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +fn delete_returning_specific_columns_with_returning_disabled() { + assert_eq!( + Query::delete() + .from_table(Glyph::Table) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) + .to_string(MysqlQueryBuilder), + r#"DELETE FROM `glyph` WHERE `id` = 1"# + ); +} diff --git a/tests/postgres/query.rs b/tests/postgres/query.rs index eb13e742a..6af4f8b8d 100644 --- a/tests/postgres/query.rs +++ b/tests/postgres/query.rs @@ -811,6 +811,40 @@ fn insert_5() { ); } +#[test] +#[allow(clippy::approx_constant)] +fn insert_returning_all_columns() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns(vec![Glyph::Image, Glyph::Aspect,]) + .values_panic(vec![ + "04108048005887010020060000204E0180400400".into(), + 3.1415.into(), + ]) + .returning(Returning::All) + .to_string(PostgresQueryBuilder), + r#"INSERT INTO "glyph" ("image", "aspect") VALUES ('04108048005887010020060000204E0180400400', 3.1415) RETURNING *"# + ); +} + +#[test] +#[allow(clippy::approx_constant)] +fn insert_returning_specific_columns() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns(vec![Glyph::Image, Glyph::Aspect,]) + .values_panic(vec![ + "04108048005887010020060000204E0180400400".into(), + 3.1415.into(), + ]) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) + .to_string(PostgresQueryBuilder), + r#"INSERT INTO "glyph" ("image", "aspect") VALUES ('04108048005887010020060000204E0180400400', 3.1415) RETURNING "id", "image""# + ); +} + #[test] fn update_1() { assert_eq!( @@ -845,6 +879,40 @@ fn update_3() { ); } +#[test] +fn update_returning_all_columns() { + assert_eq!( + Query::update() + .table(Glyph::Table) + .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) + .values(vec![( + Glyph::Image, + "24B0E11951B03B07F8300FD003983F03F0780060".into() + ),]) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::All) + .to_string(PostgresQueryBuilder), + r#"UPDATE "glyph" SET "aspect" = 60 * 24 * 24, "image" = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE "id" = 1 RETURNING *"# + ); +} + +#[test] +fn update_returning_specified_columns() { + assert_eq!( + Query::update() + .table(Glyph::Table) + .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) + .values(vec![( + Glyph::Image, + "24B0E11951B03B07F8300FD003983F03F0780060".into() + ),]) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image])) + .to_string(PostgresQueryBuilder), + r#"UPDATE "glyph" SET "aspect" = 60 * 24 * 24, "image" = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE "id" = 1 RETURNING "id", "image""# + ); +} + #[test] fn delete_1() { assert_eq!( @@ -855,3 +923,27 @@ fn delete_1() { r#"DELETE FROM "glyph" WHERE "id" = 1"# ); } + +#[test] +fn delete_returning_all_columns() { + assert_eq!( + Query::delete() + .from_table(Glyph::Table) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::All) + .to_string(PostgresQueryBuilder), + r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING *"# + ); +} + +#[test] +fn delete_returning_specific_columns() { + assert_eq!( + Query::delete() + .from_table(Glyph::Table) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) + .to_string(PostgresQueryBuilder), + r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id", "image""# + ); +} diff --git a/tests/sqlite/query.rs b/tests/sqlite/query.rs index 3c32350e4..0a70fc1c6 100644 --- a/tests/sqlite/query.rs +++ b/tests/sqlite/query.rs @@ -836,6 +836,78 @@ fn insert_5() { ); } +#[test] +#[cfg(feature = "with-returning")] +#[allow(clippy::approx_constant)] +fn insert_returning_all_columns() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns(vec![Glyph::Image, Glyph::Aspect,]) + .values_panic(vec![ + "04108048005887010020060000204E0180400400".into(), + 3.1415.into(), + ]) + .returning(Returning::All) + .to_string(SqliteQueryBuilder), + r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415) RETURNING *"# + ); +} + +#[test] +#[cfg(feature = "with-returning")] +#[allow(clippy::approx_constant)] +fn insert_returning_specific_columns() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns(vec![Glyph::Image, Glyph::Aspect,]) + .values_panic(vec![ + "04108048005887010020060000204E0180400400".into(), + 3.1415.into(), + ]) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) + .to_string(SqliteQueryBuilder), + r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415) RETURNING `id`, `image`"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +#[allow(clippy::approx_constant)] +fn insert_returning_all_columns_with_returning_disabled() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns(vec![Glyph::Image, Glyph::Aspect,]) + .values_panic(vec![ + "04108048005887010020060000204E0180400400".into(), + 3.1415.into(), + ]) + .returning(Returning::All) + .to_string(SqliteQueryBuilder), + r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415)"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +#[allow(clippy::approx_constant)] +fn insert_returning_specific_columns_with_returning_disabled() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns(vec![Glyph::Image, Glyph::Aspect,]) + .values_panic(vec![ + "04108048005887010020060000204E0180400400".into(), + 3.1415.into(), + ]) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) + .to_string(SqliteQueryBuilder), + r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415)"# + ); +} + #[test] fn update_1() { assert_eq!( @@ -866,6 +938,78 @@ fn update_3() { ); } +#[test] +#[cfg(feature = "with-returning")] +fn update_returning_all_columns() { + assert_eq!( + Query::update() + .table(Glyph::Table) + .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) + .values(vec![( + Glyph::Image, + "24B0E11951B03B07F8300FD003983F03F0780060".into() + ),]) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::All) + .to_string(SqliteQueryBuilder), + r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1 RETURNING *"# + ); +} + +#[test] +#[cfg(feature = "with-returning")] +fn update_returning_specified_columns() { + assert_eq!( + Query::update() + .table(Glyph::Table) + .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) + .values(vec![( + Glyph::Image, + "24B0E11951B03B07F8300FD003983F03F0780060".into() + ),]) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image])) + .to_string(SqliteQueryBuilder), + r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1 RETURNING `id`, `image`"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +fn update_returning_all_columns_with_returning_disabled() { + assert_eq!( + Query::update() + .table(Glyph::Table) + .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) + .values(vec![( + Glyph::Image, + "24B0E11951B03B07F8300FD003983F03F0780060".into() + ),]) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::All) + .to_string(SqliteQueryBuilder), + r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +fn update_returning_specified_columns_with_returning_disabled() { + assert_eq!( + Query::update() + .table(Glyph::Table) + .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) + .values(vec![( + Glyph::Image, + "24B0E11951B03B07F8300FD003983F03F0780060".into() + ),]) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image])) + .to_string(SqliteQueryBuilder), + r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1"# + ); +} + #[test] fn delete_1() { assert_eq!( @@ -876,3 +1020,55 @@ fn delete_1() { "DELETE FROM `glyph` WHERE `id` = 1" ); } + +#[test] +#[cfg(feature = "with-returning")] +fn delete_returning_all_columns() { + assert_eq!( + Query::delete() + .from_table(Glyph::Table) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::All) + .to_string(SqliteQueryBuilder), + r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING *"# + ); +} + +#[test] +#[cfg(feature = "with-returning")] +fn delete_returning_specific_columns() { + assert_eq!( + Query::delete() + .from_table(Glyph::Table) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) + .to_string(SqliteQueryBuilder), + r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING `id`, `image`"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +fn delete_returning_all_columns_with_returning_disabled() { + assert_eq!( + Query::delete() + .from_table(Glyph::Table) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::All) + .to_string(SqliteQueryBuilder), + r#"DELETE FROM `glyph` WHERE `id` = 1"# + ); +} + +#[test] +#[cfg(not(feature = "with-returning"))] +fn delete_returning_specific_columns_with_returning_disabled() { + assert_eq!( + Query::delete() + .from_table(Glyph::Table) + .and_where(Expr::col(Glyph::Id).eq(1)) + .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) + .to_string(SqliteQueryBuilder), + r#"DELETE FROM `glyph` WHERE `id` = 1"# + ); +} From 254d13b3d65502df3bf1cee7fff90e950151d8de Mon Sep 17 00:00:00 2001 From: Marlon Sousa Date: Tue, 5 Oct 2021 11:48:34 -0300 Subject: [PATCH 3/3] remove feature guard --- .github/workflows/rust.yml | 10 ---- Cargo.toml | 4 +- src/backend/query_builder.rs | 10 ---- src/query/delete.rs | 4 +- src/query/insert.rs | 4 +- src/query/update.rs | 4 +- tests/mysql/query.rs | 104 ----------------------------------- tests/sqlite/query.rs | 104 ----------------------------------- 8 files changed, 8 insertions(+), 236 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 4272b182f..1d757f85e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -92,16 +92,6 @@ jobs: command: test args: --all-features - - uses: actions-rs/cargo@v1 - with: - command: test - args: --no-default-features --features="backend-mysql" --test test-mysql - - - uses: actions-rs/cargo@v1 - with: - command: test - args: --no-default-features --features="backend-sqlite" --test test-sqlite - derive-test: name: Derive Tests runs-on: ubuntu-20.04 diff --git a/Cargo.toml b/Cargo.toml index f7af98d0b..9f92c54af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,7 @@ uuid = { version = "^0", optional = true } [features] backend-mysql = [] -backend-postgres = ["with-returning"] +backend-postgres = [] backend-sqlite = [] default = ["derive", "backend-mysql", "backend-postgres", "backend-sqlite"] derive = ["sea-query-derive"] @@ -67,7 +67,7 @@ with-json = ["serde_json"] with-rust_decimal = ["rust_decimal"] with-bigdecimal = ["bigdecimal"] with-uuid = ["uuid"] -with-returning = [] + [[test]] name = "test-derive" diff --git a/src/backend/query_builder.rs b/src/backend/query_builder.rs index 853504218..801d4d893 100644 --- a/src/backend/query_builder.rs +++ b/src/backend/query_builder.rs @@ -734,16 +734,6 @@ pub trait QueryBuilder: QuotedBuilder { #[doc(hidden)] /// Hook to insert "RETURNING" statements. - #[cfg(not(feature = "with-returning"))] - fn prepare_returning( - &self, - returning: &Returning, - _sql: &mut SqlWriter, - _collector: &mut dyn FnMut(Value), - ) { - } - - #[cfg(feature = "with-returning")] fn prepare_returning( &self, returning: &Returning, diff --git a/src/query/delete.rs b/src/query/delete.rs index 5d9953e96..c30a02d78 100644 --- a/src/query/delete.rs +++ b/src/query/delete.rs @@ -100,7 +100,7 @@ impl DeleteStatement { self } - /// RETURNING expressions. Enabled by default for postgres and under the feature with-returning for the remaining back ends. + /// RETURNING expressions. Supported fully by postgres, version deppendant on other databases /// /// ``` /// use sea_query::{tests_cfg::*, *}; @@ -129,7 +129,7 @@ impl DeleteStatement { self } - /// RETURNING a column after delete. Enabled by default for postgres and under the feature with-returning for the remaining back ends. + /// RETURNING a column after delete. Supported fully by postgres, version deppendant on other databases /// Wrapper over [`DeleteStatement::returning()`]. /// /// ``` diff --git a/src/query/insert.rs b/src/query/insert.rs index 896bff066..1ead717e9 100644 --- a/src/query/insert.rs +++ b/src/query/insert.rs @@ -179,7 +179,7 @@ impl InsertStatement { self.exprs(values).unwrap() } - /// RETURNING expressions. Enabled by default for postgres and under the feature with-returning for the remaining back ends. + /// RETURNING expressions. Supported fully by postgres, version deppendant on other databases /// /// ``` /// use sea_query::{tests_cfg::*, *}; @@ -209,7 +209,7 @@ impl InsertStatement { self } - /// RETURNING a column after insertion. Enabled by default for postgres and under the feature with-returning for the remaining back ends. + /// RETURNING a column after insertion. Supported fully by postgres, version deppendant on other databases /// Wrapper over [`InsertStatement::returning()`]. /// /// ``` diff --git a/src/query/update.rs b/src/query/update.rs index c43c6dcad..cca26ff11 100644 --- a/src/query/update.rs +++ b/src/query/update.rs @@ -223,7 +223,7 @@ impl UpdateStatement { self } - /// RETURNING expressions. Enabled by default for postgres and under the feature with-returning for the remaining back ends. + /// RETURNING expressions. Supported fully by postgres, version deppendant on other databases /// /// ``` /// use sea_query::{tests_cfg::*, *}; @@ -254,7 +254,7 @@ impl UpdateStatement { self } - /// RETURNING a column after update. Enabled by default for postgres and under the feature with-returning for the remaining back ends. + /// RETURNING a column after update. Supported fully by postgres, version deppendant on other databases /// Wrapper over [`UpdateStatement::returning()`]. /// /// ``` diff --git a/tests/mysql/query.rs b/tests/mysql/query.rs index 702c2ddda..fe20f7dbc 100644 --- a/tests/mysql/query.rs +++ b/tests/mysql/query.rs @@ -837,7 +837,6 @@ fn insert_5() { } #[test] -#[cfg(feature = "with-returning")] #[allow(clippy::approx_constant)] fn insert_returning_all_columns() { assert_eq!( @@ -855,7 +854,6 @@ fn insert_returning_all_columns() { } #[test] -#[cfg(feature = "with-returning")] #[allow(clippy::approx_constant)] fn insert_returning_specific_columns() { assert_eq!( @@ -872,42 +870,6 @@ fn insert_returning_specific_columns() { ); } -#[test] -#[cfg(not(feature = "with-returning"))] -#[allow(clippy::approx_constant)] -fn insert_returning_all_columns_with_returning_disabled() { - assert_eq!( - Query::insert() - .into_table(Glyph::Table) - .columns(vec![Glyph::Image, Glyph::Aspect,]) - .values_panic(vec![ - "04108048005887010020060000204E0180400400".into(), - 3.1415.into(), - ]) - .returning(Returning::All) - .to_string(MysqlQueryBuilder), - r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415)"# - ); -} - -#[test] -#[cfg(not(feature = "with-returning"))] -#[allow(clippy::approx_constant)] -fn insert_returning_specific_columns_with_returning_disabled() { - assert_eq!( - Query::insert() - .into_table(Glyph::Table) - .columns(vec![Glyph::Image, Glyph::Aspect,]) - .values_panic(vec![ - "04108048005887010020060000204E0180400400".into(), - 3.1415.into(), - ]) - .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) - .to_string(MysqlQueryBuilder), - r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415)"# - ); -} - #[test] fn update_1() { assert_eq!( @@ -943,7 +905,6 @@ fn update_3() { } #[test] -#[cfg(feature = "with-returning")] fn update_returning_all_columns() { assert_eq!( Query::update() @@ -961,7 +922,6 @@ fn update_returning_all_columns() { } #[test] -#[cfg(feature = "with-returning")] fn update_returning_specified_columns() { assert_eq!( Query::update() @@ -978,42 +938,6 @@ fn update_returning_specified_columns() { ); } -#[test] -#[cfg(not(feature = "with-returning"))] -fn update_returning_all_columns_with_returning_disabled() { - assert_eq!( - Query::update() - .table(Glyph::Table) - .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) - .values(vec![( - Glyph::Image, - "24B0E11951B03B07F8300FD003983F03F0780060".into() - ),]) - .and_where(Expr::col(Glyph::Id).eq(1)) - .returning(Returning::All) - .to_string(MysqlQueryBuilder), - r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1"# - ); -} - -#[test] -#[cfg(not(feature = "with-returning"))] -fn update_returning_specified_columns_with_returning_disabled() { - assert_eq!( - Query::update() - .table(Glyph::Table) - .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) - .values(vec![( - Glyph::Image, - "24B0E11951B03B07F8300FD003983F03F0780060".into() - ),]) - .and_where(Expr::col(Glyph::Id).eq(1)) - .returning(Returning::cols(vec![Glyph::Id, Glyph::Image])) - .to_string(MysqlQueryBuilder), - r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1"# - ); -} - #[test] fn delete_1() { assert_eq!( @@ -1028,7 +952,6 @@ fn delete_1() { } #[test] -#[cfg(feature = "with-returning")] fn delete_returning_all_columns() { assert_eq!( Query::delete() @@ -1041,7 +964,6 @@ fn delete_returning_all_columns() { } #[test] -#[cfg(feature = "with-returning")] fn delete_returning_specific_columns() { assert_eq!( Query::delete() @@ -1052,29 +974,3 @@ fn delete_returning_specific_columns() { r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING `id`, `image`"# ); } - -#[test] -#[cfg(not(feature = "with-returning"))] -fn delete_returning_all_columns_with_returning_disabled() { - assert_eq!( - Query::delete() - .from_table(Glyph::Table) - .and_where(Expr::col(Glyph::Id).eq(1)) - .returning(Returning::All) - .to_string(MysqlQueryBuilder), - r#"DELETE FROM `glyph` WHERE `id` = 1"# - ); -} - -#[test] -#[cfg(not(feature = "with-returning"))] -fn delete_returning_specific_columns_with_returning_disabled() { - assert_eq!( - Query::delete() - .from_table(Glyph::Table) - .and_where(Expr::col(Glyph::Id).eq(1)) - .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) - .to_string(MysqlQueryBuilder), - r#"DELETE FROM `glyph` WHERE `id` = 1"# - ); -} diff --git a/tests/sqlite/query.rs b/tests/sqlite/query.rs index 0a70fc1c6..555b7d940 100644 --- a/tests/sqlite/query.rs +++ b/tests/sqlite/query.rs @@ -837,7 +837,6 @@ fn insert_5() { } #[test] -#[cfg(feature = "with-returning")] #[allow(clippy::approx_constant)] fn insert_returning_all_columns() { assert_eq!( @@ -855,7 +854,6 @@ fn insert_returning_all_columns() { } #[test] -#[cfg(feature = "with-returning")] #[allow(clippy::approx_constant)] fn insert_returning_specific_columns() { assert_eq!( @@ -872,42 +870,6 @@ fn insert_returning_specific_columns() { ); } -#[test] -#[cfg(not(feature = "with-returning"))] -#[allow(clippy::approx_constant)] -fn insert_returning_all_columns_with_returning_disabled() { - assert_eq!( - Query::insert() - .into_table(Glyph::Table) - .columns(vec![Glyph::Image, Glyph::Aspect,]) - .values_panic(vec![ - "04108048005887010020060000204E0180400400".into(), - 3.1415.into(), - ]) - .returning(Returning::All) - .to_string(SqliteQueryBuilder), - r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415)"# - ); -} - -#[test] -#[cfg(not(feature = "with-returning"))] -#[allow(clippy::approx_constant)] -fn insert_returning_specific_columns_with_returning_disabled() { - assert_eq!( - Query::insert() - .into_table(Glyph::Table) - .columns(vec![Glyph::Image, Glyph::Aspect,]) - .values_panic(vec![ - "04108048005887010020060000204E0180400400".into(), - 3.1415.into(), - ]) - .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) - .to_string(SqliteQueryBuilder), - r#"INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415)"# - ); -} - #[test] fn update_1() { assert_eq!( @@ -939,7 +901,6 @@ fn update_3() { } #[test] -#[cfg(feature = "with-returning")] fn update_returning_all_columns() { assert_eq!( Query::update() @@ -957,7 +918,6 @@ fn update_returning_all_columns() { } #[test] -#[cfg(feature = "with-returning")] fn update_returning_specified_columns() { assert_eq!( Query::update() @@ -974,42 +934,6 @@ fn update_returning_specified_columns() { ); } -#[test] -#[cfg(not(feature = "with-returning"))] -fn update_returning_all_columns_with_returning_disabled() { - assert_eq!( - Query::update() - .table(Glyph::Table) - .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) - .values(vec![( - Glyph::Image, - "24B0E11951B03B07F8300FD003983F03F0780060".into() - ),]) - .and_where(Expr::col(Glyph::Id).eq(1)) - .returning(Returning::All) - .to_string(SqliteQueryBuilder), - r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1"# - ); -} - -#[test] -#[cfg(not(feature = "with-returning"))] -fn update_returning_specified_columns_with_returning_disabled() { - assert_eq!( - Query::update() - .table(Glyph::Table) - .value_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24")) - .values(vec![( - Glyph::Image, - "24B0E11951B03B07F8300FD003983F03F0780060".into() - ),]) - .and_where(Expr::col(Glyph::Id).eq(1)) - .returning(Returning::cols(vec![Glyph::Id, Glyph::Image])) - .to_string(SqliteQueryBuilder), - r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060' WHERE `id` = 1"# - ); -} - #[test] fn delete_1() { assert_eq!( @@ -1022,7 +946,6 @@ fn delete_1() { } #[test] -#[cfg(feature = "with-returning")] fn delete_returning_all_columns() { assert_eq!( Query::delete() @@ -1035,7 +958,6 @@ fn delete_returning_all_columns() { } #[test] -#[cfg(feature = "with-returning")] fn delete_returning_specific_columns() { assert_eq!( Query::delete() @@ -1046,29 +968,3 @@ fn delete_returning_specific_columns() { r#"DELETE FROM `glyph` WHERE `id` = 1 RETURNING `id`, `image`"# ); } - -#[test] -#[cfg(not(feature = "with-returning"))] -fn delete_returning_all_columns_with_returning_disabled() { - assert_eq!( - Query::delete() - .from_table(Glyph::Table) - .and_where(Expr::col(Glyph::Id).eq(1)) - .returning(Returning::All) - .to_string(SqliteQueryBuilder), - r#"DELETE FROM `glyph` WHERE `id` = 1"# - ); -} - -#[test] -#[cfg(not(feature = "with-returning"))] -fn delete_returning_specific_columns_with_returning_disabled() { - assert_eq!( - Query::delete() - .from_table(Glyph::Table) - .and_where(Expr::col(Glyph::Id).eq(1)) - .returning(Returning::cols(vec![Glyph::Id, Glyph::Image,])) - .to_string(SqliteQueryBuilder), - r#"DELETE FROM `glyph` WHERE `id` = 1"# - ); -}