Skip to content

Commit

Permalink
Support INSERT IGNORE in MySql and GenericDialect (#1004)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Lamb <[email protected]>
  • Loading branch information
emin100 and alamb authored Oct 24, 2023
1 parent 6739d37 commit 86aa1b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2126,6 +2128,7 @@ impl fmt::Display for Statement {
}
Statement::Insert {
or,
ignore,
into,
table_name,
overwrite,
Expand All @@ -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 { "" }
Expand Down
4 changes: 4 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -6852,6 +6855,7 @@ impl<'a> Parser<'a> {
Ok(Statement::Insert {
or,
table_name,
ignore,
into,
overwrite,
partitioned,
Expand Down
41 changes: 41 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (), ()";
Expand Down

0 comments on commit 86aa1b9

Please sign in to comment.