mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -31,6 +31,11 @@ public class SQLLimit extends SQLObjectImpl {
|
||||
this.setRowCount(rowCount);
|
||||
}
|
||||
|
||||
public SQLLimit(SQLExpr offset, SQLExpr rowCount) {
|
||||
this.setOffset(offset);
|
||||
this.setRowCount(rowCount);
|
||||
}
|
||||
|
||||
private SQLExpr rowCount;
|
||||
private SQLExpr offset;
|
||||
|
||||
|
||||
@@ -145,6 +145,10 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements Serializable {
|
||||
return true;
|
||||
}
|
||||
|
||||
public SQLBinaryOpExpr clone() {
|
||||
return new SQLBinaryOpExpr(left, operator, right, dbType);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return SQLUtils.toSQLString(this, getDbType());
|
||||
}
|
||||
|
||||
@@ -82,4 +82,8 @@ public class SQLIntegerExpr extends SQLNumericLiteralExpr implements SQLValuable
|
||||
public Object getValue() {
|
||||
return this.number;
|
||||
}
|
||||
|
||||
public SQLIntegerExpr clone() {
|
||||
return new SQLIntegerExpr(this.number);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,4 +104,7 @@ public class SQLVariantRefExpr extends SQLExprImpl {
|
||||
this.global = global;
|
||||
}
|
||||
|
||||
public SQLVariantRefExpr clone() {
|
||||
return new SQLVariantRefExpr(name, global);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,15 +21,29 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLAlterTableRename extends SQLObjectImpl implements SQLAlterTableItem {
|
||||
|
||||
protected SQLExpr to;
|
||||
protected SQLExprTableSource to;
|
||||
|
||||
public SQLExpr getTo() {
|
||||
public SQLAlterTableRename() {
|
||||
|
||||
}
|
||||
|
||||
public SQLAlterTableRename(SQLExpr to) {
|
||||
this.setTo(to);
|
||||
}
|
||||
|
||||
public SQLExprTableSource getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public void setTo(SQLExpr to) {
|
||||
public void setTo(SQLExprTableSource to) {
|
||||
if (to != null) {
|
||||
to.setParent(this);
|
||||
}
|
||||
this.to = to;
|
||||
to.setParent(this);
|
||||
}
|
||||
|
||||
public void setTo(SQLExpr to) {
|
||||
this.setTo(new SQLExprTableSource(to));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,9 +20,12 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLAlterTableStatement extends SQLStatementImpl implements SQLDDLStatement {
|
||||
@@ -118,6 +121,10 @@ public class SQLAlterTableStatement extends SQLStatementImpl implements SQLDDLSt
|
||||
this.tableSource = tableSource;
|
||||
}
|
||||
|
||||
public void setTableSource(SQLExpr table) {
|
||||
this.setTableSource(new SQLExprTableSource(table));
|
||||
}
|
||||
|
||||
public SQLName getName() {
|
||||
if (getTableSource() == null) {
|
||||
return null;
|
||||
@@ -141,4 +148,18 @@ public class SQLAlterTableStatement extends SQLStatementImpl implements SQLDDLSt
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public String getTableName() {
|
||||
if (tableSource == null) {
|
||||
return null;
|
||||
}
|
||||
SQLExpr expr = ((SQLExprTableSource) tableSource).getExpr();
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
return ((SQLIdentifierExpr) expr).getName();
|
||||
} else if (expr instanceof SQLPropertyExpr) {
|
||||
return ((SQLPropertyExpr) expr).getName();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
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;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElement {
|
||||
@@ -259,4 +261,24 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String computeAlias() {
|
||||
String alias = null;
|
||||
|
||||
if (name instanceof SQLIdentifierExpr) {
|
||||
alias = ((SQLIdentifierExpr) name).getName();
|
||||
} else if (name instanceof SQLPropertyExpr) {
|
||||
alias = ((SQLPropertyExpr) name).getName();
|
||||
}
|
||||
|
||||
if (alias.length() > 2) {
|
||||
char c0 = alias.charAt(0);
|
||||
char x0 = alias.charAt(alias.length() - 1);
|
||||
if ((c0 == '"' && x0 == '"') || (c0 == '`' && x0 == '`')) {
|
||||
alias = alias.substring(1, alias.length() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return alias;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,11 @@ package com.alibaba.druid.sql.ast.statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLCreateIndexStatement extends SQLStatementImpl implements SQLDDLStatement {
|
||||
@@ -55,6 +58,19 @@ public class SQLCreateIndexStatement extends SQLStatementImpl implements SQLDDLS
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
public String getTableName() {
|
||||
if (table instanceof SQLExprTableSource) {
|
||||
SQLExpr expr = ((SQLExprTableSource) table).getExpr();
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
return ((SQLIdentifierExpr) expr).getName();
|
||||
} else if (expr instanceof SQLPropertyExpr) {
|
||||
return ((SQLPropertyExpr) expr).getName();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<SQLSelectOrderByItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
public class SQLDropSequenceStatement extends SQLStatementImpl implements SQLDDLStatement {
|
||||
|
||||
private SQLName name;
|
||||
private boolean ifExists;
|
||||
|
||||
public SQLDropSequenceStatement() {
|
||||
|
||||
@@ -47,4 +48,11 @@ public class SQLDropSequenceStatement extends SQLStatementImpl implements SQLDDL
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public boolean isIfExists() {
|
||||
return ifExists;
|
||||
}
|
||||
|
||||
public void setIfExists(boolean ifExists) {
|
||||
this.ifExists = ifExists;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
public class SQLDropTriggerStatement extends SQLStatementImpl implements SQLDDLStatement {
|
||||
|
||||
private SQLName name;
|
||||
|
||||
private boolean ifExists;
|
||||
|
||||
public SQLDropTriggerStatement() {
|
||||
|
||||
}
|
||||
@@ -47,4 +48,11 @@ public class SQLDropTriggerStatement extends SQLStatementImpl implements SQLDDLS
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public boolean isIfExists() {
|
||||
return ifExists;
|
||||
}
|
||||
|
||||
public void setIfExists(boolean ifExists) {
|
||||
this.ifExists = ifExists;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,9 @@ public class SQLIfStatement extends SQLStatementImpl {
|
||||
}
|
||||
|
||||
public void setCondition(SQLExpr condition) {
|
||||
if (condition != null) {
|
||||
condition.setParent(this);
|
||||
}
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ public class SQLLoopStatement extends SQLStatementImpl {
|
||||
|
||||
private String labelName;
|
||||
|
||||
private List<SQLStatement> statements = new ArrayList<SQLStatement>();
|
||||
private final List<SQLStatement> statements = new ArrayList<SQLStatement>();
|
||||
|
||||
@Override
|
||||
public void accept0(SQLASTVisitor visitor) {
|
||||
@@ -40,10 +40,6 @@ public class SQLLoopStatement extends SQLStatementImpl {
|
||||
return statements;
|
||||
}
|
||||
|
||||
public void setStatements(List<SQLStatement> statements) {
|
||||
this.statements = statements;
|
||||
}
|
||||
|
||||
public String getLabelName() {
|
||||
return labelName;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLSelectItem extends SQLObjectImpl {
|
||||
@@ -63,6 +65,27 @@ public class SQLSelectItem extends SQLObjectImpl {
|
||||
}
|
||||
}
|
||||
|
||||
public String computeAlias() {
|
||||
String alias = this.getAlias();
|
||||
if (alias == null) {
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
alias = ((SQLIdentifierExpr) expr).getName();
|
||||
} else if (expr instanceof SQLPropertyExpr) {
|
||||
alias = ((SQLPropertyExpr) expr).getName();
|
||||
}
|
||||
}
|
||||
|
||||
if (alias.length() > 2) {
|
||||
char c0 = alias.charAt(0);
|
||||
char x0 = alias.charAt(alias.length() - 1);
|
||||
if ((c0 == '"' && x0 == '"') || (c0 == '`' && x0 == '`')) {
|
||||
alias = alias.substring(1, alias.length() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return alias;
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return this.alias;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user