Fix ODBC backend behaviour when calling fetch() without results

This makes this backend more consistent with the other ones and fixes an
error in unit tests when using SQL Server introduced by da494153 (Fix
behaviour of fetch() without any data with PostgreSQL, 2026-02-06) and
which went unnoticed for a long time due to the problem fixed in the
parent commit.
This commit is contained in:
Vadim Zeitlin
2026-09-01 00:51:23 +02:00
parent d6c9988727
commit 028c8772c4
2 changed files with 17 additions and 1 deletions

View File

@@ -309,6 +309,11 @@ private:
// First row with the error for bulk operations or -1.
int error_row_ = -1;
// True if the last executed statement returned a result set, false if it
// didn't return anything (e.g. because it was an UPDATE) or wasn't
// executed yet.
bool hasResultSet_ = false;
};
struct SOCI_ODBC_DECL odbc_rowid_backend : details::rowid_backend

View File

@@ -243,7 +243,9 @@ odbc_statement_backend::execute(int number)
SQLSMALLINT colCount;
SQLNumResultCols(hstmt_, &colCount);
if (number > 0 && colCount > 0)
hasResultSet_ = colCount > 0;
if (number > 0 && hasResultSet_)
{
return fetch(number);
}
@@ -277,6 +279,15 @@ odbc_statement_backend::do_fetch(int beginRow, int endRow)
statement_backend::exec_fetch_result
odbc_statement_backend::fetch(int number)
{
if (!hasResultSet_)
{
// The last statement didn't return any results at all, so there is
// nothing to fetch: normally we shouldn't be even called in this case,
// but if we are, just do nothing instead of calling SQLFetch() which
// would fail with "invalid cursor state" error.
return ef_no_data;
}
numRowsFetched_ = 0;
for (auto & into : intos_)