Commit e7e7fb78 authored by Vadim Zeitlin's avatar Vadim Zeitlin

Return -1 from get_affected_rows() in ODBC backend if unknown

It is not always possible to get the number of affected rows after a
partially executed statement, some drivers provide this information, but
some others, notable MS SQL native client one, do not.

Return -1 from get_affected_rows() to at least allow distinguishing this
situation from the one in which we can really know that no rows were
affected, in which case 0 would be returned.

Notice that this indirectly fixes the "Get affected rows" unit test
which was broken by the previous fix of #495 as it made the partial
insert actually work, while it was completely broken before because
"1\0" string couldn't be converted to a number and so the check for
get_affected_rows() wasn't being executed at all. But now that the
string is the correct "1", the check started being executed and failing.
parent 97a4728c
......@@ -154,20 +154,25 @@ odbc_statement_backend::execute(int number)
// ODBC functions, in order to not lose the error message.
const odbc_soci_error err(SQL_HANDLE_STMT, hstmt_, "executing statement");
// There is no universal way to determine the number of affected rows
// after a failed update.
rowsAffected_ = -1LL;
// If executing bulk operation a partial
// number of rows affected may be available.
if (hasVectorUseElements_)
rowsAffected_ = 0;
SQLLEN res = 0;
// SQLRowCount will return error after a partially executed statement.
// SQL_DIAG_ROW_COUNT returns the same info but must be collected immediatelly after the execution.
rc = SQLGetDiagField(SQL_HANDLE_STMT, hstmt_, 0, SQL_DIAG_ROW_COUNT, &res, 0, NULL);
if (!is_odbc_error(rc) && res > 0) // 'res' will be -1 for the where the statement failed.
if (!is_odbc_error(rc) && res != -1)
if (rowsAffected_ == -1LL)
rowsAffected_ = res;
rowsAffected_ += res;
--rows_processed; // Avoid unnecessary calls to SQLGetDiagField
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment