Skip to content

Commit

Permalink
Mysql INSERT IGNORE support added.
Browse files Browse the repository at this point in the history
SQL: INSERT IGNORE INTO tasks (title, priority) VALUES ('Test Some Inserts', 1)
ERROR:  sql parser error: Expected SELECT, VALUES, or a subquery in the query body, found: INTO at Line: 1, Column 15
  • Loading branch information
emin100 committed Oct 17, 2023
1 parent 83cb734 commit 861c8c7
Show file tree
Hide file tree
Showing 3 changed files with 49 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 @@ -1214,6 +1214,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 @@ -2038,6 +2040,7 @@ impl fmt::Display for Statement {
}
Statement::Insert {
or,
ignore,
into,
table_name,
overwrite,
Expand All @@ -2054,8 +2057,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
3 changes: 3 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6697,6 +6697,8 @@ impl<'a> Parser<'a> {
None
};

let ignore = dialect_of!(self is MySqlDialect) && 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 @@ -6794,6 +6796,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().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_eq!(ignore, true);

Check failure on line 991 in tests/sqlparser_mysql.rs

View workflow job for this annotation

GitHub Actions / lint

used `assert_eq!` with a literal bool
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 861c8c7

Please sign in to comment.