Search in sources :

Example 1 with SQLSelectItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project sharding-jdbc by dangdangdotcom.

the class MySQLSelectVisitor method visit.

@Override
public boolean visit(final SQLAggregateExpr x) {
    if (!(x.getParent() instanceof SQLSelectItem)) {
        return super.visit(x);
    }
    AggregationType aggregationType;
    try {
        aggregationType = AggregationType.valueOf(x.getMethodName().toUpperCase());
    } catch (final IllegalArgumentException ex) {
        return super.visit(x);
    }
    StringBuilder expression = new StringBuilder();
    x.accept(new MySqlOutputVisitor(expression));
    // TODO index获取不准,考虑使用别名替换
    AggregationColumn column = new AggregationColumn(expression.toString(), aggregationType, Optional.fromNullable(((SQLSelectItem) x.getParent()).getAlias()), null == x.getOption() ? Optional.<String>absent() : Optional.of(x.getOption().toString()), getParseContext().getItemIndex());
    getParseContext().getParsedResult().getMergeContext().getAggregationColumns().add(column);
    if (AggregationType.AVG.equals(aggregationType)) {
        getParseContext().addDerivedColumnsForAvgColumn(column);
    // TODO 将AVG列替换成常数,避免数据库再计算无用的AVG函数
    }
    return super.visit(x);
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn) MySqlOutputVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor) AggregationType(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn.AggregationType)

Example 2 with SQLSelectItem

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

the class SQLUtils method addSelectItem.

public static void addSelectItem(SQLSelectQueryBlock queryBlock, SQLExpr expr, String alias, boolean first) {
    SQLSelectItem selectItem = new SQLSelectItem(expr, alias);
    queryBlock.getSelectList().add(selectItem);
    selectItem.setParent(selectItem);
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem)

Example 3 with SQLSelectItem

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

the class SQLSelectBuilderImpl method select.

public SQLSelectBuilderImpl select(String... columns) {
    SQLSelectQueryBlock queryBlock = getQueryBlock();
    for (String column : columns) {
        SQLSelectItem selectItem = SQLUtils.toSelectItem(column, dbType);
        queryBlock.addSelectItem(selectItem);
    }
    return this;
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)

Example 4 with SQLSelectItem

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

the class SQLSelectParser method parseSelectList.

protected void parseSelectList(SQLSelectQueryBlock queryBlock) {
    final List<SQLSelectItem> selectList = queryBlock.getSelectList();
    for (; ; ) {
        final SQLSelectItem selectItem = this.exprParser.parseSelectItem();
        selectList.add(selectItem);
        selectItem.setParent(queryBlock);
        if (lexer.token() != Token.COMMA) {
            break;
        }
        lexer.nextToken();
    }
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem)

Example 5 with SQLSelectItem

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

the class PagerUtils method createCountItem.

private static SQLSelectItem createCountItem(String dbType) {
    SQLAggregateExpr countExpr = new SQLAggregateExpr("COUNT");
    countExpr.addArgument(new SQLAllColumnExpr());
    SQLSelectItem countItem = new SQLSelectItem(countExpr);
    return countItem;
}
Also used : SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

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