Skip to content

support sqlite's OR clauses in update statements #1530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ast/dml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ impl Display for Insert {
self.table_name.to_string()
};

if let Some(action) = self.or {
write!(f, "INSERT OR {action} INTO {table_name} ")?;
if let Some(on_conflict) = self.or {
write!(f, "INSERT {on_conflict} INTO {table_name} ")?;
} else {
write!(
f,
Expand Down
19 changes: 13 additions & 6 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,8 @@ pub enum Statement {
selection: Option<Expr>,
/// RETURNING
returning: Option<Vec<SelectItem>>,
/// SQLite-specific conflict resolution clause
or: Option<SqliteOnConflict>,
},
/// ```sql
/// DELETE
Expand Down Expand Up @@ -3691,8 +3693,13 @@ impl fmt::Display for Statement {
from,
selection,
returning,
or,
} => {
write!(f, "UPDATE {table}")?;
write!(f, "UPDATE ")?;
if let Some(or) = or {
write!(f, "{or} ")?;
}
write!(f, "{table}")?;
if !assignments.is_empty() {
write!(f, " SET {}", display_comma_separated(assignments))?;
}
Expand Down Expand Up @@ -6304,11 +6311,11 @@ impl fmt::Display for SqliteOnConflict {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use SqliteOnConflict::*;
match self {
Rollback => write!(f, "ROLLBACK"),
Abort => write!(f, "ABORT"),
Fail => write!(f, "FAIL"),
Ignore => write!(f, "IGNORE"),
Replace => write!(f, "REPLACE"),
Rollback => write!(f, "OR ROLLBACK"),
Abort => write!(f, "OR ABORT"),
Fail => write!(f, "OR FAIL"),
Ignore => write!(f, "OR IGNORE"),
Replace => write!(f, "OR REPLACE"),
}
}
}
Expand Down
39 changes: 21 additions & 18 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11032,24 +11032,7 @@ impl<'a> Parser<'a> {

/// Parse an INSERT statement
pub fn parse_insert(&mut self) -> Result<Statement, ParserError> {
let or = if !dialect_of!(self is SQLiteDialect) {
None
} else if self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]) {
Some(SqliteOnConflict::Replace)
} else if self.parse_keywords(&[Keyword::OR, Keyword::ROLLBACK]) {
Some(SqliteOnConflict::Rollback)
} else if self.parse_keywords(&[Keyword::OR, Keyword::ABORT]) {
Some(SqliteOnConflict::Abort)
} else if self.parse_keywords(&[Keyword::OR, Keyword::FAIL]) {
Some(SqliteOnConflict::Fail)
} else if self.parse_keywords(&[Keyword::OR, Keyword::IGNORE]) {
Some(SqliteOnConflict::Ignore)
} else if self.parse_keyword(Keyword::REPLACE) {
Some(SqliteOnConflict::Replace)
} else {
None
};

let or = self.parse_conflict_clause();
let priority = if !dialect_of!(self is MySqlDialect | GenericDialect) {
None
} else if self.parse_keyword(Keyword::LOW_PRIORITY) {
Expand Down Expand Up @@ -11208,6 +11191,24 @@ impl<'a> Parser<'a> {
}
}

fn parse_conflict_clause(&mut self) -> Option<SqliteOnConflict> {
if self.parse_keywords(&[Keyword::OR, Keyword::REPLACE]) {
Some(SqliteOnConflict::Replace)
} else if self.parse_keywords(&[Keyword::OR, Keyword::ROLLBACK]) {
Some(SqliteOnConflict::Rollback)
} else if self.parse_keywords(&[Keyword::OR, Keyword::ABORT]) {
Some(SqliteOnConflict::Abort)
} else if self.parse_keywords(&[Keyword::OR, Keyword::FAIL]) {
Some(SqliteOnConflict::Fail)
} else if self.parse_keywords(&[Keyword::OR, Keyword::IGNORE]) {
Some(SqliteOnConflict::Ignore)
} else if self.parse_keyword(Keyword::REPLACE) {
Some(SqliteOnConflict::Replace)
} else {
None
}
}

pub fn parse_insert_partition(&mut self) -> Result<Option<Vec<Expr>>, ParserError> {
if self.parse_keyword(Keyword::PARTITION) {
self.expect_token(&Token::LParen)?;
Expand Down Expand Up @@ -11243,6 +11244,7 @@ impl<'a> Parser<'a> {
}

pub fn parse_update(&mut self) -> Result<Statement, ParserError> {
let or = self.parse_conflict_clause();
let table = self.parse_table_and_joins()?;
self.expect_keyword(Keyword::SET)?;
let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
Expand All @@ -11269,6 +11271,7 @@ impl<'a> Parser<'a> {
from,
selection,
returning,
or,
})
}

Expand Down
21 changes: 21 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ fn parse_update_set_from() {
])),
}),
returning: None,
or: None,
}
);
}
Expand All @@ -457,6 +458,7 @@ fn parse_update_with_table_alias() {
from: _from,
selection,
returning,
or: None,
} => {
assert_eq!(
TableWithJoins {
Expand Down Expand Up @@ -505,6 +507,25 @@ fn parse_update_with_table_alias() {
}
}

#[test]
fn parse_update_or() {
let expect_or_clause = |sql: &str, expected_action: SqliteOnConflict| match verified_stmt(sql) {
Statement::Update { or, .. } => assert_eq!(or, Some(expected_action)),
other => unreachable!("Expected update with or, got {:?}", other),
};
expect_or_clause(
"UPDATE OR REPLACE t SET n = n + 1",
SqliteOnConflict::Replace,
);
expect_or_clause(
"UPDATE OR ROLLBACK t SET n = n + 1",
SqliteOnConflict::Rollback,
);
expect_or_clause("UPDATE OR ABORT t SET n = n + 1", SqliteOnConflict::Abort);
expect_or_clause("UPDATE OR FAIL t SET n = n + 1", SqliteOnConflict::Fail);
expect_or_clause("UPDATE OR IGNORE t SET n = n + 1", SqliteOnConflict::Ignore);
}

#[test]
fn parse_select_with_table_alias_as() {
// AS is optional
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,7 @@ fn parse_update_with_joins() {
from: _from,
selection,
returning,
or: None,
} => {
assert_eq!(
TableWithJoins {
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ fn parse_update_tuple_row_values() {
assert_eq!(
sqlite().verified_stmt("UPDATE x SET (a, b) = (1, 2)"),
Statement::Update {
or: None,
assignments: vec![Assignment {
target: AssignmentTarget::Tuple(vec![
ObjectName(vec![Ident::new("a"),]),
Expand Down
Loading