diff --git a/src/backend/mysql/query.rs b/src/backend/mysql/query.rs index b0a1eff9c3..5adc07fd34 100644 --- a/src/backend/mysql/query.rs +++ b/src/backend/mysql/query.rs @@ -56,8 +56,4 @@ impl QueryBuilder for MysqlQueryBuilder { ) { // MySQL doesn't support declaring materialization in SQL for with query. } - - fn insert_default_keyword(&self) -> &str { - "VALUES (DEFAULT)" - } } diff --git a/src/backend/query_builder.rs b/src/backend/query_builder.rs index 5093712524..0beaf50008 100644 --- a/src/backend/query_builder.rs +++ b/src/backend/query_builder.rs @@ -22,43 +22,39 @@ pub trait QueryBuilder: QuotedBuilder { write!(sql, " ").unwrap(); } - if insert.columns.is_empty() && insert.source.is_none() { - write!(sql, "{}", self.insert_default_keyword()).unwrap(); - } else { - write!(sql, "(").unwrap(); - insert.columns.iter().fold(true, |first, col| { - if !first { - write!(sql, ", ").unwrap() - } - col.prepare(sql, self.quote()); - false - }); - write!(sql, ")").unwrap(); + write!(sql, "(").unwrap(); + insert.columns.iter().fold(true, |first, col| { + if !first { + write!(sql, ", ").unwrap() + } + col.prepare(sql, self.quote()); + false + }); + write!(sql, ")").unwrap(); - if let Some(source) = &insert.source { - write!(sql, " ").unwrap(); - match source { - InsertValueSource::Values(values) => { - write!(sql, "VALUES ").unwrap(); - values.iter().fold(true, |first, row| { + if let Some(source) = &insert.source { + write!(sql, " ").unwrap(); + match source { + InsertValueSource::Values(values) => { + write!(sql, "VALUES ").unwrap(); + values.iter().fold(true, |first, row| { + if !first { + write!(sql, ", ").unwrap() + } + write!(sql, "(").unwrap(); + row.iter().fold(true, |first, col| { if !first { write!(sql, ", ").unwrap() } - write!(sql, "(").unwrap(); - row.iter().fold(true, |first, col| { - if !first { - write!(sql, ", ").unwrap() - } - self.prepare_simple_expr(col, sql, collector); - false - }); - write!(sql, ")").unwrap(); + self.prepare_simple_expr(col, sql, collector); false }); - } - InsertValueSource::Select(select_query) => { - self.prepare_select_statement(select_query.deref(), sql, collector); - } + write!(sql, ")").unwrap(); + false + }); + } + InsertValueSource::Select(select_query) => { + self.prepare_select_statement(select_query.deref(), sql, collector); } } } @@ -1182,11 +1178,6 @@ pub trait QueryBuilder: QuotedBuilder { fn char_length_function(&self) -> &str { "CHAR_LENGTH" } - - /// The keywords for insert default statement (insert without explicit values) - fn insert_default_keyword(&self) -> &str { - "DEFAULT VALUES" - } } impl SubQueryStatement { diff --git a/src/query/insert.rs b/src/query/insert.rs index 2364c540d7..1198b57dd6 100644 --- a/src/query/insert.rs +++ b/src/query/insert.rs @@ -260,19 +260,17 @@ impl InsertStatement { val_len: values.len(), }); } - if !values.is_empty() { - let values_source = if let Some(InsertValueSource::Values(values)) = &mut self.source { + let values_source = if let Some(InsertValueSource::Values(values)) = &mut self.source { + values + } else { + self.source = Some(InsertValueSource::Values(Default::default())); + if let Some(InsertValueSource::Values(values)) = &mut self.source { values } else { - self.source = Some(InsertValueSource::Values(Default::default())); - if let Some(InsertValueSource::Values(values)) = &mut self.source { - values - } else { - unreachable!(); - } - }; - values_source.push(values); - } + unreachable!(); + } + }; + values_source.push(values); Ok(self) } diff --git a/tests/mysql/query.rs b/tests/mysql/query.rs index 2fa04c2828..d49eeeed95 100644 --- a/tests/mysql/query.rs +++ b/tests/mysql/query.rs @@ -982,27 +982,6 @@ fn insert_5() { ); } -#[test] -fn insert_6() { - assert_eq!( - Query::insert() - .into_table(Glyph::Table) - .to_string(MysqlQueryBuilder), - "INSERT INTO `glyph` VALUES (DEFAULT)" - ); -} - -#[test] -fn insert_7() { - assert_eq!( - Query::insert() - .into_table(Glyph::Table) - .returning_col(Glyph::Id) - .to_string(MysqlQueryBuilder), - "INSERT INTO `glyph` VALUES (DEFAULT)" - ); -} - #[test] fn insert_from_select() { assert_eq!( diff --git a/tests/postgres/query.rs b/tests/postgres/query.rs index 4b4e17dda8..b43a8ecace 100644 --- a/tests/postgres/query.rs +++ b/tests/postgres/query.rs @@ -951,6 +951,35 @@ fn insert_5() { ); } +#[test] +fn insert_from_select() { + assert_eq!( + Query::insert() + .into_table(Glyph::Table) + .columns(vec![Glyph::Aspect, Glyph::Image]) + .select_from( + Query::select() + .column(Glyph::Aspect) + .column(Glyph::Image) + .from(Glyph::Table) + .conditions( + true, + |x| { + x.and_where(Expr::col(Glyph::Image).like("%")); + }, + |x| { + x.and_where(Expr::col(Glyph::Id).eq(6)); + }, + ) + .to_owned() + ) + .unwrap() + .to_owned() + .to_string(PostgresQueryBuilder), + r#"INSERT INTO "glyph" ("aspect", "image") SELECT "aspect", "image" FROM "glyph" WHERE "image" LIKE '%'"# + ); +} + #[test] fn insert_6() -> sea_query::error::Result<()> { let select = SelectStatement::new() @@ -985,56 +1014,6 @@ fn insert_6() -> sea_query::error::Result<()> { Ok(()) } -#[test] -fn insert_7() { - assert_eq!( - Query::insert() - .into_table(Glyph::Table) - .to_string(PostgresQueryBuilder), - "INSERT INTO \"glyph\" DEFAULT VALUES" - ); -} - -#[test] -fn insert_8() { - assert_eq!( - Query::insert() - .into_table(Glyph::Table) - .returning_col(Glyph::Id) - .to_string(PostgresQueryBuilder), - "INSERT INTO \"glyph\" DEFAULT VALUES RETURNING \"id\"" - ); -} - -#[test] -fn insert_from_select() { - assert_eq!( - Query::insert() - .into_table(Glyph::Table) - .columns(vec![Glyph::Aspect, Glyph::Image]) - .select_from( - Query::select() - .column(Glyph::Aspect) - .column(Glyph::Image) - .from(Glyph::Table) - .conditions( - true, - |x| { - x.and_where(Expr::col(Glyph::Image).like("%")); - }, - |x| { - x.and_where(Expr::col(Glyph::Id).eq(6)); - }, - ) - .to_owned() - ) - .unwrap() - .to_owned() - .to_string(PostgresQueryBuilder), - r#"INSERT INTO "glyph" ("aspect", "image") SELECT "aspect", "image" FROM "glyph" WHERE "image" LIKE '%'"# - ); -} - #[test] fn update_1() { assert_eq!( diff --git a/tests/sqlite/query.rs b/tests/sqlite/query.rs index 5f9ca2b1ca..ed87b6ed56 100644 --- a/tests/sqlite/query.rs +++ b/tests/sqlite/query.rs @@ -980,27 +980,6 @@ fn insert_5() { ); } -#[test] -fn insert_6() { - assert_eq!( - Query::insert() - .into_table(Glyph::Table) - .to_string(SqliteQueryBuilder), - r#"INSERT INTO "glyph" DEFAULT VALUES"# - ); -} - -#[test] -fn insert_7() { - assert_eq!( - Query::insert() - .into_table(Glyph::Table) - .returning_col(Glyph::Id) - .to_string(SqliteQueryBuilder), - r#"INSERT INTO "glyph" DEFAULT VALUES RETURNING "id""# - ); -} - #[test] fn update_1() { assert_eq!(