From 97916660cebbf590b6c1cdaa586930eca8997b8e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 10 Sep 2010 12:57:53 +0200 Subject: Add support for result caching --- odb/mysql/result.hxx | 5 ++++- odb/mysql/result.txx | 7 +++++++ odb/mysql/statement.cxx | 31 +++++++++++++++++++++++++++---- odb/mysql/statement.hxx | 5 +++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/odb/mysql/result.hxx b/odb/mysql/result.hxx index 812cb68..951ae91 100644 --- a/odb/mysql/result.hxx +++ b/odb/mysql/result.hxx @@ -40,9 +40,12 @@ namespace odb virtual void current (T&); - void + virtual void next (); + virtual void + cache (); + using odb::result_impl::current; private: diff --git a/odb/mysql/result.txx b/odb/mysql/result.txx index 9bbdf26..d0a65fa 100644 --- a/odb/mysql/result.txx +++ b/odb/mysql/result.txx @@ -73,5 +73,12 @@ namespace odb } } } + + template + void result_impl:: + cache () + { + statement_->cache (); + } } } diff --git a/odb/mysql/statement.cxx b/odb/mysql/statement.cxx index 0cab7cc..df1ce03 100644 --- a/odb/mysql/statement.cxx +++ b/odb/mysql/statement.cxx @@ -41,7 +41,9 @@ namespace odb query_statement:: ~query_statement () { - if (conn_.active () == this) + statement* as (conn_.active ()); + + if (cached_ || as == this) { try { @@ -51,6 +53,9 @@ namespace odb { } } + + if (as == this) + conn_.active (0); } query_statement:: @@ -59,6 +64,7 @@ namespace odb binding& image, MYSQL_BIND* parameters) : statement (conn), + cached_ (false), image_ (image), image_version_ (0), parameters_ (parameters) @@ -73,6 +79,9 @@ namespace odb if (statement* a = conn_.active ()) a->cancel (); + if (cached_) + free_result (); + if (mysql_stmt_reset (stmt_)) throw database_exception (stmt_); @@ -105,6 +114,15 @@ namespace odb conn_.active (this); } + void query_statement:: + cache () + { + if (mysql_stmt_store_result (stmt_)) + throw database_exception (stmt_); + + cached_ = true; + } + query_statement::result query_statement:: fetch () { @@ -152,16 +170,21 @@ namespace odb void query_statement:: free_result () { + cached_ = false; + if (mysql_stmt_free_result (stmt_)) throw database_exception (stmt_); - - conn_.active (0); } void query_statement:: cancel () { - free_result (); + // If we cached the result, don't free it just yet. + // + if (!cached_) + free_result (); + + conn_.active (0); } // persist_statement diff --git a/odb/mysql/statement.hxx b/odb/mysql/statement.hxx index 05deb4d..bcba2c6 100644 --- a/odb/mysql/statement.hxx +++ b/odb/mysql/statement.hxx @@ -86,6 +86,9 @@ namespace odb void execute (); + void + cache (); + result fetch (); @@ -103,6 +106,8 @@ namespace odb query_statement& operator= (const query_statement&); private: + bool cached_; + binding& image_; std::size_t image_version_; -- cgit v1.1