Search in sources :

Example 1 with JDBCCallableStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement in project dbeaver by serge-rider.

the class DB2Utils method checkExplainTables.

public static Boolean checkExplainTables(DBRProgressMonitor monitor, DB2DataSource dataSource, String explainTableSchemaName) throws DBCException {
    LOG.debug("Check EXPLAIN tables in '" + explainTableSchemaName + "'");
    monitor.beginTask("Check EXPLAIN tables", 1);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Verify EXPLAIN tables")) {
        // First Check with given schema
        try (JDBCCallableStatement stmtSP = session.prepareCall(CALL_INST_OBJ)) {
            // EXPLAIN
            stmtSP.setString(1, "EXPLAIN");
            // Verify
            stmtSP.setString(2, "V");
            // Tablespace
            stmtSP.setString(3, "");
            // Schema
            stmtSP.setString(4, explainTableSchemaName);
            stmtSP.execute();
            LOG.debug("EXPLAIN tables with schema " + explainTableSchemaName + " found.");
            return true;
        } catch (SQLException e) {
            LOG.debug("RC:" + e.getErrorCode() + " SQLState:" + e.getSQLState() + " " + e.getMessage());
            if (e.getErrorCode() == CALL_INST_OBJ_BAD_RC) {
                LOG.debug("No valid EXPLAIN tables found in schema '" + explainTableSchemaName + "'.");
                return false;
            }
            throw new DBCException(e, dataSource);
        }
    } finally {
        monitor.done();
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) SQLException(java.sql.SQLException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) JDBCCallableStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement)

Example 2 with JDBCCallableStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement in project dbeaver by serge-rider.

the class DB2Utils method generateDDLforTable.

// ------------------------
// Generate DDL
// ------------------------
// DF: Use "Undocumented" SYSPROC.DB2LK_GENERATE_DDL stored proc
// Ref to db2look :
// http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.admin.cmd.doc/doc/r0002051.html
//
// Options of db2look that do not seem to work: -dp . "-a" seems to work on v10.1+, "-l" seems OK in all versions
//
// TODO DF: Tables in SYSTOOLS tables must exist first
public static String generateDDLforTable(DBRProgressMonitor monitor, String statementDelimiter, DB2DataSource dataSource, DB2Table db2Table) throws DBException {
    LOG.debug("Generate DDL for " + db2Table.getFullyQualifiedName(DBPEvaluationContext.DDL));
    // As a workaround, display a message to the end-user
    if (db2Table.getSchema().isSystem()) {
        return DB2Messages.no_ddl_for_system_tables;
    }
    // and the db2look command looks for an uppercase table name (for example, MY TABLE).
    if (db2Table.getFullyQualifiedName(DBPEvaluationContext.DDL).contains(" ")) {
        return DB2Messages.no_ddl_for_spaces_in_name;
    }
    monitor.beginTask("Generating DDL", 3);
    int token;
    StringBuilder sb = new StringBuilder(2048);
    String command = String.format(DB2LK_COMMAND, statementDelimiter, db2Table.getFullyQualifiedName(DBPEvaluationContext.DDL));
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Generate DDL")) {
        LOG.debug("Calling DB2LK_GENERATE_DDL with command : " + command);
        try (JDBCCallableStatement stmtSP = session.prepareCall(CALL_DB2LK_GEN)) {
            stmtSP.registerOutParameter(2, java.sql.Types.INTEGER);
            stmtSP.setString(1, command);
            stmtSP.executeUpdate();
            token = stmtSP.getInt(2);
        }
        LOG.debug("Token = " + token);
        monitor.worked(1);
        // Read result
        try (JDBCPreparedStatement stmtSel = session.prepareStatement(SEL_DB2LK)) {
            stmtSel.setInt(1, token);
            try (JDBCResultSet dbResult = stmtSel.executeQuery()) {
                Clob ddlStmt;
                Long ddlLength;
                Long ddlStart = 1L;
                while (dbResult.next()) {
                    ddlStmt = dbResult.getClob(1);
                    try {
                        ddlLength = ddlStmt.length() + 1L;
                        sb.append(ddlStmt.getSubString(ddlStart, ddlLength.intValue()));
                        sb.append(LINE_SEP);
                    } finally {
                        try {
                            ddlStmt.free();
                        } catch (Throwable e) {
                            LOG.debug("Error freeing CLOB: " + e.getMessage());
                        }
                    }
                }
            }
        }
        monitor.worked(2);
        // Clean
        try (JDBCCallableStatement stmtSPClean = session.prepareCall(CALL_DB2LK_CLEAN)) {
            stmtSPClean.setInt(1, token);
            stmtSPClean.executeUpdate();
        }
        monitor.worked(3);
        LOG.debug("Terminated OK");
        return sb.toString();
    } catch (SQLException e) {
        throw new DBException(e, dataSource);
    } finally {
        monitor.done();
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DB2XMLString(org.jkiss.dbeaver.ext.db2.info.DB2XMLString) JDBCCallableStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement) Clob(java.sql.Clob)

Example 3 with JDBCCallableStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement in project dbeaver by serge-rider.

the class DB2Utils method createExplainTables.

public static void createExplainTables(DBRProgressMonitor monitor, DB2DataSource dataSource, String explainTableSchemaName, String tablespaceName) throws DBCException {
    LOG.debug("Create EXPLAIN tables in " + explainTableSchemaName);
    monitor.beginTask("Create EXPLAIN Tables", 1);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Create EXPLAIN tables")) {
        try (JDBCCallableStatement stmtSP = session.prepareCall(CALL_INST_OBJ)) {
            // EXPLAIN
            stmtSP.setString(1, "EXPLAIN");
            // Create
            stmtSP.setString(2, "C");
            // Tablespace
            stmtSP.setString(3, tablespaceName);
            // Schema
            stmtSP.setString(4, explainTableSchemaName);
            stmtSP.executeUpdate();
            LOG.debug("Creation EXPLAIN Tables : OK");
        } catch (SQLException e) {
            LOG.error("SQLException occured during EXPLAIN tables creation in schema " + explainTableSchemaName, e);
            throw new DBCException(e, dataSource);
        }
    } finally {
        monitor.done();
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) SQLException(java.sql.SQLException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) JDBCCallableStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement)

Aggregations

SQLException (java.sql.SQLException)3 JDBCCallableStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement)3 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)3 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 Clob (java.sql.Clob)1 DBException (org.jkiss.dbeaver.DBException)1 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)1 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)1 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)1