Search in sources :

Example 66 with JDBCStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.

the class GreenplumTableTest method mockResults.

private JDBCResultSet mockResults(DBRProgressMonitor monitor) throws SQLException {
    JDBCSession mockSession = Mockito.mock(JDBCSession.class);
    JDBCStatement mockStatement = Mockito.mock(JDBCStatement.class);
    JDBCResultSet mockDCResults = Mockito.mock(JDBCResultSet.class);
    Mockito.when(mockContext.openSession(Mockito.eq(monitor), Mockito.eq(DBCExecutionPurpose.META), Mockito.anyString())).thenReturn(mockSession);
    Mockito.when(mockSession.createStatement()).thenReturn(mockStatement);
    Mockito.when(mockStatement.executeQuery(Mockito.anyString())).thenReturn(mockDCResults);
    return mockDCResults;
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 67 with JDBCStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.

the class MySQLCatalog method collectObjectStatistics.

@Override
public void collectObjectStatistics(DBRProgressMonitor monitor, boolean totalSizeOnly, boolean forceRefresh) throws DBException {
    if (hasStatistics && !forceRefresh) {
        return;
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load table status")) {
        try (JDBCStatement dbStat = session.createStatement()) {
            try (JDBCResultSet dbResult = dbStat.executeQuery("SHOW TABLE STATUS FROM " + DBUtils.getQuotedIdentifier(this))) {
                while (dbResult.next()) {
                    String tableName = dbResult.getString("Name");
                    MySQLTableBase table = tableCache.getObject(monitor, this, tableName);
                    if (table instanceof MySQLTable) {
                        ((MySQLTable) table).fetchAdditionalInfo(dbResult);
                    }
                }
            }
        } catch (SQLException e) {
            throw new DBCException(e, session.getExecutionContext());
        }
    } finally {
        hasStatistics = true;
    }
}
Also used : 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) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 68 with JDBCStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.

the class JDBCStructLookupCache 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(ModelMessages.error_not_connected_to_database);
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, owner, 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 {
                    while (dbResult.next()) {
                        OBJECT remoteObject = fetchObject(session, owner, dbResult);
                        if (isValidObject(monitor, owner, remoteObject)) {
                            return remoteObject;
                        }
                    }
                } finally {
                    dbResult.close();
                }
            }
            return null;
        }
    } catch (SQLException ex) {
        throw new DBException("Error loading object metadata from database", 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 69 with JDBCStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.

the class JDBCObjectCache method loadObjects.

protected synchronized void loadObjects(DBRProgressMonitor monitor, OWNER owner) throws DBException {
    if (isFullyCached() || monitor.isCanceled()) {
        return;
    }
    List<OBJECT> tmpObjectList = new ArrayList<>();
    DBPDataSource dataSource = owner.getDataSource();
    if (dataSource == null) {
        throw new DBException(ModelMessages.error_not_connected_to_database);
    }
    if (owner.isPersisted()) {
        // Load cache from database only for persisted objects
        try {
            try (JDBCSession session = DBUtils.openMetaSession(monitor, owner, "Load objects from " + owner.getName())) {
                try (JDBCStatement dbStat = prepareObjectsStatement(session, owner)) {
                    monitor.subTask("Load " + getCacheName());
                    dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
                    dbStat.executeStatement();
                    JDBCResultSet dbResult = dbStat.getResultSet();
                    if (dbResult != null) {
                        try {
                            while (dbResult.next()) {
                                if (monitor.isCanceled()) {
                                    return;
                                }
                                OBJECT object = fetchObject(session, owner, dbResult);
                                if (object == null || !isValidObject(monitor, owner, object)) {
                                    continue;
                                }
                                tmpObjectList.add(object);
                                // monitor.subTask(object.getName());
                                if (tmpObjectList.size() == maximumCacheSize) {
                                    log.warn("Maximum cache size exceeded (" + maximumCacheSize + ") in " + this);
                                    break;
                                }
                            }
                        } finally {
                            dbResult.close();
                        }
                    }
                }
            } catch (SQLException ex) {
                throw new DBException(ex, dataSource);
            } catch (DBException ex) {
                throw ex;
            } catch (Exception ex) {
                throw new DBException("Internal driver error", ex);
            }
        } catch (Exception e) {
            if (!handleCacheReadError(e)) {
                throw e;
            }
        }
    }
    addCustomObjects(tmpObjectList);
    Comparator<OBJECT> comparator = getListOrderComparator();
    if (comparator != null) {
        tmpObjectList.sort(comparator);
    }
    detectCaseSensitivity(owner);
    mergeCache(tmpObjectList);
    this.invalidateObjects(monitor, owner, new CacheIterator());
}
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) ArrayList(java.util.ArrayList) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLException(java.sql.SQLException) DBException(org.jkiss.dbeaver.DBException)

Example 70 with JDBCStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement in project dbeaver by dbeaver.

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(ModelMessages.error_not_connected_to_database);
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, owner, 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)

Aggregations

JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)70 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)40 SQLException (java.sql.SQLException)32 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)26 DBException (org.jkiss.dbeaver.DBException)24 NotNull (org.jkiss.code.NotNull)18 ArrayList (java.util.ArrayList)16 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)12 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)12 DBCException (org.jkiss.dbeaver.model.exec.DBCException)10 SQLWarning (java.sql.SQLWarning)8 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)8 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 JDBCStatementImpl (org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl)6 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)4 BigDecimal (java.math.BigDecimal)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)2 SQLXML (java.sql.SQLXML)2 Tree (org.eclipse.swt.widgets.Tree)2 TreeColumn (org.eclipse.swt.widgets.TreeColumn)2