Implement ASSERT statement (#226)

As supported by PostgreSQL and BigQuery (with some differences between them)
This commit is contained in:
Daniël Heres 2020-07-16 17:28:03 +02:00 committed by GitHub
parent 5cab18963e
commit c24b0e01db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 0 deletions

View file

@ -148,6 +148,7 @@ impl Parser {
Keyword::BEGIN => Ok(self.parse_begin()?),
Keyword::COMMIT => Ok(self.parse_commit()?),
Keyword::ROLLBACK => Ok(self.parse_rollback()?),
Keyword::ASSERT => Ok(self.parse_assert()?),
_ => self.expected("an SQL statement", Token::Word(w)),
},
Token::LParen => {
@ -179,6 +180,22 @@ impl Parser {
}
Ok(expr)
}
pub fn parse_assert(&mut self) -> Result<Statement, ParserError> {
let condition = self.parse_expr()?;
let (separator, message) = if self.consume_token(&Token::Comma) {
(",".to_string(), Some(self.parse_expr()?))
} else if self.parse_keyword(Keyword::AS) {
("AS".to_string(), Some(self.parse_expr()?))
} else {
("".to_string(), None)
};
Ok(Statement::Assert {
condition,
separator,
message,
})
}
/// Parse an expression prefix
pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {