use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class MySqlSelectIntoParser method parseIndexHint.
private void parseIndexHint(MySqlIndexHintImpl hint) {
if (lexer.token() == Token.INDEX) {
lexer.nextToken();
} else {
accept(Token.KEY);
}
if (lexer.token() == Token.FOR) {
lexer.nextToken();
if (lexer.token() == Token.JOIN) {
lexer.nextToken();
hint.setOption(MySqlIndexHint.Option.JOIN);
} else if (lexer.token() == Token.ORDER) {
lexer.nextToken();
accept(Token.BY);
hint.setOption(MySqlIndexHint.Option.ORDER_BY);
} else {
accept(Token.GROUP);
accept(Token.BY);
hint.setOption(MySqlIndexHint.Option.GROUP_BY);
}
}
accept(Token.LPAREN);
if (lexer.token() == Token.PRIMARY) {
lexer.nextToken();
hint.getIndexList().add(new SQLIdentifierExpr("PRIMARY"));
} else {
this.exprParser.names(hint.getIndexList());
}
accept(Token.RPAREN);
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class MySqlSelectIntoParser method parseIntoArgs.
/**
* parser the select into arguments
* @return
*/
protected List<SQLExpr> parseIntoArgs() {
List<SQLExpr> args = new ArrayList<SQLExpr>();
if (lexer.token() == (Token.INTO)) {
accept(Token.INTO);
// lexer.nextToken();
for (; ; ) {
SQLExpr var = exprParser.primary();
if (var instanceof SQLIdentifierExpr) {
var = new SQLVariantRefExpr(((SQLIdentifierExpr) var).getName());
}
args.add(var);
if (lexer.token() == Token.COMMA) {
accept(Token.COMMA);
continue;
} else {
break;
}
}
}
return args;
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class FromSubqueryResolver method visit.
public boolean visit(SQLExprTableSource x) {
SQLExpr expr = x.getExpr();
if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
String ident = identifierExpr.getName();
String mappingIdent = mappings.get(ident);
if (mappingIdent != null) {
x.setExpr(new SQLIdentifierExpr(mappingIdent));
}
}
return false;
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class OdpsSelectParser method query.
@Override
public SQLSelectQuery query(SQLObject parent, boolean acceptUnion) {
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLSelectQuery select = query();
accept(Token.RPAREN);
return queryRest(select, acceptUnion);
}
OdpsSelectQueryBlock queryBlock = new OdpsSelectQueryBlock();
if (lexer.hasComment() && lexer.isKeepComments()) {
queryBlock.addBeforeComment(lexer.readAndResetComments());
}
if (lexer.token() == Token.FROM) {
parseFrom(queryBlock);
parseWhere(queryBlock);
parseGroupBy(queryBlock);
if (lexer.token() == Token.SELECT) {
lexer.nextToken();
if (lexer.token() == Token.HINT) {
this.exprParser.parseHints(queryBlock.getHints());
}
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
}
if (lexer.token() == Token.DISTINCT) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
} else if (lexer.token() == Token.UNIQUE) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
if (lexer.token() == Token.DOT) {
lexer.reset(mark);
} else {
queryBlock.setDistionOption(SQLSetQuantifier.UNIQUE);
}
} else if (lexer.token() == Token.ALL) {
String str = lexer.stringVal();
lexer.nextToken();
if (lexer.token() == Token.DOT) {
}
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
}
parseSelectList(queryBlock);
}
if (queryBlock.getWhere() == null && lexer.token() == Token.WHERE) {
parseWhere(queryBlock);
}
} else {
accept(Token.SELECT);
if (lexer.token() == Token.HINT) {
this.exprParser.parseHints(queryBlock.getHints());
}
if (lexer.token() == Token.COMMENT) {
Lexer.SavePoint mark = lexer.mark();
String tokenStr = lexer.stringVal();
lexer.nextToken();
if (lexer.token() == Token.COMMA) {
SQLIdentifierExpr expr = new SQLIdentifierExpr(tokenStr);
queryBlock.addSelectItem(expr);
lexer.nextToken();
} else {
lexer.reset(mark);
}
}
if (queryBlock.getSelectList().isEmpty()) {
if (lexer.token() == Token.DISTINCT) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
} else if (lexer.token() == Token.UNIQUE) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
if (lexer.token() == Token.DOT || lexer.token() == Token.COMMA) {
lexer.reset(mark);
} else {
queryBlock.setDistionOption(SQLSetQuantifier.UNIQUE);
}
} else if (lexer.token() == Token.ALL) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
switch(lexer.token()) {
case DOT:
case COMMA:
case SUB:
case PLUS:
case SLASH:
case GT:
case GTEQ:
case EQ:
case LT:
case LTEQ:
lexer.reset(mark);
break;
default:
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
break;
}
}
}
parseSelectList(queryBlock);
parseFrom(queryBlock);
if (queryBlock.getFrom() == null && lexer.token() == Token.LATERAL) {
lexer.nextToken();
SQLTableSource tableSource = this.parseLateralView(null);
queryBlock.setFrom(tableSource);
}
parseWhere(queryBlock);
parseGroupBy(queryBlock);
}
if (lexer.identifierEquals(FnvHash.Constants.WINDOW)) {
parseWindow(queryBlock);
}
parseGroupBy(queryBlock);
queryBlock.setOrderBy(this.exprParser.parseOrderBy());
queryBlock.setZOrderBy(this.exprParser.parseZOrderBy());
if (lexer.token() == Token.DISTRIBUTE) {
lexer.nextToken();
accept(Token.BY);
for (; ; ) {
SQLSelectOrderByItem distributeByItem = this.exprParser.parseSelectOrderByItem();
queryBlock.addDistributeBy(distributeByItem);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
} else {
break;
}
}
}
if (lexer.identifierEquals(FnvHash.Constants.ZORDER)) {
queryBlock.setZOrderBy(this.exprParser.parseZOrderBy());
}
if (lexer.identifierEquals(FnvHash.Constants.SORT)) {
lexer.nextToken();
accept(Token.BY);
for (; ; ) {
SQLSelectOrderByItem sortByItem = this.exprParser.parseSelectOrderByItem();
queryBlock.addSortBy(sortByItem);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
} else {
break;
}
}
}
if (lexer.identifierEquals(FnvHash.Constants.CLUSTER)) {
lexer.nextToken();
accept(Token.BY);
for (; ; ) {
SQLSelectOrderByItem clusterByItem = this.exprParser.parseSelectOrderByItem();
queryBlock.addClusterBy(clusterByItem);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
} else {
break;
}
}
}
if (lexer.token() == Token.LIMIT) {
SQLLimit limit = exprParser.parseLimit();
queryBlock.setLimit(limit);
}
return queryRest(queryBlock, acceptUnion);
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class NameResolveVisitor method isAliasColumn.
/**
* 是否是 select item 字段的别名
*
* @param x x 是否是 select item 字段的别名
* @param source 从 source 数据中查找 and 判断
* @return true:是、false:不是
*/
public boolean isAliasColumn(SQLExpr x, SQLSelectQueryBlock source) {
if (x instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) x;
long nameHashCode64 = identifierExpr.nameHashCode64();
SQLSelectQueryBlock queryBlock = source;
SQLSelectItem selectItem = queryBlock.findSelectItem(nameHashCode64);
if (selectItem != null) {
return true;
}
if (queryBlock.getFrom() instanceof SQLSubqueryTableSource && ((SQLSubqueryTableSource) queryBlock.getFrom()).getSelect().getQuery() instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock subQueryBlock = ((SQLSubqueryTableSource) queryBlock.getFrom()).getSelect().getQueryBlock();
if (isAliasColumn(x, subQueryBlock)) {
return true;
}
}
}
return false;
}
Aggregations