aboutsummaryrefslogtreecommitdiff
path: root/odb/pgsql/connection.cxx
blob: 6f0feafe7925eb2130d68a0d8b74778045ac2970 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// 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 <cstring> // std::strcmp
#include <cstdlib> // std::atol

#include <libpq-fe.h>

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

using namespace std;

extern "C" void
odb_pgsql_process_notice (void*, const char*)
{
}

namespace odb
{
  namespace pgsql
  {
    connection::
    connection (database_type& db)
        : odb::connection (db), db_ (db)
    {
      handle_.reset (PQconnectdb (db.conninfo ().c_str ()));

      if (handle_ == 0)
        throw bad_alloc ();
      else if (PQstatus (handle_) == CONNECTION_BAD)
        throw database_exception (PQerrorMessage (handle_));

      init ();
    }

    connection::
    connection (database_type& db, PGconn* handle)
        : odb::connection (db), db_ (db), handle_ (handle)
    {
      init ();
    }

    void connection::
    init ()
    {
      // Establish whether date/time values are represented as
      // 8-byte integers.
      //
      if (strcmp (PQparameterStatus (handle_, "integer_datetimes"), "on") != 0)
        throw database_exception ("unsupported binary format for PostgreSQL "
                                  "date-time SQL types");

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

      // Create statement cache.
      //
      statement_cache_.reset (new statement_cache_type (*this));
    }

    connection::
    ~connection ()
    {
    }

    transaction_impl* connection::
    begin ()
    {
      return new transaction_impl (connection_ptr (inc_ref (this)));
    }

    unsigned long long connection::
    execute (const char* s, std::size_t n)
    {
      // The string may not be '\0'-terminated.
      //
      string str (s, n);

      auto_handle<PGresult> h (PQexec (handle_, str.c_str ()));

      unsigned long long count (0);

      if (!is_good_result (h))
        translate_error (*this, h);
      else if (PGRES_TUPLES_OK == PQresultStatus (h))
        count = static_cast<unsigned long long> (PQntuples (h));
      else
      {
        const char* s (PQcmdTuples (h));

        if (s[0] != '\0' && s[1] == '\0')
          count = static_cast<unsigned long long> (s[0] - '0');
        else
          count = static_cast<unsigned long long> (atol (s));
      }

      return count;
    }
  }
}