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);
}
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);
}
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();
}
}
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()]);
}
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);
}
Aggregations