improved sql parser.

This commit is contained in:
wenshao
2017-09-17 17:11:26 +08:00
parent b54a987cc8
commit 3c98a1eee5
23 changed files with 955 additions and 31 deletions
@@ -60,7 +60,7 @@ public class Lexer {
protected long hash;
protected int commentCount = 0;
protected List<String> comments = new ArrayList<String>(2);
protected List<String> comments = null;
protected boolean skipComment = true;
private SavePoint savePoint = null;
@@ -913,7 +913,7 @@ public class SQLExprParser extends SQLParser {
SQLExpr owner = null;
String trimOption = null;
long hash_lower;
long hash_lower = 0L;
if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
methodName = identifierExpr.getName();
@@ -964,7 +964,7 @@ public class SQLExprParser extends SQLParser {
return aggregateExpr;
}
methodInvokeExpr = new SQLMethodInvokeExpr(methodName);
methodInvokeExpr = new SQLMethodInvokeExpr(methodName, hash_lower);
if (owner != null) {
methodInvokeExpr.setOwner(owner);
}
@@ -565,10 +565,16 @@ public class SQLSelectParser extends SQLParser {
protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) {
if ((tableSource.getAlias() == null) || (tableSource.getAlias().length() == 0)) {
if (lexer.token != Token.LEFT && lexer.token != Token.RIGHT && lexer.token != Token.FULL
&& (!lexer.identifierEquals(FnvHash.Constants.STRAIGHT_JOIN))
&& (!lexer.identifierEquals(FnvHash.Constants.CROSS))
&& lexer.token != Token.OUTER) {
Token token = lexer.token;
long hash;
if (token != Token.LEFT
&& token != Token.RIGHT
&& token != Token.FULL
&& token != Token.OUTER
&& !(token == Token.IDENTIFIER
&& ((hash = lexer.hash_lower()) == FnvHash.Constants.STRAIGHT_JOIN
|| hash == FnvHash.Constants.CROSS)))
{
String alias = tableAlias();
if (alias != null) {
tableSource.setAlias(alias);
@@ -15,10 +15,14 @@
*/
package com.alibaba.druid.sql.parser;
import java.nio.charset.Charset;
/**
* @author wenshao[szujobs@hotmail.com]
*/
public class SymbolTable {
private static final Charset UTF8 = Charset.forName("UTF-8");
public static SymbolTable global = new SymbolTable(8192);
private final Entry[] entries;
@@ -29,15 +33,12 @@ public class SymbolTable {
this.entries = new Entry[tableSize];
}
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) {
if (hash == entry.hash) {
return entry.value;
}
@@ -52,12 +53,64 @@ public class SymbolTable {
return str;
}
public String addSymbol(byte[] buffer, int offset, int len, long hash) {
final int bucket = ((int) hash) & indexMask;
Entry entry = entries[bucket];
if (entry != null) {
if (hash == entry.hash) {
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;
}
public String addSymbol(String symbol, long hash) {
final int bucket = ((int) hash) & indexMask;
Entry entry = entries[bucket];
if (entry != null) {
if (hash == entry.hash) {
return entry.value;
}
return symbol;
}
entry = new Entry(hash, symbol.length(), symbol);
entries[bucket] = entry;
return symbol;
}
public String findSymbol(long hash) {
final int bucket = ((int) hash) & indexMask;
Entry entry = entries[bucket];
if (entry != null && entry.hash == hash) {
return entry.value;
}
return null;
}
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 String subString(byte[] bytes, int from, int len) {
byte[] strBytes = new byte[len];
System.arraycopy(bytes, from, strBytes, 0, len);
return new String(strBytes, UTF8);
}
private static class Entry {
public final long hash;
public final int len;