aboutsummaryrefslogtreecommitdiff
path: root/brep/database.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'brep/database.cxx')
-rw-r--r--brep/database.cxx48
1 files changed, 48 insertions, 0 deletions
diff --git a/brep/database.cxx b/brep/database.cxx
new file mode 100644
index 0000000..4b7494e
--- /dev/null
+++ b/brep/database.cxx
@@ -0,0 +1,48 @@
+// file : brep/database.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <brep/database>
+
+#include <stdexcept> // runtime_error
+
+#include <odb/pgsql/database.hxx>
+
+#include <brep/types>
+#include <brep/utility>
+
+namespace brep
+{
+ shared_ptr<odb::database>
+ shared_database (const options::db& o)
+ {
+ using odb::pgsql::database;
+ static weak_ptr<database> db;
+
+ // In C++11, function-static variable initialization is guaranteed to be
+ // thread-safe, thought this doesn't seem to be enough in our case
+ // (because we are re-initializing the weak pointer).
+ //
+ if (shared_ptr<database> d = db.lock ())
+ {
+ if (o.db_user () != d->user () ||
+ o.db_password () != d->password () ||
+ o.db_name () != d->db () ||
+ o.db_host () != d->host () ||
+ o.db_port () != d->port ())
+ throw std::runtime_error ("shared database options mismatch");
+
+ return d;
+ }
+ else
+ {
+ d = make_shared<database> (o.db_user (),
+ o.db_password (),
+ o.db_name (),
+ o.db_host (),
+ o.db_port ());
+ db = d;
+ return d;
+ }
+ }
+}