diff options
-rw-r--r-- | odb/pgsql/connection.cxx | 47 | ||||
-rw-r--r-- | odb/pgsql/connection.hxx | 57 | ||||
-rw-r--r-- | odb/pgsql/statement-cache.hxx | 67 |
3 files changed, 77 insertions, 94 deletions
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 <odb/pgsql/connection.hxx> #include <odb/pgsql/error.hxx> #include <odb/pgsql/exceptions.hxx> +#include <odb/pgsql/statement-cache.hxx> 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_type> statement_cache_; - - // typedef std::vector<MYSQL_STMT*> stmt_handles; - // stmt_handles stmt_handles_; + std::auto_ptr<statement_cache_type> 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 <constantin@codesynthesis.com> +// 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 <odb/pre.hxx> + +#include <map> +#include <typeinfo> + +#include <odb/forward.hxx> + +#include <odb/pgsql/version.hxx> +#include <odb/pgsql/object-statements.hxx> + +#include <odb/details/shared-ptr.hxx> +#include <odb/details/type-info.hxx> + +#include <odb/pgsql/details/export.hxx> + +namespace odb +{ + namespace pgsql + { + class connection; + + class LIBODB_PGSQL_EXPORT statement_cache + { + public: + statement_cache (connection& conn) + : conn_ (conn) + { + } + + template <typename T> + object_statements<T>& + find () + { + map::iterator i (map_.find (&typeid (T))); + + if (i != map_.end ()) + return static_cast<object_statements<T>&> (*i->second); + + details::shared_ptr<object_statements<T> > p ( + new (details::shared) object_statements<T> (conn_)); + + map_.insert (map::value_type (&typeid (T), p)); + return *p; + } + + private: + typedef std::map<const std::type_info*, + details::shared_ptr<object_statements_base>, + details::type_info_comparator> map; + + connection& conn_; + map map_; + }; + } +} + +#include <odb/post.hxx> + +#endif // ODB_PGSQL_STATEMENT_CACHE_HXX |