Search in sources :

Example 1 with DB2Table

use of org.jkiss.dbeaver.ext.db2.model.DB2Table in project dbeaver by serge-rider.

the class DB2TriggerCache method fetchObject.

@Override
protected DB2Trigger fetchObject(@NotNull JDBCSession session, @NotNull DB2Schema db2Schema, @NotNull JDBCResultSet dbResult) throws SQLException, DBException {
    // Look for related table
    String tableSchemaName = JDBCUtils.safeGetStringTrimmed(dbResult, "TABSCHEMA");
    String tableName = JDBCUtils.safeGetStringTrimmed(dbResult, "TABNAME");
    DB2Table db2Table = DB2Utils.findTableBySchemaNameAndName(session.getProgressMonitor(), db2Schema.getDataSource(), tableSchemaName, tableName);
    return new DB2Trigger(session.getProgressMonitor(), db2Schema, db2Table, dbResult);
}
Also used : DB2Table(org.jkiss.dbeaver.ext.db2.model.DB2Table) DB2Trigger(org.jkiss.dbeaver.ext.db2.model.DB2Trigger)

Example 2 with DB2Table

use of org.jkiss.dbeaver.ext.db2.model.DB2Table in project dbeaver by serge-rider.

the class DB2Utils method findColumnBySchemaNameAndTableNameAndName.

public static DB2TableColumn findColumnBySchemaNameAndTableNameAndName(DBRProgressMonitor monitor, DB2DataSource db2DataSource, String db2SchemaName, String db2TableName, String db2ColumnName) throws DBException {
    DB2Schema db2Schema = db2DataSource.getSchema(monitor, db2SchemaName);
    if (db2Schema == null) {
        return null;
    }
    DB2Table db2Table = db2Schema.getTable(monitor, db2TableName);
    if (db2Table == null) {
        return null;
    }
    return db2Table.getAttribute(monitor, db2ColumnName);
}
Also used : DB2Table(org.jkiss.dbeaver.ext.db2.model.DB2Table) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema)

Example 3 with DB2Table

use of org.jkiss.dbeaver.ext.db2.model.DB2Table 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 4 with DB2Table

use of org.jkiss.dbeaver.ext.db2.model.DB2Table in project dbeaver by serge-rider.

the class DB2TableTablespaceListProvider method getPossibleValues.

@Override
public Object[] getPossibleValues(DB2Table db2Table) {
    Collection<DB2Tablespace> colTablespaces = db2Table.getDataSource().getTablespaceCache().getCachedObjects();
    List<DB2Tablespace> validTablespaces = new ArrayList<>(colTablespaces.size());
    for (DB2Tablespace db2Tablespace : colTablespaces) {
        if (db2Tablespace.getDataType().isValidForUserTables()) {
            validTablespaces.add(db2Tablespace);
        }
    }
    return validTablespaces.toArray(new DB2Tablespace[validTablespaces.size()]);
}
Also used : ArrayList(java.util.ArrayList) DB2Tablespace(org.jkiss.dbeaver.ext.db2.model.DB2Tablespace)

Example 5 with DB2Table

use of org.jkiss.dbeaver.ext.db2.model.DB2Table in project dbeaver by serge-rider.

the class DB2IndexCache method fetchObject.

@Override
protected DB2Index fetchObject(@NotNull JDBCSession session, @NotNull DB2Schema db2Schema, @NotNull JDBCResultSet dbResult) throws SQLException, DBException {
    // Look for related table...or nickname...or MQT
    String tableOrNicknameSchemaName = JDBCUtils.safeGetStringTrimmed(dbResult, "TABSCHEMA");
    String tableOrNicknameName = JDBCUtils.safeGetStringTrimmed(dbResult, "TABNAME");
    DB2Schema objectSchema = db2Schema.getDataSource().getSchema(session.getProgressMonitor(), tableOrNicknameSchemaName);
    if (objectSchema == null) {
        log.error("Schema '" + tableOrNicknameSchemaName + "' not found");
        return null;
    }
    // FIXME: here we cache all tables to avoid spam in table lookup
    // FIXME: because we always read all indexes. Make index cache lookup cache
    objectSchema.getTables(session.getProgressMonitor());
    DB2TableBase db2Table = objectSchema.getTable(session.getProgressMonitor(), tableOrNicknameName);
    if (db2Table == null) {
        db2Table = DB2Utils.findNicknameBySchemaNameAndName(session.getProgressMonitor(), db2Schema.getDataSource(), tableOrNicknameSchemaName, tableOrNicknameName);
    }
    if (db2Table == null) {
        db2Table = DB2Utils.findMaterializedQueryTableBySchemaNameAndName(session.getProgressMonitor(), db2Schema.getDataSource(), tableOrNicknameSchemaName, tableOrNicknameName);
    }
    if (db2Table == null) {
        log.error("Object '" + tableOrNicknameName + "' not found in schema '" + tableOrNicknameSchemaName + "'");
        return null;
    }
    return new DB2Index(session.getProgressMonitor(), db2Schema, db2Table, dbResult);
}
Also used : DB2TableBase(org.jkiss.dbeaver.ext.db2.model.DB2TableBase) DB2Index(org.jkiss.dbeaver.ext.db2.model.DB2Index) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema)

Aggregations

Nullable (org.jkiss.code.Nullable)5 DB2Table (org.jkiss.dbeaver.ext.db2.model.DB2Table)4 DB2TableColumn (org.jkiss.dbeaver.ext.db2.model.DB2TableColumn)4 DB2Schema (org.jkiss.dbeaver.ext.db2.model.DB2Schema)3 DB2TableKeyColumn (org.jkiss.dbeaver.ext.db2.model.DB2TableKeyColumn)3 SQLException (java.sql.SQLException)2 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)2 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)2 Clob (java.sql.Clob)1 ArrayList (java.util.ArrayList)1 Font (org.eclipse.swt.graphics.Font)1 Tree (org.eclipse.swt.widgets.Tree)1 TreeColumn (org.eclipse.swt.widgets.TreeColumn)1 TreeItem (org.eclipse.swt.widgets.TreeItem)1 NotNull (org.jkiss.code.NotNull)1 DBException (org.jkiss.dbeaver.DBException)1 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)1 DB2Index (org.jkiss.dbeaver.ext.db2.model.DB2Index)1 DB2TableBase (org.jkiss.dbeaver.ext.db2.model.DB2TableBase)1 DB2TableCheckConstraintColumn (org.jkiss.dbeaver.ext.db2.model.DB2TableCheckConstraintColumn)1