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
Add ability for dialects to override prefix, infix, and statement parsing #581
Add ability for dialects to override prefix, infix, and statement parsing #581
Conversation
Pull Request Test Coverage Report for Build 2889163360
💛 - Coveralls |
Keyword::REPLACE if dialect_of!(self is SQLiteDialect ) => { | ||
self.prev_token(); | ||
Ok(self.parse_insert()?) | ||
} | ||
Keyword::COMMENT if dialect_of!(self is PostgreSqlDialect) => { | ||
Ok(self.parse_comment()?) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This dialect-specific code has now moved to the appropriate dialects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it looks like a great idea -- thank you @andygrove
I had a small comment on tests but otherwise 👍 🚀
src/parser.rs
Outdated
let sql = "SELECT 1 + 2"; | ||
let ast = Parser::parse_sql(&dialect, sql)?; | ||
let query = &ast[0]; | ||
assert_eq!("SELECT NULL + 2", &format!("{}", query)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆
src/parser.rs
Outdated
@@ -4926,4 +4910,124 @@ mod tests { | |||
assert_eq!(ast.to_string(), sql.to_string()); | |||
}); | |||
} | |||
|
|||
#[test] | |||
fn custom_prefix_parser() -> Result<(), ParserError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend putting these tests into tests
somewhere so that we can be sure everything needed is exposed pub
lically.
Perhaps tests/sqlparser_custom_dialects.rs
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. I have moved them.
Closes #580
This change adds some new functions to
Dialect
:The parser will invoke these methods to see if the dialect wants to override the parsing of these items; otherwise, it will parse them itself. This is a much cleaner way to add dialect-specific parsing, IMO and will allow us to start removing all the
if dialect_of
hacks from the main parser.I moved the Postgres parsing of comments and the SqlLite parsing of REPLACE into their respective dialects to demonstrate how this works.