improved sql parser.

This commit is contained in:
wenshao
2018-04-07 22:42:19 +08:00
parent 542e132272
commit 22353f3a20
18 changed files with 940 additions and 374 deletions
@@ -58,6 +58,7 @@ public enum SQLBinaryOperator {
LessThanOrGreater("<>", 110),
Like("LIKE", 110),
SoudsLike("SOUNDS LIKE", 110),
NotLike("NOT LIKE", 110),
ILike("ILIKE", 110),
@@ -15,17 +15,14 @@
*/
package com.alibaba.druid.sql.ast.expr;
import com.alibaba.druid.sql.ast.SQLDataType;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.ast.SQLObjectWithDataType;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class SQLCastExpr extends SQLExprImpl implements SQLObjectWithDataType {
public class SQLCastExpr extends SQLExprImpl implements SQLObjectWithDataType, SQLReplaceable {
protected SQLExpr expr;
protected SQLDataType dataType;
@@ -64,6 +61,16 @@ public class SQLCastExpr extends SQLExprImpl implements SQLObjectWithDataType {
visitor.endVisit(this);
}
@Override
public boolean replace(SQLExpr expr, SQLExpr target) {
if (this.expr == expr) {
setExpr(target);
return true;
}
return false;
}
@Override
public List getChildren() {
return Arrays.asList(this.expr, this.dataType);
@@ -0,0 +1,168 @@
/*
* Copyright 1999-2018 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.ast.*;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public final class SQLContainsExpr extends SQLExprImpl implements SQLReplaceable, Serializable {
private static final long serialVersionUID = 1L;
private boolean not = false;
private SQLExpr expr;
private List<SQLExpr> targetList = new ArrayList<SQLExpr>();
public SQLContainsExpr() {
}
public SQLContainsExpr(SQLExpr expr) {
this.setExpr(expr);
}
public SQLContainsExpr(SQLExpr expr, boolean not) {
this.setExpr(expr);
this.not = not;
}
public SQLContainsExpr clone() {
SQLContainsExpr x = new SQLContainsExpr();
x.not = not;
if (expr != null) {
x.setExpr(expr.clone());
}
for (SQLExpr e : targetList) {
SQLExpr e2 = e.clone();
e2.setParent(x);
x.targetList.add(e2);
}
return x;
}
public boolean isNot() {
return this.not;
}
public void setNot(boolean not) {
this.not = not;
}
public SQLExpr getExpr() {
return this.expr;
}
public void setExpr(SQLExpr expr) {
if (expr != null) {
expr.setParent(this);
}
this.expr = expr;
}
public List<SQLExpr> getTargetList() {
return this.targetList;
}
public void setTargetList(List<SQLExpr> targetList) {
this.targetList = targetList;
}
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.expr);
acceptChild(visitor, this.targetList);
}
visitor.endVisit(this);
}
public List<SQLObject> getChildren() {
List<SQLObject> children = new ArrayList<SQLObject>();
if (this.expr != null) {
children.add(this.expr);
}
children.addAll(this.targetList);
return children;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((expr == null) ? 0 : expr.hashCode());
result = prime * result + (not ? 1231 : 1237);
result = prime * result + ((targetList == null) ? 0 : targetList.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
SQLContainsExpr other = (SQLContainsExpr) obj;
if (expr == null) {
if (other.expr != null) {
return false;
}
} else if (!expr.equals(other.expr)) {
return false;
}
if (not != other.not) {
return false;
}
if (targetList == null) {
if (other.targetList != null) {
return false;
}
} else if (!targetList.equals(other.targetList)) {
return false;
}
return true;
}
public SQLDataType computeDataType() {
return SQLBooleanExpr.DEFAULT_DATA_TYPE;
}
@Override
public boolean replace(SQLExpr expr, SQLExpr target) {
if (this.expr == expr) {
setExpr(target);
return true;
}
for (int i = 0; i < targetList.size(); i++) {
if (targetList.get(i) == expr) {
targetList.set(i, target);
target.setParent(this);
return true;
}
}
return false;
}
}
@@ -0,0 +1,104 @@
/*
* Copyright 1999-2018 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.ast.SQLObject;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.util.Collections;
import java.util.List;
public class SQLRealExpr extends SQLNumericLiteralExpr implements SQLValuableExpr {
private Float value;
public SQLRealExpr(){
}
public SQLRealExpr(Float value){
super();
this.value = value;
}
public SQLRealExpr clone() {
return new SQLRealExpr(value);
}
@Override
public List<SQLObject> getChildren() {
return Collections.emptyList();
}
@Override
public Number getNumber() {
return value;
}
public Float getValue() {
return value;
}
public void setValue(Float value) {
this.value = value;
}
protected void accept0(SQLASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
SQLRealExpr other = (SQLRealExpr) obj;
if (value == null) {
if (other.value != null) {
return false;
}
} else if (!value.equals(other.value)) {
return false;
}
return true;
}
@Override
public void setNumber(Number number) {
if (number == null) {
this.setValue(null);
return;
}
this.setValue(number.floatValue());
}
}
@@ -163,7 +163,7 @@ public class SQLSelectItem extends SQLObjectImpl implements SQLReplaceable {
@Override
public boolean replace(SQLExpr expr, SQLExpr target) {
if (expr == expr) {
if (this.expr == expr) {
setExpr(target);
return true;
}