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);
}
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());
}
Aggregations