Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/ast/dml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ pub struct Update {
pub output: Option<OutputClause>,
/// SQLite-specific conflict resolution clause
pub or: Option<SqliteOnConflict>,
/// ORDER BY (MySQL extension for single-table UPDATE)
/// See <https://dev.mysql.com/doc/refman/8.4/en/update.html>
pub order_by: Vec<OrderByExpr>,
/// LIMIT
pub limit: Option<Expr>,
}
Expand Down Expand Up @@ -434,6 +437,11 @@ impl Display for Update {
f.write_str("RETURNING")?;
indented_list(f, returning)?;
}
if !self.order_by.is_empty() {
SpaceOrNewline.fmt(f)?;
f.write_str("ORDER BY")?;
indented_list(f, &self.order_by)?;
}
if let Some(limit) = &self.limit {
SpaceOrNewline.fmt(f)?;
write!(f, "LIMIT {limit}")?;
Expand Down
2 changes: 2 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ impl Spanned for Update {
returning,
output,
or: _,
order_by,
limit,
} = self;

Expand All @@ -963,6 +964,7 @@ impl Spanned for Update {
.chain(selection.iter().map(|i| i.span()))
.chain(returning.iter().flat_map(|i| i.iter().map(|k| k.span())))
.chain(output.iter().map(|i| i.span()))
.chain(order_by.iter().map(|i| i.span()))
.chain(limit.iter().map(|i| i.span())),
)
}
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ impl Dialect for GenericDialect {
true
}

fn supports_update_order_by(&self) -> bool {
true
}

fn supports_from_first_select(&self) -> bool {
true
}
Expand Down
10 changes: 10 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,16 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports `ORDER BY` in `UPDATE` statements.
///
/// ```sql
/// UPDATE foo SET bar = false WHERE foo = true ORDER BY foo ASC;
/// ```
/// See <https://dev.mysql.com/doc/refman/8.4/en/update.html>
fn supports_update_order_by(&self) -> bool {
false
}

/// Returns true if the dialect supports an `EXCEPT` clause following a
/// wildcard in a select list.
///
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ impl Dialect for MySqlDialect {
true
}

/// See: <https://dev.mysql.com/doc/refman/8.4/en/update.html>
fn supports_update_order_by(&self) -> bool {
true
}

fn supports_data_type_signed_suffix(&self) -> bool {
true
}
Expand Down
8 changes: 8 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17745,6 +17745,13 @@ impl<'a> Parser<'a> {
} else {
None
};
let order_by = if self.dialect.supports_update_order_by()
&& self.parse_keywords(&[Keyword::ORDER, Keyword::BY])
{
self.parse_comma_separated(Parser::parse_order_by_expr)?
} else {
vec![]
};
let limit = if self.parse_keyword(Keyword::LIMIT) {
Some(self.parse_expr()?)
} else {
Expand All @@ -17760,6 +17767,7 @@ impl<'a> Parser<'a> {
returning,
output,
or,
order_by,
limit,
}
.into())
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ fn parse_update_set_from() {
returning: None,
output: None,
or: None,
order_by: vec![],
limit: None
})
);
Expand All @@ -554,6 +555,7 @@ fn parse_update_with_table_alias() {
selection,
returning,
or: None,
order_by: _,
limit: None,
optimizer_hints,
update_token: _,
Expand Down
52 changes: 52 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,7 @@ fn parse_update_with_joins() {
selection,
returning,
or: None,
order_by: _,
limit: None,
optimizer_hints,
update_token: _,
Expand Down Expand Up @@ -2784,6 +2785,57 @@ fn parse_update_with_joins() {
}
}

#[test]
fn parse_update_with_order_by() {
let sql = "UPDATE foo SET bar = false WHERE foo = true ORDER BY foo ASC";
match mysql_and_generic().verified_stmt(sql) {
Statement::Update(Update { order_by, .. }) => {
assert_eq!(
vec![OrderByExpr {
expr: Expr::Identifier(Ident {
value: "foo".to_owned(),
quote_style: None,
span: Span::empty(),
}),
options: OrderByOptions {
asc: Some(true),
nulls_first: None,
},
with_fill: None,
}],
order_by
);
}
_ => unreachable!(),
}
}

#[test]
fn parse_update_with_order_by_and_limit() {
let sql = "UPDATE foo SET bar = false WHERE foo = true ORDER BY foo ASC LIMIT 10";
match mysql_and_generic().verified_stmt(sql) {
Statement::Update(Update { order_by, limit, .. }) => {
assert_eq!(
vec![OrderByExpr {
expr: Expr::Identifier(Ident {
value: "foo".to_owned(),
quote_style: None,
span: Span::empty(),
}),
options: OrderByOptions {
asc: Some(true),
nulls_first: None,
},
with_fill: None,
}],
order_by
);
assert_eq!(Some(Expr::value(number("10"))), limit);
}
_ => unreachable!(),
}
}

#[test]
fn parse_delete_with_order_by() {
let sql = "DELETE FROM customers ORDER BY id DESC";
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ fn parse_update_tuple_row_values() {
from: None,
returning: None,
output: None,
order_by: vec![],
limit: None,
update_token: AttachedToken::empty()
})
Expand Down