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
feat: Support escaped string literals (PostgreSQL) #502
Conversation
Signed-off-by: Dmitry Patsura <talk@dmtry.me>
@alamb can you take a look to verify that it's a correct approach. Thanks |
Pull Request Test Coverage Report for Build 2379093604
💛 - Coveralls |
Thanks -- I am a little behind in sqlparser-rs review. I will try and find
time this weekend to review
…On Wed, May 18, 2022 at 2:52 PM Coveralls ***@***.***> wrote:
Pull Request Test Coverage Report for Build 2347539602
<https://coveralls.io/builds/49246117>
- *63* of *80* *(78.75%)* changed or added relevant lines in *4* files
are covered.
- *6* unchanged lines in *2* files lost coverage.
- Overall coverage decreased (*-0.1%*) to *90.384%*
------------------------------
Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser.rs
<https://coveralls.io/builds/49246117/source?filename=src%2Fparser.rs#L908>
4 6 66.67%
src/ast/value.rs
<https://coveralls.io/builds/49246117/source?filename=src%2Fast%2Fvalue.rs#L207>
12 18 66.67%
src/tokenizer.rs
<https://coveralls.io/builds/49246117/source?filename=src%2Ftokenizer.rs#L402>
31 40 77.5%
Files with Coverage Reduction New Missed Lines %
src/parser.rs
<https://coveralls.io/builds/49246117/source?filename=src%2Fparser.rs#L909>
1 82.94%
src/tokenizer.rs
<https://coveralls.io/builds/49246117/source?filename=src%2Ftokenizer.rs#L775>
5 89.5%
Totals [image: Coverage Status] <https://coveralls.io/builds/49246117>
Change from base Build 2328175823 <https://coveralls.io/builds/49129477>:
-0.1%
Covered Lines: 8365
Relevant Lines: 9255
------------------------------
💛 - Coveralls <https://coveralls.io>
—
Reply to this email directly, view it on GitHub
<#502 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADXZMNOF6E5YP7YTXV5CJ3VKU35NANCNFSM5WJMUQGQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
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.
Thank you @ovr -- I like this PR; very nice.
The only comment I think needs to be addressed prior to merge is the comment on EscapeEscapedStringLiteral
src/ast/value.rs
Outdated
|
||
impl<'a> fmt::Display for EscapeEscapedStringLiteral<'a> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let mut is_escaped = true; |
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 don't see is_escaped
ever set to false
-- I would expect it would start with is_escaped = true
and then is_escaped
wold be set to false after each character was written
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 would this work with: First \n second \\ third \n fourth \
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
@@ -496,6 +496,10 @@ impl<'a> Parser<'a> { | |||
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?), | |||
}) | |||
} | |||
Token::EscapedStringLiteral(_) if dialect_of!(self is PostgreSqlDialect) => { |
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.
💯 for conditionalizing on postgres dialect
let mut s = String::new(); | ||
chars.next(); // consume the opening quote | ||
|
||
// slash escaping |
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.
FYIW this example from stack overflow looks like it might be a nice way to avoid macro overhead (and thus code bloat): https://stackoverflow.com/questions/58551211/how-do-i-interpret-escaped-characters-in-a-string
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 the same because this function tries to find & escapes the string from the query. It tries to find a single quote that can be escaped or not escaped (end of the string).
in our case string are wrapped in single quotes, i.e e'str'
tests/sqlparser_postgres.rs
Outdated
|
||
#[test] | ||
fn parse_escaped_literal_string() { | ||
let sql = r#"SELECT E's1 \n s1', E's2 \\n s2', E's3 \\\n s3', E's4 \\\\n s4', E'\''"#; |
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 also recommend some negative tests like ' Foo\'
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 a correct value because the last quote was escaped, there is no single quote which should close the string expr. f46b07e
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
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.
Looks good -- thank you @ovr !
Hello!
It's a draft which implements special PostgreSQL escaped string syntax.
https://www.postgresql.org/docs/8.3/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
Thanks