refactor sql parser.

This commit is contained in:
wenshao
2017-08-28 01:26:21 +08:00
parent a983b2685b
commit 833a9b5a65
91 changed files with 2964 additions and 1368 deletions
@@ -1,5 +1,21 @@
/*
* 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.ast.expr;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -75,4 +91,8 @@ public class SQLBinaryOpExprGroup extends SQLExprImpl {
public SQLBinaryOperator getOperator() {
return operator;
}
public String toString() {
return SQLUtils.toSQLString(this, dbType);
}
}
@@ -68,9 +68,28 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
return lowerName;
}
public long hash_lower() {
if (hash_lower == 0) {
hash_lower = FNVUtils.fnv_64_lower(name);
public long name_hash_lower() {
if (hash_lower == 0
&& name != null) {
final int len = name.length();
boolean quote = false;
String name = this.name;
if (len > 2) {
char c0 = name.charAt(0);
char c1 = name.charAt(len - 1);
if ((c0 == '`' && c1 == '`')
|| (c0 == '"' && c1 == '"')
|| (c0 == '[' && c1 == ']')) {
quote = true;
}
}
if (quote) {
hash_lower = FNVUtils.fnv_64_lower(name, 1, len -1);
} else {
hash_lower = FNVUtils.fnv_64_lower(name);
}
}
return hash_lower;
}
@@ -200,7 +219,7 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
if (queryBlock == null) {
return null;
}
SQLSelectItem selectItem = queryBlock.findSelectItem(name);
SQLSelectItem selectItem = queryBlock.findSelectItem(name_hash_lower());
if (selectItem != null) {
return selectItem.computeDataType();
}
@@ -23,12 +23,15 @@ import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.FNVUtils;
public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
private SQLExpr owner;
private String name;
protected long name_hash_lower;
private transient SQLColumnDefinition resolvedColumn;
private transient SQLTableSource resolvedTableSource;
@@ -41,6 +44,12 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
this.name = name;
}
public SQLPropertyExpr(SQLExpr owner, String name, long name_hash_lower){
setOwner(owner);
this.name = name;
this.name_hash_lower = name_hash_lower;
}
public SQLPropertyExpr(){
}
@@ -153,6 +162,32 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
return false;
}
public long name_hash_lower() {
if (name_hash_lower == 0
&& name != null) {
final int len = name.length();
boolean quote = false;
String name = this.name;
if (len > 2) {
char c0 = name.charAt(0);
char c1 = name.charAt(len - 1);
if ((c0 == '`' && c1 == '`')
|| (c0 == '"' && c1 == '"')
|| (c0 == '[' && c1 == ']')) {
quote = true;
}
}
if (quote) {
name_hash_lower = FNVUtils.fnv_64_lower(name, 1, len -1);
} else {
name_hash_lower = FNVUtils.fnv_64_lower(name);
}
}
return name_hash_lower;
}
public String normalizedName() {
String ownerName;
@@ -195,7 +230,7 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
if (queryBlock == null) {
return null;
}
SQLSelectItem selectItem = queryBlock.findSelectItem(name);
SQLSelectItem selectItem = queryBlock.findSelectItem(name_hash_lower());
if (selectItem != null) {
return selectItem.computeDataType();
}