Support ALTER VIEW, MySQL syntax (#907)

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
liadgiladi 2023-06-29 20:33:51 +03:00 committed by GitHub
parent 9effeba0d8
commit f05f71e20d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 1 deletions

View file

@ -1375,6 +1375,15 @@ pub enum Statement {
name: ObjectName,
operation: AlterIndexOperation,
},
/// ALTER VIEW
AlterView {
/// View name
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
name: ObjectName,
columns: Vec<Ident>,
query: Box<Query>,
with_options: Vec<SqlOption>,
},
/// DROP
Drop {
/// The type of the object to drop: TABLE, VIEW, etc.
@ -2534,6 +2543,21 @@ impl fmt::Display for Statement {
Statement::AlterIndex { name, operation } => {
write!(f, "ALTER INDEX {name} {operation}")
}
Statement::AlterView {
name,
columns,
query,
with_options,
} => {
write!(f, "ALTER VIEW {name}")?;
if !with_options.is_empty() {
write!(f, " WITH ({})", display_comma_separated(with_options))?;
}
if !columns.is_empty() {
write!(f, " ({})", display_comma_separated(columns))?;
}
write!(f, " AS {query}")
}
Statement::Drop {
object_type,
if_exists,

View file

@ -3898,8 +3898,10 @@ impl<'a> Parser<'a> {
}
pub fn parse_alter(&mut self) -> Result<Statement, ParserError> {
let object_type = self.expect_one_of_keywords(&[Keyword::TABLE, Keyword::INDEX])?;
let object_type =
self.expect_one_of_keywords(&[Keyword::VIEW, Keyword::TABLE, Keyword::INDEX])?;
match object_type {
Keyword::VIEW => self.parse_alter_view(),
Keyword::TABLE => {
let _ = self.parse_keyword(Keyword::ONLY); // [ ONLY ]
let table_name = self.parse_object_name()?;
@ -4097,6 +4099,23 @@ impl<'a> Parser<'a> {
}
}
pub fn parse_alter_view(&mut self) -> Result<Statement, ParserError> {
let name = self.parse_object_name()?;
let columns = self.parse_parenthesized_column_list(Optional, false)?;
let with_options = self.parse_options(Keyword::WITH)?;
self.expect_keyword(Keyword::AS)?;
let query = Box::new(self.parse_query()?);
Ok(Statement::AlterView {
name,
columns,
query,
with_options,
})
}
/// Parse a copy statement
pub fn parse_copy(&mut self) -> Result<Statement, ParserError> {
let source;