-
Notifications
You must be signed in to change notification settings - Fork 612
create if not exists for all dialect #163
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
create if not exists for all dialect #163
Conversation
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.
It's not in the standard, but at least PostgreSQL has it.
Thanks for the PR! Please see the comments inline. And please run cargo fmt
and rebase on master, otherwise the CI fails.
src/parser.rs
Outdated
@@ -932,6 +933,37 @@ impl Parser { | |||
} | |||
|
|||
pub fn parse_create_table(&mut self) -> Result<Statement, ParserError> { | |||
let if_keyword = self.parse_keyword("IF"); |
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 you can simply use let if_not_exists = self.parse_keywords(vec!["IF", "NOT", "EXISTS"]);
, why not?
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.
If I use let if_not_exists = self.parse_keywords(vec!["IF", "NOT", "EXISTS"]);
I wouldn't be able to say if user typed in just CREATE TABLE
or missed one of keyword because self.parse_keywords
returns bool
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.
Not sure I fully understand what you're saying.. When parse_keywords
returns false
the current token is left unchanged. So CREATE TABLE if (...)
would be accepted (which seems more appropriate for the generic parser at least; I don't know if Postgres rejects that), and missing some of the keywords like CREATE TABLE IF NOT ...
would fail with an error like "Expected end of statement, found: NOT" instead of more specific error message, which seems acceptable too.
Listing all possible errors for multiple dialects doesn't seem maintainable, and we already use parse_keywords
in other places.
eb59356
to
112234d
Compare
@nickolay thank you for review, I've rebased over master and moved tests to only PostgreSQL dialect module, but I have general question how do I make sure that |
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.
how do I make sure that CREATE TABLE IF NOT EXISTS applies only to Postgres dialect?
You don't; the dialects only affect tokenizing at this point.. Just running the test for pg_and_generic()
dialects is fine.
src/parser.rs
Outdated
@@ -932,6 +933,37 @@ impl Parser { | |||
} | |||
|
|||
pub fn parse_create_table(&mut self) -> Result<Statement, ParserError> { | |||
let if_keyword = self.parse_keyword("IF"); |
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.
Not sure I fully understand what you're saying.. When parse_keywords
returns false
the current token is left unchanged. So CREATE TABLE if (...)
would be accepted (which seems more appropriate for the generic parser at least; I don't know if Postgres rejects that), and missing some of the keywords like CREATE TABLE IF NOT ...
would fail with an error like "Expected end of statement, found: NOT" instead of more specific error message, which seems acceptable too.
Listing all possible errors for multiple dialects doesn't seem maintainable, and we already use parse_keywords
in other places.
Note that CI still thinks you didn't run rustfmt before comitting: https://travis-ci.org/github/andygrove/sqlparser-rs/builds/677142460#L641 - have you? |
Thank you! |
This PR attempts to implement
CREATE TABLE IF NOT EXISTS table_name
for all dialects.However, I am not sure that it is a standard SQL statement - please correct me if I am wrong.