From 32887518ce62dadc7c1022fa45ebf251ad679409 Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Sun, 9 Feb 2025 12:59:08 +0200 Subject: [PATCH] sqlite3-parser: separate boxed Delete struct --- core/translate/mod.rs | 13 +++---- .../sqlite3-parser/src/parser/ast/check.rs | 27 +++++++++----- vendored/sqlite3-parser/src/parser/ast/fmt.rs | 19 +++++----- vendored/sqlite3-parser/src/parser/ast/mod.rs | 36 ++++++++++--------- vendored/sqlite3-parser/src/parser/parse.y | 8 ++--- 5 files changed, 59 insertions(+), 44 deletions(-) diff --git a/core/translate/mod.rs b/core/translate/mod.rs index d9c3f7421..d279ffb3b 100644 --- a/core/translate/mod.rs +++ b/core/translate/mod.rs @@ -74,12 +74,13 @@ pub fn translate( ast::Stmt::CreateVirtualTable { .. } => { bail_parse_error!("CREATE VIRTUAL TABLE not supported yet") } - ast::Stmt::Delete { - tbl_name, - where_clause, - limit, - .. - } => { + ast::Stmt::Delete(delete) => { + let Delete { + tbl_name, + where_clause, + limit, + .. + } = *delete; change_cnt_on = true; translate_delete(query_mode, schema, &tbl_name, where_clause, limit, syms)? } diff --git a/vendored/sqlite3-parser/src/parser/ast/check.rs b/vendored/sqlite3-parser/src/parser/ast/check.rs index d885f5485..372135014 100644 --- a/vendored/sqlite3-parser/src/parser/ast/check.rs +++ b/vendored/sqlite3-parser/src/parser/ast/check.rs @@ -56,10 +56,13 @@ impl Stmt { /// Like `sqlite3_column_count` but more limited pub fn column_count(&self) -> ColumnCount { match self { - Self::Delete { - returning: Some(returning), - .. - } => column_count(returning), + Self::Delete(delete) => { + let Delete { returning, .. } = &**delete; + match returning { + Some(returning) => column_count(returning), + None => ColumnCount::None, + } + } Self::Insert { returning: Some(returning), .. @@ -156,11 +159,17 @@ impl Stmt { _ => Ok(()), } } - Self::Delete { - order_by: Some(_), - limit: None, - .. - } => Err(custom_err!("ORDER BY without LIMIT on DELETE")), + Self::Delete(delete) => { + let Delete { + order_by, limit, .. + } = &**delete; + if let Some(_) = order_by { + if limit.is_none() { + return Err(custom_err!("ORDER BY without LIMIT on DELETE")); + } + } + Ok(()) + } Self::Insert { columns: Some(columns), body, diff --git a/vendored/sqlite3-parser/src/parser/ast/fmt.rs b/vendored/sqlite3-parser/src/parser/ast/fmt.rs index 5fc10dc89..0088497ef 100644 --- a/vendored/sqlite3-parser/src/parser/ast/fmt.rs +++ b/vendored/sqlite3-parser/src/parser/ast/fmt.rs @@ -305,15 +305,16 @@ impl ToTokens for Stmt { } s.append(TK_RP, None) } - Self::Delete { - with, - tbl_name, - indexed, - where_clause, - returning, - order_by, - limit, - } => { + Self::Delete(delete) => { + let Delete { + with, + tbl_name, + indexed, + where_clause, + returning, + order_by, + limit, + } = &**delete; if let Some(with) = with { with.to_tokens(s)?; } diff --git a/vendored/sqlite3-parser/src/parser/ast/mod.rs b/vendored/sqlite3-parser/src/parser/ast/mod.rs index 58af258bd..270876114 100644 --- a/vendored/sqlite3-parser/src/parser/ast/mod.rs +++ b/vendored/sqlite3-parser/src/parser/ast/mod.rs @@ -141,22 +141,7 @@ pub enum Stmt { args: Option>, // TODO smol str }, /// `DELETE` - Delete { - /// CTE - with: Option, - /// `FROM` table name - tbl_name: QualifiedName, - /// `INDEXED` - indexed: Option, - /// `WHERE` clause - where_clause: Option>, - /// `RETURNING` - returning: Option>, - /// `ORDER BY` - order_by: Option>, - /// `LIMIT` - limit: Option>, - }, + Delete(Box), /// `DETACH DATABASE`: db name Detach(Expr), // TODO distinction between DETACH and DETACH DATABASE /// `DROP INDEX` @@ -276,6 +261,25 @@ pub struct Update { pub limit: Option>, } +/// `DELETE` +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Delete { + /// CTE + pub with: Option, + /// `FROM` table name + pub tbl_name: QualifiedName, + /// `INDEXED` + pub indexed: Option, + /// `WHERE` clause + pub where_clause: Option>, + /// `RETURNING` + pub returning: Option>, + /// `ORDER BY` + pub order_by: Option>, + /// `LIMIT` + pub limit: Option>, +} + /// SQL expression // https://sqlite.org/syntax/expr.html #[derive(Clone, Debug, PartialEq, Eq)] diff --git a/vendored/sqlite3-parser/src/parser/parse.y b/vendored/sqlite3-parser/src/parser/parse.y index f24204d81..d56cd178e 100644 --- a/vendored/sqlite3-parser/src/parser/parse.y +++ b/vendored/sqlite3-parser/src/parser/parse.y @@ -761,14 +761,14 @@ limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). cmd ::= with(C) DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W) orderby_opt(O) limit_opt(L). { let (where_clause, returning) = W; - self.ctx.stmt = Some(Stmt::Delete{ with: C, tbl_name: X, indexed: I, where_clause: where_clause.map(Box::new), returning, - order_by: O, limit: L }); + self.ctx.stmt = Some(Stmt::Delete(Box::new(Delete{ with: C, tbl_name: X, indexed: I, where_clause: where_clause.map(Box::new), returning, + order_by: O, limit: L }))); } %else cmd ::= with(C) DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). { let (where_clause, returning) = W; - self.ctx.stmt = Some(Stmt::Delete{ with: C, tbl_name: X, indexed: I, where_clause: where_clause.map(Box::new), returning, - order_by: None, limit: None }); + self.ctx.stmt = Some(Stmt::Delete(Box::new(Delete{ with: C, tbl_name: X, indexed: I, where_clause: where_clause.map(Box::new), returning, + order_by: None, limit: None }))); } %endif