aboutsummaryrefslogtreecommitdiff
path: root/mod/database.cxx
blob: 67a862b11baa9c3332c758338ea5e1a895d07971 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// file      : mod/database.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <mod/database.hxx>

#include <map>

#include <odb/pgsql/database.hxx>
#include <odb/pgsql/connection-factory.hxx>

namespace brep
{
  struct db_key
  {
    string user;
    string password;
    string name;
    string host;
    uint16_t port;
  };

  static bool
  operator< (const db_key& x, const db_key& y)
  {
    int r;
    if ((r = x.user.compare (y.user)) != 0 ||
        (r = x.password.compare (y.password)) != 0 ||
        (r = x.name.compare (y.name)) != 0 ||
        (r = x.host.compare (y.host)))
      return r < 0;

    return x.port < y.port;
  }

  using namespace odb;

  shared_ptr<database>
  shared_database (string user,
                   string password,
                   string name,
                   string host,
                   uint16_t port,
                   size_t max_connections)
  {
    static std::map<db_key, weak_ptr<database>> databases;

    db_key k ({move (user), move (password), move (name), host, port});

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

    unique_ptr<pgsql::connection_factory>
      f (new pgsql::connection_pool_factory (max_connections));

    shared_ptr<database> d (
      make_shared<pgsql::database> (
        k.user,
        k.password,
        k.name,
        k.host,
        k.port,
        "options='-c default_transaction_isolation=serializable'",
        move (f)));

    databases[move (k)] = d;
    return d;
  }
}