Search in sources :

Example 1 with SQLAggregateExpr

use of com.alibaba.druid.sql.ast.expr.SQLAggregateExpr in project Mycat-Server by MyCATApache.

the class JoinParser method parserFields.

private void parserFields(List<SQLSelectItem> mysqlSelectList) {
    //显示的字段
    String key = "";
    String value = "";
    for (SQLSelectItem item : mysqlSelectList) {
        if (item.getExpr() instanceof SQLAllColumnExpr) {
            //*解析
            setField(item.toString(), item.toString());
        } else {
            if (item.getExpr() instanceof SQLAggregateExpr) {
                SQLAggregateExpr expr = (SQLAggregateExpr) item.getExpr();
                key = getExprFieldName(expr);
            //value=expr.
            } else {
                key = getFieldName(item);
                value = item.getAlias();
            }
            setField(key, value);
        }
    }
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 2 with SQLAggregateExpr

use of com.alibaba.druid.sql.ast.expr.SQLAggregateExpr 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)

Example 3 with SQLAggregateExpr

use of com.alibaba.druid.sql.ast.expr.SQLAggregateExpr in project druid by alibaba.

the class PagerUtils method limitDB2.

private static String limitDB2(SQLSelect select, String dbType, int offset, int count) {
    SQLSelectQuery query = select.getQuery();
    SQLBinaryOpExpr gt = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.GreaterThan, //
    new SQLNumberExpr(offset), JdbcConstants.DB2);
    SQLBinaryOpExpr lteq = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.LessThanOrEqual, //
    new SQLNumberExpr(count + offset), JdbcConstants.DB2);
    SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq, JdbcConstants.DB2);
    if (query instanceof SQLSelectQueryBlock) {
        DB2SelectQueryBlock queryBlock = (DB2SelectQueryBlock) query;
        if (offset <= 0) {
            queryBlock.setFirst(new SQLNumberExpr(count));
            return SQLUtils.toSQLString(select, dbType);
        }
        SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
        SQLOrderBy orderBy = select.getOrderBy();
        if (orderBy == null && select.getQuery() instanceof SQLSelectQueryBlock) {
            SQLSelectQueryBlock selectQueryBlcok = (SQLSelectQueryBlock) select.getQuery();
            orderBy = selectQueryBlcok.getOrderBy();
            selectQueryBlcok.setOrderBy(null);
        } else {
            select.setOrderBy(null);
        }
        aggregateExpr.setOver(new SQLOver(orderBy));
        queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
        DB2SelectQueryBlock countQueryBlock = new DB2SelectQueryBlock();
        countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
        countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
        countQueryBlock.setWhere(pageCondition);
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    DB2SelectQueryBlock countQueryBlock = new DB2SelectQueryBlock();
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
    SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
    SQLOrderBy orderBy = select.getOrderBy();
    aggregateExpr.setOver(new SQLOver(orderBy));
    select.setOrderBy(null);
    countQueryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
    countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
    if (offset <= 0) {
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    DB2SelectQueryBlock offsetQueryBlock = new DB2SelectQueryBlock();
    offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
    offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
    offsetQueryBlock.setWhere(pageCondition);
    return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) DB2SelectQueryBlock(com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 4 with SQLAggregateExpr

use of com.alibaba.druid.sql.ast.expr.SQLAggregateExpr in project druid by alibaba.

the class OracleExprParser method parseAggregateExpr.

protected SQLAggregateExpr parseAggregateExpr(String methodName) {
    methodName = methodName.toUpperCase();
    SQLAggregateExpr aggregateExpr;
    if (lexer.token() == Token.UNIQUE) {
        aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateOption.UNIQUE);
        lexer.nextToken();
    } else if (lexer.token() == (Token.ALL)) {
        aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateOption.ALL);
        lexer.nextToken();
    } else if (lexer.token() == (Token.DISTINCT)) {
        aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateOption.DISTINCT);
        lexer.nextToken();
    } else {
        aggregateExpr = new SQLAggregateExpr(methodName);
    }
    exprList(aggregateExpr.getArguments(), aggregateExpr);
    if (lexer.stringVal().equalsIgnoreCase("IGNORE")) {
        lexer.nextToken();
        identifierEquals("NULLS");
        aggregateExpr.setIgnoreNulls(true);
    }
    accept(Token.RPAREN);
    if (identifierEquals("WITHIN")) {
        lexer.nextToken();
        accept(Token.GROUP);
        accept(Token.LPAREN);
        SQLOrderBy withinGroup = this.parseOrderBy();
        aggregateExpr.setWithinGroup(withinGroup);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.KEEP) {
        lexer.nextToken();
        SQLKeep keep = new SQLKeep();
        accept(Token.LPAREN);
        acceptIdentifier("DENSE_RANK");
        if (identifierEquals("FIRST")) {
            lexer.nextToken();
            keep.setDenseRank(DenseRank.FIRST);
        } else {
            acceptIdentifier("LAST");
            keep.setDenseRank(DenseRank.LAST);
        }
        SQLOrderBy orderBy = this.parseOrderBy();
        keep.setOrderBy(orderBy);
        aggregateExpr.setKeep(keep);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.OVER) {
        OracleAnalytic over = new OracleAnalytic();
        lexer.nextToken();
        accept(Token.LPAREN);
        if (identifierEquals("PARTITION")) {
            lexer.nextToken();
            accept(Token.BY);
            if (lexer.token() == (Token.LPAREN)) {
                lexer.nextToken();
                exprList(over.getPartitionBy(), over);
                accept(Token.RPAREN);
            } else {
                exprList(over.getPartitionBy(), over);
            }
        }
        over.setOrderBy(parseOrderBy());
        if (over.getOrderBy() != null) {
            OracleAnalyticWindowing windowing = null;
            if (lexer.stringVal().equalsIgnoreCase("ROWS")) {
                lexer.nextToken();
                windowing = new OracleAnalyticWindowing();
                windowing.setType(OracleAnalyticWindowing.Type.ROWS);
            } else if (lexer.stringVal().equalsIgnoreCase("RANGE")) {
                lexer.nextToken();
                windowing = new OracleAnalyticWindowing();
                windowing.setType(OracleAnalyticWindowing.Type.RANGE);
            }
            if (windowing != null) {
                if (lexer.stringVal().equalsIgnoreCase("CURRENT")) {
                    lexer.nextToken();
                    if (lexer.stringVal().equalsIgnoreCase("ROW")) {
                        lexer.nextToken();
                        windowing.setExpr(new SQLIdentifierExpr("CURRENT ROW"));
                        over.setWindowing(windowing);
                    }
                    throw new ParserException("syntax error");
                }
                if (lexer.stringVal().equalsIgnoreCase("UNBOUNDED")) {
                    lexer.nextToken();
                    if (lexer.stringVal().equalsIgnoreCase("PRECEDING")) {
                        lexer.nextToken();
                        windowing.setExpr(new SQLIdentifierExpr("UNBOUNDED PRECEDING"));
                    } else {
                        throw new ParserException("syntax error");
                    }
                }
                over.setWindowing(windowing);
            }
        }
        accept(Token.RPAREN);
        aggregateExpr.setOver(over);
    }
    return aggregateExpr;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLKeep(com.alibaba.druid.sql.ast.SQLKeep) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) OracleAnalytic(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalytic) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr) OracleAnalyticWindowing(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalyticWindowing)

