Skip to content
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

fixes the TSQL string escape parsing inside codemirror sql.js #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 12 additions & 6 deletions App/StackExchange.DataExplorer/Scripts/codemirror/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ CodeMirror.defineMode("sql", function (config, parserConfig) {

function tokenBase(stream, state) {
var ch = stream.next();
// start of string?
if (ch == "'") {
// start of string? Both single and double-quote is allowed
if (ch == "'" || ch == '"') {
return chain(stream, state, tokenString(ch));
}
else if (ch == "[") {
Expand Down Expand Up @@ -102,11 +102,17 @@ CodeMirror.defineMode("sql", function (config, parserConfig) {

function tokenString(quote) {
return function (stream, state) {
var escaped = false, next, end = false;
var escaped = false, next, previous, end = false;
while ((next = stream.next()) != null) {
// This isn't how T-SQL strings work, need to fix
if (next == quote && !escaped) { end = true; break; }
escaped = !escaped && next == "\\";
// T-SQL strings escape a quote by doubling up, hence the previous and next handling
// when next is a quote, this is what previous contains
// string '' -> previous == null
// string 'fubar' -> previous != null and not equals quote
// string 'fu''bar' -> previous != null and equal quote, keep state as escaped
// the escapedd state needs to be captured for the case where the string literal is multiline
if (next == quote && (previous == null || previous != quote)) { end = true; break; }
escaped = !escaped && (next == quote && previous == quote);
previous = next;
}
if (end || !(escaped || multiLineStrings))
state.tokenize = tokenBase;
Expand Down