Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Jan 19, 2022
1 parent f31327b commit c86cf07
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 63 deletions.
48 changes: 19 additions & 29 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::backend::query_builder::InsertValueSource;
use crate::*;
use std::ops::Deref;

Expand Down Expand Up @@ -35,41 +34,32 @@ pub trait QueryBuilder: QuotedBuilder {

if let Some(source) = &insert.source {
write!(sql, " ").unwrap();
self.prepare_insert_value_source(source, sql, collector);
}

self.prepare_returning(&insert.returning, sql, collector);
}

fn prepare_insert_value_source(
&self,
source: &InsertValueSource,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value),
) {
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| {
match source {
InsertValueSource::Values(values) => {
write!(sql, "VALUES ").unwrap();
values.iter().fold(true, |first, row| {
if !first {
write!(sql, ", ").unwrap()
}
self.prepare_simple_expr(col, sql, collector);
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();
false
});
write!(sql, ")").unwrap();
false
});
}
InsertValueSource::Select(select_query) => {
self.prepare_select_statement(select_query.deref(), sql, collector);
}
InsertValueSource::Select(select_query) => {
self.prepare_select_statement(select_query.deref(), sql, collector);
}
}
}

self.prepare_returning(&insert.returning, sql, collector);
}

/// Translate [`SelectStatement`] into SQL statement.
Expand Down
36 changes: 2 additions & 34 deletions src/query/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
/// [`InsertValueSource`] is a node in the expression tree and can represent a raw value set
/// ('VALUES') or a select query.
#[derive(Debug, Clone)]
enum InsertValueSource {
pub(crate) enum InsertValueSource {
Values(Vec<Vec<SimpleExpr>>),
Select(Box<SelectStatement>),
}
Expand Down Expand Up @@ -190,39 +190,7 @@ impl InsertStatement {
}

/// Specify the value source of the insert.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::insert()
/// .into_table(Glyph::Table)
/// .columns(vec![Glyph::Aspect, Glyph::Image])
/// .value_source(InsertValueSource::Select(Box::new(Query::select()
/// .column(Glyph::Aspect)
/// .column(Glyph::Image)
/// .from(Glyph::Table)
/// .and_where(Expr::col(Glyph::Image).like("0%"))
/// .to_owned()))
/// )
/// .unwrap()
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"INSERT INTO `glyph` (`aspect`, `image`) SELECT `aspect`, `image` FROM `glyph` WHERE `image` LIKE '0%'"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"INSERT INTO "glyph" ("aspect", "image") SELECT "aspect", "image" FROM "glyph" WHERE "image" LIKE '0%'"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"INSERT INTO "glyph" ("aspect", "image") SELECT "aspect", "image" FROM "glyph" WHERE "image" LIKE '0%'"#
/// );
/// ```
fn value_source<S>(&mut self, source: S) -> Result<&mut InsertStatement>
fn value_source<S>(&mut self, source: S) -> Result<&mut Self>
where
S: Into<InsertValueSource>,
{
Expand Down

0 comments on commit c86cf07

Please sign in to comment.