From 9542fdc4434c3af3eaed91bf10e5b487044d8cc7 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 7 Mar 2012 10:21:07 +0200 Subject: Use RAII to free select statement results --- odb/mysql/object-result.txx | 16 ++++++++++++++++ odb/mysql/statement.cxx | 39 +++++++++++++++++++-------------------- odb/mysql/statement.hxx | 14 ++++++++++++++ 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:: ~object_result_impl () { + if (!this->end_) + statement_->free_result (); } template @@ -115,6 +117,9 @@ namespace odb this->end_ = count_ > statement_->result_size (); else fetch (); + + if (this->end_) + statement_->free_result (); } template @@ -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:: ~object_result_impl_no_id () { + if (!this->end_) + statement_->free_result (); } template @@ -260,6 +270,9 @@ namespace odb this->end_ = count_ > statement_->result_size (); else fetch (); + + if (this->end_) + statement_->free_result (); } template @@ -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:: ~view_result_impl () { + if (!this->end_) + statement_->free_result (); } template @@ -60,6 +62,9 @@ namespace odb this->end_ = count_ > statement_->result_size (); else fetch (); + + if (this->end_) + statement_->free_result (); } template @@ -132,7 +137,10 @@ namespace odb statement_->cache (); if (count_ >= statement_->result_size ()) + { + statement_->free_result (); this->end_ = true; + } } } -- cgit v1.1