Search in sources :

Example 11 with JDBCSession

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.

the class JDBCObjectLookupCache method reloadObject.

protected OBJECT reloadObject(@NotNull DBRProgressMonitor monitor, @NotNull OWNER owner, @Nullable OBJECT object, @Nullable String objectName) throws DBException {
    DBPDataSource dataSource = owner.getDataSource();
    if (dataSource == null) {
        throw new DBException("Not connected to database");
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, object == null ? "Load object '" + objectName + "' from " + owner.getName() : "Reload object '" + object + "' from " + owner.getName())) {
        try (JDBCStatement dbStat = prepareLookupStatement(session, owner, object, objectName)) {
            dbStat.setFetchSize(1);
            dbStat.executeStatement();
            JDBCResultSet dbResult = dbStat.getResultSet();
            if (dbResult != null) {
                try {
                    if (dbResult.next()) {
                        return fetchObject(session, owner, dbResult);
                    }
                } finally {
                    dbResult.close();
                }
            }
            return null;
        }
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 12 with JDBCSession

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.

the class JDBCStructCache method loadChildren.

/**
     * Reads children objects from database
     * 
     * @param monitor
     *            monitor
     * @param forObject
     *            object for which to read children. If null then reads children for all objects in this container.
     * @throws org.jkiss.dbeaver.DBException
     *             on error
     */
public synchronized void loadChildren(DBRProgressMonitor monitor, OWNER owner, @Nullable final OBJECT forObject) throws DBException {
    if ((forObject == null && this.childrenCached) || (forObject != null && (!forObject.isPersisted() || isChildrenCached(forObject))) || monitor.isCanceled()) {
        return;
    }
    if (forObject == null) {
        // If we have some child objects read before that - do not clear them.
        // We have to reuse them because there could be some references in cached model
        //clearChildrenCache(null);
        super.loadObjects(monitor, owner);
    }
    DBPDataSource dataSource = owner.getDataSource();
    if (dataSource == null) {
        throw new DBException("Not connected to database");
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Load child objects")) {
        Map<OBJECT, List<CHILD>> objectMap = new HashMap<>();
        // Load columns
        try (JDBCStatement dbStat = prepareChildrenStatement(session, owner, forObject)) {
            dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
            dbStat.executeStatement();
            JDBCResultSet dbResult = dbStat.getResultSet();
            if (dbResult != null) {
                try {
                    while (dbResult.next()) {
                        if (monitor.isCanceled()) {
                            break;
                        }
                        String objectName;
                        if (objectNameColumn instanceof Number) {
                            objectName = JDBCUtils.safeGetString(dbResult, ((Number) objectNameColumn).intValue());
                        } else {
                            objectName = JDBCUtils.safeGetStringTrimmed(dbResult, objectNameColumn.toString());
                        }
                        if (objectName == null) {
                            log.debug("NULL object name in " + this);
                            continue;
                        }
                        OBJECT object = forObject;
                        if (object == null) {
                            object = super.getCachedObject(objectName);
                            if (object == null) {
                                log.debug("Object '" + objectName + "' not found");
                                continue;
                            }
                        }
                        if (isChildrenCached(object)) {
                            // Already read
                            continue;
                        }
                        CHILD child = fetchChild(session, owner, object, dbResult);
                        if (child == null) {
                            continue;
                        }
                        // Add to map
                        List<CHILD> children = objectMap.get(object);
                        if (children == null) {
                            children = new ArrayList<>();
                            objectMap.put(object, children);
                        }
                        children.add(child);
                    }
                    if (monitor.isCanceled()) {
                        return;
                    }
                    // All children are read. Now assign them to parents
                    for (Map.Entry<OBJECT, List<CHILD>> colEntry : objectMap.entrySet()) {
                        if (!isChildrenCached(colEntry.getKey())) {
                            // isChildrenCached may return true if the same cache was read in other thread
                            // just skip
                            cacheChildren(colEntry.getKey(), colEntry.getValue());
                        }
                    }
                    if (forObject == null) {
                        if (objectMap.isEmpty()) {
                        // Nothing was read. May be it means empty list of children
                        // but possibly this feature is not supported [JDBC: SQLite]
                        } else {
                            // Now set empty column list for other tables
                            for (OBJECT tmpObject : getAllObjects(monitor, owner)) {
                                if (!isChildrenCached(tmpObject) && !objectMap.containsKey(tmpObject)) {
                                    cacheChildren(tmpObject, new ArrayList<CHILD>());
                                }
                            }
                            this.childrenCached = true;
                        }
                    } else if (!objectMap.containsKey(forObject)) {
                        cacheChildren(forObject, new ArrayList<CHILD>());
                    }
                } finally {
                    dbResult.close();
                }
            }
        }
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) SQLException(java.sql.SQLException) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 13 with JDBCSession

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.

the class DB2Utils method checkExplainTables.

public static Boolean checkExplainTables(DBRProgressMonitor monitor, DB2DataSource dataSource, String explainTableSchemaName) throws DBCException {
    LOG.debug("Check EXPLAIN tables in '" + explainTableSchemaName + "'");
    monitor.beginTask("Check EXPLAIN tables", 1);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Verify EXPLAIN tables")) {
        // First Check with given schema
        try (JDBCCallableStatement stmtSP = session.prepareCall(CALL_INST_OBJ)) {
            // EXPLAIN
            stmtSP.setString(1, "EXPLAIN");
            // Verify
            stmtSP.setString(2, "V");
            // Tablespace
            stmtSP.setString(3, "");
            // Schema
            stmtSP.setString(4, explainTableSchemaName);
            stmtSP.execute();
            LOG.debug("EXPLAIN tables with schema " + explainTableSchemaName + " found.");
            return true;
        } catch (SQLException e) {
            LOG.debug("RC:" + e.getErrorCode() + " SQLState:" + e.getSQLState() + " " + e.getMessage());
            if (e.getErrorCode() == CALL_INST_OBJ_BAD_RC) {
                LOG.debug("No valid EXPLAIN tables found in schema '" + explainTableSchemaName + "'.");
                return false;
            }
            throw new DBCException(e, dataSource);
        }
    } finally {
        monitor.done();
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) SQLException(java.sql.SQLException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) JDBCCallableStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement)

Example 14 with JDBCSession

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession in project dbeaver by serge-rider.

the class DB2DataSource method initialize.

// -----------------------
// Initialisation/Structure
// -----------------------
@Override
public void initialize(@NotNull DBRProgressMonitor monitor) throws DBException {
    super.initialize(monitor);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load data source meta info")) {
        // First try to get active schema from special register 'CURRENT SCHEMA'
        this.activeSchemaName = determineActiveSchema(session);
        this.db2CurrentUserPrivileges = new DB2CurrentUserPrivileges(monitor, session, activeSchemaName, this);
    } catch (SQLException e) {
        LOG.warn("Error reading active schema", e);
    }
    try {
        this.dataTypeCache.getAllObjects(monitor, this);
    } catch (DBException e) {
        LOG.warn("Error reading types info", e);
        this.dataTypeCache.setCache(Collections.<DB2DataType>emptyList());
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException)

Example 15 with JDBCSession

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCSession 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)

Aggregations

JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)73 SQLException (java.sql.SQLException)67 DBException (org.jkiss.dbeaver.DBException)54 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)54 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)46 DBCException (org.jkiss.dbeaver.model.exec.DBCException)16 ArrayList (java.util.ArrayList)15 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)7 NotNull (org.jkiss.code.NotNull)5 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)5 DatabaseMetaData (java.sql.DatabaseMetaData)3 GenericDataSource (org.jkiss.dbeaver.ext.generic.model.GenericDataSource)3 JDBCCallableStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement)3 Matcher (java.util.regex.Matcher)2 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)2 GenericMetaObject (org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject)2 JDBCDatabaseMetaData (org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData)2 Property (org.jkiss.dbeaver.model.meta.Property)2 DBSObjectType (org.jkiss.dbeaver.model.struct.DBSObjectType)2 DBSProcedureType (org.jkiss.dbeaver.model.struct.rdb.DBSProcedureType)2