Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Supports updating valid column names whose names are the same as… #725

Merged
merged 2 commits into from Nov 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 21 additions & 25 deletions src/parser.rs
Expand Up @@ -3886,41 +3886,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 @@ -5282,7 +5260,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 @@ -6295,4 +6273,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"#
);
}
}