improved sql parser.

This commit is contained in:
wenshao
2017-08-24 10:40:09 +08:00
parent 0c46c2a165
commit 026295ddbd
41 changed files with 565 additions and 186 deletions
@@ -23,7 +23,7 @@ import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLAggregateExpr extends SQLExprImpl implements Serializable {
public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLReplaceable {
private static final long serialVersionUID = 1L;
protected String methodName;
@@ -223,4 +223,18 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable {
return null;
}
public boolean replace(SQLExpr expr, SQLExpr target) {
if (target == null) {
return false;
}
for (int i = 0; i < arguments.size(); ++i) {
if (arguments.get(i) == expr) {
arguments.set(i, target);
target.setParent(this);
return true;
}
}
return false;
}
}
@@ -16,9 +16,11 @@
package com.alibaba.druid.sql.ast.expr;
import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLAllColumnExpr extends SQLExprImpl {
private transient SQLTableSource resolvedTableSource;
public SQLAllColumnExpr(){
@@ -42,6 +44,17 @@ public class SQLAllColumnExpr extends SQLExprImpl {
}
public SQLAllColumnExpr clone() {
return new SQLAllColumnExpr();
SQLAllColumnExpr x = new SQLAllColumnExpr();
x.resolvedTableSource = resolvedTableSource;
return x;
}
public SQLTableSource getResolvedTableSource() {
return resolvedTableSource;
}
public void setResolvedTableSource(SQLTableSource resolvedTableSource) {
this.resolvedTableSource = resolvedTableSource;
}
}
@@ -18,7 +18,7 @@ package com.alibaba.druid.sql.ast.expr;
import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLBinaryExpr extends SQLExprImpl implements SQLLiteralExpr {
public class SQLBinaryExpr extends SQLExprImpl implements SQLLiteralExpr, SQLValuableExpr {
private String value;
@@ -21,7 +21,7 @@ import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.ast.statement.SQLCharacterDataType;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLBooleanExpr extends SQLExprImpl implements SQLExpr, SQLLiteralExpr {
public class SQLBooleanExpr extends SQLExprImpl implements SQLExpr, SQLLiteralExpr, SQLValuableExpr {
public static final SQLDataType DEFAULT_DATA_TYPE = new SQLCharacterDataType("boolean");
private boolean value;
@@ -34,7 +34,11 @@ public class SQLBooleanExpr extends SQLExprImpl implements SQLExpr, SQLLiteralEx
this.value = value;
}
public boolean getValue() {
public boolean getBooleanValue() {
return value;
}
public Boolean getValue() {
return value;
}
@@ -19,6 +19,7 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -270,4 +271,8 @@ public class SQLCaseExpr extends SQLExprImpl implements SQLReplaceable, Serializ
return null;
}
public String toString() {
return SQLUtils.toSQLString(this, null);
}
}
@@ -22,7 +22,7 @@ import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleExpr;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLDateExpr extends SQLExprImpl implements SQLLiteralExpr {
public class SQLDateExpr extends SQLExprImpl implements SQLLiteralExpr, SQLValuableExpr {
public static final SQLDataType DEFAULT_DATA_TYPE = new SQLCharacterDataType("date");
private String literal;
@@ -43,6 +43,10 @@ public class SQLDateExpr extends SQLExprImpl implements SQLLiteralExpr {
this.literal = literal;
}
public String getValue() {
return literal;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
visitor.visit(this);
@@ -19,7 +19,7 @@ import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.HexBin;
public class SQLHexExpr extends SQLExprImpl implements SQLLiteralExpr {
public class SQLHexExpr extends SQLExprImpl implements SQLLiteralExpr, SQLValuableExpr {
private final String hex;
@@ -84,4 +84,8 @@ public class SQLHexExpr extends SQLExprImpl implements SQLLiteralExpr {
public SQLHexExpr clone () {
return new SQLHexExpr(hex);
}
public byte[] getValue() {
return toBytes();
}
}
@@ -135,7 +135,10 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
}
public SQLIdentifierExpr clone() {
return new SQLIdentifierExpr(this.name);
SQLIdentifierExpr x = new SQLIdentifierExpr(this.name);
x.resolvedColumn = resolvedColumn;
x.resolvedTableSource = resolvedTableSource;
return x;
}
public String normalizedName() {
@@ -35,6 +35,7 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
private final List<SQLExpr> parameters = new ArrayList<SQLExpr>();
private SQLExpr from;
private SQLExpr using;
public SQLMethodInvokeExpr(){
@@ -173,6 +174,10 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
x.setFrom(from.clone());
}
if (using != null) {
x.setUsing(using.clone());
}
return x;
}
@@ -216,6 +221,11 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
}
public SQLDataType computeDataType() {
if (SQLUtils.nameEquals("to_date", methodName)
|| SQLUtils.nameEquals("add_months", methodName)) {
return SQLDateExpr.DEFAULT_DATA_TYPE;
}
if (parameters.size() == 1) {
if (SQLUtils.nameEquals("trunc", methodName)) {
return parameters.get(0).computeDataType();
@@ -234,4 +244,15 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
}
return null;
}
public SQLExpr getUsing() {
return using;
}
public void setUsing(SQLExpr using) {
if (using != null) {
using.setParent(this);
}
this.using = using;
}
}
@@ -22,7 +22,7 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.io.IOException;
import java.math.BigDecimal;
public class SQLNumberExpr extends SQLNumericLiteralExpr {
public class SQLNumberExpr extends SQLNumericLiteralExpr implements SQLValuableExpr {
private static SQLDataType defaultDataType = new SQLDataTypeImpl("number");
private Number number;
@@ -48,6 +48,10 @@ public class SQLNumberExpr extends SQLNumericLiteralExpr {
return this.number;
}
public Number getValue() {
return getNumber();
}
public void setNumber(Number number) {
this.number = number;
this.chars = null;
@@ -133,12 +133,16 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
}
public SQLPropertyExpr clone() {
SQLPropertyExpr propertyExpr = new SQLPropertyExpr();
propertyExpr.name = this.name;
SQLPropertyExpr x = new SQLPropertyExpr();
x.name = this.name;
if (owner != null) {
propertyExpr.setOwner(owner.clone());
x.setOwner(owner.clone());
}
return propertyExpr;
x.resolvedColumn = resolvedColumn;
x.resolvedTableSource = resolvedTableSource;
return x;
}
public boolean matchOwner(String alias) {
@@ -21,7 +21,7 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class SQLTimestampExpr extends SQLExprImpl {
public class SQLTimestampExpr extends SQLExprImpl implements SQLValuableExpr {
public static final SQLDataType DEFAULT_DATA_TYPE = new SQLCharacterDataType("datetime");
protected String literal;
@@ -40,6 +40,10 @@ public class SQLTimestampExpr extends SQLExprImpl {
return x;
}
public String getValue() {
return literal;
}
public String getLiteral() {
return literal;
}