Support VALUES clauses in FROM

VALUES clauses are not just valid in INSERT statements. They're also
valid (basically) anywhere table expressions are accepted, like in FROM
clauses.
This commit is contained in:
Nikhil Benesch 2019-05-28 19:28:43 -04:00
parent a3aaa49a7e
commit 9420070a0d
No known key found for this signature in database
GPG key ID: F7386C5DEADABA7F
4 changed files with 83 additions and 39 deletions

View file

@ -1352,6 +1352,8 @@ impl Parser {
let subquery = self.parse_query()?;
self.expect_token(&Token::RParen)?;
SQLSetExpr::Query(Box::new(subquery))
} else if self.parse_keyword("VALUES") {
SQLSetExpr::Values(self.parse_values()?)
} else {
return self.expected("SELECT or a subquery in the query body", self.peek_token());
};
@ -1564,13 +1566,11 @@ impl Parser {
let table_name = self.parse_object_name()?;
let columns = self.parse_parenthesized_column_list(Optional)?;
self.expect_keyword("VALUES")?;
self.expect_token(&Token::LParen)?;
let values = self.parse_expr_list()?;
self.expect_token(&Token::RParen)?;
let values = self.parse_values()?;
Ok(SQLStatement::SQLInsert {
table_name,
columns,
values: vec![values],
values,
})
}
@ -1688,6 +1688,20 @@ impl Parser {
quantity,
})
}
pub fn parse_values(&mut self) -> Result<SQLValues, ParserError> {
let mut values = vec![];
loop {
self.expect_token(&Token::LParen)?;
values.push(self.parse_expr_list()?);
self.expect_token(&Token::RParen)?;
match self.peek_token() {
Some(Token::Comma) => self.next_token(),
_ => break,
};
}
Ok(SQLValues(values))
}
}
impl SQLWord {