aboutsummaryrefslogtreecommitdiff
path: root/brep/database.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-12-22 19:29:04 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2016-01-08 18:26:00 +0200
commitc715379c625935bd4b28bebb35f34721342cc7f3 (patch)
tree97e55364e786ab794514450e879006ee61ac8643 /brep/database.cxx
parenteb16296f88ce0fdb4a98a08950b58a346a6e2bd9 (diff)
Source file names fixup
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;
+ }
+ }
+}