diff options
-rw-r--r-- | odb/sqlite/connection-factory.cxx | 59 | ||||
-rw-r--r-- | odb/sqlite/connection-factory.hxx | 52 |
2 files changed, 75 insertions, 36 deletions
diff --git a/odb/sqlite/connection-factory.cxx b/odb/sqlite/connection-factory.cxx index 71f8b3e..358fa0c 100644 --- a/odb/sqlite/connection-factory.cxx +++ b/odb/sqlite/connection-factory.cxx @@ -38,6 +38,13 @@ namespace odb lock l (mutex_); } + single_connection_factory::single_connection_ptr + single_connection_factory:: + create () + { + return single_connection_ptr (new (shared) single_connection (*db_, 0)); + } + connection_ptr single_connection_factory:: connect () { @@ -52,7 +59,7 @@ namespace odb database (database_type& db) { db_ = &db; - connection_.reset (new (shared) single_connection (*db_, 0, 0)); + connection_ = create (); } bool single_connection_factory:: @@ -69,10 +76,17 @@ namespace odb // single_connection_factory::single_connection:: - single_connection (database_type& db, - int extra_flags, - single_connection_factory* factory) - : connection (db, extra_flags), factory_ (factory) + single_connection (database_type& db, int extra_flags) + : connection (db, extra_flags), factory_ (0) + { + callback_.arg = this; + callback_.zero_counter = &zero_counter; + shared_base::callback_ = &callback_; + } + + single_connection_factory::single_connection:: + single_connection (database_type& db, sqlite3* handle) + : connection (db, handle), factory_ (0) { callback_.arg = this; callback_.zero_counter = &zero_counter; @@ -112,6 +126,13 @@ namespace odb // connection_pool_factory // + connection_pool_factory::pooled_connection_ptr connection_pool_factory:: + create () + { + return pooled_connection_ptr ( + new (shared) pooled_connection (*db_, extra_flags_)); + } + connection_pool_factory:: ~connection_pool_factory () { @@ -149,8 +170,8 @@ namespace odb // if(max_ == 0 || in_use_ < max_) { - shared_ptr<pooled_connection> c ( - new (shared) pooled_connection (*db_, extra_flags_, this)); + shared_ptr<pooled_connection> c (create ()); + c->pool_ = this; in_use_++; return c; } @@ -178,11 +199,7 @@ namespace odb connections_.reserve (min_); for(size_t i (0); i < min_; ++i) - { - connections_.push_back ( - shared_ptr<pooled_connection> ( - new (shared) pooled_connection (*db_, extra_flags_, 0))); - } + connections_.push_back (create ()); } } @@ -202,8 +219,7 @@ namespace odb in_use_--; if (keep) - connections_.push_back ( - shared_ptr<pooled_connection> (inc_ref (c))); + connections_.push_back (pooled_connection_ptr (inc_ref (c))); if (waiters_ != 0) cond_.signal (); @@ -216,10 +232,17 @@ namespace odb // connection_pool_factory::pooled_connection:: - pooled_connection (database_type& db, - int extra_flags, - connection_pool_factory* pool) - : connection (db, extra_flags), pool_ (pool) + pooled_connection (database_type& db, int extra_flags) + : connection (db, extra_flags), pool_ (0) + { + callback_.arg = this; + callback_.zero_counter = &zero_counter; + shared_base::callback_ = &callback_; + } + + connection_pool_factory::pooled_connection:: + pooled_connection (database_type& db, sqlite3* handle) + : connection (db, handle), pool_ (0) { callback_.arg = this; callback_.zero_counter = &zero_counter; diff --git a/odb/sqlite/connection-factory.hxx b/odb/sqlite/connection-factory.hxx index 8166fc4..04f91bf 100644 --- a/odb/sqlite/connection-factory.hxx +++ b/odb/sqlite/connection-factory.hxx @@ -61,15 +61,12 @@ namespace odb single_connection_factory (const single_connection_factory&); single_connection_factory& operator= (const single_connection_factory&); - private: + protected: class single_connection: public connection { public: - // NULL factory value indicates that the connection is not in use. - // - single_connection (database_type&, - int extra_flags, - single_connection_factory*); + single_connection (database_type&, int extra_flags); + single_connection (database_type&, sqlite3*); private: static bool @@ -79,21 +76,32 @@ namespace odb friend class single_connection_factory; shared_base::refcount_callback callback_; + + // NULL factory value indicates that the connection is not in use. + // single_connection_factory* factory_; }; friend class single_connection; - private: + typedef details::shared_ptr<single_connection> single_connection_ptr; + + // This function is called when the factory needs to create the + // connection. + // + virtual single_connection_ptr + create (); + + protected: // Return true if the connection should be deleted, false otherwise. // bool release (single_connection*); - private: + protected: database_type* db_; details::mutex mutex_; - details::shared_ptr<single_connection> connection_; + single_connection_ptr connection_; }; // Create a new connection every time one is requested. @@ -167,15 +175,12 @@ namespace odb connection_pool_factory (const connection_pool_factory&); connection_pool_factory& operator= (const connection_pool_factory&); - private: + protected: class pooled_connection: public connection { public: - // NULL pool value indicates that the connection is not in use. - // - pooled_connection (database_type&, - int extra_flags, - connection_pool_factory*); + pooled_connection (database_type&, int extra_flags); + pooled_connection (database_type&, sqlite3*); private: static bool @@ -185,19 +190,30 @@ namespace odb friend class connection_pool_factory; shared_base::refcount_callback callback_; + + // NULL pool value indicates that the connection is not in use. + // connection_pool_factory* pool_; }; friend class pooled_connection; - typedef std::vector<details::shared_ptr<pooled_connection> > connections; - private: + typedef details::shared_ptr<pooled_connection> pooled_connection_ptr; + typedef std::vector<pooled_connection_ptr> connections; + + // This function is called whenever the pool needs to create a new + // connection. + // + virtual pooled_connection_ptr + create (); + + protected: // Return true if the connection should be deleted, false otherwise. // bool release (pooled_connection*); - private: + protected: const std::size_t max_; const std::size_t min_; int extra_flags_; |