diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-03-07 10:21:07 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-03-07 10:21:07 +0200 |
commit | 9542fdc4434c3af3eaed91bf10e5b487044d8cc7 (patch) | |
tree | 93a09e5995fee093cc048e585c29c3d88937dbdb | |
parent | 6b9ed661b4bf7f1a07ee34fe205d3e0802df535c (diff) |
Use RAII to free select statement results
-rw-r--r-- | odb/mysql/object-result.txx | 16 | ||||
-rw-r--r-- | odb/mysql/statement.cxx | 39 | ||||
-rw-r--r-- | odb/mysql/statement.hxx | 14 | ||||
-rw-r--r-- | odb/mysql/view-result.txx | 8 |
4 files changed, 57 insertions, 20 deletions
diff --git a/odb/mysql/object-result.txx b/odb/mysql/object-result.txx index 81770c3..8144578 100644 --- a/odb/mysql/object-result.txx +++ b/odb/mysql/object-result.txx @@ -21,6 +21,8 @@ namespace odb object_result_impl<T>:: ~object_result_impl () { + if (!this->end_) + statement_->free_result (); } template <typename T> @@ -115,6 +117,9 @@ namespace odb this->end_ = count_ > statement_->result_size (); else fetch (); + + if (this->end_) + statement_->free_result (); } template <typename T> @@ -193,7 +198,10 @@ namespace odb statement_->cache (); if (count_ >= statement_->result_size ()) + { + statement_->free_result (); this->end_ = true; + } } } @@ -215,6 +223,8 @@ namespace odb object_result_impl_no_id<T>:: ~object_result_impl_no_id () { + if (!this->end_) + statement_->free_result (); } template <typename T> @@ -260,6 +270,9 @@ namespace odb this->end_ = count_ > statement_->result_size (); else fetch (); + + if (this->end_) + statement_->free_result (); } template <typename T> @@ -333,7 +346,10 @@ namespace odb statement_->cache (); if (count_ >= statement_->result_size ()) + { + statement_->free_result (); this->end_ = true; + } } } diff --git a/odb/mysql/statement.cxx b/odb/mysql/statement.cxx index 9278138..6882a27 100644 --- a/odb/mysql/statement.cxx +++ b/odb/mysql/statement.cxx @@ -104,16 +104,7 @@ namespace odb select_statement:: ~select_statement () { - if (cached_ || conn_.active () == this) - { - try - { - free_result (); - } - catch (...) - { - } - } + assert (freed_); } select_statement:: @@ -124,6 +115,7 @@ namespace odb : statement (conn, t), end_ (false), cached_ (false), + freed_ (true), rows_ (0), param_ (¶m), param_version_ (0), @@ -141,6 +133,7 @@ namespace odb : statement (conn, t, ct), end_ (false), cached_ (false), + freed_ (true), rows_ (0), param_ (¶m), param_version_ (0), @@ -154,6 +147,7 @@ namespace odb : statement (conn, t), end_ (false), cached_ (false), + freed_ (true), rows_ (0), param_ (0), result_ (result), @@ -169,6 +163,7 @@ namespace odb : statement (conn, t, ct), end_ (false), cached_ (false), + freed_ (true), rows_ (0), param_ (0), result_ (result), @@ -179,10 +174,9 @@ namespace odb void select_statement:: execute () { - conn_.clear (); + assert (freed_); - if (cached_) - free_result (); + conn_.clear (); end_ = false; rows_ = 0; @@ -209,6 +203,7 @@ namespace odb if (mysql_stmt_execute (stmt_)) translate_error (conn_, stmt_); + freed_ = false; conn_.active (this); } @@ -306,15 +301,19 @@ namespace odb void select_statement:: free_result () { - end_ = true; - cached_ = false; - rows_ = 0; + if (!freed_) + { + if (mysql_stmt_free_result (stmt_)) + translate_error (conn_, stmt_); - if (mysql_stmt_free_result (stmt_)) - translate_error (conn_, stmt_); + if (conn_.active () == this) + conn_.active (0); - if (conn_.active () == this) - conn_.active (0); + end_ = true; + cached_ = false; + freed_ = true; + rows_ = 0; + } } void select_statement:: diff --git a/odb/mysql/statement.hxx b/odb/mysql/statement.hxx index 9ab95e8..700a7f5 100644 --- a/odb/mysql/statement.hxx +++ b/odb/mysql/statement.hxx @@ -146,6 +146,7 @@ namespace odb private: bool end_; bool cached_; + bool freed_; std::size_t rows_; std::size_t size_; @@ -156,6 +157,19 @@ namespace odb std::size_t result_version_; }; + struct LIBODB_MYSQL_EXPORT auto_result + { + explicit auto_result (select_statement& s): s_ (s) {} + ~auto_result () {s_.free_result ();} + + private: + auto_result (const auto_result&); + auto_result& operator= (const auto_result&); + + private: + select_statement& s_; + }; + class LIBODB_MYSQL_EXPORT insert_statement: public statement { public: diff --git a/odb/mysql/view-result.txx b/odb/mysql/view-result.txx index 6161909..6f1896d 100644 --- a/odb/mysql/view-result.txx +++ b/odb/mysql/view-result.txx @@ -15,6 +15,8 @@ namespace odb view_result_impl<T>:: ~view_result_impl () { + if (!this->end_) + statement_->free_result (); } template <typename T> @@ -60,6 +62,9 @@ namespace odb this->end_ = count_ > statement_->result_size (); else fetch (); + + if (this->end_) + statement_->free_result (); } template <typename T> @@ -132,7 +137,10 @@ namespace odb statement_->cache (); if (count_ >= statement_->result_size ()) + { + statement_->free_result (); this->end_ = true; + } } } |