Search in sources :

Example 51 with SQLIdentifierExpr

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);
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Example 52 with SQLIdentifierExpr

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;
}
Also used : ArrayList(java.util.ArrayList) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 53 with SQLIdentifierExpr

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;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 54 with SQLIdentifierExpr

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);
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock)

Example 55 with SQLIdentifierExpr

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;
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)

Aggregations

SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)152 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)68 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)45 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)19 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)17 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)16 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)16 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)15 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)15 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)13 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)11 SQLAssignItem (com.alibaba.druid.sql.ast.statement.SQLAssignItem)11 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)10 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)10 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)10 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)9 ArrayList (java.util.ArrayList)9 SQLName (com.alibaba.druid.sql.ast.SQLName)8