Search in sources :

Example 56 with DBException

use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.

the class DB2IndexColumn method getDependentView.

// -----------------
// Helpers
// -----------------
private DB2View getDependentView(DBRProgressMonitor monitor, DB2DataSource db2DataSource, String indexSchema, String indexName) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, db2DataSource, "Read Index view dependency")) {
        try (JDBCPreparedStatement stmtSel = session.prepareStatement(I_DEP)) {
            stmtSel.setString(1, indexSchema);
            stmtSel.setString(2, indexName);
            JDBCResultSet dbResult = stmtSel.executeQuery();
            if (dbResult.next()) {
                String viewSchema = dbResult.getString("BSCHEMA").trim();
                String viewName = dbResult.getString("BNAME");
                return DB2Utils.findViewBySchemaNameAndName(monitor, db2DataSource, viewSchema, viewName);
            } else {
                return null;
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, db2DataSource);
    }
}
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)

Example 57 with DBException

use of org.jkiss.dbeaver.DBException 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 58 with DBException

use of org.jkiss.dbeaver.DBException 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);
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DBSObjectType(org.jkiss.dbeaver.model.struct.DBSObjectType) ArrayList(java.util.ArrayList) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema) NotNull(org.jkiss.code.NotNull)

Example 59 with DBException

use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.

the class BookmarksHandlerImpl method createBookmark.

public static void createBookmark(final DBNDatabaseNode node, String title, IFolder folder) throws DBException {
    if (folder == null) {
        final IProject project = node.getOwnerProject();
        if (project != null) {
            folder = getBookmarksFolder(project, true);
        }
    }
    if (folder == null) {
        throw new DBException("Can't detect folder for bookmark");
    }
    IFile file = ContentUtils.getUniqueFile(folder, CommonUtils.escapeFileName(title), BOOKMARK_EXT);
    updateBookmark(node, title, file);
}
Also used : DBException(org.jkiss.dbeaver.DBException) IFile(org.eclipse.core.resources.IFile) IProject(org.eclipse.core.resources.IProject)

Example 60 with DBException

use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.

the class DBNBookmark method rename.

@Override
public void rename(DBRProgressMonitor monitor, String newName) throws DBException {
    IFile file = (IFile) getResource();
    if (file != null) {
        try {
            storage.setTitle(newName);
            InputStream data = storage.serialize();
            file.setContents(data, true, false, RuntimeUtils.getNestedMonitor(monitor));
        } catch (Exception e) {
            throw new DBException("Can't rename bookmark", e);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) IFile(org.eclipse.core.resources.IFile) InputStream(java.io.InputStream) CoreException(org.eclipse.core.runtime.CoreException) DBException(org.jkiss.dbeaver.DBException)

Aggregations

DBException (org.jkiss.dbeaver.DBException)232 SQLException (java.sql.SQLException)58 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)51 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)50 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)43 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)42 ArrayList (java.util.ArrayList)37 InvocationTargetException (java.lang.reflect.InvocationTargetException)23 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)23 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)16 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)14 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)13 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)13 GridData (org.eclipse.swt.layout.GridData)12 DBCException (org.jkiss.dbeaver.model.exec.DBCException)12 CoreException (org.eclipse.core.runtime.CoreException)11 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 IStatus (org.eclipse.core.runtime.IStatus)9 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)8 XMLException (org.jkiss.utils.xml.XMLException)8