From 88f6ae3ce895a85f47178531cfed3632e5722160 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Fri, 27 May 2011 15:23:10 +0200 Subject: Add statement-cache and enable relevant functionality in connection --- odb/pgsql/connection.cxx | 47 ++---------------------------- odb/pgsql/connection.hxx | 57 +++++------------------------------- odb/pgsql/statement-cache.hxx | 67 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 94 deletions(-) create mode 100644 odb/pgsql/statement-cache.hxx diff --git a/odb/pgsql/connection.cxx b/odb/pgsql/connection.cxx index 37f359f..51e09b2 100644 --- a/odb/pgsql/connection.cxx +++ b/odb/pgsql/connection.cxx @@ -10,6 +10,7 @@ #include #include #include +#include using namespace std; @@ -20,9 +21,8 @@ namespace odb connection:: connection (database_type& db) : db_ (db), - handle_ (0) - // active_ (0), - // statement_cache_ (new statement_cache_type (*this)) + handle_ (0), + statement_cache_ (new statement_cache_type (*this)) { handle_ = PQconnectdb (db.conninfo ().c_str ()); @@ -40,48 +40,7 @@ namespace odb connection:: ~connection () { - // if (stmt_handles_.size () > 0) - // free_stmt_handles (); - PQfinish (handle_); } - - // void connection:: - // clear_ () - // { - // active_->cancel (); // Should clear itself from active_. - // } - - // MYSQL_STMT* connection:: - // alloc_stmt_handle () - // { - // MYSQL_STMT* stmt (mysql_stmt_init (handle_)); - - // if (stmt == 0) - // throw bad_alloc (); - - // return stmt; - // } - - // void connection:: - // free_stmt_handle (MYSQL_STMT* stmt) - // { - // if (active_ == 0) - // mysql_stmt_close (stmt); - // else - // stmt_handles_.push_back (stmt); - // } - - // void connection:: - // free_stmt_handles () - // { - // for (stmt_handles::iterator i (stmt_handles_.begin ()), - // e (stmt_handles_.end ()); i != e; ++i) - // { - // mysql_stmt_close (*i); - // } - - // stmt_handles_.clear (); - // } } } diff --git a/odb/pgsql/connection.hxx b/odb/pgsql/connection.hxx index 038ce70..6764794 100644 --- a/odb/pgsql/connection.hxx +++ b/odb/pgsql/connection.hxx @@ -32,7 +32,7 @@ namespace odb class LIBODB_PGSQL_EXPORT connection: public details::shared_base { public: - // typedef pgsql::statement_cache statement_cache_type; + typedef pgsql::statement_cache statement_cache_type; typedef pgsql::database database_type; virtual @@ -53,65 +53,22 @@ namespace odb return handle_; } - // statement_cache_type& - // statement_cache () - // { - // return *statement_cache_; - // } - - public: - // statement* - // active () - // { - // return active_; - // } - - // void - // active (statement* s) - // { - // active_ = s; - - // if (s == 0 && stmt_handles_.size () > 0) - // free_stmt_handles (); - // } - - // // Cancel and clear the active statement, if any. - // // - // void - // clear () - // { - // if (active_ != 0) - // clear_ (); - // } - - public: - // MYSQL_STMT* - // alloc_stmt_handle (); - - // void - // free_stmt_handle (MYSQL_STMT*); + statement_cache_type& + statement_cache () + { + return *statement_cache_; + } private: connection (const connection&); connection& operator= (const connection&); private: - // void - // free_stmt_handles (); - - // void - // clear_ (); - - private: database_type& db_; PGconn* handle_; - // statement* active_; - // std::auto_ptr statement_cache_; - - // typedef std::vector stmt_handles; - // stmt_handles stmt_handles_; + std::auto_ptr statement_cache_; }; } } diff --git a/odb/pgsql/statement-cache.hxx b/odb/pgsql/statement-cache.hxx new file mode 100644 index 0000000..8164cd8 --- /dev/null +++ b/odb/pgsql/statement-cache.hxx @@ -0,0 +1,67 @@ +// file : odb/pgsql/statement-cache.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_PGSQL_STATEMENT_CACHE_HXX +#define ODB_PGSQL_STATEMENT_CACHE_HXX + +#include + +#include +#include + +#include + +#include +#include + +#include +#include + +#include + +namespace odb +{ + namespace pgsql + { + class connection; + + class LIBODB_PGSQL_EXPORT statement_cache + { + public: + statement_cache (connection& conn) + : conn_ (conn) + { + } + + template + object_statements& + find () + { + map::iterator i (map_.find (&typeid (T))); + + if (i != map_.end ()) + return static_cast&> (*i->second); + + details::shared_ptr > p ( + new (details::shared) object_statements (conn_)); + + map_.insert (map::value_type (&typeid (T), p)); + return *p; + } + + private: + typedef std::map, + details::type_info_comparator> map; + + connection& conn_; + map map_; + }; + } +} + +#include + +#endif // ODB_PGSQL_STATEMENT_CACHE_HXX -- cgit v1.1