improved sql parser.

This commit is contained in:
wenshao
2017-10-18 15:55:24 +08:00
parent 29a5b5637c
commit 377d9346b8
18 changed files with 705 additions and 84 deletions
@@ -0,0 +1,80 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.parser;
import com.alibaba.druid.sql.ast.SQLExpr;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class InsertColumnsCache {
public static InsertColumnsCache global;
static {
global = new InsertColumnsCache(8192);
}
public ConcurrentMap<Long, Entry> cache = new ConcurrentHashMap<Long, Entry>();
private final Entry[] buckets;
private final int indexMask;
public InsertColumnsCache(int tableSize){
this.indexMask = tableSize - 1;
this.buckets = new Entry[tableSize];
}
public final Entry get(long hashCode64) {
final int bucket = ((int) hashCode64) & indexMask;
for (Entry entry = buckets[bucket]; entry != null; entry = entry.next) {
if (hashCode64 == entry.hashCode64) {
return entry;
}
}
return null;
}
public boolean put(long hashCode64, String columnsString, List<SQLExpr> columns) {
final int bucket = ((int) hashCode64) & indexMask;
for (Entry entry = buckets[bucket]; entry != null; entry = entry.next) {
if (hashCode64 == entry.hashCode64) {
return true;
}
}
Entry entry = new Entry(hashCode64, columnsString, columns, buckets[bucket]);
buckets[bucket] = entry; // 并发是处理时会可能导致缓存丢失,但不影响正确性
return false;
}
public final static class Entry {
public final long hashCode64;
public final String columnsString;
public final List<SQLExpr> columns;
public final Entry next;
public Entry(long hashCode64, String columnsString, List<SQLExpr> columns, Entry next) {
this.hashCode64 = hashCode64;
this.columnsString = columnsString;
this.columns = columns;
this.next = next;
}
}
}
@@ -43,7 +43,7 @@ public class Lexer {
protected static SymbolTable symbols_l2 = new SymbolTable(512);
protected int features = 0; //SQLParserFeature.of(SQLParserFeature.EnableSQLBinaryOpExprGroup);
protected final String text;
public final String text;
protected int pos;
protected int mark;
@@ -211,6 +211,11 @@ public class Lexer {
this.reset(this.savePoint);
}
public void reset(int pos) {
this.pos = pos;
this.ch = charAt(pos);
}
public Lexer(String input, boolean skipComment){
this.skipComment = skipComment;
@@ -1244,12 +1244,17 @@ public class SQLExprParser extends SQLParser {
public SQLName name() {
String identName;
long hash = 0;
if (lexer.token == Token.LITERAL_ALIAS) {
identName = lexer.stringVal();
lexer.nextToken();
} else if (lexer.token == Token.IDENTIFIER) {
identName = lexer.stringVal();
char c0 = identName.charAt(0);
if (c0 != '[') {
hash = lexer.hash_lower();
}
lexer.nextToken();
} else if (lexer.token == Token.LITERAL_CHARS) {
identName = '\'' + lexer.stringVal() + '\'';
@@ -1312,7 +1317,7 @@ public class SQLExprParser extends SQLParser {
}
}
SQLName name = new SQLIdentifierExpr(identName);
SQLName name = new SQLIdentifierExpr(identName, hash);
name = nameRest(name);
@@ -17,6 +17,7 @@ package com.alibaba.druid.sql.parser;
public enum SQLParserFeature {
KeepInsertValueClauseOriginalString,
UseInsertColumnsCache,
EnableSQLBinaryOpExprGroup,
OptimizedForParameterized,
KeepComments,
@@ -34,6 +34,7 @@ public class SQLStatementParser extends SQLParser {
protected boolean parseCompleteValues = true;
protected int parseValuesSize = 3;
protected SQLSelectListCache selectListCache = null;
protected InsertColumnsCache insertColumnsCache = null;
public SQLStatementParser(String sql){
this(sql, null);