mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-23 17:05:36 +01:00
sqlite3-parser: box AlterTable
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
|
||||
@@ -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)?;
|
||||
|
||||
@@ -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`
|
||||
|
||||
@@ -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 ::= .
|
||||
|
||||
Reference in New Issue
Block a user