Search in sources :

Example 21 with JDBCResultSet

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet 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 22 with JDBCResultSet

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet 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 23 with JDBCResultSet

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

the class DB2Utils method getListOfUsableTsForExplain.

// ------------------------
// EXPLAIN
// ------------------------
public static List<String> getListOfUsableTsForExplain(DBRProgressMonitor monitor, JDBCSession session) throws SQLException {
    LOG.debug("Get List Of Usable Tablespaces For Explain Tables");
    List<String> listTablespaces = new ArrayList<>();
    try (JDBCPreparedStatement dbStat = session.prepareStatement(SEL_LIST_TS_EXPLAIN)) {
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            while (dbResult.next()) {
                listTablespaces.add(dbResult.getString(1));
            }
        }
    }
    return listTablespaces;
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) ArrayList(java.util.ArrayList) DB2XMLString(org.jkiss.dbeaver.ext.db2.info.DB2XMLString)

Example 24 with JDBCResultSet

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet 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 25 with JDBCResultSet

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

JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)82 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)66 SQLException (java.sql.SQLException)57 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)54 DBException (org.jkiss.dbeaver.DBException)53 ArrayList (java.util.ArrayList)20 DBCException (org.jkiss.dbeaver.model.exec.DBCException)15 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)12 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)10 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)5 GenericMetaObject (org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject)4 NotNull (org.jkiss.code.NotNull)3 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)3 DB2DataSource (org.jkiss.dbeaver.ext.db2.model.DB2DataSource)3 GenericDataSource (org.jkiss.dbeaver.ext.generic.model.GenericDataSource)3 Matcher (java.util.regex.Matcher)2 DB2Parameter (org.jkiss.dbeaver.ext.db2.info.DB2Parameter)2 DB2Schema (org.jkiss.dbeaver.ext.db2.model.DB2Schema)2