|
28 | 28 | // limitations under the License.
|
29 | 29 | use log::debug;
|
30 | 30 |
|
31 |
| -use crate::ast::{CommentObject, Statement}; |
| 31 | +use crate::ast::{CommentObject, ObjectName, Statement, UserDefinedTypeRepresentation}; |
32 | 32 | use crate::dialect::{Dialect, Precedence};
|
33 | 33 | use crate::keywords::Keyword;
|
34 | 34 | use crate::parser::{Parser, ParserError};
|
@@ -138,6 +138,9 @@ impl Dialect for PostgreSqlDialect {
|
138 | 138 | fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
|
139 | 139 | if parser.parse_keyword(Keyword::COMMENT) {
|
140 | 140 | Some(parse_comment(parser))
|
| 141 | + } else if parser.parse_keyword(Keyword::CREATE) { |
| 142 | + parser.prev_token(); // unconsume the CREATE in case we don't end up parsing anything |
| 143 | + parse_create(parser) |
141 | 144 | } else {
|
142 | 145 | None
|
143 | 146 | }
|
@@ -225,3 +228,33 @@ pub fn parse_comment(parser: &mut Parser) -> Result<Statement, ParserError> {
|
225 | 228 | if_exists,
|
226 | 229 | })
|
227 | 230 | }
|
| 231 | + |
| 232 | +pub fn parse_create(parser: &mut Parser) -> Option<Result<Statement, ParserError>> { |
| 233 | + let name = parser.maybe_parse(|parser| -> Result<ObjectName, ParserError> { |
| 234 | + parser.expect_keyword(Keyword::CREATE)?; |
| 235 | + parser.expect_keyword(Keyword::TYPE)?; |
| 236 | + let name = parser.parse_object_name(false)?; |
| 237 | + parser.expect_keyword(Keyword::AS)?; |
| 238 | + parser.expect_keyword(Keyword::ENUM)?; |
| 239 | + Ok(name) |
| 240 | + }); |
| 241 | + name.map(|name| parse_create_type_as_enum(parser, name)) |
| 242 | +} |
| 243 | + |
| 244 | +// https://www.postgresql.org/docs/current/sql-createtype.html |
| 245 | +pub fn parse_create_type_as_enum( |
| 246 | + parser: &mut Parser, |
| 247 | + name: ObjectName, |
| 248 | +) -> Result<Statement, ParserError> { |
| 249 | + if !parser.consume_token(&Token::LParen) { |
| 250 | + return parser.expected("'(' after CREATE TYPE AS ENUM", parser.peek_token()); |
| 251 | + } |
| 252 | + |
| 253 | + let labels = parser.parse_comma_separated0(|p| p.parse_identifier(false), Token::RParen)?; |
| 254 | + parser.expect_token(&Token::RParen)?; |
| 255 | + |
| 256 | + Ok(Statement::CreateType { |
| 257 | + name, |
| 258 | + representation: UserDefinedTypeRepresentation::Enum { labels }, |
| 259 | + }) |
| 260 | +} |
0 commit comments