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:
@@ -17,6 +17,8 @@ package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLCommentHint;
|
||||
import com.alibaba.druid.sql.ast.SQLDataTypeImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
import java.util.List;
|
||||
@@ -78,6 +80,17 @@ public class SQLCharacterDataType extends SQLDataTypeImpl {
|
||||
this.hints = hints;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
if (this.arguments.size() == 1) {
|
||||
SQLExpr arg = this.arguments.get(0);
|
||||
if (arg instanceof SQLIntegerExpr) {
|
||||
return ((SQLIntegerExpr) arg).getNumber().intValue();
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.HashMap;
|
||||
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;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
@@ -27,6 +28,7 @@ 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;
|
||||
import com.alibaba.druid.util.lang.Consumer;
|
||||
|
||||
public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLStatement {
|
||||
|
||||
@@ -213,4 +215,16 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void forEachColumn(Consumer<SQLColumnDefinition> columnConsumer) {
|
||||
if (columnConsumer == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (SQLTableElement element : this.tableElementList) {
|
||||
if (element instanceof SQLColumnDefinition) {
|
||||
columnConsumer.accept((SQLColumnDefinition) element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,4 +157,28 @@ public class SQLSelectItem extends SQLObjectImpl implements SQLReplaceable {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean match(String alias) {
|
||||
if (alias == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String alias_normalized = SQLUtils.normalize(alias);
|
||||
|
||||
if (alias_normalized.equalsIgnoreCase(this.alias)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
String ident = ((SQLIdentifierExpr) expr).getName();
|
||||
return alias_normalized.equalsIgnoreCase(SQLUtils.normalize(ident));
|
||||
}
|
||||
|
||||
if (expr instanceof SQLPropertyExpr) {
|
||||
String ident = ((SQLPropertyExpr) expr).getName();
|
||||
return alias_normalized.equalsIgnoreCase(SQLUtils.normalize(ident));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,9 @@ 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.*;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery, SQLReplaceable {
|
||||
@@ -421,4 +423,20 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public SQLSelectItem findSelectItem(String ident) {
|
||||
if (ident == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String ident_normalized = SQLUtils.normalize(ident);
|
||||
|
||||
for (SQLSelectItem item : this.selectList) {
|
||||
if (item.match(ident_normalized)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -982,7 +982,7 @@ public class OracleExprParser extends SQLExprParser {
|
||||
rightExp = equalityRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp, getDbType());
|
||||
} else if (lexer.token() == Token.BANGEQ) {
|
||||
} else if (lexer.token() == Token.BANGEQ || lexer.token() == Token.CARETEQ) {
|
||||
lexer.nextToken();
|
||||
rightExp = shift();
|
||||
|
||||
|
||||
@@ -582,7 +582,12 @@ public class Lexer {
|
||||
break;
|
||||
case '^':
|
||||
scanChar();
|
||||
token = Token.CARET;
|
||||
if (ch == '=') {
|
||||
scanChar();
|
||||
token = Token.CARETEQ;
|
||||
} else {
|
||||
token = Token.CARET;
|
||||
}
|
||||
break;
|
||||
case '%':
|
||||
scanChar();
|
||||
|
||||
@@ -334,7 +334,8 @@ public enum Token {
|
||||
SLASH("/"),
|
||||
AMP("&"),
|
||||
BAR("|"),
|
||||
CARET("^"),
|
||||
CARET("^"),
|
||||
CARETEQ("^="),
|
||||
PERCENT("%"),
|
||||
LTLT("<<"),
|
||||
GTGT(">>"),
|
||||
|
||||
Reference in New Issue
Block a user