From 83aede3c9e27a8370c71dbde3549905c60524702 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 29 Mar 2011 14:43:47 +0200 Subject: Add support for starting immediate and exclusive transactions --- odb/sqlite/database.cxx | 20 +++++++++++++++++++- odb/sqlite/database.hxx | 8 ++++++++ odb/sqlite/statement-cache.cxx | 22 +++++++++++++++++++--- odb/sqlite/statement-cache.hxx | 27 +++++++++++++++++++++++++++ odb/sqlite/transaction-impl.cxx | 23 +++++++++++++++++++++-- odb/sqlite/transaction-impl.hxx | 9 ++++++++- 6 files changed, 102 insertions(+), 7 deletions(-) (limited to 'odb') diff --git a/odb/sqlite/database.cxx b/odb/sqlite/database.cxx index 176c392..912ec97 100644 --- a/odb/sqlite/database.cxx +++ b/odb/sqlite/database.cxx @@ -97,7 +97,25 @@ namespace odb if (transaction::has_current ()) throw already_in_transaction (); - return new transaction_impl (*this); + return new transaction_impl (*this, transaction_impl::deferred); + } + + transaction_impl* database:: + begin_immediate () + { + if (transaction::has_current ()) + throw already_in_transaction (); + + return new transaction_impl (*this, transaction_impl::immediate); + } + + transaction_impl* database:: + begin_exclusive () + { + if (transaction::has_current ()) + throw already_in_transaction (); + + return new transaction_impl (*this, transaction_impl::exclusive); } } } diff --git a/odb/sqlite/database.hxx b/odb/sqlite/database.hxx index 121f85c..cfff30f 100644 --- a/odb/sqlite/database.hxx +++ b/odb/sqlite/database.hxx @@ -78,6 +78,8 @@ namespace odb } public: + using odb::database::execute; + virtual unsigned long long execute (const char* statement, std::size_t length); @@ -85,6 +87,12 @@ namespace odb virtual transaction_impl* begin (); + transaction_impl* + begin_immediate (); + + transaction_impl* + begin_exclusive (); + public: details::shared_ptr connection (); diff --git a/odb/sqlite/statement-cache.cxx b/odb/sqlite/statement-cache.cxx index 383a78e..ba7b56c 100644 --- a/odb/sqlite/statement-cache.cxx +++ b/odb/sqlite/statement-cache.cxx @@ -17,13 +17,29 @@ namespace odb // String lengths below include '\0', as per SQLite manual // suggestions. // - begin_ (new (shared) simple_statement (conn, "BEGIN", 6)), - commit_ (new (shared) simple_statement (conn, "COMMIT", 7)), - rollback_ (new (shared) simple_statement (conn, "ROLLBACK", 9)) + begin_ (new (shared) simple_statement (conn_, "BEGIN", 6)), + commit_ (new (shared) simple_statement (conn_, "COMMIT", 7)), + rollback_ (new (shared) simple_statement (conn_, "ROLLBACK", 9)) { rollback_->cached (true); commit_->cached (true); begin_->cached (true); } + + void statement_cache:: + begin_immediate_statement_ () const + { + begin_immediate_.reset ( + new (shared) simple_statement (conn_, "BEGIN IMMEDIATE", 16)); + begin_immediate_->cached (true); + } + + void statement_cache:: + begin_exclusive_statement_ () const + { + begin_exclusive_.reset ( + new (shared) simple_statement (conn_, "BEGIN EXCLUSIVE", 16)); + begin_exclusive_->cached (true); + } } } diff --git a/odb/sqlite/statement-cache.hxx b/odb/sqlite/statement-cache.hxx index 1b3d4b2..ca4ab2e 100644 --- a/odb/sqlite/statement-cache.hxx +++ b/odb/sqlite/statement-cache.hxx @@ -38,6 +38,24 @@ namespace odb } simple_statement& + begin_immediate_statement () const + { + if (!begin_immediate_) + begin_immediate_statement_ (); + + return *begin_immediate_; + } + + simple_statement& + begin_exclusive_statement () const + { + if (!begin_exclusive_) + begin_exclusive_statement_ (); + + return *begin_exclusive_; + } + + simple_statement& commit_statement () const { return *commit_; @@ -66,6 +84,13 @@ namespace odb } private: + void + begin_immediate_statement_ () const; + + void + begin_exclusive_statement_ () const; + + private: typedef std::map, details::type_info_comparator> map; @@ -73,6 +98,8 @@ namespace odb connection& conn_; details::shared_ptr begin_; + mutable details::shared_ptr begin_immediate_; + mutable details::shared_ptr begin_exclusive_; details::shared_ptr commit_; details::shared_ptr rollback_; diff --git a/odb/sqlite/transaction-impl.cxx b/odb/sqlite/transaction-impl.cxx index 36d6c2b..41254a1 100644 --- a/odb/sqlite/transaction-impl.cxx +++ b/odb/sqlite/transaction-impl.cxx @@ -14,10 +14,29 @@ namespace odb namespace sqlite { transaction_impl:: - transaction_impl (database_type& db) + transaction_impl (database_type& db, lock l) : odb::transaction_impl (db), connection_ (db.connection ()) { - connection_->statement_cache ().begin_statement ().execute (); + statement_cache& c (connection_->statement_cache ()); + + switch (l) + { + case deferred: + { + c.begin_statement ().execute (); + break; + } + case immediate: + { + c.begin_immediate_statement ().execute (); + break; + } + case exclusive: + { + c.begin_exclusive_statement ().execute (); + break; + } + } } transaction_impl:: diff --git a/odb/sqlite/transaction-impl.hxx b/odb/sqlite/transaction-impl.hxx index cc38039..3b09bcf 100644 --- a/odb/sqlite/transaction-impl.hxx +++ b/odb/sqlite/transaction-impl.hxx @@ -28,7 +28,14 @@ namespace odb typedef sqlite::database database_type; typedef sqlite::connection connection_type; - transaction_impl (database_type&); + enum lock + { + deferred, + immediate, + exclusive + }; + + transaction_impl (database_type&, lock); virtual ~transaction_impl (); -- cgit v1.1