Merge pull request #97 from benesch/update

Support UPDATE statements
This commit is contained in:
Nikhil Benesch 2019-06-03 23:32:23 -04:00 committed by GitHub
commit a594375966
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 3 deletions

View file

@ -117,6 +117,7 @@ impl Parser {
"DROP" => Ok(self.parse_drop()?),
"DELETE" => Ok(self.parse_delete()?),
"INSERT" => Ok(self.parse_insert()?),
"UPDATE" => Ok(self.parse_update()?),
"ALTER" => Ok(self.parse_alter()?),
"COPY" => Ok(self.parse_copy()?),
_ => parser_err!(format!(
@ -1615,6 +1616,31 @@ impl Parser {
})
}
pub fn parse_update(&mut self) -> Result<SQLStatement, ParserError> {
let table_name = self.parse_object_name()?;
self.expect_keyword("SET")?;
let mut assignments = vec![];
loop {
let id = self.parse_identifier()?;
self.expect_token(&Token::Eq)?;
let value = self.parse_expr()?;
assignments.push(SQLAssignment { id, value });
if !self.consume_token(&Token::Comma) {
break;
}
}
let selection = if self.parse_keyword("WHERE") {
Some(self.parse_expr()?)
} else {
None
};
Ok(SQLStatement::SQLUpdate {
table_name,
assignments,
selection,
})
}
/// Parse a comma-delimited list of SQL expressions
pub fn parse_expr_list(&mut self) -> Result<Vec<ASTNode>, ParserError> {
let mut expr_list: Vec<ASTNode> = vec![];