aboutsummaryrefslogtreecommitdiff
path: root/brep/database.cxx
blob: 17605c54b0c3c27c9c62f041207b2fb29bbf1c89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// file      : brep/database.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <brep/database>

#include <map>

#include <odb/pgsql/database.hxx>

namespace brep
{
  namespace options
  {
    bool
    operator< (const db& x, const db& y)
    {
      int r;
      if ((r = x.db_user ().compare (y.db_user ())) != 0 ||
          (r = x.db_password ().compare (y.db_password ())) != 0 ||
          (r = x.db_name ().compare (y.db_name ())) != 0 ||
          (r = x.db_host ().compare (y.db_host ())))
        return r < 0;

      return x.db_port () < y.db_port ();
    }
  }

  shared_ptr<odb::database>
  shared_database (const options::db& o)
  {
    using odb::pgsql::database;

    static std::map<options::db, weak_ptr<database>> databases;

    auto i (databases.find (o));
    if (i != databases.end ())
    {
      if (shared_ptr<database> d = i->second.lock ())
        return d;
    }

    shared_ptr<database> d (
      make_shared<database> (o.db_user (),
                             o.db_password (),
                             o.db_name (),
                             o.db_host (),
                             o.db_port ()));

    databases[o] = d;
    return d;
  }
}