Skip to content

Commit 86aa1b9

Browse files
emin100alamb
andauthored
Support INSERT IGNORE in MySql and GenericDialect (#1004)
Co-authored-by: Andrew Lamb <[email protected]>
1 parent 6739d37 commit 86aa1b9

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/ast/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,8 @@ pub enum Statement {
12961296
Insert {
12971297
/// Only for Sqlite
12981298
or: Option<SqliteOnConflict>,
1299+
/// Only for mysql
1300+
ignore: bool,
12991301
/// INTO - optional keyword
13001302
into: bool,
13011303
/// TABLE
@@ -2126,6 +2128,7 @@ impl fmt::Display for Statement {
21262128
}
21272129
Statement::Insert {
21282130
or,
2131+
ignore,
21292132
into,
21302133
table_name,
21312134
overwrite,
@@ -2142,8 +2145,9 @@ impl fmt::Display for Statement {
21422145
} else {
21432146
write!(
21442147
f,
2145-
"INSERT{over}{int}{tbl} {table_name} ",
2148+
"INSERT{ignore}{over}{int}{tbl} {table_name} ",
21462149
table_name = table_name,
2150+
ignore = if *ignore { " IGNORE" } else { "" },
21472151
over = if *overwrite { " OVERWRITE" } else { "" },
21482152
int = if *into { " INTO" } else { "" },
21492153
tbl = if *table { " TABLE" } else { "" }

src/parser/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6755,6 +6755,9 @@ impl<'a> Parser<'a> {
67556755
None
67566756
};
67576757

6758+
let ignore = dialect_of!(self is MySqlDialect | GenericDialect)
6759+
&& self.parse_keyword(Keyword::IGNORE);
6760+
67586761
let action = self.parse_one_of_keywords(&[Keyword::INTO, Keyword::OVERWRITE]);
67596762
let into = action == Some(Keyword::INTO);
67606763
let overwrite = action == Some(Keyword::OVERWRITE);
@@ -6852,6 +6855,7 @@ impl<'a> Parser<'a> {
68526855
Ok(Statement::Insert {
68536856
or,
68546857
table_name,
6858+
ignore,
68556859
into,
68566860
overwrite,
68576861
partitioned,

tests/sqlparser_mysql.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,47 @@ fn parse_simple_insert() {
972972
}
973973
}
974974

975+
#[test]
976+
fn parse_ignore_insert() {
977+
let sql = r"INSERT IGNORE INTO tasks (title, priority) VALUES ('Test Some Inserts', 1)";
978+
979+
match mysql_and_generic().verified_stmt(sql) {
980+
Statement::Insert {
981+
table_name,
982+
columns,
983+
source,
984+
on,
985+
ignore,
986+
..
987+
} => {
988+
assert_eq!(ObjectName(vec![Ident::new("tasks")]), table_name);
989+
assert_eq!(vec![Ident::new("title"), Ident::new("priority")], columns);
990+
assert!(on.is_none());
991+
assert!(ignore);
992+
assert_eq!(
993+
Box::new(Query {
994+
with: None,
995+
body: Box::new(SetExpr::Values(Values {
996+
explicit_row: false,
997+
rows: vec![vec![
998+
Expr::Value(Value::SingleQuotedString("Test Some Inserts".to_string())),
999+
Expr::Value(number("1"))
1000+
]]
1001+
})),
1002+
order_by: vec![],
1003+
limit: None,
1004+
limit_by: vec![],
1005+
offset: None,
1006+
fetch: None,
1007+
locks: vec![]
1008+
}),
1009+
source
1010+
);
1011+
}
1012+
_ => unreachable!(),
1013+
}
1014+
}
1015+
9751016
#[test]
9761017
fn parse_empty_row_insert() {
9771018
let sql = "INSERT INTO tb () VALUES (), ()";

0 commit comments

Comments
 (0)