Move TableFactor to be a separate enum

ASTNode can now be renamed SQLExpression, as it represents a node in
the "expression" part of the AST -- other nodes have their own types.
This commit is contained in:
Nickolay Ponomarev 2019-02-06 06:02:54 +03:00
parent e0ceacd1ad
commit 9967031cba
4 changed files with 82 additions and 67 deletions

View file

@ -1119,8 +1119,8 @@ impl Parser {
pub fn parse_select(&mut self) -> Result<SQLSelect, ParserError> {
let projection = self.parse_expr_list()?;
let (relation, joins): (Option<Box<ASTNode>>, Vec<Join>) = if self.parse_keyword("FROM") {
let relation = Some(Box::new(self.parse_table_factor()?));
let (relation, joins) = if self.parse_keyword("FROM") {
let relation = Some(self.parse_table_factor()?);
let joins = self.parse_joins()?;
(relation, joins)
} else {
@ -1171,20 +1171,21 @@ impl Parser {
}
/// A table name or a parenthesized subquery, followed by optional `[AS] alias`
pub fn parse_table_factor(&mut self) -> Result<ASTNode, ParserError> {
let relation = if self.consume_token(&Token::LParen) {
pub fn parse_table_factor(&mut self) -> Result<TableFactor, ParserError> {
if self.consume_token(&Token::LParen) {
self.expect_keyword("SELECT")?;
let subquery = self.parse_select()?;
self.expect_token(&Token::RParen)?;
ASTNode::SQLSubquery(subquery)
Ok(TableFactor::Derived {
subquery: Box::new(subquery),
alias: self.parse_optional_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?,
})
} else {
ASTNode::SQLCompoundIdentifier(self.parse_object_name()?.0)
};
let alias = self.parse_optional_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
Ok(ASTNode::TableFactor {
relation: Box::new(relation),
alias,
})
Ok(TableFactor::Table {
name: self.parse_object_name()?,
alias: self.parse_optional_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?,
})
}
}
fn parse_join_constraint(&mut self, natural: bool) -> Result<JoinConstraint, ParserError> {