Search in sources :

Example 11 with SQLAggregateExpr

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

the class WallVisitorUtils method isSimpleCountTableSource.

public static boolean isSimpleCountTableSource(WallVisitor visitor, SQLSelect select) {
    SQLSelectQuery query = select.getQuery();
    if (query instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
        boolean allawTrueWhere = false;
        if (queryBlock.getWhere() == null) {
            allawTrueWhere = true;
        } else {
            Object whereValue = getValue(visitor, queryBlock.getWhere());
            if (whereValue == Boolean.TRUE) {
                allawTrueWhere = true;
            } else if (whereValue == Boolean.FALSE) {
                return false;
            }
        }
        boolean simpleCount = false;
        if (queryBlock.getSelectList().size() == 1) {
            SQLExpr selectItemExpr = queryBlock.getSelectList().get(0).getExpr();
            if (selectItemExpr instanceof SQLAggregateExpr) {
                if (((SQLAggregateExpr) selectItemExpr).getMethodName().equalsIgnoreCase("COUNT")) {
                    simpleCount = true;
                }
            }
        }
        if (allawTrueWhere && simpleCount) {
            return true;
        }
    }
    return false;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 12 with SQLAggregateExpr

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

the class EqualTest_aggreate_over method test_exits.

public void test_exits() throws Exception {
    String sql = "count(*) OVER (ORDER BY f1)";
    String sql_c = "count(id) OVER (ORDER BY f2)";
    SQLAggregateExpr exprA, exprB, exprC;
    {
        SQLExprParser parser = new SQLExprParser(sql);
        exprA = (SQLAggregateExpr) parser.expr();
    }
    {
        SQLExprParser parser = new SQLExprParser(sql);
        exprB = (SQLAggregateExpr) parser.expr();
    }
    {
        SQLExprParser parser = new SQLExprParser(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 : SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr) SQLExprParser(com.alibaba.druid.sql.parser.SQLExprParser)

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