Search in sources :

Example 41 with SQLSelectItem

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

the class SQLServerExprParser method parserOutput.

protected SQLServerOutput parserOutput() {
    if (lexer.identifierEquals("OUTPUT")) {
        lexer.nextToken();
        SQLServerOutput output = new SQLServerOutput();
        final List<SQLSelectItem> selectList = output.getSelectList();
        for (; ; ) {
            final SQLSelectItem selectItem = parseSelectItem();
            selectList.add(selectItem);
            if (lexer.token() != Token.COMMA) {
                break;
            }
            lexer.nextToken();
        }
        if (lexer.token() == Token.INTO) {
            lexer.nextToken();
            output.setInto(new SQLExprTableSource(this.name()));
            if (lexer.token() == (Token.LPAREN)) {
                lexer.nextToken();
                this.exprList(output.getColumns(), output);
                accept(Token.RPAREN);
            }
        }
        return output;
    }
    return null;
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLServerOutput(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource)

Example 42 with SQLSelectItem

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

the class SQLServerOutput method clone.

public SQLServerOutput clone() {
    SQLServerOutput x = new SQLServerOutput();
    if (into != null) {
        x.setInto(into.clone());
    }
    for (SQLExpr c : columns) {
        SQLExpr c2 = c.clone();
        c2.setParent(x);
        x.columns.add(c2);
    }
    for (SQLSelectItem item : selectList) {
        SQLSelectItem item2 = item.clone();
        item2.setParent(x);
        x.selectList.add(item2);
    }
    return x;
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 43 with SQLSelectItem

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

the class SQLRefactorVisitor method visit.

public boolean visit(SQLPropertyExpr x) {
    TableMapping mapping = null;
    SchemaObject schemaObject = null;
    boolean aliasOwer = false;
    SQLObject ownerObject = x.getResolvedOwnerObject();
    if (ownerObject instanceof SQLExprTableSource) {
        SQLExprTableSource exprTableSource = (SQLExprTableSource) ownerObject;
        if (exprTableSource.getAlias() != null && x.getOwner() instanceof SQLIdentifierExpr) {
            if (FnvHash.hashCode64(exprTableSource.getAlias()) == ((SQLIdentifierExpr) x.getOwner()).nameHashCode64()) {
                aliasOwer = true;
            }
        }
        mapping = findMapping(exprTableSource);
        schemaObject = (exprTableSource).getSchemaObject();
    }
    if (mapping == null) {
        return false;
    }
    String srcName = x.getName();
    String mappingColumn = mapping.getMappingColumn(srcName);
    if (mappingColumn != null) {
        x.setName(quote(mappingColumn));
    }
    SQLObject parent = x.getParent();
    if (parent instanceof SQLSelectItem && ((SQLSelectItem) parent).getAlias() == null) {
        ((SQLSelectItem) parent).setAlias(srcName);
    }
    if (x.getOwner() instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) x.getOwner()).nameHashCode64() == mapping.getSrcTableHash() && !aliasOwer) {
        x.setOwner(new SQLIdentifierExpr(quote(mapping.getDestTable())));
    }
    return false;
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource)

Example 44 with SQLSelectItem

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

the class SQLRefactorVisitor method visit.

public boolean visit(SQLIdentifierExpr x) {
    TableMapping mapping = null;
    if (groupByLevel > 0 || havingLevel > 0) {
        SQLSelectQueryBlock queryBlock = null;
        for (SQLObject parent = x.getParent(); parent != null; parent = parent.getParent()) {
            if (parent instanceof SQLSelectQueryBlock) {
                queryBlock = (SQLSelectQueryBlock) parent;
                break;
            }
        }
        boolean matchAlias = false;
        if (queryBlock != null) {
            for (SQLSelectItem item : queryBlock.getSelectList()) {
                if (item.alias_hash() == x.hashCode64()) {
                    matchAlias = true;
                    break;
                }
            }
        }
        if (matchAlias) {
            SQLObject parent = x.getParent();
            if (parent instanceof SQLOrderBy || parent instanceof SQLSelectGroupByClause) {
                return false;
            }
            if (havingLevel > 0) {
                boolean agg = false;
                for (; parent != null; parent = parent.getParent()) {
                    if (parent instanceof SQLSelectQueryBlock) {
                        break;
                    }
                    if (parent instanceof SQLAggregateExpr) {
                        agg = true;
                        break;
                    }
                }
                if (!agg) {
                    return false;
                }
            }
        }
    }
    SQLObject ownerObject = x.getResolvedOwnerObject();
    if (ownerObject instanceof SQLExprTableSource) {
        mapping = findMapping((SQLExprTableSource) ownerObject);
    }
    if (mapping == null) {
        return false;
    }
    String srcName = x.getName();
    String mappingColumn = mapping.getMappingColumn(srcName);
    if (mappingColumn != null) {
        x.setName(quote(mappingColumn));
    }
    SQLObject parent = x.getParent();
    if (parent instanceof SQLSelectItem && ((SQLSelectItem) parent).getAlias() == null) {
        ((SQLSelectItem) parent).setAlias(srcName);
    }
    return false;
}
Also used : SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelectGroupByClause(com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 45 with SQLSelectItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectItem 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

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