Support INSERT IGNORE in MySql and GenericDialect (#1004)

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
Mehmet Emin KARAKAŞ 2023-10-24 12:45:25 +03:00 committed by GitHub
parent 6739d377bd
commit 86aa1b96be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 1 deletions

View file

@ -1296,6 +1296,8 @@ pub enum Statement {
Insert {
/// Only for Sqlite
or: Option<SqliteOnConflict>,
/// Only for mysql
ignore: bool,
/// INTO - optional keyword
into: bool,
/// TABLE
@ -2126,6 +2128,7 @@ impl fmt::Display for Statement {
}
Statement::Insert {
or,
ignore,
into,
table_name,
overwrite,
@ -2142,8 +2145,9 @@ impl fmt::Display for Statement {
} else {
write!(
f,
"INSERT{over}{int}{tbl} {table_name} ",
"INSERT{ignore}{over}{int}{tbl} {table_name} ",
table_name = table_name,
ignore = if *ignore { " IGNORE" } else { "" },
over = if *overwrite { " OVERWRITE" } else { "" },
int = if *into { " INTO" } else { "" },
tbl = if *table { " TABLE" } else { "" }

View file

@ -6755,6 +6755,9 @@ impl<'a> Parser<'a> {
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 into = action == Some(Keyword::INTO);
let overwrite = action == Some(Keyword::OVERWRITE);
@ -6852,6 +6855,7 @@ impl<'a> Parser<'a> {
Ok(Statement::Insert {
or,
table_name,
ignore,
into,
overwrite,
partitioned,

View file

@ -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]
fn parse_empty_row_insert() {
let sql = "INSERT INTO tb () VALUES (), ()";