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:
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.ast.statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
@@ -271,14 +272,6 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
|
||||
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;
|
||||
return SQLUtils.normalize(alias);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,9 +19,14 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
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.dialect.mysql.ast.statement.MySqlTableIndex;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
|
||||
public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLStatement {
|
||||
|
||||
@@ -152,5 +157,60 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
|
||||
return !comments.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
public String computeName() {
|
||||
if (tableSource == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
SQLExpr expr = tableSource.getExpr();
|
||||
if (expr instanceof SQLName) {
|
||||
String name = ((SQLName) expr).getSimpleName();
|
||||
return SQLUtils.normalize(name);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public SQLColumnDefinition findColumn(String columName) {
|
||||
for (SQLTableElement element : tableElementList) {
|
||||
if (element instanceof SQLColumnDefinition) {
|
||||
SQLColumnDefinition column = (SQLColumnDefinition) element;
|
||||
String name = column.computeAlias();
|
||||
if (columName.equalsIgnoreCase(name)) {
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public SQLTableElement findKey(String columnName) {
|
||||
for (SQLTableElement element : tableElementList) {
|
||||
List<SQLExpr> keyColumns = null;
|
||||
if (element instanceof SQLUniqueConstraint) {
|
||||
SQLUniqueConstraint unique = (SQLUniqueConstraint) element;
|
||||
keyColumns = unique.getColumns();
|
||||
} else if (element instanceof MySqlTableIndex) {
|
||||
keyColumns = ((MySqlTableIndex) element).getColumns();
|
||||
}
|
||||
|
||||
if (keyColumns == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (SQLExpr columnExpr : keyColumns) {
|
||||
if (columnExpr instanceof SQLIdentifierExpr) {
|
||||
String keyColumName = ((SQLIdentifierExpr) columnExpr).getName();
|
||||
keyColumName = SQLUtils.normalize(keyColumName);
|
||||
if (keyColumName.equalsIgnoreCase(columnName)) {
|
||||
return element;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.ast.statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
@@ -117,15 +118,7 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
return SQLUtils.normalize(alias);
|
||||
}
|
||||
|
||||
public SQLExprTableSource clone() {
|
||||
@@ -135,6 +128,8 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
|
||||
}
|
||||
|
||||
public void cloneTo(SQLExprTableSource x) {
|
||||
x.alias = alias;
|
||||
|
||||
if (expr != null) {
|
||||
x.expr = expr.clone();
|
||||
}
|
||||
|
||||
@@ -165,4 +165,33 @@ public class SQLJoinTableSource extends SQLTableSourceImpl {
|
||||
return joinType.name;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void cloneTo(SQLJoinTableSource x) {
|
||||
x.alias = alias;
|
||||
|
||||
if (left != null) {
|
||||
x.setLeft(left.clone());
|
||||
}
|
||||
|
||||
x.joinType = joinType;
|
||||
|
||||
if (right != null) {
|
||||
x.setRight(right.clone());
|
||||
}
|
||||
|
||||
for (SQLExpr item : using) {
|
||||
SQLExpr item2 = item.clone();
|
||||
item2.setParent(x);
|
||||
x.using.add(item2);
|
||||
}
|
||||
|
||||
x.natural = natural;
|
||||
}
|
||||
|
||||
public SQLJoinTableSource clone() {
|
||||
SQLJoinTableSource x = new SQLJoinTableSource();
|
||||
cloneTo(x);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public class SQLSelect extends SQLObjectImpl {
|
||||
protected SQLObject restriction;
|
||||
|
||||
protected boolean forBrowse;
|
||||
protected List<String> forXmlOptions = new ArrayList<String>(4);
|
||||
protected List<String> forXmlOptions = null;
|
||||
protected SQLExpr xmlPath;
|
||||
|
||||
protected SQLExpr rowCount;
|
||||
@@ -148,7 +148,7 @@ public class SQLSelect extends SQLObjectImpl {
|
||||
x.withSubQuery = this.withSubQuery;
|
||||
x.query = this.query;
|
||||
if (orderBy != null) {
|
||||
x.setOrderBy(this.orderBy);
|
||||
x.setOrderBy(this.orderBy.clone());
|
||||
}
|
||||
if (restriction != null) {
|
||||
x.setRestriction(restriction.clone());
|
||||
@@ -160,9 +160,39 @@ public class SQLSelect extends SQLObjectImpl {
|
||||
}
|
||||
}
|
||||
|
||||
x.forBrowse = forBrowse;
|
||||
|
||||
if (forXmlOptions != null) {
|
||||
x.forXmlOptions = new ArrayList<String>(forXmlOptions);
|
||||
}
|
||||
|
||||
if (xmlPath != null) {
|
||||
x.setXmlPath(xmlPath.clone());
|
||||
}
|
||||
|
||||
if (rowCount != null) {
|
||||
x.setRowCount(rowCount.clone());
|
||||
}
|
||||
|
||||
if (offset != null) {
|
||||
x.setOffset(offset.clone());
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
public boolean isSimple() {
|
||||
return withSubQuery == null
|
||||
&& orderBy == null
|
||||
&& (hints == null || hints.size() == 0)
|
||||
&& restriction == null
|
||||
&& (!forBrowse)
|
||||
&& (forXmlOptions == null || forXmlOptions.size() == 0)
|
||||
&& xmlPath == null
|
||||
&& rowCount == null
|
||||
&& offset == null;
|
||||
}
|
||||
|
||||
public SQLObject getRestriction() {
|
||||
return this.restriction;
|
||||
}
|
||||
@@ -183,11 +213,18 @@ public class SQLSelect extends SQLObjectImpl {
|
||||
}
|
||||
|
||||
public List<String> getForXmlOptions() {
|
||||
if (forXmlOptions == null) {
|
||||
forXmlOptions = new ArrayList<String>(4);
|
||||
}
|
||||
|
||||
return forXmlOptions;
|
||||
}
|
||||
|
||||
public void setForXmlOptions(List<String> forXmlOptions) {
|
||||
this.forXmlOptions = forXmlOptions;
|
||||
public int getForXmlOptionsSize() {
|
||||
if (forXmlOptions == null) {
|
||||
return 0;
|
||||
}
|
||||
return forXmlOptions.size();
|
||||
}
|
||||
|
||||
public SQLExpr getRowCount() {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
@@ -75,15 +76,7 @@ public class SQLSelectItem extends SQLObjectImpl {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
return SQLUtils.normalize(alias);
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
|
||||
@@ -119,4 +119,15 @@ public class SQLSelectOrderByItem extends SQLObjectImpl {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
public SQLSelectOrderByItem clone() {
|
||||
SQLSelectOrderByItem x = new SQLSelectOrderByItem();
|
||||
if (expr != null) {
|
||||
x.setExpr(expr.clone());
|
||||
}
|
||||
x.collate = collate;
|
||||
x.type = type;
|
||||
x.nullsOrderType = nullsOrderType;
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,11 +318,11 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
|
||||
public SQLSelectQueryBlock clone() {
|
||||
SQLSelectQueryBlock x = new SQLSelectQueryBlock();
|
||||
clone(x);
|
||||
cloneTo(x);
|
||||
return x;
|
||||
}
|
||||
|
||||
public void clone(SQLSelectQueryBlock x) {
|
||||
public void cloneTo(SQLSelectQueryBlock x) {
|
||||
|
||||
x.distionOption = distionOption;
|
||||
|
||||
|
||||
@@ -69,6 +69,9 @@ public class SQLSubqueryTableSource extends SQLTableSourceImpl {
|
||||
|
||||
public SQLSubqueryTableSource clone() {
|
||||
SQLSubqueryTableSource x = new SQLSubqueryTableSource();
|
||||
|
||||
x.alias = alias;
|
||||
|
||||
if (select != null) {
|
||||
x.select = select.clone();
|
||||
}
|
||||
|
||||
@@ -29,4 +29,6 @@ public interface SQLTableSource extends SQLObject {
|
||||
List<SQLHint> getHints();
|
||||
|
||||
SQLTableSource clone();
|
||||
|
||||
String computeAlias();
|
||||
}
|
||||
|
||||
@@ -64,7 +64,11 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
|
||||
}
|
||||
|
||||
public SQLTableSource clone() {
|
||||
throw new UnsupportedOperationException();
|
||||
throw new UnsupportedOperationException(this.getClass().getName());
|
||||
}
|
||||
|
||||
public String computeAlias() {
|
||||
return alias;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user