Skip to content

Commit 2eba8c2

Browse files
authored
Merge pull request #12 from fmguerreiro/feat/alter-set
feat: parse ALTER TABLE/INDEX SET TABLESPACE, ALTER DOMAIN, ALTER TRIGGER, ALTER EXTENSION, ALTER PROCEDURE
2 parents 145ef5f + 30cd573 commit 2eba8c2

5 files changed

Lines changed: 577 additions & 8 deletions

File tree

src/ast/ddl.rs

Lines changed: 269 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,14 @@ pub enum AlterTableOperation {
535535
/// Parenthesized options supplied to `SET (...)`.
536536
options: Vec<SqlOption>,
537537
},
538+
/// `SET TABLESPACE tablespace_name`
539+
///
540+
/// Note: this is a PostgreSQL-specific operation.
541+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
542+
SetTablespace {
543+
/// The target tablespace name.
544+
tablespace_name: Ident,
545+
},
538546
}
539547

540548
/// An `ALTER Policy` (`Statement::AlterPolicy`) operation
@@ -700,6 +708,14 @@ pub enum AlterIndexOperation {
700708
/// The new name for the index.
701709
index_name: ObjectName,
702710
},
711+
/// `SET TABLESPACE tablespace_name`
712+
///
713+
/// Note: this is a PostgreSQL-specific operation.
714+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterindex.html)
715+
SetTablespace {
716+
/// The target tablespace name.
717+
tablespace_name: Ident,
718+
},
703719
}
704720

705721
impl fmt::Display for AlterTableOperation {
@@ -1045,6 +1061,9 @@ impl fmt::Display for AlterTableOperation {
10451061
AlterTableOperation::SetOptionsParens { options } => {
10461062
write!(f, "SET ({})", display_comma_separated(options))
10471063
}
1064+
AlterTableOperation::SetTablespace { tablespace_name } => {
1065+
write!(f, "SET TABLESPACE {tablespace_name}")
1066+
}
10481067
}
10491068
}
10501069
}
@@ -1055,6 +1074,9 @@ impl fmt::Display for AlterIndexOperation {
10551074
AlterIndexOperation::RenameIndex { index_name } => {
10561075
write!(f, "RENAME TO {index_name}")
10571076
}
1077+
AlterIndexOperation::SetTablespace { tablespace_name } => {
1078+
write!(f, "SET TABLESPACE {tablespace_name}")
1079+
}
10581080
}
10591081
}
10601082
}
@@ -5390,13 +5412,16 @@ pub enum AlterFunctionKind {
53905412
Function,
53915413
/// `AGGREGATE`
53925414
Aggregate,
5415+
/// `PROCEDURE`
5416+
Procedure,
53935417
}
53945418

