Skip to content
Permalink
Browse files

[CONJ-618] Client preparestatement parsing error on escaped ' / " in …

…query (not parameters)

(cherry picked from commit e9e8acc)
  • Loading branch information...
rusher committed Jun 14, 2018
1 parent f0ca4dd commit 5a8e22a233a8513da32d6d434e34655be99e95c9
@@ -107,9 +107,8 @@ public static ClientPrepareResult parameterParts(String queryString, boolean noB
int queryLength = query.length;
for (int i = 0; i < queryLength; i++) {

if (state == LexState.Escape) state = LexState.String;

char car = query[i];
if (state == LexState.Escape && !((car == '\'' && singleQuotes) || (car == '"' && !singleQuotes))) state = LexState.String;
switch (car) {
case '*':
if (state == LexState.Normal && lastChar == '/') state = LexState.SlashStarComment;
@@ -147,6 +146,8 @@ public static ClientPrepareResult parameterParts(String queryString, boolean noB
singleQuotes = false;
} else if (state == LexState.String && !singleQuotes) {
state = LexState.Normal;
} else if (state == LexState.Escape && !singleQuotes) {
state = LexState.String;
}
break;

@@ -156,6 +157,8 @@ public static ClientPrepareResult parameterParts(String queryString, boolean noB
singleQuotes = true;
} else if (state == LexState.String && singleQuotes) {
state = LexState.Normal;
} else if (state == LexState.Escape && singleQuotes) {
state = LexState.String;
}
break;

@@ -226,7 +229,7 @@ public static boolean canAggregateSemiColon(String queryString, boolean noBacksl

for (char car : query) {

if (state == LexState.Escape) state = LexState.String;
if (state == LexState.Escape && !((car == '\'' && singleQuotes) || (car == '"' && !singleQuotes))) state = LexState.String;

switch (car) {
case '*':
@@ -266,6 +269,8 @@ public static boolean canAggregateSemiColon(String queryString, boolean noBacksl
singleQuotes = false;
} else if (state == LexState.String && !singleQuotes) {
state = LexState.Normal;
} else if (state == LexState.Escape && !singleQuotes) {
state = LexState.String;
}
break;

@@ -275,6 +280,8 @@ public static boolean canAggregateSemiColon(String queryString, boolean noBacksl
singleQuotes = true;
} else if (state == LexState.String && singleQuotes) {
state = LexState.Normal;
} else if (state == LexState.Escape && singleQuotes) {
state = LexState.String;
}
break;

@@ -373,13 +380,13 @@ public static ClientPrepareResult rewritableParts(String queryString, boolean no
int queryLength = query.length;
for (int i = 0; i < queryLength; i++) {

if (state == LexState.Escape) {
sb.append(query[i]);
char car = query[i];
if (state == LexState.Escape && !((car == '\'' && singleQuotes) || (car == '"' && !singleQuotes))) {
sb.append(car);
state = LexState.String;
continue;
}

char car = query[i];
switch (car) {
case '*':
if (state == LexState.Normal && lastChar == '/') state = LexState.SlashStarComment;
@@ -413,6 +420,8 @@ public static ClientPrepareResult rewritableParts(String queryString, boolean no
singleQuotes = false;
} else if (state == LexState.String && !singleQuotes) {
state = LexState.Normal;
} else if (state == LexState.Escape && !singleQuotes) {
state = LexState.String;
}
break;
case ';':
@@ -427,6 +436,8 @@ public static ClientPrepareResult rewritableParts(String queryString, boolean no
singleQuotes = true;
} else if (state == LexState.String && singleQuotes) {
state = LexState.Normal;
} else if (state == LexState.Escape && singleQuotes) {
state = LexState.String;
}
break;

@@ -81,6 +81,21 @@ private boolean checkParsing(String sql, int paramNumber, boolean rewritable, bo
return true;
}

@Test
public void stringEscapeParsing() throws Exception {
assertTrue(checkParsing("select '\\'' as a, ? as b, \"\\\"\" as c, ? as d",
2, true, true,
new String[]{
"select '\\'' as a, ",
" as b, \"\\\"\" as c, ",
" as d"},
new String[]{
"select '\\'' as a, ",
" as b, \"\\\"\" as c, ",
" as d"}));
}


@Test
public void testRewritableWithConstantParameter() throws Exception {
assertTrue(checkParsing("INSERT INTO TABLE(col1,col2,col3,col4, col5) VALUES (9, ?, 5, ?, 8) ON DUPLICATE KEY UPDATE col2=col2+10",

0 comments on commit 5a8e22a

Please sign in to comment.
You can’t perform that action at this time.