aboutsummaryrefslogtreecommitdiff
path: root/brep/database-lock.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2016-01-18 07:35:12 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2016-01-23 17:47:47 +0200
commit1dc38cf49b6c7a8b661a9cc675ded94c8ab33c36 (patch)
tree5a216148adb9d842a5a15c032a671182faa9ba06 /brep/database-lock.cxx
parentfe6182a8c89675f92e72c881d707e21cdf56f376 (diff)
Implement brep-migrate utility
Diffstat (limited to 'brep/database-lock.cxx')
-rw-r--r--brep/database-lock.cxx44
1 files changed, 44 insertions, 0 deletions
diff --git a/brep/database-lock.cxx b/brep/database-lock.cxx
new file mode 100644
index 0000000..3b8ae21
--- /dev/null
+++ b/brep/database-lock.cxx
@@ -0,0 +1,44 @@
+// file : brep/database-lock.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <brep/database-lock>
+
+#include <odb/pgsql/database.hxx>
+#include <odb/pgsql/exceptions.hxx>
+#include <odb/pgsql/transaction.hxx>
+
+namespace brep
+{
+ using namespace odb::pgsql;
+
+ database_lock::
+ database_lock (database& db)
+ {
+ // Before locking the table make sure it exists.
+ //
+ {
+ transaction t (db.begin ());
+ db.execute ("CREATE TABLE IF NOT EXISTS database_mutex ()");
+ t.commit ();
+ }
+
+ connection_ = db.connection ();
+
+ // Don't make current. Will be rolled back in destructor.
+ //
+ transaction_.reset (new transaction (connection_->begin (), false));
+
+ try
+ {
+ connection_->execute ("LOCK TABLE database_mutex NOWAIT");
+ }
+ catch (const database_exception& e)
+ {
+ if (e.sqlstate () == "55P03") // The table is already locked.
+ throw database_locked ();
+
+ throw;
+ }
+ }
+}