Support UPDATE statements

Fix #10.
This commit is contained in:
Nikhil Benesch 2019-05-30 13:55:12 -04:00
parent 2308c1c6f7
commit 1cef68e510
No known key found for this signature in database
GPG key ID: F7386C5DEADABA7F
3 changed files with 80 additions and 3 deletions

View file

@ -471,6 +471,7 @@ impl ToString for SQLStatement {
} => {
let mut s = format!("UPDATE {}", table_name.to_string());
if !assignments.is_empty() {
s += " SET ";
s += &comma_separated_string(assignments);
}
if let Some(selection) = selection {
@ -560,13 +561,13 @@ impl ToString for SQLObjectName {
/// SQL assignment `foo = expr` as used in SQLUpdate
#[derive(Debug, Clone, PartialEq)]
pub struct SQLAssignment {
id: SQLIdent,
value: ASTNode,
pub id: SQLIdent,
pub value: ASTNode,
}
impl ToString for SQLAssignment {
fn to_string(&self) -> String {
format!("SET {} = {}", self.id, self.value.to_string())
format!("{} = {}", self.id, self.value.to_string())
}
}

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!(
@ -1606,6 +1607,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![];