From 79cb2f05559e1ed9e724efd4f5dbceca794e7162 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 29 Apr 2021 12:37:09 +0200 Subject: Add serial_connection_factory This factory can be used when the database access is guaranteed to be serial. --- odb/sqlite/connection-factory.cxx | 34 ++++++++++++++++++++++++++++++++++ odb/sqlite/connection-factory.hxx | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/odb/sqlite/connection-factory.cxx b/odb/sqlite/connection-factory.cxx index 1269db3..1a1f85a 100644 --- a/odb/sqlite/connection-factory.cxx +++ b/odb/sqlite/connection-factory.cxx @@ -17,6 +17,40 @@ namespace odb namespace sqlite { // + // serial_connection_factory + // + + serial_connection_factory:: + ~serial_connection_factory () + { + // We should hold the last reference to the connection. + // + if (connection_ != 0) + assert (connection_.count () == 1); + } + + connection_ptr serial_connection_factory:: + create () + { + return connection_ptr (new (shared) connection (*this)); + } + + connection_ptr serial_connection_factory:: + connect () + { + return connection_; + } + + void serial_connection_factory:: + database (database_type& db) + { + connection_factory::database (db); + + if (!connection_) + connection_ = create (); + } + + // // single_connection_factory // diff --git a/odb/sqlite/connection-factory.hxx b/odb/sqlite/connection-factory.hxx index b665625..141fff6 100644 --- a/odb/sqlite/connection-factory.hxx +++ b/odb/sqlite/connection-factory.hxx @@ -23,7 +23,42 @@ namespace odb { namespace sqlite { - // Share a single connection. + // Share a single connection in a guaranteed serial database access. + // + // For example, a single-threaded application that executes all the + // operations via the database instance without messing with multiple + // connections/transactions would qualify. + // + class LIBODB_SQLITE_EXPORT serial_connection_factory: + public connection_factory + { + public: + serial_connection_factory () {} + + virtual connection_ptr + connect (); + + virtual void + database (database_type&); + + virtual + ~serial_connection_factory (); + + private: + serial_connection_factory (const serial_connection_factory&); + serial_connection_factory& operator= (const serial_connection_factory&); + + protected: + // This function is called when the factory needs to create the + // connection. + // + virtual connection_ptr + create (); + + connection_ptr connection_; + }; + + // Share a single connection potentially between multiple threads. // class LIBODB_SQLITE_EXPORT single_connection_factory: public connection_factory -- cgit v1.1