53955419
impl fmt::Display for AlterFunctionKind {
53965420
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53975421
match self {
53985422
Self::Function => write!(f, "FUNCTION"),
53995423
Self::Aggregate => write!(f, "AGGREGATE"),
5424+
Self::Procedure => write!(f, "PROCEDURE"),
54005425
}
54015426
}
54025427
}
@@ -5471,7 +5496,7 @@ impl fmt::Display for AlterFunction {
54715496
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54725497
write!(f, "ALTER {} ", self.kind)?;
54735498
match self.kind {
5474-
AlterFunctionKind::Function => {
5499+
AlterFunctionKind::Function | AlterFunctionKind::Procedure => {
54755500
write!(f, "{} ", self.function)?;
54765501
}
54775502
AlterFunctionKind::Aggregate => {
@@ -6137,6 +6162,124 @@ impl From<CreateTextSearchTemplate> for crate::ast::Statement {
61376162
}
61386163
}
61396164

6165+
/// `ALTER DOMAIN` statement.
6166+
///
6167+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterdomain.html)
6168+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6169+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6170+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6171+
pub struct AlterDomain {
6172+
/// Name of the domain being altered.
6173+
pub name: ObjectName,
6174+
/// The operation to perform.
6175+
pub operation: AlterDomainOperation,
6176+
}
6177+
6178+
/// An [AlterDomain] operation.
6179+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6180+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6181+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6182+
pub enum AlterDomainOperation {
6183+
/// `ADD CONSTRAINT constraint_name CHECK (expr) [NOT VALID]`
6184+
AddConstraint {
6185+
/// The constraint to add.
6186+
constraint: TableConstraint,
6187+
/// Whether `NOT VALID` was specified.
6188+
not_valid: bool,
6189+
},
6190+
/// `DROP CONSTRAINT [IF EXISTS] constraint_name [CASCADE | RESTRICT]`
6191+
DropConstraint {
6192+
/// Whether `IF EXISTS` was specified.
6193+
if_exists: bool,
6194+
/// Name of the constraint to drop.
6195+
name: Ident,
6196+
/// Optional drop behavior.
6197+
drop_behavior: Option<DropBehavior>,
6198+
},
6199+
/// `RENAME CONSTRAINT old_name TO new_name`
6200+
RenameConstraint {
6201+
/// Existing constraint name.
6202+
old_name: Ident,
6203+
/// New constraint name.
6204+
new_name: Ident,
6205+
},
6206+
/// `OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }`
6207+
OwnerTo(Owner),
6208+
/// `RENAME TO new_name`
6209+
RenameTo {
6210+
/// New name for the domain.
6211+
new_name: Ident,
6212+
},
6213+
/// `SET SCHEMA schema_name`
6214+
SetSchema {
6215+
/// The target schema name.
6216+
schema_name: ObjectName,
6217+
},
6218+
/// `SET DEFAULT expr`
6219+
SetDefault {
6220+
/// Default value expression.
6221+
default: Expr,
6222+
},
6223+
/// `DROP DEFAULT`
6224+
DropDefault,
6225+
/// `VALIDATE CONSTRAINT constraint_name`
6226+
ValidateConstraint {
6227+
/// Name of the constraint to validate.
6228+
name: Ident,
6229+
},
6230+
}
6231+
6232+
impl fmt::Display for AlterDomain {
6233+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6234+
write!(f, "ALTER DOMAIN {} {}", self.name, self.operation)
6235+
}
6236+
}
6237+
6238+
impl fmt::Display for AlterDomainOperation {
6239+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6240+
match self {
6241+
AlterDomainOperation::AddConstraint {
6242+
constraint,
6243+
not_valid,
6244+
} => {
6245+
write!(f, "ADD {constraint}")?;
6246+
if *not_valid {
6247+
write!(f, " NOT VALID")?;
6248+
}
6249+
Ok(())
6250+
}
6251+
AlterDomainOperation::DropConstraint {
6252+
if_exists,
6253+
name,
6254+
drop_behavior,
6255+
} => {
6256+
write!(f, "DROP CONSTRAINT")?;
6257+
if *if_exists {
6258+
write!(f, " IF EXISTS")?;
6259+
}
6260+
write!(f, " {name}")?;
6261+
if let Some(behavior) = drop_behavior {
6262+
write!(f, " {behavior}")?;
6263+
}
6264+
Ok(())
6265+
}
6266+
AlterDomainOperation::RenameConstraint { old_name, new_name } => {
6267+
write!(f, "RENAME CONSTRAINT {old_name} TO {new_name}")
6268+
}
6269+
AlterDomainOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
6270+
AlterDomainOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
6271+
AlterDomainOperation::SetSchema { schema_name } => {
6272+
write!(f, "SET SCHEMA {schema_name}")
6273+
}
6274+
AlterDomainOperation::SetDefault { default } => write!(f, "SET DEFAULT {default}"),
6275+
AlterDomainOperation::DropDefault => write!(f, "DROP DEFAULT"),
6276+
AlterDomainOperation::ValidateConstraint { name } => {
6277+
write!(f, "VALIDATE CONSTRAINT {name}")
6278+
}
6279+
}
6280+
}
6281+
}
6282+
61406283
/// The target of a `CREATE PUBLICATION` statement: which rows to publish.
61416284
///
61426285
/// See <https://www.postgresql.org/docs/current/sql-createpublication.html>
@@ -6170,6 +6313,131 @@ impl fmt::Display for PublicationTarget {
61706313
}
61716314
}
61726315

