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
(sql) single backslash inside a string literal seems to not be valid #1748
Comments
Are backslashes not used to escape things in SQL? I forget. Our grammars thinks that's what they are for. |
Nope, at least not in TSQL (MS Sql Server), if you need a single quote inside of a string, you just double the single quote. MSSQL: MYSQL: POSTGRES:
|
What about escaping newlines and other characters? |
Mysql for example: https://dev.mysql.com/doc/refman/8.0/en/string-literals.html So what you pasted looks invalid the way I read this. I think we need some more SQL people to weigh in... |
The problem is, it's vendor specific, but we have one-language-fits-all (except PostgreSQL and, hopefully, MSSQL). |
Well, it's worse than that even. See the MySQL docs... it's a CONFIGURABLE option... :-) Sometimes \ means escape, and sometimes it means literal |
I have a feeling this might stay open a while until someone can explain how to handle both these correctly:
Obviously if one SQL provider is 100% strict in how they handle this we could fix it in their own grammar, but right now having the escaping in the core |
For Microsoft SQL Server and other RDMSes, the string ''' (backslash, single quote) is not a valid string literal. Thus VSCode displays an "Unclosed quotation mark after the character string '''' + foo from bar" error. If you escape the embedded single quote with a single quote, then there's no parsing error and the syntax highlighting is correct. See the "Character string constants" section here in the Docs - https://docs.microsoft.com/en-us/sql/t-sql/data-types/constants-transact-sql Oracle - https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm Using the delimiter to escape itself is actually a nice solution, because SQL is often written by non-programmers and they don't have to remember to escape all the other special characters within the string. So, for example:
Bear in mind, if you "fix" the problem is highlight.js, then syntax highlighting will be "correct" but parsing will still report an error. As the PostgreSQL link above says "Note that in a standard-conforming string literal, \ just means \ anyway." It seems to me that maybe MySQL by itself has gone off the rails a bit as far as string literals are concerned. Yes, I mean MySQL is non-standard conforming in this case. |
@schallm Can you point to anything in the SQL spec documentation that this is indeed a SQL issue and not specific to one of the more specific grammars such as T-SQL? If not I plan to close this issue. |
From what I understand and others have commented, the SQL spec is actually defined as I'm requesting.
Is there a publicly available version of the SQL spec that I can research and point to? The only one I can find is not freely available. |
Yeah, that's annoying. No idea. Not sure it would have to be the spec... but we need two things answered here:
Is a little vague for me. Sadly @jwk6 focused a lot of thought on how to escape quotes rather than the actual validity of
|
Interestingly postgres says this:
|
Evidentially double quotes aren't part of standard SQL either though, lol... https://stackoverflow.com/questions/881194/how-do-i-escape-special-characters-in-mysql/881252#881252 So I'm left a little unsure how to fix this since our SQL is already blurred and SQL can mean so many things... I think I've talked myself out of closing it though, but I still don't see a quick fix right now. |
First of all: @schallm as long as your issue concerns MSSQL, you have you tried Now, for the sake of discussion:
No, but there are publicly available drafts which hopefully don't differ much from the actual standard.
As I can see in my copy of the standard, backslash is just a regular symbol, it is not used for escaping.
There is no way, I'm afraid... For PostgreSQL and MSSQL we already have different grammars, so I think we can exclude them from the discussion. This leaves us with:
So maybe it's worth removing backslash escaping from |
I'm still not sure there is a perfect solution here... I worry that our "sql" is bundled by default and often serves as a stand-on for "ALL sqls" (which might have bearing here), meaning we shouldn't so quickly dismiss the fact that we have separate But I also don't really have a strong opinion to not change it. No one really spoke up to defend it, so we can always change it and then see if people show up raising issues to revert it. :-) @schallm Would you be willing to whip up a small PR for this change? |
I have not looked closely at what this would take, but I will take a look at it soon. |
It shouldn't be too difficult. But you might have to look at both double and single quoted strings... I suppose your test case could be the issue actually brought up here... |
I opened a ticket against VS Code for a colorization issue (microsoft/vscode#50059). They closed it as upstream to this project.
It boils down to the following gets incorrect highlighting due to the backslash.
```sql
select '\' + foo from bar
```
Same issue displays here on github:
vs
The text was updated successfully, but these errors were encountered: