mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-22 13:42:31 +00:00
Further simplify parse_compound_identifier (5/8)
This part changes behavior: - Fail when no identifier is found. - Avoid rewinding if EOF was hit right after the identifier.
This commit is contained in:
parent
991fd19b87
commit
7bbf69f513
2 changed files with 25 additions and 17 deletions
|
@ -945,36 +945,37 @@ impl Parser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parse one or more identifiers with the specified separator between them
|
||||||
pub fn parse_compound_identifier(&mut self, separator: &Token) -> Result<ASTNode, ParserError> {
|
pub fn parse_compound_identifier(&mut self, separator: &Token) -> Result<ASTNode, ParserError> {
|
||||||
let mut idents = vec![];
|
let mut idents = vec![];
|
||||||
let mut expect_identifier = true;
|
let mut expect_identifier = true;
|
||||||
loop {
|
loop {
|
||||||
let token = &self.next_token();
|
let token = &self.next_token();
|
||||||
match token {
|
match token {
|
||||||
Some(Token::SQLWord(s)) => {
|
Some(Token::SQLWord(s)) if expect_identifier => {
|
||||||
if expect_identifier {
|
expect_identifier = false;
|
||||||
expect_identifier = false;
|
idents.push(s.to_string());
|
||||||
idents.push(s.to_string());
|
|
||||||
} else {
|
|
||||||
self.prev_token();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Some(token) if token == separator => {
|
Some(token) if token == separator && !expect_identifier => {
|
||||||
if expect_identifier {
|
expect_identifier = true;
|
||||||
return parser_err!(format!("Expecting identifier, found {:?}", token));
|
continue;
|
||||||
} else {
|
|
||||||
expect_identifier = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
self.prev_token();
|
if token.is_some() {
|
||||||
|
self.prev_token();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(ASTNode::SQLCompoundIdentifier(idents))
|
if expect_identifier {
|
||||||
|
parser_err!(format!(
|
||||||
|
"Expecting identifier, found {:?}",
|
||||||
|
self.peek_token()
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
Ok(ASTNode::SQLCompoundIdentifier(idents))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_tablename(&mut self) -> Result<String, ParserError> {
|
pub fn parse_tablename(&mut self) -> Result<String, ParserError> {
|
||||||
|
|
|
@ -103,6 +103,13 @@ fn parse_invalid_table_name() {
|
||||||
assert!(ast.is_err());
|
assert!(ast.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_no_table_name() {
|
||||||
|
let mut parser = parser("");
|
||||||
|
let ast = parser.parse_tablename();
|
||||||
|
assert!(ast.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_insert_with_columns() {
|
fn parse_insert_with_columns() {
|
||||||
let sql = String::from("INSERT INTO public.customer (id, name, active) VALUES(1, 2, 3)");
|
let sql = String::from("INSERT INTO public.customer (id, name, active) VALUES(1, 2, 3)");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue