use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class DB2Utils method callAdminCmd.
// ------------------------
// Admin Command
// ------------------------
public static void callAdminCmd(DBRProgressMonitor monitor, DB2DataSource dataSource, String command) throws SQLException {
LOG.debug("Call admin_cmd with '" + command + "'");
String sql = String.format(CALL_ADMIN_CMD, command);
monitor.beginTask("Executing command " + command, 1);
try (JDBCSession session = DBUtils.openUtilSession(monitor, dataSource, "ADMIN_CMD")) {
JDBCUtils.executeProcedure(session, sql);
} finally {
monitor.done();
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession 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.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class DB2StructureAssistant method findObjectsByMask.
@NotNull
@Override
public List<DBSObjectReference> findObjectsByMask(DBRProgressMonitor monitor, DBSObject parentObject, DBSObjectType[] objectTypes, String objectNameMask, boolean caseSensitive, boolean globalSearch, int maxResults) throws DBException {
LOG.debug(objectNameMask);
List<DB2ObjectType> db2ObjectTypes = new ArrayList<>(objectTypes.length);
for (DBSObjectType dbsObjectType : objectTypes) {
db2ObjectTypes.add((DB2ObjectType) dbsObjectType);
}
DB2Schema schema = parentObject instanceof DB2Schema ? (DB2Schema) parentObject : null;
if (schema == null && !globalSearch) {
schema = dataSource.getDefaultObject();
}
try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Find objects by name")) {
return searchAllObjects(session, schema, objectNameMask, db2ObjectTypes, caseSensitive, maxResults);
} catch (SQLException ex) {
throw new DBException(ex, dataSource);
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class PostgreMetaModel method loadTriggers.
@Override
public List<PostgreGenericTrigger> loadTriggers(DBRProgressMonitor monitor, @NotNull GenericStructContainer container, @Nullable GenericTable table) throws DBException {
try (JDBCSession session = DBUtils.openMetaSession(monitor, container.getDataSource(), "Read triggers")) {
StringBuilder sql = new StringBuilder();
sql.append("SELECT trigger_name,event_manipulation,action_order,action_condition,action_statement,action_orientation,action_timing\n" + "FROM INFORMATION_SCHEMA.TRIGGERS\n" + "WHERE ");
if (table == null) {
sql.append("trigger_schema=? AND event_object_table IS NULL");
} else {
sql.append("event_object_schema=? AND event_object_table=?");
}
try (JDBCPreparedStatement dbStat = session.prepareStatement(sql.toString())) {
if (table == null) {
dbStat.setString(1, container.getSchema().getName());
} else {
dbStat.setString(1, table.getSchema().getName());
dbStat.setString(2, table.getName());
}
Map<String, PostgreGenericTrigger> result = new LinkedHashMap<>();
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
while (dbResult.next()) {
String name = JDBCUtils.safeGetString(dbResult, "trigger_name");
if (name == null) {
continue;
}
String manipulation = JDBCUtils.safeGetString(dbResult, "event_manipulation");
PostgreGenericTrigger trigger = result.get(name);
if (trigger != null) {
trigger.addManipulation(manipulation);
continue;
}
String description = "";
trigger = new PostgreGenericTrigger(container, table, name, description, manipulation, JDBCUtils.safeGetString(dbResult, "action_orientation"), JDBCUtils.safeGetString(dbResult, "action_timing"), JDBCUtils.safeGetString(dbResult, "action_statement"));
result.put(name, trigger);
}
}
return new ArrayList<>(result.values());
}
} catch (SQLException e) {
throw new DBException(e, container.getDataSource());
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.
the class PostgreSessionManager method alterSession.
@Override
public void alterSession(DBCSession session, PostgreSession sessionType, Map<String, Object> options) throws DBException {
try {
try (JDBCPreparedStatement dbStat = ((JDBCSession) session).prepareStatement("SELECT pg_catalog.pg_terminate_backend(?)")) {
dbStat.setInt(1, sessionType.getPid());
dbStat.execute();
}
} catch (SQLException e) {
throw new DBException(e, session.getDataSource());
}
}
Aggregations