Skip to content

Commit

Permalink
Implement get_affected_rows() for ODBC backend.
Browse files Browse the repository at this point in the history
Use SQLRowCount() to implement this but also check for SQL_NO_DATA to avoid
calling it unnecessarily when we are sure that no rows were affected anyhow.

Signed-off-by: Vadim Zeitlin <vz-soci@zeitlins.org>
  • Loading branch information
vadz committed Jul 6, 2012
1 parent cbeda1d commit eba063a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/backends/odbc/soci-odbc.h
Expand Up @@ -197,6 +197,7 @@ struct odbc_statement_backend : details::statement_backend
bool hasVectorUseElements_;
bool boundByName_;
bool boundByPos_;
bool lastNoData_; // true if last query returned SQL_NO_DATA

std::string query_;
std::vector<std::string> names_; // list of names for named binds
Expand Down
22 changes: 19 additions & 3 deletions src/backends/odbc/statement.cpp
Expand Up @@ -24,7 +24,8 @@ using namespace soci::details;

odbc_statement_backend::odbc_statement_backend(odbc_session_backend &session)
: session_(session), hstmt_(0), numRowsFetched_(0),
hasVectorUseElements_(false), boundByName_(false), boundByPos_(false)
hasVectorUseElements_(false), boundByName_(false), boundByPos_(false),
lastNoData_(false)
{
}

Expand Down Expand Up @@ -163,6 +164,8 @@ odbc_statement_backend::execute(int number)
"Statement Execute");
}

lastNoData_ = rc == SQL_NO_DATA;

SQLSMALLINT colCount;
SQLNumResultCols(hstmt_, &colCount);

Expand Down Expand Up @@ -201,8 +204,21 @@ odbc_statement_backend::fetch(int number)

long long odbc_statement_backend::get_affected_rows()
{
// ...
return -1;
// Calling SQLRowCount() when the last call to SQLExecute() returned
// SQL_NO_DATA can fail, so simply always return 0 in this case as we know
// that nothing was done anyhow.
if (lastNoData_)
return 0;

SQLLEN res;
SQLRETURN rc = SQLRowCount(hstmt_, &res);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_STMT, hstmt_,
"Getting number of affected rows");
}

return res;
}

int odbc_statement_backend::get_number_of_rows()
Expand Down

0 comments on commit eba063a

Please sign in to comment.