Example 5 with SQLAggregateExpr

use of com.alibaba.druid.sql.ast.expr.SQLAggregateExpr in project druid by alibaba.

the class EqualTest_aggreate method test_exits.

public void test_exits() throws Exception {
    String sql = "count(*)";
    String sql_c = "count(id)";
    SQLAggregateExpr exprA, exprB, exprC;
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprA = (SQLAggregateExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprB = (SQLAggregateExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql_c);
        exprC = (SQLAggregateExpr) parser.expr();
    }
    Assert.assertEquals(exprA, exprB);
    Assert.assertNotEquals(exprA, exprC);
    Assert.assertTrue(exprA.equals(exprA));
    Assert.assertFalse(exprA.equals(new Object()));
    Assert.assertEquals(exprA.hashCode(), exprB.hashCode());
    Assert.assertEquals(new SQLAggregateExpr(null), new SQLAggregateExpr(null));
    Assert.assertEquals(new SQLAggregateExpr(null).hashCode(), new SQLAggregateExpr(null).hashCode());
}
Also used : OracleExprParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Aggregations

SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)12 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)8 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)6 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)6 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)5 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)4 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)3 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)3 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)3 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)3 SQLOver (com.alibaba.druid.sql.ast.SQLOver)2 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)2 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)2 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)2 OracleSelect (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect)2 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)2 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)2