Search in sources :

Example 56 with JDBCStatement

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

the class ExasolViewCache method prepareChildrenStatement.

@Override
protected JDBCStatement prepareChildrenStatement(@NotNull JDBCSession session, @NotNull ExasolSchema exasolSchema, @Nullable ExasolView forView) throws SQLException {
    String sql;
    String tablePrefix = exasolSchema.getDataSource().getTablePrefix(ExasolSysTablePrefix.ALL);
    if (exasolSchema.getName().equals("SYS") || exasolSchema.getName().equals("EXA_STATISTICS")) {
        if (forView != null) {
            sql = String.format(SQL_COLS_SYS_VIEW, ExasolUtils.quoteString(exasolSchema.getName()), ExasolUtils.quoteString(forView.getName()));
        } else {
            sql = String.format(SQL_COLS_SYS_ALL, ExasolUtils.quoteString(exasolSchema.getName()));
        }
    } else {
        if (forView != null) {
            sql = String.format(SQL_COLS_VIEW, tablePrefix, ExasolUtils.quoteString(exasolSchema.getName()), ExasolUtils.quoteString(forView.getName()));
        } else {
            sql = String.format(SQL_COLS_ALL, tablePrefix, ExasolUtils.quoteString(exasolSchema.getName()));
        }
    }
    JDBCStatement dbStat = session.createStatement();
    dbStat.setQueryString(sql);
    return dbStat;
}
Also used : JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)

Example 57 with JDBCStatement

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

the class ExasolViewCache method prepareObjectsStatement.

@NotNull
@Override
protected JDBCStatement prepareObjectsStatement(@NotNull JDBCSession session, @NotNull ExasolSchema exasolSchema) throws SQLException {
    String sql = String.format(SQL_VIEWS, exasolSchema.getName(), exasolSchema.getName());
    JDBCStatement dbstat = session.createStatement();
    dbstat.setQueryString(sql);
    return dbstat;
}
Also used : JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) NotNull(org.jkiss.code.NotNull)

Example 58 with JDBCStatement

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

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 59 with JDBCStatement

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

the class JDBCTable method readData.

// //////////////////////////////////////////////////////////////////
// Select
@NotNull
@Override
public DBCStatistics readData(@NotNull DBCExecutionSource source, @NotNull DBCSession session, @NotNull DBDDataReceiver dataReceiver, @Nullable DBDDataFilter dataFilter, long firstRow, long maxRows, long flags, int fetchSize) throws DBCException {
    DBCStatistics statistics = new DBCStatistics();
    boolean hasLimits = firstRow >= 0 && maxRows > 0;
    DBPDataSource dataSource = session.getDataSource();
    DBRProgressMonitor monitor = session.getProgressMonitor();
    try {
        readRequiredMeta(monitor);
    } catch (DBException e) {
        log.warn(e);
    }
    DBDPseudoAttribute rowIdAttribute = (flags & FLAG_READ_PSEUDO) != 0 ? DBUtils.getRowIdAttribute(this) : null;
    // Always use alias if we have criteria or ROWID.
    // Some criteria doesn't work without alias
    // (e.g. structured attributes in Oracle requires table alias)
    String tableAlias = null;
    if ((dataFilter != null && dataFilter.hasConditions()) || rowIdAttribute != null) {
        {
            if (dataSource.getSQLDialect().supportsAliasInSelect()) {
                tableAlias = DEFAULT_TABLE_ALIAS;
            }
        }
    }
    if (rowIdAttribute != null && tableAlias == null) {
        log.warn("Can't query ROWID - table alias not supported");
        rowIdAttribute = null;
    }
    StringBuilder query = new StringBuilder(100);
    query.append("SELECT ");
    appendSelectSource(monitor, query, tableAlias, rowIdAttribute);
    query.append(" FROM ").append(getFullyQualifiedName(DBPEvaluationContext.DML));
    if (tableAlias != null) {
        // $NON-NLS-1$
        query.append(" ").append(tableAlias);
    }
    SQLUtils.appendQueryConditions(dataSource, query, tableAlias, dataFilter);
    SQLUtils.appendQueryOrder(dataSource, query, tableAlias, dataFilter);
    String sqlQuery = query.toString();
    statistics.setQueryText(sqlQuery);
    monitor.subTask(ModelMessages.model_jdbc_fetch_table_data);
    try (DBCStatement dbStat = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, firstRow, maxRows)) {
        if (monitor.isCanceled()) {
            return statistics;
        }
        if (dbStat instanceof JDBCStatement && (fetchSize > 0 || maxRows > 0)) {
            DBExecUtils.setStatementFetchSize(dbStat, firstRow, maxRows, fetchSize);
        }
        long startTime = System.currentTimeMillis();
        boolean executeResult = dbStat.executeStatement();
        statistics.setExecuteTime(System.currentTimeMillis() - startTime);
        if (executeResult) {
            DBCResultSet dbResult = dbStat.openResultSet();
            if (dbResult != null && !monitor.isCanceled()) {
                try {
                    dataReceiver.fetchStart(session, dbResult, firstRow, maxRows);
                    DBFetchProgress fetchProgress = new DBFetchProgress(session.getProgressMonitor());
                    while (dbResult.nextRow()) {
                        if (fetchProgress.isCanceled() || (hasLimits && fetchProgress.isMaxRowsFetched(maxRows))) {
                            // Fetch not more than max rows
                            break;
                        }
                        dataReceiver.fetchRow(session, dbResult);
                        fetchProgress.monitorRowFetch();
                    }
                    fetchProgress.dumpStatistics(statistics);
                } finally {
                    // First - close cursor
                    try {
                        dbResult.close();
                    } catch (Throwable e) {
                        // $NON-NLS-1$
                        log.error("Error closing result set", e);
                    }
                    // Then - signal that fetch was ended
                    try {
                        dataReceiver.fetchEnd(session, dbResult);
                    } catch (Throwable e) {
                        // $NON-NLS-1$
                        log.error("Error while finishing result set fetch", e);
                    }
                }
            }
        }
        return statistics;
    } finally {
        dataReceiver.close();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) NotNull(org.jkiss.code.NotNull)

Example 60 with JDBCStatement

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

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)

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