use of org.jkiss.dbeaver.ext.db2.model.DB2DataSource 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.DB2DataSource in project dbeaver by serge-rider.
the class DB2Utils method formatSQLProcedureDDL.
public static String formatSQLProcedureDDL(DB2DataSource db2DataSource, String rawText) {
// First let the defaut SQL formater operate
String result = SQLUtils.formatSQL(db2DataSource, rawText);
// Put some kwywords on the same line
result = result.replace("CREATE\r\n PROCEDURE", "CREATE PROCEDURE");
result = result.replace("\r\nOR REPLACE", " OR REPLACE");
// Put each definition keywords on one line
result = result.replace(" LANGUAGE ", "\r\nLANGUAGE ");
result = result.replace(" SPECIFIC ", "\r\nSPECIFIC ");
result = result.replace(" DYNAMIC RESULT ", "\r\nDYNAMIC RESULT ");
result = result.replace(" MODIFIES SQL ", "\r\nMODIFIES SQL ");
result = result.replace(" CONTAINS SQL ", "\r\nCONTAINS SQL ");
result = result.replace(" READS SQL DATA ", "\r\nREADS SQL DATA ");
result = result.replace(" NOT DETERMINISTIC ", "\r\nNOT DETERMINISTIC ");
result = result.replace(" DETERMINISTIC ", "\r\nDETERMINISTIC ");
result = result.replace(" CALLED ON NULL INPUT ", "\r\nCALLED ON NULL INPUT ");
result = result.replace(" COMMIT ON RETURN ", "\r\nCOMMIT ON RETURN ");
result = result.replace(" AUTONOMOUS ", "\r\nAUTONOMOUS ");
result = result.replace(" INHERIT SPECIAL ", "\r\nINHERIT SPECIAL ");
result = result.replace(" OLD SAVEPOINT ", "\r\nOLD SAVEPOINT ");
result = result.replace(" NEW SAVEPOINT ", "\r\nNEW SAVEPOINT ");
result = result.replace(" EXTERNAL ACTION ", "\r\nEXTERNAL ACTION ");
result = result.replace(" NO EXTERNAL ", "\r\nNO EXTERNAL ");
result = result.replace(" PARAMETER CCSID ", "\r\nPARAMETER CCSID ");
result = result.replace(" BEGIN ", "\r\nBEGIN\r\n");
// Put a CR after ";"
result = result.replaceAll(";", ";\r\n");
// Suppress the CRs before ";"
result = result.replaceAll("\\r\\n;", ";");
// Remove CR space
result = result.replaceAll("\\r\\n ", "\r\n");
// Remove some CRs
result = result.replaceAll("SET\\r\\n", "SET ");
result = result.replaceAll("INTO\\r\\n", "INTO ");
result = result.replaceAll("FROM\\r\\n", "FROM ");
result = result.replaceAll("FETCH\\r\\n", "FETCH ");
result = result.replaceAll("WHERE\\r\\n", "WHERE ");
return result;
}
use of org.jkiss.dbeaver.ext.db2.model.DB2DataSource in project dbeaver by serge-rider.
the class DB2Utils method readXMLStrings.
public static List<DB2XMLString> readXMLStrings(DBRProgressMonitor monitor, JDBCSession session) throws SQLException {
LOG.debug("readXMLStrings");
List<DB2XMLString> listXMLStrings = new ArrayList<>();
try (JDBCPreparedStatement dbStat = session.prepareStatement(SEL_XMLSTRINGS)) {
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
while (dbResult.next()) {
listXMLStrings.add(new DB2XMLString((DB2DataSource) session.getDataSource(), dbResult));
}
}
}
return listXMLStrings;
}
use of org.jkiss.dbeaver.ext.db2.model.DB2DataSource 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.ext.db2.model.DB2DataSource 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();
}
}
Aggregations