ODBC support for get_affected_rows() during bulk operations.

This commit is contained in:
Ricardo Andrade
2013-03-11 13:29:00 -05:00
parent c97a666ef6
commit 5254f963ac
3 changed files with 53 additions and 26 deletions

View File

@@ -226,7 +226,8 @@ struct odbc_statement_backend : details::statement_backend
bool hasVectorUseElements_;
bool boundByName_;
bool boundByPos_;
bool lastNoData_; // true if last query returned SQL_NO_DATA
long long rowsAffected_; // number of rows affected by the last operation
std::string query_;
std::vector<std::string> names_; // list of names for named binds
@@ -403,7 +404,7 @@ inline bool odbc_standard_type_backend_base::use_string_for_bigint() const
struct odbc_backend_factory : backend_factory
{
odbc_backend_factory() {}
odbc_backend_factory() {}
virtual odbc_session_backend * make_session(
std::string const &connectString) const;
};

View File

@@ -25,7 +25,7 @@ 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),
lastNoData_(false)
rowsAffected_(-1LL)
{
}
@@ -44,6 +44,8 @@ void odbc_statement_backend::alloc()
void odbc_statement_backend::clean_up()
{
rowsAffected_ = -1LL;
SQLFreeHandle(SQL_HANDLE_STMT, hstmt_);
}
@@ -160,12 +162,49 @@ odbc_statement_backend::execute(int number)
SQLRETURN rc = SQLExecute(hstmt_);
if (is_odbc_error(rc))
{
// 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.
{
rowsAffected_ += res;
}
--rows_processed; // Avoid unnecessary calls to SQLGetDiagField
}
// Move forward to the next result while there are rows processed.
while (rows_processed > 0 && SQLMoreResults(hstmt_) == SQL_SUCCESS);
}
throw odbc_soci_error(SQL_HANDLE_STMT, hstmt_,
"Statement Execute");
}
// We should preserve the number of rows affected here
// where we know for sure that a bulk operation was executed.
else
{
rowsAffected_ = 0;
lastNoData_ = rc == SQL_NO_DATA;
do {
SQLLEN res = 0;
SQLRETURN rc = SQLRowCount(hstmt_, &res);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_STMT, hstmt_,
"Getting number of affected rows");
}
rowsAffected_ += res;
}
// Move forward to the next result if executing a bulk operation.
while (hasVectorUseElements_ && SQLMoreResults(hstmt_) == SQL_SUCCESS);
}
SQLSMALLINT colCount;
SQLNumResultCols(hstmt_, &colCount);
@@ -204,21 +243,7 @@ odbc_statement_backend::fetch(int number)
long long odbc_statement_backend::get_affected_rows()
{
// 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;
return rowsAffected_;
}
int odbc_statement_backend::get_number_of_rows()

View File

@@ -3623,14 +3623,15 @@ void test_get_affected_rows()
try { st5.execute(true); }
catch(...) {}
// test the preserved 'number of rows
// affected' after a potential failure.
assert(st5.get_affected_rows() != 0);
// confirm the partial insertion.
int val = 0;
sql << "select val from soci_test", into(val);
assert(val == 1);
sql << "select count(val) from soci_test", into(val);
if(val != 0)
{
// test the preserved 'number of rows
// affected' after a potential failure.
assert(st5.get_affected_rows() != 0);
}
}
std::cout << "test get_affected_rows passed" << std::endl;