Search in sources :

Example 16 with OracleSelectQueryBlock

use of com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock 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 17 with OracleSelectQueryBlock

use of com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock in project druid by alibaba.

the class Issue1994 method test_for_issue.

public void test_for_issue() throws Exception {
    String sql = "INSERT INTO MKTG_H_EXEC_RESULT_FACT\n" + "(THE_DATE, AREA_ID, SCENE_ID, MKTG_CNT, MKTG_SUC_CNT\n" + ", TASK_CNT, TASK_F_CNT, TASK_F_SUC_CNT, CON_CNT, CON_SUC_CNT)\n" + "SELECT TRUNC(SYSDATE), T1.AREA_ID\n" + ", RTRIM(TO_CHAR(T2.PID))\n" + ", SUM(T1.MKTG_CNT), SUM(T1.MKTG_SUC_CNT)\n" + ", SUM(T1.TASK_CNT), SUM(T1.TASK_F_CNT)\n" + ", SUM(T1.TASK_F_SUC_CNT), SUM(T1.CON_CNT)\n" + ", SUM(T1.CON_SUC_CNT)\n" + "FROM MKTG_H_EXEC_RESULT_FACT T1, (\n" + "SELECT DISTINCT MKTG_PLAN_LVL1_ID AS PID, MKTG_PLAN_LVL4_ID AS SCENE_ID\n" + "FROM DMN_MKTG_PLAN_TYPE\n" + "UNION ALL\n" + "SELECT DISTINCT MKTG_PLAN_LVL2_ID AS PID, MKTG_PLAN_LVL4_ID AS SCENE_ID\n" + "FROM DMN_MKTG_PLAN_TYPE_TWO\n" + "WHERE MKTG_PLAN_LVL2_ID <> MKTG_PLAN_LVL4_ID\n" + "UNION ALL\n" + "SELECT DISTINCT MKTG_PLAN_LVL3_ID AS PID, MKTG_PLAN_LVL4_ID AS SCENE_ID\n" + "FROM DMN_MKTG_PLAN_TYPE\n" + "WHERE MKTG_PLAN_LVL3_ID <> MKTG_PLAN_LVL4_ID\n" + ") T2\n" + "WHERE T1.THE_DATE = TRUNC(SYSDATE)\n" + "AND T1.SCENE_ID = T2.SCENE_ID\n" + "GROUP BY T1.AREA_ID, RTRIM(TO_CHAR(T2.PID))";
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
    assertEquals(1, stmtList.size());
    SQLStatement stmt = stmtList.get(0);
    System.out.println(stmt);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ORACLE);
    stmt.accept(statVisitor);
    System.out.println("columns : " + statVisitor.getColumns());
    assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "THE_DATE"));
    assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "AREA_ID"));
    assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "SCENE_ID"));
    assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "MKTG_CNT"));
    assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "MKTG_SUC_CNT"));
    assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "TASK_CNT"));
    assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "TASK_F_CNT"));
    assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "TASK_F_SUC_CNT"));
    assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "CON_CNT"));
    assertTrue(statVisitor.containsColumn("MKTG_H_EXEC_RESULT_FACT", "CON_SUC_CNT"));
    assertTrue(statVisitor.containsColumn("DMN_MKTG_PLAN_TYPE", "MKTG_PLAN_LVL1_ID"));
    assertTrue(statVisitor.containsColumn("DMN_MKTG_PLAN_TYPE", "MKTG_PLAN_LVL4_ID"));
    assertTrue(statVisitor.containsColumn("DMN_MKTG_PLAN_TYPE_TWO", "MKTG_PLAN_LVL2_ID"));
    assertTrue(statVisitor.containsColumn("DMN_MKTG_PLAN_TYPE_TWO", "MKTG_PLAN_LVL4_ID"));
    assertTrue(statVisitor.containsColumn("DMN_MKTG_PLAN_TYPE", "MKTG_PLAN_LVL3_ID"));
    OracleInsertStatement insertStmt = (OracleInsertStatement) stmt;
    OracleSelectQueryBlock queryBlock = (OracleSelectQueryBlock) insertStmt.getQuery().getQueryBlock();
    SQLSelectItem selectItem = queryBlock.getSelectList().get(0);
    assertEquals("TRUNC(SYSDATE)", selectItem.toString());
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) OracleInsertStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleInsertStatement) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Aggregations

OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)17 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)9 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)9 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)9 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)7 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)7 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)7 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)6 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)5 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)5 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)4 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)4 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 OracleSelect (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect)4 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)3 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)3 DB2SelectQueryBlock (com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock)3 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)3 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)3 ArrayList (java.util.ArrayList)3