Skip to content

Commit

Permalink
fix: Supports updating valid column names whose names are the same as… (
Browse files Browse the repository at this point in the history
#725)

* fix: Supports updating valid column names whose names are the same as keywords

* fix: warning
  • Loading branch information
step-baby committed Nov 27, 2022
1 parent 57083a0 commit 10652b6
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions src/parser.rs
Expand Up @@ -3916,41 +3916,19 @@ impl<'a> Parser<'a> {
Ok(ObjectName(idents))
}

/// Parse identifiers strictly i.e. don't parse keywords
pub fn parse_identifiers_non_keywords(&mut self) -> Result<Vec<Ident>, ParserError> {
/// Parse identifiers
pub fn parse_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
let mut idents = vec![];
loop {
match self.peek_token() {
Token::Word(w) => {
if w.keyword != Keyword::NoKeyword {
break;
}

idents.push(w.to_ident());
}
Token::EOF | Token::Eq => break,
_ => {}
}

self.next_token();
}

Ok(idents)
}

/// Parse identifiers
pub fn parse_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
let mut idents = vec![];
loop {
match self.next_token() {
Token::Word(w) => {
idents.push(w.to_ident());
}
Token::EOF => break,
_ => {}
}
}

Ok(idents)
}

Expand Down Expand Up @@ -5312,7 +5290,7 @@ impl<'a> Parser<'a> {

/// Parse a `var = expr` assignment, used in an UPDATE statement
pub fn parse_assignment(&mut self) -> Result<Assignment, ParserError> {
let id = self.parse_identifiers_non_keywords()?;
let id = self.parse_identifiers()?;
self.expect_token(&Token::Eq)?;
let value = self.parse_expr()?;
Ok(Assignment { id, value })
Expand Down Expand Up @@ -6325,4 +6303,22 @@ mod tests {
}
);
}

#[test]
fn test_update_has_keyword() {
let sql = r#"UPDATE test SET name=$1,
value=$2,
where=$3,
create=$4,
is_default=$5,
classification=$6,
sort=$7
WHERE id=$8"#;
let pg_dialect = PostgreSqlDialect {};
let ast = Parser::parse_sql(&pg_dialect, sql).unwrap();
assert_eq!(
ast[0].to_string(),
r#"UPDATE test SET name = $1, value = $2, where = $3, create = $4, is_default = $5, classification = $6, sort = $7 WHERE id = $8"#
);
}
}

0 comments on commit 10652b6

Please sign in to comment.