aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libpqxx/src/connection.cxx
blob: 982b6d60f960dd172678cfa4076dce5a1e90f56b (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/** Implementation of the pqxx::connection and sibling classes.
 *
 * Different ways of setting up a backend connection.
 *
 * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
 *
 * See COPYING for copyright license.  If you did not receive a file called
 * COPYING with this source code, please notify the distributor of this mistake,
 * or contact the author.
 */
#include "pqxx/compiler-internal.hxx"

#include <stdexcept>

extern "C"
{
#include "libpq-fe.h"
}

#include "pqxx/connection"


pqxx::connectionpolicy::connectionpolicy(const std::string &opts) :
  m_options{opts}
{
}


pqxx::connectionpolicy::~connectionpolicy() noexcept
{
}


pqxx::connectionpolicy::handle
pqxx::connectionpolicy::normalconnect(handle orig)
{
  if (orig) return orig;
  orig = PQconnectdb(options().c_str());
  if (orig == nullptr) throw std::bad_alloc{};
  if (PQstatus(orig) != CONNECTION_OK)
  {
    const std::string msg{PQerrorMessage(orig)};
    PQfinish(orig);
    throw broken_connection{msg};
  }
  return orig;
}


pqxx::connectionpolicy::handle
pqxx::connectionpolicy::do_startconnect(handle orig)
{
  return orig;
}

pqxx::connectionpolicy::handle
pqxx::connectionpolicy::do_completeconnect(handle orig)
{
  return orig;
}

pqxx::connectionpolicy::handle
pqxx::connectionpolicy::do_dropconnect(handle orig) noexcept
{
  return orig;
}

pqxx::connectionpolicy::handle
pqxx::connectionpolicy::do_disconnect(handle orig) noexcept
{
  orig = do_dropconnect(orig);
  if (orig) PQfinish(orig);
  return nullptr;
}


bool pqxx::connectionpolicy::is_ready(handle h) const noexcept
{
  return h != nullptr;
}


pqxx::connectionpolicy::handle
pqxx::connect_direct::do_startconnect(handle orig)
{
  if (orig) return orig;
  orig = normalconnect(orig);
  if (PQstatus(orig) != CONNECTION_OK)
  {
    const std::string msg{PQerrorMessage(orig)};
    do_disconnect(orig);
    throw broken_connection{msg};
  }
  return orig;
}


pqxx::connectionpolicy::handle
pqxx::connect_lazy::do_completeconnect(handle orig)
{
  return normalconnect(orig);
}


pqxx::connect_async::connect_async(const std::string &opts) :
  connectionpolicy{opts},
  m_connecting{false}
{
}

pqxx::connectionpolicy::handle
pqxx::connect_async::do_startconnect(handle orig)
{
  if (orig != nullptr) return orig;	// Already connecting or connected.
  m_connecting = false;
  orig = PQconnectStart(options().c_str());
  if (orig == nullptr) throw std::bad_alloc{};
  if (PQstatus(orig) == CONNECTION_BAD)
  {
    do_dropconnect(orig);
    throw broken_connection{std::string{PQerrorMessage(orig)}};
  }
  m_connecting = true;
  return orig;
}


pqxx::connectionpolicy::handle
pqxx::connect_async::do_completeconnect(handle orig)
{
  const bool makenew = (orig == nullptr);
  if (makenew) orig = do_startconnect(orig);
  if (not m_connecting) return orig;

  // Our "attempt to connect" state ends here, for better or for worse
  m_connecting = false;

  PostgresPollingStatusType pollstatus = PGRES_POLLING_WRITING;

  do
  {
    switch (pollstatus)
    {
    case PGRES_POLLING_FAILED:
      if (makenew) do_disconnect(orig);
      throw broken_connection{std::string{PQerrorMessage(orig)}};

    case PGRES_POLLING_READING:
      internal::wait_read(orig);
      break;

    case PGRES_POLLING_WRITING:
      internal::wait_write(orig);
      break;

    case PGRES_POLLING_OK:
      break;

    default:
      // Meaningless, really, but deals with the obsolete PGRES_POLLING_ACTIVE
      // without requiring it to be defined.
      break;
    }
    pollstatus = PQconnectPoll(orig);
  } while (pollstatus != PGRES_POLLING_OK);

  return orig;
}


pqxx::connectionpolicy::handle
pqxx::connect_async::do_dropconnect(handle orig) noexcept
{
  m_connecting = false;
  return orig;
}


bool pqxx::connect_async::is_ready(handle h) const noexcept
{
  return h != nullptr and not m_connecting;
}