mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -15,6 +15,11 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.parser;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.util.Utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -25,6 +30,9 @@ public class Keywords {
|
||||
|
||||
private final Map<String, Token> keywords;
|
||||
|
||||
private long[] hashArray;
|
||||
private Token[] tokens;
|
||||
|
||||
public final static Keywords DEFAULT_KEYWORDS;
|
||||
|
||||
public final static Keywords SQLITE_KEYWORDS;
|
||||
@@ -169,11 +177,38 @@ public class Keywords {
|
||||
|
||||
public Keywords(Map<String, Token> keywords){
|
||||
this.keywords = keywords;
|
||||
|
||||
this.hashArray = new long[keywords.size()];
|
||||
this.tokens = new Token[keywords.size()];
|
||||
|
||||
int index = 0;
|
||||
for (String k : keywords.keySet()) {
|
||||
hashArray[index++] = Utils.fnv_64_lower(k);
|
||||
}
|
||||
Arrays.sort(hashArray);
|
||||
for (Map.Entry<String, Token> entry : keywords.entrySet()) {
|
||||
long k = Utils.fnv_64_lower(entry.getKey());
|
||||
index = Arrays.binarySearch(hashArray, k);
|
||||
tokens[index] = entry.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
public Token getKeyword(long hash) {
|
||||
int index = Arrays.binarySearch(hashArray, hash);
|
||||
if (index < 0) {
|
||||
return null;
|
||||
}
|
||||
return tokens[index];
|
||||
}
|
||||
|
||||
public Token getKeyword(String key) {
|
||||
key = key.toUpperCase();
|
||||
return keywords.get(key);
|
||||
long k = Utils.fnv_64_lower(key);
|
||||
int index = Arrays.binarySearch(hashArray, k);
|
||||
if (index < 0) {
|
||||
return null;
|
||||
}
|
||||
return tokens[index];
|
||||
// return keywords.get(key);
|
||||
}
|
||||
|
||||
public Map<String, Token> getKeywords() {
|
||||
|
||||
@@ -1417,6 +1417,9 @@ public class Lexer {
|
||||
* The value of a literal token, recorded as a string. For integers, leading 0x and 'l' suffixes are suppressed.
|
||||
*/
|
||||
public final String stringVal() {
|
||||
if (stringVal == null) {
|
||||
stringVal = subString(mark, bufPos);
|
||||
}
|
||||
return stringVal;
|
||||
}
|
||||
|
||||
|
||||
@@ -659,7 +659,7 @@ public class SQLExprParser extends SQLParser {
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new ParserException("ERROR" + lexer.info());
|
||||
throw new ParserException("ERROR. " + lexer.info());
|
||||
}
|
||||
|
||||
SQLExpr expr = primaryRest(sqlExpr);
|
||||
@@ -882,6 +882,12 @@ public class SQLExprParser extends SQLParser {
|
||||
methodInvokeExpr.setFrom(from);
|
||||
}
|
||||
|
||||
if (lexer.token == Token.USING) {
|
||||
lexer.nextToken();
|
||||
SQLExpr using = this.primary();
|
||||
methodInvokeExpr.setUsing(using);
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
if (lexer.token() == Token.OVER) {
|
||||
|
||||
Reference in New Issue
Block a user