Search in sources :

Example 31 with JDBCPreparedStatement

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

the class DB2IndexColumn method getDependentView.

// -----------------
// Helpers
// -----------------
private DB2View getDependentView(DBRProgressMonitor monitor, DB2DataSource db2DataSource, String indexSchema, String indexName) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, db2DataSource, "Read Index view dependency")) {
        try (JDBCPreparedStatement stmtSel = session.prepareStatement(I_DEP)) {
            stmtSel.setString(1, indexSchema);
            stmtSel.setString(2, indexName);
            JDBCResultSet dbResult = stmtSel.executeQuery();
            if (dbResult.next()) {
                String viewSchema = dbResult.getString("BSCHEMA").trim();
                String viewName = dbResult.getString("BNAME");
                return DB2Utils.findViewBySchemaNameAndName(monitor, db2DataSource, viewSchema, viewName);
            } else {
                return null;
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, db2DataSource);
    }
}
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)

Example 32 with JDBCPreparedStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement 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 33 with JDBCPreparedStatement

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

the class DB2Utils method readDBMCfg.

public static List<DB2Parameter> readDBMCfg(DBRProgressMonitor monitor, JDBCSession session) throws SQLException {
    LOG.debug("readDBMCfg");
    List<DB2Parameter> listDBMParameters = new ArrayList<>();
    try (JDBCPreparedStatement dbStat = session.prepareStatement(SEL_DBMCFG)) {
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            while (dbResult.next()) {
                listDBMParameters.add(new DB2Parameter((DB2DataSource) session.getDataSource(), dbResult));
            }
        }
    }
    return listDBMParameters;
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DB2DataSource(org.jkiss.dbeaver.ext.db2.model.DB2DataSource) ArrayList(java.util.ArrayList) DB2Parameter(org.jkiss.dbeaver.ext.db2.info.DB2Parameter)

Example 34 with JDBCPreparedStatement

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

the class DB2IndexCache method prepareLookupStatement.

@NotNull
@Override
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull DB2Schema db2Schema, DB2Index db2Index, String db2IndexName) throws SQLException {
    if (db2Index != null || db2IndexName != null) {
        final JDBCPreparedStatement dbStat = session.prepareStatement(SQL_IND);
        dbStat.setString(1, db2Schema.getName());
        dbStat.setString(2, db2Index != null ? db2Index.getName() : db2IndexName);
        return dbStat;
    } else {
        final JDBCPreparedStatement dbStat = session.prepareStatement(SQL_IND_ALL);
        dbStat.setString(1, db2Schema.getName());
        return dbStat;
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) NotNull(org.jkiss.code.NotNull)

Example 35 with JDBCPreparedStatement

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

the class DB2MaterializedQueryTableCache method prepareLookupStatement.

@NotNull
@Override
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull DB2Schema db2Schema, DB2MaterializedQueryTable db2MaterializedQueryTable, String db2MaterializedQueryTableName) throws SQLException {
    if (db2MaterializedQueryTable != null || db2MaterializedQueryTableName != null) {
        final JDBCPreparedStatement dbStat = session.prepareStatement(SQL_MQT);
        dbStat.setString(1, db2Schema.getName());
        dbStat.setString(2, db2MaterializedQueryTable != null ? db2MaterializedQueryTable.getName() : db2MaterializedQueryTableName);
        return dbStat;
    } else {
        final JDBCPreparedStatement dbStat = session.prepareStatement(SQL_MQT_ALL);
        dbStat.setString(1, db2Schema.getName());
        return dbStat;
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) NotNull(org.jkiss.code.NotNull)

Aggregations

JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)100 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)66 SQLException (java.sql.SQLException)49 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)46 DBException (org.jkiss.dbeaver.DBException)45 ArrayList (java.util.ArrayList)18 DBCException (org.jkiss.dbeaver.model.exec.DBCException)15 NotNull (org.jkiss.code.NotNull)12 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)10 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 DB2DataSource (org.jkiss.dbeaver.ext.db2.model.DB2DataSource)4 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)3 GenericDataSource (org.jkiss.dbeaver.ext.generic.model.GenericDataSource)3 ResultSet (java.sql.ResultSet)2 DB2Parameter (org.jkiss.dbeaver.ext.db2.info.DB2Parameter)2 DB2Schema (org.jkiss.dbeaver.ext.db2.model.DB2Schema)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Clob (java.sql.Clob)1 SQLXML (java.sql.SQLXML)1