mirror of
https://github.com/allankoechke/soci.git
synced 2026-11-30 10:58:24 +00:00
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.
This commit is contained in:
@@ -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;
|
||||
|
||||
do
|
||||
{
|
||||
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;
|
||||
else
|
||||
rowsAffected_ += res;
|
||||
}
|
||||
--rows_processed; // Avoid unnecessary calls to SQLGetDiagField
|
||||
|
||||
Reference in New Issue
Block a user