mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve sql parser
This commit is contained in:
@@ -76,7 +76,7 @@ public class HiveLexer extends Lexer {
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if (ch == '*' && buf[bp + 1] == '/') {
|
||||
if (ch == '*' && charAt(bp + 1) == '/') {
|
||||
sp += 2;
|
||||
scanChar();
|
||||
scanChar();
|
||||
@@ -88,10 +88,10 @@ public class HiveLexer extends Lexer {
|
||||
}
|
||||
|
||||
if (isHint) {
|
||||
stringVal = new String(buf, np + startHintSp, (sp - startHintSp) - 2).trim();
|
||||
stringVal = subString(np + startHintSp, (sp - startHintSp) - 2).trim();
|
||||
token = Token.HINT;
|
||||
} else {
|
||||
stringVal = new String(buf, np, sp);
|
||||
stringVal = subString(np, sp);
|
||||
token = Token.MULTI_LINE_COMMENT;
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ public class HiveLexer extends Lexer {
|
||||
|
||||
for (;;) {
|
||||
if (ch == '\r') {
|
||||
if (buf[bp + 1] == '\n') {
|
||||
if (charAt(bp + 1) == '\n') {
|
||||
sp += 2;
|
||||
scanChar();
|
||||
break;
|
||||
@@ -131,7 +131,7 @@ public class HiveLexer extends Lexer {
|
||||
sp++;
|
||||
}
|
||||
|
||||
stringVal = new String(buf, np + 1, sp);
|
||||
stringVal = subString(np + 1, sp);
|
||||
token = Token.LINE_COMMENT;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -67,86 +67,76 @@ public class MySqlLexer extends Lexer {
|
||||
throw new SQLParseException("illegal variable");
|
||||
}
|
||||
|
||||
int hash = first;
|
||||
|
||||
np = bp;
|
||||
sp = 1;
|
||||
|
||||
if (buf[bp + 1] == '@') {
|
||||
ch = buf[++bp];
|
||||
hash = 31 * hash + ch;
|
||||
|
||||
if (charAt(bp + 1) == '@') {
|
||||
ch = charAt(++bp);
|
||||
sp++;
|
||||
}
|
||||
|
||||
if (buf[bp + 1] == '`') {
|
||||
if (charAt(bp + 1) == '`') {
|
||||
++bp;
|
||||
++sp;
|
||||
char ch;
|
||||
for (;;) {
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
|
||||
if (ch == '`') {
|
||||
sp++;
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
break;
|
||||
} else if (ch == EOI) {
|
||||
throw new SQLParseException("illegal identifier");
|
||||
}
|
||||
|
||||
hash = 31 * hash + ch;
|
||||
|
||||
sp++;
|
||||
continue;
|
||||
}
|
||||
|
||||
this.ch = buf[bp];
|
||||
this.ch = charAt(bp);
|
||||
|
||||
stringVal = symbolTable.addSymbol(buf, np, sp, hash);
|
||||
stringVal = subString(np, sp);
|
||||
token = Token.VARIANT;
|
||||
} else if (buf[bp + 1] == '{') {
|
||||
} else if (charAt(bp + 1) == '{') {
|
||||
++bp;
|
||||
++sp;
|
||||
char ch;
|
||||
for (;;) {
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
|
||||
if (ch == '}') {
|
||||
sp++;
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
break;
|
||||
} else if (ch == EOI) {
|
||||
throw new SQLParseException("illegal identifier");
|
||||
}
|
||||
|
||||
hash = 31 * hash + ch;
|
||||
|
||||
sp++;
|
||||
continue;
|
||||
}
|
||||
|
||||
this.ch = buf[bp];
|
||||
this.ch = charAt(bp);
|
||||
|
||||
stringVal = symbolTable.addSymbol(buf, np, sp, hash);
|
||||
stringVal = subString(np, sp);
|
||||
token = Token.VARIANT;
|
||||
} else {
|
||||
for (;;) {
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
|
||||
if (!isIdentifierChar(ch)) {
|
||||
break;
|
||||
}
|
||||
|
||||
hash = 31 * hash + ch;
|
||||
|
||||
sp++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
this.ch = buf[bp];
|
||||
this.ch = charAt(bp);
|
||||
|
||||
stringVal = symbolTable.addSymbol(buf, np, sp, hash);
|
||||
stringVal = subString(np, sp);
|
||||
token = Token.VARIANT;
|
||||
}
|
||||
|
||||
@@ -154,31 +144,28 @@ public class MySqlLexer extends Lexer {
|
||||
final char first = ch;
|
||||
|
||||
if (ch == '`') {
|
||||
int hash = first;
|
||||
|
||||
np = bp;
|
||||
sp = 1;
|
||||
char ch;
|
||||
for (;;) {
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
|
||||
if (ch == '`') {
|
||||
sp++;
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
break;
|
||||
} else if (ch == EOI) {
|
||||
throw new SQLParseException("illegal identifier");
|
||||
}
|
||||
|
||||
hash = 31 * hash + ch;
|
||||
|
||||
sp++;
|
||||
continue;
|
||||
}
|
||||
|
||||
this.ch = buf[bp];
|
||||
this.ch = charAt(bp);
|
||||
|
||||
stringVal = symbolTable.addSymbol(buf, np, sp, hash);
|
||||
stringVal = subString(np, sp);
|
||||
Token tok = keywods.getKeyword(stringVal);
|
||||
if (tok != null) {
|
||||
token = tok;
|
||||
@@ -198,7 +185,7 @@ public class MySqlLexer extends Lexer {
|
||||
sp = 1;
|
||||
char ch;
|
||||
for (;;) {
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
|
||||
if (!isIdentifierChar(ch)) {
|
||||
break;
|
||||
@@ -210,9 +197,9 @@ public class MySqlLexer extends Lexer {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.ch = buf[bp];
|
||||
this.ch = charAt(bp);
|
||||
|
||||
stringVal = symbolTable.addSymbol(buf, np, sp, hash);
|
||||
stringVal = addSymbol(hash);
|
||||
Token tok = keywods.getKeyword(stringVal);
|
||||
if (tok != null) {
|
||||
token = tok;
|
||||
@@ -227,17 +214,20 @@ public class MySqlLexer extends Lexer {
|
||||
boolean hasSpecial = false;
|
||||
|
||||
for (;;) {
|
||||
if (bp >= buflen) {
|
||||
if (isEOF()) {
|
||||
lexError(tokenPos, "unclosed.str.lit");
|
||||
return;
|
||||
}
|
||||
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
|
||||
if (ch == '\\') {
|
||||
scanChar();
|
||||
if (!hasSpecial) {
|
||||
System.arraycopy(buf, np + 1, sbuf, 0, sp);
|
||||
if (sbuf == null) {
|
||||
sbuf = new char[32];
|
||||
}
|
||||
arraycopy(np + 1, sbuf, 0, sp);
|
||||
hasSpecial = true;
|
||||
}
|
||||
|
||||
@@ -282,7 +272,10 @@ public class MySqlLexer extends Lexer {
|
||||
token = LITERAL_CHARS;
|
||||
break;
|
||||
} else {
|
||||
System.arraycopy(buf, np + 1, sbuf, 0, sp);
|
||||
if (sbuf == null) {
|
||||
sbuf = new char[32];
|
||||
}
|
||||
arraycopy(np + 1, sbuf, 0, sp);
|
||||
hasSpecial = true;
|
||||
putChar('\'');
|
||||
continue;
|
||||
@@ -302,7 +295,7 @@ public class MySqlLexer extends Lexer {
|
||||
}
|
||||
|
||||
if (!hasSpecial) {
|
||||
stringVal = new String(buf, np + 1, sp);
|
||||
stringVal = subString(np + 1, sp);
|
||||
} else {
|
||||
stringVal = new String(sbuf, 0, sp);
|
||||
}
|
||||
@@ -336,7 +329,7 @@ public class MySqlLexer extends Lexer {
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if (ch == '*' && buf[bp + 1] == '/') {
|
||||
if (ch == '*' && charAt(bp + 1) == '/') {
|
||||
sp += 2;
|
||||
scanChar();
|
||||
scanChar();
|
||||
@@ -348,10 +341,10 @@ public class MySqlLexer extends Lexer {
|
||||
}
|
||||
|
||||
if (isHint) {
|
||||
stringVal = new String(buf, np + startHintSp, (sp - startHintSp) - 1);
|
||||
stringVal = subString(np + startHintSp, (sp - startHintSp) - 1);
|
||||
token = Token.HINT;
|
||||
} else {
|
||||
stringVal = new String(buf, np, sp);
|
||||
stringVal = subString(np, sp);
|
||||
token = Token.MULTI_LINE_COMMENT;
|
||||
}
|
||||
|
||||
@@ -372,7 +365,7 @@ public class MySqlLexer extends Lexer {
|
||||
|
||||
for (;;) {
|
||||
if (ch == '\r') {
|
||||
if (buf[bp + 1] == '\n') {
|
||||
if (charAt(bp + 1) == '\n') {
|
||||
sp += 2;
|
||||
scanChar();
|
||||
break;
|
||||
@@ -393,7 +386,7 @@ public class MySqlLexer extends Lexer {
|
||||
sp++;
|
||||
}
|
||||
|
||||
stringVal = new String(buf, np + 1, sp);
|
||||
stringVal = subString(np + 1, sp);
|
||||
token = Token.LINE_COMMENT;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -114,12 +114,12 @@ public class OracleLexer extends Lexer {
|
||||
|
||||
boolean quoteFlag = false;
|
||||
boolean mybatisFlag = false;
|
||||
if (buf[bp + 1] == '"') {
|
||||
if (charAt(bp + 1) == '"') {
|
||||
hash = 31 * hash + '"';
|
||||
bp++;
|
||||
sp++;
|
||||
quoteFlag = true;
|
||||
} else if (buf[bp + 1] == '{') {
|
||||
} else if (charAt(bp + 1) == '{') {
|
||||
hash = 31 * hash + '"';
|
||||
bp++;
|
||||
sp++;
|
||||
@@ -127,7 +127,7 @@ public class OracleLexer extends Lexer {
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
|
||||
if (!isIdentifierChar(ch)) {
|
||||
break;
|
||||
@@ -155,9 +155,9 @@ public class OracleLexer extends Lexer {
|
||||
sp++;
|
||||
}
|
||||
|
||||
this.ch = buf[bp];
|
||||
this.ch = charAt(bp);
|
||||
|
||||
stringVal = symbolTable.addSymbol(buf, np, sp, hash);
|
||||
stringVal = addSymbol(hash);
|
||||
Token tok = keywods.getKeyword(stringVal);
|
||||
if (tok != null) {
|
||||
token = tok;
|
||||
@@ -194,7 +194,7 @@ public class OracleLexer extends Lexer {
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if (ch == '*' && buf[bp + 1] == '/') {
|
||||
if (ch == '*' && charAt(bp + 1) == '/') {
|
||||
sp += 2;
|
||||
scanChar();
|
||||
scanChar();
|
||||
@@ -206,10 +206,10 @@ public class OracleLexer extends Lexer {
|
||||
}
|
||||
|
||||
if (isHint) {
|
||||
stringVal = new String(buf, np + startHintSp, (sp - startHintSp) - 1);
|
||||
stringVal = subString(np + startHintSp, (sp - startHintSp) - 1);
|
||||
token = Token.HINT;
|
||||
} else {
|
||||
stringVal = new String(buf, np, sp);
|
||||
stringVal = subString(np, sp);
|
||||
token = Token.MULTI_LINE_COMMENT;
|
||||
}
|
||||
|
||||
@@ -230,7 +230,7 @@ public class OracleLexer extends Lexer {
|
||||
|
||||
for (;;) {
|
||||
if (ch == '\r') {
|
||||
if (buf[bp + 1] == '\n') {
|
||||
if (charAt(bp + 1) == '\n') {
|
||||
sp += 2;
|
||||
scanChar();
|
||||
break;
|
||||
@@ -251,7 +251,7 @@ public class OracleLexer extends Lexer {
|
||||
sp++;
|
||||
}
|
||||
|
||||
stringVal = new String(buf, np + 1, sp);
|
||||
stringVal = subString(np + 1, sp);
|
||||
token = Token.LINE_COMMENT;
|
||||
return;
|
||||
}
|
||||
@@ -262,7 +262,7 @@ public class OracleLexer extends Lexer {
|
||||
|
||||
if (ch == '-') {
|
||||
sp++;
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
@@ -271,18 +271,18 @@ public class OracleLexer extends Lexer {
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
}
|
||||
|
||||
boolean isDouble = false;
|
||||
|
||||
if (ch == '.') {
|
||||
if (buf[bp + 1] == '.') {
|
||||
if (charAt(bp + 1) == '.') {
|
||||
token = Token.LITERAL_INT;
|
||||
return;
|
||||
}
|
||||
sp++;
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
isDouble = true;
|
||||
|
||||
for (;;) {
|
||||
@@ -291,17 +291,17 @@ public class OracleLexer extends Lexer {
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
}
|
||||
}
|
||||
|
||||
if (ch == 'e' || ch == 'E') {
|
||||
sp++;
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
|
||||
if (ch == '+' || ch == '-') {
|
||||
sp++;
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
@@ -310,7 +310,7 @@ public class OracleLexer extends Lexer {
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
}
|
||||
|
||||
isDouble = true;
|
||||
|
||||
@@ -74,17 +74,20 @@ public class PGLexer extends Lexer {
|
||||
boolean hasSpecial = false;
|
||||
|
||||
for (;;) {
|
||||
if (bp >= buflen) {
|
||||
if (isEOF()) {
|
||||
lexError(tokenPos, "unclosed.str.lit");
|
||||
return;
|
||||
}
|
||||
|
||||
ch = buf[++bp];
|
||||
ch = charAt(++bp);
|
||||
|
||||
if (ch == '\\') {
|
||||
scanChar();
|
||||
if (!hasSpecial) {
|
||||
System.arraycopy(buf, np + 1, sbuf, 0, sp);
|
||||
if (sbuf == null) {
|
||||
sbuf = new char[32];
|
||||
}
|
||||
arraycopy(np + 1, sbuf, 0, sp);
|
||||
hasSpecial = true;
|
||||
}
|
||||
|
||||
@@ -129,7 +132,7 @@ public class PGLexer extends Lexer {
|
||||
token = LITERAL_CHARS;
|
||||
break;
|
||||
} else {
|
||||
System.arraycopy(buf, np + 1, sbuf, 0, sp);
|
||||
arraycopy(np + 1, sbuf, 0, sp);
|
||||
hasSpecial = true;
|
||||
putChar('\'');
|
||||
continue;
|
||||
@@ -149,7 +152,7 @@ public class PGLexer extends Lexer {
|
||||
}
|
||||
|
||||
if (!hasSpecial) {
|
||||
stringVal = new String(buf, np + 1, sp);
|
||||
stringVal = subString(np + 1, sp);
|
||||
} else {
|
||||
stringVal = new String(sbuf, 0, sp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user