6316+
impl From<AlterDomain> for crate::ast::Statement {
6317+
fn from(a: AlterDomain) -> Self {
6318+
crate::ast::Statement::AlterDomain(a)
6319+
}
6320+
}
6321+
6322+
/// `ALTER TRIGGER name ON table_name RENAME TO new_name` statement.
6323+
///
6324+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertrigger.html)
6325+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6326+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6327+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6328+
pub struct AlterTrigger {
6329+
/// Name of the trigger being altered.
6330+
pub name: Ident,
6331+
/// Name of the table the trigger is defined on.
6332+
pub table_name: ObjectName,
6333+
/// The operation to perform.
6334+
pub operation: AlterTriggerOperation,
6335+
}
6336+
6337+
/// An [AlterTrigger] operation.
6338+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6339+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6340+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6341+
pub enum AlterTriggerOperation {
6342+
/// `RENAME TO new_name`
6343+
RenameTo {
6344+
/// New name for the trigger.
6345+
new_name: Ident,
6346+
},
6347+
}
6348+
6349+
impl fmt::Display for AlterTrigger {
6350+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6351+
write!(
6352+
f,
6353+
"ALTER TRIGGER {} ON {} {}",
6354+
self.name, self.table_name, self.operation
6355+
)
6356+
}
6357+
}
6358+
6359+
impl fmt::Display for AlterTriggerOperation {
6360+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6361+
match self {
6362+
AlterTriggerOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
6363+
}
6364+
}
6365+
}
6366+
6367+
impl From<AlterTrigger> for crate::ast::Statement {
6368+
fn from(a: AlterTrigger) -> Self {
6369+
crate::ast::Statement::AlterTrigger(a)
6370+
}
6371+
}
6372+
6373+
/// `ALTER EXTENSION` statement.
6374+
///
6375+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterextension.html)
6376+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6377+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6378+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6379+
pub struct AlterExtension {
6380+
/// Name of the extension being altered.
6381+
pub name: Ident,
6382+
/// The operation to perform.
6383+
pub operation: AlterExtensionOperation,
6384+
}
6385+
6386+
/// An [AlterExtension] operation.
6387+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6388+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6389+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6390+
pub enum AlterExtensionOperation {
6391+
/// `UPDATE [ TO new_version ]`
6392+
UpdateTo {
6393+
/// Optional target version string or identifier.
6394+
version: Option<Ident>,
6395+
},
6396+
/// `SET SCHEMA schema_name`
6397+
SetSchema {
6398+
/// The target schema name.
6399+
schema_name: ObjectName,
6400+
},
6401+
/// `OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }`
6402+
OwnerTo(Owner),
6403+
/// `RENAME TO new_name`
6404+
RenameTo {
6405+
/// New name for the extension.
6406+
new_name: Ident,
6407+
},
6408+
}
6409+
6410+
impl fmt::Display for AlterExtension {
6411+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6412+
write!(f, "ALTER EXTENSION {} {}", self.name, self.operation)
6413+
}
6414+
}
6415+
6416+
impl fmt::Display for AlterExtensionOperation {
6417+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6418+
match self {
6419+
AlterExtensionOperation::UpdateTo { version } => {
6420+
write!(f, "UPDATE")?;
6421+
if let Some(v) = version {
6422+
write!(f, " TO {v}")?;
6423+
}
6424+
Ok(())
6425+
}
6426+
AlterExtensionOperation::SetSchema { schema_name } => {
6427+
write!(f, "SET SCHEMA {schema_name}")
6428+
}
6429+
AlterExtensionOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
6430+
AlterExtensionOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
6431+
}
6432+
}
6433+
}
6434+
6435+
impl From<AlterExtension> for crate::ast::Statement {
6436+
fn from(a: AlterExtension) -> Self {
6437+
crate::ast::Statement::AlterExtension(a)
6438+
}
6439+
}
6440+
61736441
/// A `CREATE PUBLICATION` statement.
61746442
///
61756443
/// Note: this is a PostgreSQL-specific statement.

src/ast/mod.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ pub use self::dcl::{
6161
};
6262
pub use self::ddl::{
6363
Alignment, AlterCollation, AlterCollationOperation, AlterColumnOperation, AlterConnectorOwner,
64+
AlterDomain, AlterDomainOperation, AlterExtension, AlterExtensionOperation,
6465
AlterFunction, AlterFunctionAction, AlterFunctionKind, AlterFunctionOperation,
6566
AlterIndexOperation, AlterOperator, AlterOperatorClass, AlterOperatorClassOperation,
6667
AlterOperatorFamily, AlterOperatorFamilyOperation, AlterOperatorOperation, AlterPolicy,
6768
AlterPolicyOperation, AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm,
68-
AlterTableLock, AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue,
69+
AlterTableLock, AlterTableOperation, AlterTableType, AlterTrigger, AlterTriggerOperation,
70+
AlterType, AlterTypeAddValue,
6971
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
7072
AggregateModifyKind, ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions,
7173
ColumnPolicy, ColumnPolicyProperty, ConstraintCharacteristics, CreateAggregate,
@@ -3775,18 +3777,35 @@ pub enum Statement {
37753777
with_options: Vec<SqlOption>,
37763778
},
37773779
/// ```sql
3780+
/// ALTER DOMAIN
3781+
/// ```
3782+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterdomain.html)
3783+
AlterDomain(AlterDomain),
3784+
/// ```sql
3785+
/// ALTER EXTENSION
3786+
/// ```
3787+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterextension.html)
3788+
AlterExtension(AlterExtension),
3789+
/// ```sql
37783790
/// ALTER FUNCTION
37793791
/// ALTER AGGREGATE
3792+
/// ALTER PROCEDURE
37803793
/// ```
37813794
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterfunction.html)
37823795
/// and [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteraggregate.html)
3796+
/// and [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterprocedure.html)
37833797
AlterFunction(AlterFunction),
37843798
/// ```sql
37853799
/// ALTER TYPE
37863800
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertype.html)
37873801
/// ```
37883802
AlterType(AlterType),
37893803
/// ```sql
3804+
/// ALTER TRIGGER
3805+
/// ```
3806+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertrigger.html)
3807+
AlterTrigger(AlterTrigger),
3808+
/// ```sql
37903809
/// ALTER COLLATION
37913810
/// ```
37923811
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-altercollation.html)
@@ -5594,7 +5613,10 @@ impl fmt::Display for Statement {
55945613
}
55955614
write!(f, " AS {query}")
55965615
}
5616+
Statement::AlterDomain(alter_domain) => write!(f, "{alter_domain}"),
5617+
Statement::AlterExtension(alter_extension) => write!(f, "{alter_extension}"),
55975618
Statement::AlterFunction(alter_function) => write!(f, "{alter_function}"),
5619+
Statement::AlterTrigger(alter_trigger) => write!(f, "{alter_trigger}"),
55985620
Statement::AlterType(AlterType { name, operation }) => {
55995621
write!(f, "ALTER TYPE {name} {operation}")
56005622
}

0 commit comments

Comments
 (0)