Search in sources :

Example 1 with IntKeyMap

use of org.jkiss.utils.IntKeyMap in project dbeaver by serge-rider.

the class OraclePlanAnalyser method explain.

public void explain(JDBCSession session) throws DBCException {
    String planStmtId = SecurityUtils.generateUniqueId();
    try {
        // Detect plan table
        String planTableName = dataSource.getPlanTableName(session);
        if (planTableName == null) {
            throw new DBCException("Plan table not found - query can't be explained");
        }
        // Delete previous statement rows
        // (actually there should be no statement with this id -
        // but let's do it, just in case)
        JDBCPreparedStatement dbStat = session.prepareStatement("DELETE FROM " + planTableName + " WHERE STATEMENT_ID=? ");
        try {
            dbStat.setString(1, planStmtId);
            dbStat.execute();
        } finally {
            dbStat.close();
        }
        // Explain plan
        StringBuilder explainSQL = new StringBuilder();
        explainSQL.append("EXPLAIN PLAN ").append("\n").append("SET STATEMENT_ID = '").append(planStmtId).append("'\n").append("INTO ").append(planTableName).append("\n").append("FOR ").append(query);
        dbStat = session.prepareStatement(explainSQL.toString());
        try {
            dbStat.execute();
        } finally {
            dbStat.close();
        }
        // Read explained plan
        dbStat = session.prepareStatement("SELECT * FROM " + planTableName + " WHERE STATEMENT_ID=? ORDER BY ID");
        try {
            dbStat.setString(1, planStmtId);
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                rootNodes = new ArrayList<>();
                IntKeyMap<OraclePlanNode> allNodes = new IntKeyMap<>();
                while (dbResult.next()) {
                    OraclePlanNode node = new OraclePlanNode(dataSource, allNodes, dbResult);
                    allNodes.put(node.getId(), node);
                    if (node.getParent() == null) {
                        rootNodes.add(node);
                    }
                }
            }
        } finally {
            dbStat.close();
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) IntKeyMap(org.jkiss.utils.IntKeyMap) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Aggregations

SQLException (java.sql.SQLException)1 DBCException (org.jkiss.dbeaver.model.exec.DBCException)1 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)1 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)1 IntKeyMap (org.jkiss.utils.IntKeyMap)1