improved sql parser.

This commit is contained in:
wenshao
2017-08-31 11:20:04 +08:00
parent ce2daf6b9a
commit 947059f715
36 changed files with 1291 additions and 670 deletions
@@ -16,7 +16,6 @@
package com.alibaba.druid.sql.ast;
public interface SQLExpr extends SQLObject, Cloneable {
SQLExpr clone();
SQLExpr clone();
SQLDataType computeDataType();
}
@@ -21,37 +21,24 @@ import java.util.Map;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public interface SQLObject {
void accept(SQLASTVisitor visitor);
SQLObject clone();
void accept(SQLASTVisitor visitor);
SQLObject getParent();
void setParent(SQLObject parent);
SQLObject getParent();
void setParent(SQLObject parent);
Map<String, Object> getAttributes();
Object getAttribute(String name);
void putAttribute(String name, Object value);
Object getAttribute(String name);
void putAttribute(String name, Object value);
Map<String, Object> getAttributesDirect();
void addBeforeComment(String comment);
void addBeforeComment(List<String> comments);
List<String> getBeforeCommentsDirect();
void addAfterComment(String comment);
void addAfterComment(List<String> comments);
List<String> getAfterCommentsDirect();
boolean hasBeforeComment();
boolean hasAfterComment();
void output(StringBuffer buf);
void output(StringBuffer buf);
SQLObject clone();
void addBeforeComment(String comment);
void addBeforeComment(List<String> comments);
List<String> getBeforeCommentsDirect();
void addAfterComment(String comment);
void addAfterComment(List<String> comments);
List<String> getAfterCommentsDirect();
boolean hasBeforeComment();
boolean hasAfterComment();
}
@@ -16,11 +16,8 @@
package com.alibaba.druid.sql.ast;
public interface SQLStatement extends SQLObject {
String getDbType();
boolean isAfterSemi();
void setAfterSemi(boolean afterSemi);
String getDbType();
boolean isAfterSemi();
void setAfterSemi(boolean afterSemi);
SQLStatement clone();
}
@@ -21,11 +21,8 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.util.List;
public abstract class SQLStatementImpl extends SQLObjectImpl implements SQLStatement {
protected String dbType;
private boolean afterSemi;
protected String dbType;
protected boolean afterSemi;
protected List<SQLCommentHint> headHints;
public SQLStatementImpl(){
@@ -18,12 +18,9 @@ package com.alibaba.druid.sql.ast;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLSubPartition extends SQLObjectImpl {
protected SQLName name;
protected SQLPartitionValue values;
private SQLName tableSpace;
protected SQLName tableSpace;
public SQLName getName() {
return name;
@@ -22,11 +22,9 @@ import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
public abstract class SQLSubPartitionBy extends SQLObjectImpl {
protected SQLExpr subPartitionsCount;
protected boolean linear;
protected List<SQLAssignItem> options = new ArrayList<SQLAssignItem>();
protected SQLExpr subPartitionsCount;
protected boolean linear;
protected List<SQLAssignItem> options = new ArrayList<SQLAssignItem>();
protected List<SQLSubPartition> subPartitionTemplate = new ArrayList<SQLSubPartition>();
public SQLExpr getSubPartitionsCount() {
@@ -22,7 +22,7 @@ import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.FnvConstants;
import com.alibaba.druid.util.FnvHash.Constants;
import com.alibaba.druid.util.FnvHash;
public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLReplaceable {
@@ -220,8 +220,8 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
public SQLDataType computeDataType() {
long hash = methodNameHashCod64();
if (hash == FnvConstants.COUNT
|| hash == FnvConstants.ROW_NUMBER) {
if (hash == FnvHash.Constants.COUNT
|| hash == FnvHash.Constants.ROW_NUMBER) {
return SQLIntegerExpr.DEFAULT_DATA_TYPE;
}
@@ -232,8 +232,8 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
}
}
if (hash == FnvConstants.WM_CONAT
|| hash == FnvConstants.GROUP_CONCAT) {
if (hash == FnvHash.Constants.WM_CONAT
|| hash == FnvHash.Constants.GROUP_CONCAT) {
return SQLCharExpr.DEFAULT_DATA_TYPE;
}
@@ -109,6 +109,14 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
return x;
}
public SQLIdentifierExpr simplify() {
String normalized = SQLUtils.normalize(name);
if (normalized != name) {
return new SQLIdentifierExpr(name, hashCode64);
}
return this;
}
public String normalizedName() {
return SQLUtils.normalize(name);
}
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.ast.expr;
import com.alibaba.druid.sql.ast.SQLDataType;
import com.alibaba.druid.sql.ast.SQLDataTypeImpl;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.Utils;
import java.io.IOException;
import java.math.BigDecimal;
@@ -80,13 +81,12 @@ public class SQLNumberExpr extends SQLNumericLiteralExpr implements SQLValuableE
@Override
public int hashCode() {
if (chars != null && number == null) {
this.number = new BigDecimal(chars);
Number number = getNumber();
if (number == null) {
return 0;
}
final int prime = 31;
int result = 1;
result = prime * result + ((number == null) ? 0 : number.hashCode());
return result;
return number.hashCode();
}
@Override
@@ -104,19 +104,9 @@ public class SQLNumberExpr extends SQLNumericLiteralExpr implements SQLValuableE
if (getClass() != obj.getClass()) {
return false;
}
SQLNumberExpr other = (SQLNumberExpr) obj;
if (other.chars != null && other.number == null) {
other.number = new BigDecimal(other.chars);
}
if (number == null) {
if (other.number != null) {
return false;
}
} else if (!number.equals(other.number)) {
return false;
}
return true;
SQLNumberExpr other = (SQLNumberExpr) obj;
return Utils.equals(getNumber(), other.getNumber());
}
public SQLNumberExpr clone() {
@@ -94,6 +94,14 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
return name;
}
public long nameHashCode64() {
if (name == null) {
return 0;
}
return name.hashCode64();
}
public String getNameAsString() {
if (name == null) {
return null;
@@ -379,8 +387,10 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
if (this.name instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) this.name;
String columnName = identExpr.getName();
columnName = SQLUtils.normalize(columnName, dbType);
this.setName(columnName);
String normalized = SQLUtils.normalize(columnName, dbType);
if (normalized != columnName) {
this.setName(normalized);
}
}
}
@@ -110,8 +110,11 @@ public abstract class SQLConstraintImpl extends SQLObjectImpl implements SQLCons
if (this.name instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) this.name;
String columnName = identExpr.getName();
columnName = SQLUtils.normalize(columnName, dbType);
this.setName(columnName);
String normalized = SQLUtils.normalize(columnName, dbType);
if (columnName != normalized) {
this.setName(normalized);
}
}
}
}
@@ -573,15 +573,21 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
if (name instanceof SQLPropertyExpr) {
String tableName = ((SQLPropertyExpr) name).getName();
tableName = SQLUtils.normalize(tableName);
setName(tableName);
name = getName();
String normalized = SQLUtils.normalize(tableName, dbType);
if (tableName != normalized) {
this.setName(normalized);
name = getName();
}
}
if (name instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) name;
String tableName = identExpr.getName();
tableName = SQLUtils.normalize(tableName, dbType);
setName(tableName);
String normalized = SQLUtils.normalize(tableName, dbType);
if (normalized != tableName) {
setName(normalized);
}
}
for (SQLTableElement element : this.tableElementList) {
@@ -97,8 +97,10 @@ public class SQLUnique extends SQLConstraintImpl implements SQLUniqueConstraint,
if (column instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) column;
String columnName = identExpr.getName();
columnName = SQLUtils.normalize(columnName, dbType);
item.setExpr(new SQLIdentifierExpr(columnName));
String normalized = SQLUtils.normalize(columnName, dbType);
if (normalized != columnName) {
item.setExpr(new SQLIdentifierExpr(columnName));
}
}
}
}
@@ -16,6 +16,7 @@
package com.alibaba.druid.sql.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
@@ -70,4 +71,12 @@ public class SQLUpdateSetItem extends SQLObjectImpl {
}
return false;
}
public boolean columnMatch(long columnHash) {
if (this.column instanceof SQLName) {
return ((SQLName) this.column).nameHashCode64() == columnHash;
}
return false;
}
}