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)" : ""));
}
}
Aggregations