Search in sources :

Example 21 with AbstractExecutionSource

use of org.jkiss.dbeaver.model.impl.AbstractExecutionSource in project dbeaver by dbeaver.

the class SQLScriptProcessor method executeStatement.

private void executeStatement(@NotNull DBCSession session, SQLQuery sqlQuery, long startTime) throws DBCException {
    SQLQueryDataContainer dataContainer = new SQLQueryDataContainer(() -> executionContext, sqlQuery, scriptContext, log);
    DBCExecutionSource source = new AbstractExecutionSource(dataContainer, session.getExecutionContext(), this, sqlQuery);
    final DBCStatement statement = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, 0, 0);
    DBExecUtils.setStatementFetchSize(statement, 0, 0, fetchSize);
    // Execute statement
    try {
        DBRProgressMonitor monitor = session.getProgressMonitor();
        log.debug(STAT_LOG_PREFIX + "Execute query\n" + sqlQuery.getText());
        boolean hasResultSet = statement.executeStatement();
        statistics.addExecuteTime(System.currentTimeMillis() - startTime);
        statistics.addStatementsCount();
        long updateCount = -1;
        while (true) {
            // Fetch data only if we have to fetch all results or if it is rs requested
            {
                if (hasResultSet) {
                    DBCResultSet resultSet = statement.openResultSet();
                    if (resultSet == null) {
                        // Kind of bug in the driver. It says it has resultset but returns null
                        break;
                    } else {
                        hasResultSet = fetchQueryData(session, resultSet, dataReceiver);
                    }
                }
            }
            if (!hasResultSet) {
                try {
                    updateCount = statement.getUpdateRowCount();
                    if (updateCount >= 0) {
                        statistics.addRowsUpdated(updateCount);
                    }
                } catch (DBCException e) {
                    // In some cases we can't read update count
                    // This is bad but we can live with it
                    // Just print a warning
                    log.warn("Can't obtain update count", e);
                }
            }
            if (!hasResultSet && updateCount < 0) {
                // Nothing else to fetch
                break;
            }
            if (session.getDataSource().getInfo().supportsMultipleResults()) {
                try {
                    hasResultSet = statement.nextResults();
                } catch (DBCException e) {
                    if (session.getDataSource().getInfo().isMultipleResultsFetchBroken()) {
                        log.error(e);
                        // #2792: Check this twice. Some drivers (e.g. Sybase jConnect)
                        // throw error on n'th result fetch - but it still can keep fetching next results
                        hasResultSet = statement.nextResults();
                    } else {
                        throw e;
                    }
                }
                updateCount = hasResultSet ? -1 : 0;
            } else {
                break;
            }
        }
    } finally {
        try {
            Throwable[] warnings = statement.getStatementWarnings();
            if (warnings != null) {
                for (Throwable warning : warnings) {
                    scriptContext.getOutputWriter().println(warning.getMessage());
                }
            }
        } catch (Throwable e) {
            log.warn("Can't read execution warnings", e);
        }
        try {
            statement.close();
        } catch (Throwable e) {
            log.error("Error closing statement", e);
        }
        log.debug(STAT_LOG_PREFIX + "Time: " + RuntimeUtils.formatExecutionTime(statistics.getExecuteTime()) + (statistics.getRowsFetched() >= 0 ? ", fetched " + statistics.getRowsFetched() + " row(s)" : "") + (statistics.getRowsUpdated() >= 0 ? ", updated " + statistics.getRowsUpdated() + " row(s)" : ""));
    }
}
Also used : SQLQueryDataContainer(org.jkiss.dbeaver.model.sql.data.SQLQueryDataContainer) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Aggregations

AbstractExecutionSource (org.jkiss.dbeaver.model.impl.AbstractExecutionSource)21 DBException (org.jkiss.dbeaver.DBException)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)4 DBDDataReceiver (org.jkiss.dbeaver.model.data.DBDDataReceiver)4 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)4 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)3 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)3 BigDecimal (java.math.BigDecimal)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 DBDAttributeConstraint (org.jkiss.dbeaver.model.data.DBDAttributeConstraint)2 DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)2 SQLQueryDataContainer (org.jkiss.dbeaver.model.sql.data.SQLQueryDataContainer)2 DBSDataContainer (org.jkiss.dbeaver.model.struct.DBSDataContainer)2 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)2 IOException (java.io.IOException)1 MockGeneratorDescriptor (org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor)1 MockValueGenerator (org.jkiss.dbeaver.ext.mockdata.model.MockValueGenerator)1 DBDAttributeValue (org.jkiss.dbeaver.model.data.DBDAttributeValue)1 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)1