sqlite3-parser: box AlterTable

This commit is contained in:
Jussi Saurio
2025-02-09 14:10:18 +02:00
parent 72a055e5fe
commit 36a3cb1d5e
5 changed files with 29 additions and 22 deletions

View File

@@ -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(())
}

View File

@@ -119,7 +119,8 @@ impl Display for Cmd {
impl ToTokens for Stmt {
fn to_tokens<S: TokenStream>(&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)?;

View File

@@ -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<QualifiedName>),
/// `ATTACH DATABASE`