aboutsummaryrefslogtreecommitdiff
path: root/odb/mssql/statement.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2012-03-07 10:21:07 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2012-03-07 10:21:07 +0200
commitc1b8b6f16e4881e57aa0da67e6f09ad71ee6099d (patch)
treeda1f5c81451d5a75f5ed12ae6752eb3feac969f7 /odb/mssql/statement.cxx
parent2534b3aa626d6a426559c556b20faf7b69b47717 (diff)
Use RAII to free select statement results
Diffstat (limited to 'odb/mssql/statement.cxx')
-rw-r--r--odb/mssql/statement.cxx51
1 files changed, 17 insertions, 34 deletions
diff --git a/odb/mssql/statement.cxx b/odb/mssql/statement.cxx
index 8c3870d..e6cd7b8 100644
--- a/odb/mssql/statement.cxx
+++ b/odb/mssql/statement.cxx
@@ -694,13 +694,17 @@ namespace odb
//
// select_statement
//
+ select_statement::
+ ~select_statement ()
+ {
+ }
select_statement::
select_statement (connection& conn,
const string& t,
binding& param,
binding& result)
- : statement (conn, t), result_ (result), executed_ (false)
+ : statement (conn, t), result_ (result)
{
bind_param (param.bind, param.count);
first_long_ = bind_result (result.bind, result.count);
@@ -712,7 +716,7 @@ namespace odb
binding& param,
binding& result,
bool ct)
- : statement (conn, t, ct), result_ (result), executed_ (false)
+ : statement (conn, t, ct), result_ (result)
{
bind_param (param.bind, param.count);
first_long_ = bind_result (result.bind, result.count);
@@ -720,7 +724,7 @@ namespace odb
select_statement::
select_statement (connection& conn, const string& t, binding& result)
- : statement (conn, t), result_ (result), executed_ (false)
+ : statement (conn, t), result_ (result)
{
first_long_ = bind_result (result.bind, result.count);
}
@@ -730,38 +734,18 @@ namespace odb
const char* t,
binding& result,
bool ct)
- : statement (conn, t, ct), result_ (result), executed_ (false)
+ : statement (conn, t, ct), result_ (result)
{
first_long_ = bind_result (result.bind, result.count);
}
- select_statement::
- ~select_statement ()
- {
- if (executed_)
- {
- try
- {
- free_result ();
- }
- catch (...)
- {
- }
- }
- }
-
void select_statement::
execute ()
{
- if (executed_)
- free_result ();
-
SQLRETURN r (statement::execute ());
if (!SQL_SUCCEEDED (r))
translate_error (r, conn_, stmt_);
-
- executed_ = true;
}
select_statement::result select_statement::
@@ -786,17 +770,16 @@ namespace odb
void select_statement::
free_result ()
{
- if (executed_)
- {
- // If we cannot close the cursor, there is no point in trying again.
- //
- executed_ = false;
-
- SQLRETURN r (SQLCloseCursor (stmt_));
+ // Use SQLFreeStmt(SQL_CLOSE) instead of SQLCloseCursor() to avoid an
+ // error if a cursor is already closed. This can happens, for example,
+ // if we are trying to close the cursor after the transaction has been
+ // committed (e.g., when destroying the query result) which also closes
+ // the cursor.
+ //
+ SQLRETURN r (SQLFreeStmt (stmt_, SQL_CLOSE));
- if (!SQL_SUCCEEDED (r))
- translate_error (r, conn_, stmt_);
- }
+ if (!SQL_SUCCEEDED (r))
+ translate_error (r, conn_, stmt_);
}
//