Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4402,6 +4402,142 @@ impl Spanned for DropExtension {
}
}

/// CREATE COLLATION statement.
/// Note: this is a PostgreSQL-specific statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct CreateCollation {
/// Whether `IF NOT EXISTS` was specified.
pub if_not_exists: bool,
/// Name of the collation being created.
pub name: ObjectName,
/// Source definition for the collation.
pub definition: CreateCollationDefinition,
}

/// Definition forms supported by `CREATE COLLATION`.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum CreateCollationDefinition {
/// Create from an existing collation.
///
/// ```sql
/// CREATE COLLATION name FROM existing_collation
/// ```
From(ObjectName),
/// Create with an option list.
///
/// ```sql
/// CREATE COLLATION name (key = value, ...)
/// ```
Options(Vec<SqlOption>),
}

impl fmt::Display for CreateCollation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"CREATE COLLATION {if_not_exists}{name}",
if_not_exists = if self.if_not_exists {
"IF NOT EXISTS "
} else {
""
},
name = self.name
)?;
match &self.definition {
CreateCollationDefinition::From(existing_collation) => {
write!(f, " FROM {existing_collation}")
}
CreateCollationDefinition::Options(options) => {
write!(f, " ({})", display_comma_separated(options))
}
}
}
}

impl Spanned for CreateCollation {
fn span(&self) -> Span {
Span::empty()
}
}

/// ALTER COLLATION statement.
/// Note: this is a PostgreSQL-specific statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct AlterCollation {
/// Name of the collation being altered.
pub name: ObjectName,
/// The operation to perform on the collation.
pub operation: AlterCollationOperation,
}

/// Operations supported by `ALTER COLLATION`.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum AlterCollationOperation {
/// Rename the collation.
///
/// ```sql
/// ALTER COLLATION name RENAME TO new_name
/// ```
RenameTo {
/// New collation name.
new_name: Ident,
},
/// Change the collation owner.
///
/// ```sql
/// ALTER COLLATION name OWNER TO role_name
/// ```
OwnerTo(Owner),
/// Move the collation to another schema.
///
/// ```sql
/// ALTER COLLATION name SET SCHEMA new_schema
/// ```
SetSchema {
/// Target schema name.
schema_name: ObjectName,
},
/// Refresh collation version metadata.
///
/// ```sql
/// ALTER COLLATION name REFRESH VERSION
/// ```
RefreshVersion,
}

impl fmt::Display for AlterCollationOperation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AlterCollationOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
AlterCollationOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
AlterCollationOperation::SetSchema { schema_name } => {
write!(f, "SET SCHEMA {schema_name}")
}
AlterCollationOperation::RefreshVersion => write!(f, "REFRESH VERSION"),
}
}
}

impl fmt::Display for AlterCollation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ALTER COLLATION {} {}", self.name, self.operation)
}
}

impl Spanned for AlterCollation {
fn span(&self) -> Span {
Span::empty()
}
}

