Search in sources :

Example 1 with DB2DataSource

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);
}
Also used : DB2Table(org.jkiss.dbeaver.ext.db2.model.DB2Table) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema)

Example 2 with DB2DataSource

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;
}
Also used : DB2XMLString(org.jkiss.dbeaver.ext.db2.info.DB2XMLString)

Example 3 with DB2DataSource

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;
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DB2XMLString(org.jkiss.dbeaver.ext.db2.info.DB2XMLString) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DB2DataSource(org.jkiss.dbeaver.ext.db2.model.DB2DataSource) ArrayList(java.util.ArrayList)

Example 4 with DB2DataSource

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();
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DB2XMLString(org.jkiss.dbeaver.ext.db2.info.DB2XMLString)

Example 5 with DB2DataSource

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

Aggregations

DB2DataSource (org.jkiss.dbeaver.ext.db2.model.DB2DataSource)7 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)5 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)4 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)4 ArrayList (java.util.ArrayList)3 DBException (org.jkiss.dbeaver.DBException)2 DB2Parameter (org.jkiss.dbeaver.ext.db2.info.DB2Parameter)2 DB2Schema (org.jkiss.dbeaver.ext.db2.model.DB2Schema)2 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)2 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 IContributionManager (org.eclipse.jface.action.IContributionManager)1 Separator (org.eclipse.jface.action.Separator)1 DB2ObjectType (org.jkiss.dbeaver.ext.db2.editors.DB2ObjectType)1 DB2Index (org.jkiss.dbeaver.ext.db2.model.DB2Index)1 DB2Package (org.jkiss.dbeaver.ext.db2.model.DB2Package)1 DB2Routine (org.jkiss.dbeaver.ext.db2.model.DB2Routine)1 DB2Sequence (org.jkiss.dbeaver.ext.db2.model.DB2Sequence)1 DB2Table (org.jkiss.dbeaver.ext.db2.model.DB2Table)1 DB2TableBase (org.jkiss.dbeaver.ext.db2.model.DB2TableBase)1