mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-22 23:14:07 +00:00
Support INSERT IGNORE
in MySql
and GenericDialect
(#1004)
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
parent
6739d377bd
commit
86aa1b96be
3 changed files with 50 additions and 1 deletions
|
@ -1296,6 +1296,8 @@ pub enum Statement {
|
||||||
Insert {
|
Insert {
|
||||||
/// Only for Sqlite
|
/// Only for Sqlite
|
||||||
or: Option<SqliteOnConflict>,
|
or: Option<SqliteOnConflict>,
|
||||||
|
/// Only for mysql
|
||||||
|
ignore: bool,
|
||||||
/// INTO - optional keyword
|
/// INTO - optional keyword
|
||||||
into: bool,
|
into: bool,
|
||||||
/// TABLE
|
/// TABLE
|
||||||
|
@ -2126,6 +2128,7 @@ impl fmt::Display for Statement {
|
||||||
}
|
}
|
||||||
Statement::Insert {
|
Statement::Insert {
|
||||||
or,
|
or,
|
||||||
|
ignore,
|
||||||
into,
|
into,
|
||||||
table_name,
|
table_name,
|
||||||
overwrite,
|
overwrite,
|
||||||
|
@ -2142,8 +2145,9 @@ impl fmt::Display for Statement {
|
||||||
} else {
|
} else {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"INSERT{over}{int}{tbl} {table_name} ",
|
"INSERT{ignore}{over}{int}{tbl} {table_name} ",
|
||||||
table_name = table_name,
|
table_name = table_name,
|
||||||
|
ignore = if *ignore { " IGNORE" } else { "" },
|
||||||
over = if *overwrite { " OVERWRITE" } else { "" },
|
over = if *overwrite { " OVERWRITE" } else { "" },
|
||||||
int = if *into { " INTO" } else { "" },
|
int = if *into { " INTO" } else { "" },
|
||||||
tbl = if *table { " TABLE" } else { "" }
|
tbl = if *table { " TABLE" } else { "" }
|
||||||
|
|
|
@ -6755,6 +6755,9 @@ impl<'a> Parser<'a> {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let ignore = dialect_of!(self is MySqlDialect | GenericDialect)
|
||||||
|
&& self.parse_keyword(Keyword::IGNORE);
|
||||||
|
|
||||||
let action = self.parse_one_of_keywords(&[Keyword::INTO, Keyword::OVERWRITE]);
|
let action = self.parse_one_of_keywords(&[Keyword::INTO, Keyword::OVERWRITE]);
|
||||||
let into = action == Some(Keyword::INTO);
|
let into = action == Some(Keyword::INTO);
|
||||||
let overwrite = action == Some(Keyword::OVERWRITE);
|
let overwrite = action == Some(Keyword::OVERWRITE);
|
||||||
|
@ -6852,6 +6855,7 @@ impl<'a> Parser<'a> {
|
||||||
Ok(Statement::Insert {
|
Ok(Statement::Insert {
|
||||||
or,
|
or,
|
||||||
table_name,
|
table_name,
|
||||||
|
ignore,
|
||||||
into,
|
into,
|
||||||
overwrite,
|
overwrite,
|
||||||
partitioned,
|
partitioned,
|
||||||
|
|
|
@ -972,6 +972,47 @@ fn parse_simple_insert() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_ignore_insert() {
|
||||||
|
let sql = r"INSERT IGNORE INTO tasks (title, priority) VALUES ('Test Some Inserts', 1)";
|
||||||
|
|
||||||
|
match mysql_and_generic().verified_stmt(sql) {
|
||||||
|
Statement::Insert {
|
||||||
|
table_name,
|
||||||
|
columns,
|
||||||
|
source,
|
||||||
|
on,
|
||||||
|
ignore,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
assert_eq!(ObjectName(vec![Ident::new("tasks")]), table_name);
|
||||||
|
assert_eq!(vec![Ident::new("title"), Ident::new("priority")], columns);
|
||||||
|
assert!(on.is_none());
|
||||||
|
assert!(ignore);
|
||||||
|
assert_eq!(
|
||||||
|
Box::new(Query {
|
||||||
|
with: None,
|
||||||
|
body: Box::new(SetExpr::Values(Values {
|
||||||
|
explicit_row: false,
|
||||||
|
rows: vec![vec![
|
||||||
|
Expr::Value(Value::SingleQuotedString("Test Some Inserts".to_string())),
|
||||||
|
Expr::Value(number("1"))
|
||||||
|
]]
|
||||||
|
})),
|
||||||
|
order_by: vec![],
|
||||||
|
limit: None,
|
||||||
|
limit_by: vec![],
|
||||||
|
offset: None,
|
||||||
|
fetch: None,
|
||||||
|
locks: vec![]
|
||||||
|
}),
|
||||||
|
source
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_empty_row_insert() {
|
fn parse_empty_row_insert() {
|
||||||
let sql = "INSERT INTO tb () VALUES (), ()";
|
let sql = "INSERT INTO tb () VALUES (), ()";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue