Search in sources :

Example 36 with SQLSelectItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.

the class SQLAnyExpr method computeDataType.

public SQLDataType computeDataType() {
    if (subQuery == null) {
        return null;
    }
    SQLSelectQueryBlock queryBlock = subQuery.getFirstQueryBlock();
    if (queryBlock == null) {
        return null;
    }
    List<SQLSelectItem> selectList = queryBlock.getSelectList();
    if (selectList.size() == 1) {
        return selectList.get(0).computeDataType();
    }
    return null;
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)

Example 37 with SQLSelectItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.

the class SQLSomeExpr method computeDataType.

public SQLDataType computeDataType() {
    if (subQuery == null) {
        return null;
    }
    SQLSelectQueryBlock queryBlock = subQuery.getFirstQueryBlock();
    if (queryBlock == null) {
        return null;
    }
    List<SQLSelectItem> selectList = queryBlock.getSelectList();
    if (selectList.size() == 1) {
        return selectList.get(0).computeDataType();
    }
    return null;
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)

Example 38 with SQLSelectItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.

the class MySqlSelectQueryBlock method accept0.

@Override
public void accept0(MySqlASTVisitor visitor) {
    if (visitor.visit(this)) {
        for (int i = 0; i < this.selectList.size(); i++) {
            SQLSelectItem item = this.selectList.get(i);
            if (item != null) {
                item.accept(visitor);
            }
        }
        if (this.from != null) {
            this.from.accept(visitor);
        }
        if (this.into != null) {
            this.into.accept(visitor);
        }
        if (this.where != null) {
            this.where.accept(visitor);
        }
        if (this.startWith != null) {
            this.startWith.accept(visitor);
        }
        if (this.connectBy != null) {
            this.connectBy.accept(visitor);
        }
        if (this.groupBy != null) {
            this.groupBy.accept(visitor);
        }
        if (this.windows != null) {
            for (SQLWindow item : windows) {
                item.accept(visitor);
            }
        }
        if (this.orderBy != null) {
            this.orderBy.accept(visitor);
        }
        if (this.distributeBy != null) {
            for (int i = 0; i < distributeBy.size(); i++) {
                SQLSelectOrderByItem item = distributeBy.get(i);
                item.accept(visitor);
            }
        }
        if (this.sortBy != null) {
            for (int i = 0; i < sortBy.size(); i++) {
                SQLSelectOrderByItem item = sortBy.get(i);
                item.accept(visitor);
            }
        }
        if (this.waitTime != null) {
            this.waitTime.accept(visitor);
        }
        if (this.limit != null) {
            this.limit.accept(visitor);
        }
        if (this.procedureName != null) {
            this.procedureName.accept(visitor);
        }
        if (this.procedureArgumentList != null) {
            for (SQLExpr item : procedureArgumentList) {
                item.accept(visitor);
            }
        }
    }
    visitor.endVisit(this);
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLWindow(com.alibaba.druid.sql.ast.SQLWindow) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 39 with SQLSelectItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.

the class OrderByResolve method visit.

public boolean visit(SQLSelect x) {
    SQLSelectQueryBlock queryBlock = x.getQueryBlock();
    if (queryBlock == null) {
        return super.visit(x);
    }
    if (x.getOrderBy() != null && queryBlock.isForUpdate() && queryBlock.getOrderBy() == null) {
        queryBlock.setOrderBy(x.getOrderBy());
        x.setOrderBy(null);
    }
    SQLOrderBy orderBy = queryBlock.getOrderBy();
    if (orderBy == null) {
        return super.visit(x);
    }
    if (!queryBlock.selectItemHasAllColumn(false)) {
        List<SQLSelectOrderByItem> notContainsOrderBy = new ArrayList<SQLSelectOrderByItem>();
        for (SQLSelectOrderByItem orderByItem : orderBy.getItems()) {
            SQLExpr orderByExpr = orderByItem.getExpr();
            if (orderByExpr instanceof SQLName) {
                if (((SQLName) orderByExpr).hashCode64() == DBMS_RANDOM_VALUE) {
                    continue;
                }
                long hashCode64 = ((SQLName) orderByExpr).nameHashCode64();
                SQLSelectItem selectItem = queryBlock.findSelectItem(hashCode64);
                if (selectItem == null) {
                    queryBlock.addSelectItem(orderByExpr.clone());
                }
            }
        }
        if (notContainsOrderBy.size() > 0) {
            for (SQLSelectOrderByItem orderByItem : notContainsOrderBy) {
                queryBlock.addSelectItem(orderByItem.getExpr());
            }
            OracleSelectQueryBlock queryBlock1 = new OracleSelectQueryBlock();
            queryBlock1.setFrom(queryBlock, "x");
            x.setQuery(queryBlock1);
        }
    }
    return super.visit(x);
}
Also used : SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) ArrayList(java.util.ArrayList) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 40 with SQLSelectItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.

the class SQLServerExprParser method parseSelectItem.

public SQLSelectItem parseSelectItem() {
    SQLExpr expr;
    if (lexer.token() == Token.IDENTIFIER) {
        expr = new SQLIdentifierExpr(lexer.stringVal());
        lexer.nextTokenComma();
        if (lexer.token() != Token.COMMA) {
            expr = this.primaryRest(expr);
            expr = this.exprRest(expr);
        }
    } else {
        expr = this.expr();
    }
    final String alias = as();
    return new SQLSelectItem(expr, alias);
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)50 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)25 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)23 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)21 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)17 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)15 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)12 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)11 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)11 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)10 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)10 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)10 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)8 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)8 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)7 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)7 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)6 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)5