Skip to content
Merged
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
9 changes: 9 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3084,6 +3084,7 @@ impl fmt::Display for CreateConnector {
/// An `ALTER SCHEMA` (`Statement::AlterSchema`) operation.
///
/// See [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_schema_collate_statement)
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterschema.html)
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
Expand All @@ -3101,6 +3102,12 @@ pub enum AlterSchemaOperation {
SetOptionsParens {
options: Vec<SqlOption>,
},
Rename {
name: ObjectName,
},
OwnerTo {
owner: Owner,
},
}

impl fmt::Display for AlterSchemaOperation {
Expand All @@ -3120,6 +3127,8 @@ impl fmt::Display for AlterSchemaOperation {
AlterSchemaOperation::SetOptionsParens { options } => {
write!(f, "SET OPTIONS ({})", display_comma_separated(options))
}
AlterSchemaOperation::Rename { name } => write!(f, "RENAME TO {name}"),
AlterSchemaOperation::OwnerTo { owner } => write!(f, "OWNER TO {owner}"),
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use crate::ast::{
ddl::AlterSchema, query::SelectItemQualifiedWildcardKind, AlterSchemaOperation, ColumnOptions,
ExportData, TypedString,
ExportData, Owner, TypedString,
};
use core::iter;

Expand Down Expand Up @@ -2424,6 +2424,14 @@ impl Spanned for AlterSchemaOperation {
AlterSchemaOperation::SetOptionsParens { options } => {
union_spans(options.iter().map(|i| i.span()))
}
AlterSchemaOperation::Rename { name } => name.span(),
AlterSchemaOperation::OwnerTo { owner } => {
if let Owner::Ident(ident) = owner {
ident.span
} else {
Span::empty()
}
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9444,6 +9444,12 @@ impl<'a> Parser<'a> {
} else if self.parse_keywords(&[Keyword::DROP, Keyword::REPLICA]) {
let replica = self.parse_identifier()?;
AlterSchemaOperation::DropReplica { replica }
} else if self.parse_keywords(&[Keyword::RENAME, Keyword::TO]) {
let new_name = self.parse_object_name(false)?;
AlterSchemaOperation::Rename { name: new_name }
} else if self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) {
let owner = self.parse_owner()?;
AlterSchemaOperation::OwnerTo { owner }
} else {
return self.expected_ref("ALTER SCHEMA operation", self.peek_token_ref());
};
Expand Down
63 changes: 63 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6576,3 +6576,66 @@ fn parse_create_server() {
assert_eq!(stmt, expected);
}
}

#[test]
fn parse_alter_schema() {
match pg_and_generic().verified_stmt("ALTER SCHEMA foo RENAME TO bar") {
Statement::AlterSchema(AlterSchema { operations, .. }) => {
assert_eq!(
operations,
vec![AlterSchemaOperation::Rename {
name: ObjectName::from(vec!["bar".into()])
}]
);
}
_ => unreachable!(),
}

match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO bar") {
Statement::AlterSchema(AlterSchema { operations, .. }) => {
assert_eq!(
operations,
vec![AlterSchemaOperation::OwnerTo {
owner: Owner::Ident("bar".into())
}]
);
}
_ => unreachable!(),
}

match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO CURRENT_ROLE") {
Statement::AlterSchema(AlterSchema { operations, .. }) => {
assert_eq!(
operations,
vec![AlterSchemaOperation::OwnerTo {
owner: Owner::CurrentRole
}]
);
}
_ => unreachable!(),
}

match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO CURRENT_USER") {
Statement::AlterSchema(AlterSchema { operations, .. }) => {
assert_eq!(
operations,
vec![AlterSchemaOperation::OwnerTo {
owner: Owner::CurrentUser
}]
);
}
_ => unreachable!(),
}

match pg_and_generic().verified_stmt("ALTER SCHEMA foo OWNER TO SESSION_USER") {
Statement::AlterSchema(AlterSchema { operations, .. }) => {
assert_eq!(
operations,
vec![AlterSchemaOperation::OwnerTo {
owner: Owner::SessionUser
}]
);
}
_ => unreachable!(),
}
}
Loading