diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2021-04-29 12:37:09 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2021-04-30 10:07:08 +0200 |
commit | 79cb2f05559e1ed9e724efd4f5dbceca794e7162 (patch) | |
tree | 595995944c05483b66f8a1b69beb7ceaf7424da6 | |
parent | edaa6f79149a79d6b59efa711bbd5feeeb40513e (diff) |
Add serial_connection_factory
This factory can be used when the database access is guaranteed to be serial.
-rw-r--r-- | odb/sqlite/connection-factory.cxx | 34 | ||||
-rw-r--r-- | odb/sqlite/connection-factory.hxx | 37 |
2 files changed, 70 insertions, 1 deletions
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 |