From 36a3cb1d5e58884cf2696a8686bf78785fc88bc1 Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Sun, 9 Feb 2025 14:10:18 +0200 Subject: [PATCH] sqlite3-parser: box AlterTable --- core/translate/mod.rs | 2 +- .../sqlite3-parser/src/parser/ast/check.rs | 36 +++++++++++-------- vendored/sqlite3-parser/src/parser/ast/fmt.rs | 3 +- vendored/sqlite3-parser/src/parser/ast/mod.rs | 2 +- vendored/sqlite3-parser/src/parser/parse.y | 8 ++--- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/core/translate/mod.rs b/core/translate/mod.rs index 33504d369..7ff780e2b 100644 --- a/core/translate/mod.rs +++ b/core/translate/mod.rs @@ -52,7 +52,7 @@ pub fn translate( let mut change_cnt_on = false; let program = match stmt { - ast::Stmt::AlterTable(_, _) => bail_parse_error!("ALTER TABLE not supported yet"), + ast::Stmt::AlterTable(_) => bail_parse_error!("ALTER TABLE not supported yet"), ast::Stmt::Analyze(_) => bail_parse_error!("ANALYZE not supported yet"), ast::Stmt::Attach { .. } => bail_parse_error!("ATTACH not supported yet"), ast::Stmt::Begin(_, _) => bail_parse_error!("BEGIN not supported yet"), diff --git a/vendored/sqlite3-parser/src/parser/ast/check.rs b/vendored/sqlite3-parser/src/parser/ast/check.rs index be830afb9..3df2c1c97 100644 --- a/vendored/sqlite3-parser/src/parser/ast/check.rs +++ b/vendored/sqlite3-parser/src/parser/ast/check.rs @@ -103,22 +103,28 @@ impl Stmt { /// check for extra rules pub fn check(&self) -> Result<(), ParserError> { match self { - Self::AlterTable(old_name, AlterTableBody::RenameTo(new_name)) => { - if *new_name == old_name.name { - return Err(custom_err!( - "there is already another table or index with this name: {}", - new_name - )); - } - Ok(()) - } - Self::AlterTable(.., AlterTableBody::AddColumn(cd)) => { - for c in cd { - if let ColumnConstraint::PrimaryKey { .. } = c { - return Err(custom_err!("Cannot add a PRIMARY KEY column")); - } else if let ColumnConstraint::Unique(..) = c { - return Err(custom_err!("Cannot add a UNIQUE column")); + Self::AlterTable(alter_table) => { + let (old_name, body) = &**alter_table; + match body { + AlterTableBody::RenameTo(new_name) => { + if *new_name == old_name.name { + return Err(custom_err!( + "there is already another table or index with this name: {}", + new_name + )); + } } + AlterTableBody::AddColumn(cd) => { + for c in cd { + if let ColumnConstraint::PrimaryKey { .. } = c { + return Err(custom_err!("Cannot add a PRIMARY KEY column")); + } + if let ColumnConstraint::Unique(..) = c { + return Err(custom_err!("Cannot add a UNIQUE column")); + } + } + } + _ => {} } Ok(()) } diff --git a/vendored/sqlite3-parser/src/parser/ast/fmt.rs b/vendored/sqlite3-parser/src/parser/ast/fmt.rs index e3f83e2c3..e6959b94e 100644 --- a/vendored/sqlite3-parser/src/parser/ast/fmt.rs +++ b/vendored/sqlite3-parser/src/parser/ast/fmt.rs @@ -119,7 +119,8 @@ impl Display for Cmd { impl ToTokens for Stmt { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - Self::AlterTable(tbl_name, body) => { + Self::AlterTable(alter_table) => { + let (tbl_name, body) = &**alter_table; s.append(TK_ALTER, None)?; s.append(TK_TABLE, None)?; tbl_name.to_tokens(s)?; diff --git a/vendored/sqlite3-parser/src/parser/ast/mod.rs b/vendored/sqlite3-parser/src/parser/ast/mod.rs index 19e506e8a..6d093c4dd 100644 --- a/vendored/sqlite3-parser/src/parser/ast/mod.rs +++ b/vendored/sqlite3-parser/src/parser/ast/mod.rs @@ -71,7 +71,7 @@ pub(crate) enum ExplainKind { #[derive(Clone, Debug, PartialEq, Eq)] pub enum Stmt { /// `ALTER TABLE`: table name, body - AlterTable(QualifiedName, AlterTableBody), + AlterTable(Box<(QualifiedName, AlterTableBody)>), /// `ANALYSE`: object name Analyze(Option), /// `ATTACH DATABASE` diff --git a/vendored/sqlite3-parser/src/parser/parse.y b/vendored/sqlite3-parser/src/parser/parse.y index e939a724d..16c8b9f00 100644 --- a/vendored/sqlite3-parser/src/parser/parse.y +++ b/vendored/sqlite3-parser/src/parser/parse.y @@ -1305,19 +1305,19 @@ cmd ::= ANALYZE fullname(X). {self.ctx.stmt = Some(Stmt::Analyze(Some(X)));} //////////////////////// ALTER TABLE table ... //////////////////////////////// %ifndef SQLITE_OMIT_ALTERTABLE cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). { - self.ctx.stmt = Some(Stmt::AlterTable(X, AlterTableBody::RenameTo(Z))); + self.ctx.stmt = Some(Stmt::AlterTable(Box::new((X, AlterTableBody::RenameTo(Z))))); } cmd ::= ALTER TABLE fullname(X) ADD kwcolumn_opt columnname(Y) carglist(C). { let (col_name, col_type) = Y; let cd = ColumnDefinition{ col_name, col_type, constraints: C }; - self.ctx.stmt = Some(Stmt::AlterTable(X, AlterTableBody::AddColumn(cd))); + self.ctx.stmt = Some(Stmt::AlterTable(Box::new((X, AlterTableBody::AddColumn(cd))))); } cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). { - self.ctx.stmt = Some(Stmt::AlterTable(X, AlterTableBody::RenameColumn{ old: Y, new: Z })); + self.ctx.stmt = Some(Stmt::AlterTable(Box::new((X, AlterTableBody::RenameColumn{ old: Y, new: Z })))); } cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). { - self.ctx.stmt = Some(Stmt::AlterTable(X, AlterTableBody::DropColumn(Y))); + self.ctx.stmt = Some(Stmt::AlterTable(Box::new((X, AlterTableBody::DropColumn(Y))))); } kwcolumn_opt ::= .