Support arbitrary INSERT sources

INSERT takes arbitrary queries as sources, not just VALUES clauses. For
example, `INSERT INTO foo SELECT * FROM bar` is perfectly valid.
This commit is contained in:
Nikhil Benesch 2019-05-28 19:37:00 -04:00
parent 9420070a0d
commit 14e07ebdda
No known key found for this signature in database
GPG key ID: F7386C5DEADABA7F
3 changed files with 17 additions and 11 deletions

View file

@ -342,8 +342,8 @@ pub enum SQLStatement {
table_name: SQLObjectName,
/// COLUMNS
columns: Vec<SQLIdent>,
/// VALUES (vector of rows to insert)
values: SQLValues,
/// A SQL query that specifies what to insert
source: Box<SQLQuery>,
},
SQLCopy {
/// TABLE
@ -409,13 +409,13 @@ impl ToString for SQLStatement {
SQLStatement::SQLInsert {
table_name,
columns,
values,
source,
} => {
let mut s = format!("INSERT INTO {} ", table_name.to_string());
if !columns.is_empty() {
s += &format!("({}) ", columns.join(", "));
}
s += &format!(" {}", values.to_string());
s += &source.to_string();
s
}
SQLStatement::SQLCopy {

View file

@ -1565,12 +1565,11 @@ impl Parser {
self.expect_keyword("INTO")?;
let table_name = self.parse_object_name()?;
let columns = self.parse_parenthesized_column_list(Optional)?;
self.expect_keyword("VALUES")?;
let values = self.parse_values()?;
let source = Box::new(self.parse_query()?);
Ok(SQLStatement::SQLInsert {
table_name,
columns,
values,
source,
})
}

View file

@ -52,16 +52,23 @@ fn parse_insert_values() {
SQLStatement::SQLInsert {
table_name,
columns,
values,
source,
..
} => {
assert_eq!(table_name.to_string(), expected_table_name);
assert_eq!(columns, expected_columns);
assert_eq!(values.0.as_slice(), expected_rows);
match &source.body {
SQLSetExpr::Values(SQLValues(values)) => {
assert_eq!(values.as_slice(), expected_rows)
}
_ => unreachable!(),
}
}
_ => unreachable!(),
}
}
verified_stmt("INSERT INTO customer WITH foo AS (SELECT 1) SELECT * FROM foo UNION VALUES (1)");
}
#[test]