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:
@@ -40,6 +40,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
|
||||
import com.alibaba.druid.util.FNVUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
|
||||
@@ -62,6 +63,8 @@ public class Lexer {
|
||||
protected Keywords keywods = Keywords.DEFAULT_KEYWORDS;
|
||||
|
||||
protected String stringVal;
|
||||
protected long hash_lower; // fnv1a_64
|
||||
protected long hash;
|
||||
|
||||
protected int commentCount = 0;
|
||||
|
||||
@@ -1422,6 +1425,31 @@ public class Lexer {
|
||||
}
|
||||
return stringVal;
|
||||
}
|
||||
|
||||
public final boolean identifierEquals(String text) {
|
||||
if (token != Token.IDENTIFIER) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (stringVal == null) {
|
||||
stringVal = subString(mark, bufPos);
|
||||
}
|
||||
return text.equalsIgnoreCase(stringVal);
|
||||
}
|
||||
|
||||
public final boolean identifierEquals(long hash_lower) {
|
||||
if (token != Token.IDENTIFIER) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.hash_lower == 0) {
|
||||
if (stringVal == null) {
|
||||
stringVal = subString(mark, bufPos);
|
||||
}
|
||||
this.hash_lower = FNVUtils.fnv_64_lower(stringVal);
|
||||
}
|
||||
return this.hash_lower == hash_lower;
|
||||
}
|
||||
|
||||
public final List<String> readAndResetComments() {
|
||||
List<String> comments = this.comments;
|
||||
|
||||
@@ -60,10 +60,10 @@ public class SQLCreateTableParser extends SQLDDLParser {
|
||||
accept(Token.CREATE);
|
||||
}
|
||||
|
||||
if (identifierEquals("GLOBAL")) {
|
||||
if (lexer.identifierEquals("GLOBAL")) {
|
||||
lexer.nextToken();
|
||||
|
||||
if (identifierEquals("TEMPORARY")) {
|
||||
if (lexer.identifierEquals("TEMPORARY")) {
|
||||
lexer.nextToken();
|
||||
createTable.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY);
|
||||
} else {
|
||||
@@ -134,7 +134,7 @@ public class SQLCreateTableParser extends SQLDDLParser {
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
if (identifierEquals("INHERITS")) {
|
||||
if (lexer.identifierEquals("INHERITS")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.LPAREN);
|
||||
SQLName inherits = this.exprParser.name();
|
||||
|
||||
@@ -44,6 +44,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUnique;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGPolygonExpr;
|
||||
import com.alibaba.druid.util.FNVUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class SQLExprParser extends SQLParser {
|
||||
@@ -791,7 +792,7 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
expr = dotRest(expr);
|
||||
return primaryRest(expr);
|
||||
} else if (identifierEquals("SETS") //
|
||||
} else if (lexer.identifierEquals("SETS") //
|
||||
&& expr.getClass() == SQLIdentifierExpr.class //
|
||||
&& "GROUPING".equalsIgnoreCase(((SQLIdentifierExpr) expr).getName())) {
|
||||
SQLGroupingSetExpr groupingSets = new SQLGroupingSetExpr();
|
||||
@@ -1661,7 +1662,7 @@ public class SQLExprParser extends SQLParser {
|
||||
rightExp = primary();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Escape, rightExp, getDbType());
|
||||
}
|
||||
} else if (identifierEquals("RLIKE")) {
|
||||
} else if (lexer.identifierEquals(FNVUtils.RLIKE)) {
|
||||
lexer.nextToken();
|
||||
rightExp = equality();
|
||||
|
||||
|
||||
@@ -50,11 +50,11 @@ public class SQLParser {
|
||||
}
|
||||
|
||||
protected boolean identifierEquals(String text) {
|
||||
return lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase(text);
|
||||
return lexer.identifierEquals(text);
|
||||
}
|
||||
|
||||
protected void acceptIdentifier(String text) {
|
||||
if (identifierEquals(text)) {
|
||||
if (lexer.identifierEquals(text)) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
setErrorEndPos(lexer.pos());
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.alibaba.druid.sql.parser;
|
||||
|
||||
/**
|
||||
* @author wenshao[szujobs@hotmail.com]
|
||||
*/
|
||||
public class SymbolTable {
|
||||
public static SymbolTable global = new SymbolTable(8192);
|
||||
|
||||
private final Entry[] entries;
|
||||
private final int indexMask;
|
||||
|
||||
public SymbolTable(int tableSize){
|
||||
this.indexMask = tableSize - 1;
|
||||
this.entries = new Entry[tableSize];
|
||||
|
||||
// this.addSymbol("id", 0, 2, "id".hashCode());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String addSymbol(String buffer, int offset, int len, long hash) {
|
||||
final int bucket = ((int) hash) & indexMask;
|
||||
|
||||
Entry entry = entries[bucket];
|
||||
if (entry != null) {
|
||||
if (hash == entry.hash //
|
||||
&& len == entry.len) {
|
||||
return entry.value;
|
||||
}
|
||||
|
||||
String str = subString(buffer, offset, len);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
String str = subString(buffer, offset, len);
|
||||
entry = new Entry(hash, len, str);
|
||||
entries[bucket] = entry;
|
||||
return str;
|
||||
}
|
||||
|
||||
private static String subString(String src, int offset, int len) {
|
||||
char[] chars = new char[len];
|
||||
src.getChars(offset, offset + len, chars, 0);
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
private static class Entry {
|
||||
public final long hash;
|
||||
public final int len;
|
||||
public final String value;
|
||||
|
||||
public Entry(long hash, int len, String value) {
|
||||
this.hash = hash;
|
||||
this.len = len;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user