mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for mysql concat literal string.
This commit is contained in:
@@ -330,6 +330,46 @@ public class Lexer {
|
||||
nextToken();
|
||||
}
|
||||
|
||||
public final void nextTokenCommaValue() {
|
||||
if (ch == ' ') {
|
||||
scanChar();
|
||||
}
|
||||
|
||||
if (ch == ',' || ch == ',') {
|
||||
scanChar();
|
||||
token = COMMA;
|
||||
return;
|
||||
}
|
||||
|
||||
if (ch == ')' || ch == ')') {
|
||||
scanChar();
|
||||
token = RPAREN;
|
||||
return;
|
||||
}
|
||||
|
||||
if (ch == '.') {
|
||||
scanChar();
|
||||
token = DOT;
|
||||
return;
|
||||
}
|
||||
|
||||
if (ch == 'a' || ch == 'A') {
|
||||
char ch_next = charAt(pos + 1);
|
||||
if (ch_next == 's' || ch_next == 'S') {
|
||||
char ch_next_2 = charAt(pos + 2);
|
||||
if (ch_next_2 == ' ') {
|
||||
pos += 2;
|
||||
ch = ' ';
|
||||
token = Token.AS;
|
||||
stringVal = "AS";
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nextTokenValue();
|
||||
}
|
||||
|
||||
public final void nextTokenEq() {
|
||||
if (ch == ' ') {
|
||||
scanChar();
|
||||
@@ -389,7 +429,25 @@ public class Lexer {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
if (ch == '"') {
|
||||
bufPos = 0;
|
||||
scanString2_d();
|
||||
return;
|
||||
}
|
||||
|
||||
if (ch == '0') {
|
||||
bufPos = 0;
|
||||
if (charAt(pos + 1) == 'x') {
|
||||
scanChar();
|
||||
scanChar();
|
||||
scanHexaDecimal();
|
||||
} else {
|
||||
scanNumber();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (ch > '0' && ch <= '9') {
|
||||
bufPos = 0;
|
||||
scanNumber();
|
||||
return;
|
||||
@@ -1156,6 +1214,149 @@ public class Lexer {
|
||||
stringVal = new String(buf, 0, bufPos);
|
||||
}
|
||||
}
|
||||
|
||||
protected final void scanString2_d() {
|
||||
{
|
||||
boolean hasSpecial = false;
|
||||
int startIndex = pos + 1;
|
||||
int endIndex = -1; // text.indexOf('\'', startIndex);
|
||||
for (int i = startIndex; i < text.length(); ++i) {
|
||||
final char ch = text.charAt(i);
|
||||
if (ch == '\\') {
|
||||
hasSpecial = true;
|
||||
continue;
|
||||
}
|
||||
if (ch == '"') {
|
||||
endIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (endIndex == -1) {
|
||||
throw new ParserException("unclosed str. " + info());
|
||||
}
|
||||
|
||||
String stringVal;
|
||||
if (token == Token.AS) {
|
||||
stringVal = subString(pos, endIndex + 1 - pos);
|
||||
} else {
|
||||
stringVal = subString(startIndex, endIndex - startIndex);
|
||||
}
|
||||
// hasSpecial = stringVal.indexOf('\\') != -1;
|
||||
|
||||
if (!hasSpecial) {
|
||||
this.stringVal = stringVal;
|
||||
int pos = endIndex + 1;
|
||||
char ch = charAt(pos);
|
||||
if (ch != '\'') {
|
||||
this.pos = pos;
|
||||
this.ch = ch;
|
||||
token = LITERAL_CHARS;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mark = pos;
|
||||
boolean hasSpecial = false;
|
||||
for (;;) {
|
||||
if (isEOF()) {
|
||||
lexError("unclosed.str.lit");
|
||||
return;
|
||||
}
|
||||
|
||||
ch = charAt(++pos);
|
||||
|
||||
if (ch == '\\') {
|
||||
scanChar();
|
||||
if (!hasSpecial) {
|
||||
initBuff(bufPos);
|
||||
arraycopy(mark + 1, buf, 0, bufPos);
|
||||
hasSpecial = true;
|
||||
}
|
||||
|
||||
|
||||
switch (ch) {
|
||||
case '0':
|
||||
putChar('\0');
|
||||
break;
|
||||
case '\'':
|
||||
putChar('\'');
|
||||
break;
|
||||
case '"':
|
||||
putChar('"');
|
||||
break;
|
||||
case 'b':
|
||||
putChar('\b');
|
||||
break;
|
||||
case 'n':
|
||||
putChar('\n');
|
||||
break;
|
||||
case 'r':
|
||||
putChar('\r');
|
||||
break;
|
||||
case 't':
|
||||
putChar('\t');
|
||||
break;
|
||||
case '\\':
|
||||
putChar('\\');
|
||||
break;
|
||||
case 'Z':
|
||||
putChar((char) 0x1A); // ctrl + Z
|
||||
break;
|
||||
case '%':
|
||||
if(JdbcConstants.MYSQL.equals(dbType)) {
|
||||
putChar('\\');
|
||||
}
|
||||
putChar('%');
|
||||
break;
|
||||
case '_':
|
||||
if(JdbcConstants.MYSQL.equals(dbType)) {
|
||||
putChar('\\');
|
||||
}
|
||||
putChar('_');
|
||||
break;
|
||||
default:
|
||||
putChar(ch);
|
||||
break;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
if (ch == '"') {
|
||||
scanChar();
|
||||
if (ch != '"') {
|
||||
token = LITERAL_CHARS;
|
||||
break;
|
||||
} else {
|
||||
if (!hasSpecial) {
|
||||
initBuff(bufPos);
|
||||
arraycopy(mark + 1, buf, 0, bufPos);
|
||||
hasSpecial = true;
|
||||
}
|
||||
putChar('"');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasSpecial) {
|
||||
bufPos++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (bufPos == buf.length) {
|
||||
putChar(ch);
|
||||
} else {
|
||||
buf[bufPos++] = ch;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasSpecial) {
|
||||
stringVal = subString(mark + 1, bufPos);
|
||||
} else {
|
||||
stringVal = new String(buf, 0, bufPos);
|
||||
}
|
||||
}
|
||||
|
||||
protected final void scanAlias() {
|
||||
{
|
||||
@@ -1260,13 +1461,14 @@ public class Lexer {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '\'') {
|
||||
char ch_next = charAt(pos + 1);
|
||||
if (ch_next == '"') {
|
||||
scanChar();
|
||||
continue;
|
||||
}
|
||||
} else if (ch == '\"') {
|
||||
// if (ch == '\'') {
|
||||
// char ch_next = charAt(pos + 1);
|
||||
// if (ch_next == '"') {
|
||||
// scanChar();
|
||||
// continue;
|
||||
// }
|
||||
// } else
|
||||
if (ch == '\"') {
|
||||
char ch_next = charAt(pos + 1);
|
||||
if (ch_next == '"' || ch_next == '\'') {
|
||||
scanChar();
|
||||
|
||||
@@ -347,36 +347,27 @@ public class SQLExprParser extends SQLParser {
|
||||
break;
|
||||
case LITERAL_CHARS: {
|
||||
sqlExpr = new SQLCharExpr(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
|
||||
if (JdbcConstants.MYSQL.equals(dbType)) {
|
||||
SQLMethodInvokeExpr concat = null;
|
||||
lexer.nextTokenValue();
|
||||
|
||||
for (; ; ) {
|
||||
if (lexer.token == Token.LITERAL_ALIAS) {
|
||||
if (concat == null) {
|
||||
concat = new SQLMethodInvokeExpr("CONCAT");
|
||||
concat.addParameter(sqlExpr);
|
||||
sqlExpr = concat;
|
||||
}
|
||||
String alias = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
SQLCharExpr concat_right = new SQLCharExpr(alias.substring(1, alias.length() - 1));
|
||||
concat.addParameter(concat_right);
|
||||
String concat = ((SQLCharExpr) sqlExpr).getText();
|
||||
concat += lexer.stringVal();
|
||||
lexer.nextTokenValue();
|
||||
sqlExpr = new SQLCharExpr(concat);
|
||||
} else if (lexer.token == Token.LITERAL_CHARS || lexer.token == Token.LITERAL_NCHARS) {
|
||||
if (concat == null) {
|
||||
concat = new SQLMethodInvokeExpr("CONCAT");
|
||||
concat.addParameter(sqlExpr);
|
||||
sqlExpr = concat;
|
||||
}
|
||||
|
||||
String chars = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
SQLCharExpr concat_right = new SQLCharExpr(chars);
|
||||
concat.addParameter(concat_right);
|
||||
String concat = ((SQLCharExpr) sqlExpr).getText();
|
||||
concat += lexer.stringVal();
|
||||
lexer.nextTokenValue();
|
||||
sqlExpr = new SQLCharExpr(concat);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
lexer.nextToken();
|
||||
}
|
||||
break;
|
||||
} case LITERAL_NCHARS:
|
||||
@@ -2091,7 +2082,7 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrGreater, rightExp, getDbType());
|
||||
} else if (token == Token.LIKE) {
|
||||
lexer.nextToken();
|
||||
lexer.nextTokenValue();
|
||||
rightExp = bitOr();
|
||||
|
||||
// rightExp = relationalRest(rightExp);
|
||||
@@ -2232,7 +2223,7 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
public SQLExpr notRationalRest(SQLExpr expr) {
|
||||
if (lexer.token == (Token.LIKE)) {
|
||||
lexer.nextToken();
|
||||
lexer.nextTokenValue();
|
||||
SQLExpr rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
@@ -3228,7 +3228,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
} else {
|
||||
expr = new SQLIntegerExpr(lexer.integerValue());
|
||||
}
|
||||
lexer.nextTokenComma();
|
||||
lexer.nextTokenCommaValue();
|
||||
|
||||
if (lexer.token != Token.COMMA && lexer.token != Token.RPAREN) {
|
||||
expr = this.exprParser.exprRest(expr);
|
||||
@@ -3240,7 +3240,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
} else {
|
||||
expr = new SQLCharExpr(lexer.stringVal());
|
||||
}
|
||||
lexer.nextTokenComma();
|
||||
lexer.nextTokenCommaValue();
|
||||
if (lexer.token != Token.COMMA && lexer.token != Token.RPAREN) {
|
||||
expr = this.exprParser.exprRest(expr);
|
||||
}
|
||||
@@ -3251,7 +3251,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
} else {
|
||||
expr = new SQLNCharExpr(lexer.stringVal());
|
||||
}
|
||||
lexer.nextTokenComma();
|
||||
lexer.nextTokenCommaValue();
|
||||
if (lexer.token != Token.COMMA && lexer.token != Token.RPAREN) {
|
||||
expr = this.exprParser.exprRest(expr);
|
||||
}
|
||||
@@ -3262,7 +3262,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
} else {
|
||||
expr = new SQLNullExpr();
|
||||
}
|
||||
lexer.nextTokenComma();
|
||||
lexer.nextTokenCommaValue();
|
||||
if (lexer.token != Token.COMMA && lexer.token != Token.RPAREN) {
|
||||
expr = this.exprParser.exprRest(expr);
|
||||
}
|
||||
@@ -3287,7 +3287,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
|
||||
valueExprList.add(expr);
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
lexer.nextTokenValue();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user