aboutsummaryrefslogtreecommitdiff
path: root/odb/pgsql/connection.cxx
blob: fb1ea261c6b467dfaa894073e19a8c1f3e9078b5 (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
// file      : odb/pgsql/connection.cxx
// author    : Constantin Michael <constantin@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <new>    // std::bad_alloc
#include <string>

#include <odb/pgsql/database.hxx>
#include <odb/pgsql/connection.hxx>
#include <odb/pgsql/error.hxx>
#include <odb/pgsql/exceptions.hxx>
#include <odb/pgsql/statement-cache.hxx>

using namespace std;

namespace odb
{
  namespace pgsql
  {
    static void
    nop_process_notice (void*, const char*)
    {
    }

    connection::
    connection (database_type& db)
        : db_ (db),
          handle_ (0),
          statement_cache_ (new statement_cache_type (*this))
    {
      handle_ = PQconnectdb (db.conninfo ().c_str ());

      if (handle_ == 0)
        throw bad_alloc ();
      else if (PQstatus (handle_) == CONNECTION_BAD)
      {
        std::string m (PQerrorMessage (handle_));
        PQfinish (handle_);

        throw database_exception (m);
      }

      // Suppress server notifications to stdout.
      //
      PQsetNoticeProcessor (handle_, &nop_process_notice, 0);

      // Establish whether date/time values are represented as
      // 8-byte integers.
      //
      integer_datetimes_ =
        *PQparameterStatus (handle_, "integer_datetimes") == '1';
    }

    connection::
    ~connection ()
    {
      // Deallocate prepared statements before we close the connection.
      //
      statement_cache_.reset ();

      PQfinish (handle_);
    }
  }
}