/// Table type for ALTER TABLE statements.
/// Used to distinguish between regular tables, Iceberg tables, and Dynamic tables.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down
56 changes: 44 additions & 12 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,19 @@ pub use self::dcl::{
SetConfigValue, Use,
};
pub use self::ddl::{
Alignment, AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterOperator,
AlterOperatorClass, AlterOperatorClassOperation, AlterOperatorFamily,
AlterOperatorFamilyOperation, AlterOperatorOperation, AlterPolicy, AlterPolicyOperation,
AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm, AlterTableLock,
AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition,
AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue, ClusteredBy, ColumnDef,
ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy, ColumnPolicyProperty,
ConstraintCharacteristics, CreateConnector, CreateDomain, CreateExtension, CreateFunction,
CreateIndex, CreateOperator, CreateOperatorClass, CreateOperatorFamily, CreatePolicy,
CreatePolicyCommand, CreatePolicyType, CreateTable, CreateTrigger, CreateView, Deduplicate,
DeferrableInitial, DropBehavior, DropExtension, DropFunction, DropOperator, DropOperatorClass,
DropOperatorFamily, DropOperatorSignature, DropPolicy, DropTrigger, ForValues, GeneratedAs,
Alignment, AlterCollation, AlterCollationOperation, AlterColumnOperation, AlterConnectorOwner,
AlterIndexOperation, AlterOperator, AlterOperatorClass, AlterOperatorClassOperation,
AlterOperatorFamily, AlterOperatorFamilyOperation, AlterOperatorOperation, AlterPolicy,
AlterPolicyOperation, AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm,
AlterTableLock, AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue,
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
ColumnPolicyProperty, ConstraintCharacteristics, CreateCollation, CreateCollationDefinition,
CreateConnector, CreateDomain, CreateExtension, CreateFunction, CreateIndex, CreateOperator,
CreateOperatorClass, CreateOperatorFamily, CreatePolicy, CreatePolicyCommand, CreatePolicyType,
CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial, DropBehavior,
DropExtension, DropFunction, DropOperator, DropOperatorClass, DropOperatorFamily,
DropOperatorSignature, DropPolicy, DropTrigger, ForValues, GeneratedAs,
GeneratedExpressionMode, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
IdentityPropertyKind, IdentityPropertyOrder, IndexColumn, IndexOption, IndexType,
KeyOrIndexDisplay, Msck, NullsDistinctOption, OperatorArgTypes, OperatorClassItem,
Expand Down Expand Up @@ -2437,6 +2438,8 @@ impl fmt::Display for ShowCreateObject {
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
/// Objects that can be targeted by a `COMMENT` statement.
pub enum CommentObject {
/// A collation.
Collation,
/// A table column.
Column,
/// A database.
Expand Down Expand Up @@ -2472,6 +2475,7 @@ pub enum CommentObject {
impl fmt::Display for CommentObject {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CommentObject::Collation => f.write_str("COLLATION"),
CommentObject::Column => f.write_str("COLUMN"),
CommentObject::Database => f.write_str("DATABASE"),
CommentObject::Domain => f.write_str("DOMAIN"),
Expand Down Expand Up @@ -3744,6 +3748,11 @@ pub enum Statement {
/// ```
AlterType(AlterType),
/// ```sql
/// ALTER COLLATION
/// ```
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-altercollation.html)
AlterCollation(AlterCollation),
/// ```sql
/// ALTER OPERATOR
/// ```
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteroperator.html)
Expand Down Expand Up @@ -3940,6 +3949,12 @@ pub enum Statement {
/// Note: this is a PostgreSQL-specific statement,
CreateExtension(CreateExtension),
/// ```sql
/// CREATE COLLATION
/// ```
/// Note: this is a PostgreSQL-specific statement.
/// <https://www.postgresql.org/docs/current/sql-createcollation.html>
CreateCollation(CreateCollation),
/// ```sql
/// DROP EXTENSION [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
/// ```
/// Note: this is a PostgreSQL-specific statement.
Expand Down Expand Up @@ -5389,6 +5404,7 @@ impl fmt::Display for Statement {
}
Statement::CreateIndex(create_index) => create_index.fmt(f),
Statement::CreateExtension(create_extension) => write!(f, "{create_extension}"),
Statement::CreateCollation(create_collation) => write!(f, "{create_collation}"),
Statement::DropExtension(drop_extension) => write!(f, "{drop_extension}"),
Statement::DropOperator(drop_operator) => write!(f, "{drop_operator}"),
Statement::DropOperatorFamily(drop_operator_family) => {
Expand Down Expand Up @@ -5465,6 +5481,7 @@ impl fmt::Display for Statement {
Statement::AlterType(AlterType { name, operation }) => {
write!(f, "ALTER TYPE {name} {operation}")
}
Statement::AlterCollation(alter_collation) => write!(f, "{alter_collation}"),
Statement::AlterOperator(alter_operator) => write!(f, "{alter_operator}"),
Statement::AlterOperatorFamily(alter_operator_family) => {
write!(f, "{alter_operator_family}")
Expand Down Expand Up @@ -8230,6 +8247,8 @@ impl fmt::Display for HavingBoundKind {
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
/// Types of database objects referenced by DDL statements.
pub enum ObjectType {
/// A collation.
Collation,
/// A table.
Table,
/// A view.
Expand Down Expand Up @@ -8259,6 +8278,7 @@ pub enum ObjectType {
impl fmt::Display for ObjectType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match self {
ObjectType::Collation => "COLLATION",
ObjectType::Table => "TABLE",
ObjectType::View => "VIEW",
ObjectType::MaterializedView => "MATERIALIZED VIEW",
Expand Down Expand Up @@ -11816,6 +11836,12 @@ impl From<CreateExtension> for Statement {
}
}

impl From<CreateCollation> for Statement {
fn from(c: CreateCollation) -> Self {
Self::CreateCollation(c)
}
}

impl From<DropExtension> for Statement {
fn from(de: DropExtension) -> Self {
Self::DropExtension(de)
Expand Down Expand Up @@ -11924,6 +11950,12 @@ impl From<AlterType> for Statement {
}
}

impl From<AlterCollation> for Statement {
fn from(a: AlterCollation) -> Self {
Self::AlterCollation(a)
}
}

impl From<AlterOperator> for Statement {
fn from(a: AlterOperator) -> Self {
Self::AlterOperator(a)
Expand Down
4 changes: 4 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ impl Spanned for Values {
/// - [Statement::DropSecret]
/// - [Statement::Declare]
/// - [Statement::CreateExtension]
/// - [Statement::CreateCollation]
/// - [Statement::AlterCollation]
/// - [Statement::Fetch]
/// - [Statement::Flush]
/// - [Statement::Discard]
Expand Down Expand Up @@ -376,6 +378,7 @@ impl Spanned for Statement {
Statement::CreateIndex(create_index) => create_index.span(),
Statement::CreateRole(create_role) => create_role.span(),
Statement::CreateExtension(create_extension) => create_extension.span(),
Statement::CreateCollation(create_collation) => create_collation.span(),
Statement::DropExtension(drop_extension) => drop_extension.span(),
Statement::DropOperator(drop_operator) => drop_operator.span(),
Statement::DropOperatorFamily(drop_operator_family) => drop_operator_family.span(),
Expand Down Expand Up @@ -403,6 +406,7 @@ impl Spanned for Statement {
),
// These statements need to be implemented
Statement::AlterType { .. } => Span::empty(),
Statement::AlterCollation { .. } => Span::empty(),
Statement::AlterOperator { .. } => Span::empty(),
Statement::AlterOperatorFamily { .. } => Span::empty(),
Statement::AlterOperatorClass { .. } => Span::empty(),
Expand Down
Loading
Loading