4 #ifndef DBALLE_SQL_POSTGRESQL_H
5 #define DBALLE_SQL_POSTGRESQL_H
28 const char* what()
const noexcept
override {
return msg.c_str(); }
31 static void throwf(PGresult* db,
const char* fmt, ...) WREPORT_THROWF_ATTRS(2, 3);
34 namespace postgresql {
36 int64_t encode_datetime(
const Datetime& arg);
37 int64_t encode_int64_t(int64_t arg);
40 template<
typename... ARGS>
struct Params
42 static const int count =
sizeof...(ARGS);
43 const char* args[
sizeof...(ARGS)];
44 int lengths[
sizeof...(ARGS)];
45 int formats[
sizeof...(ARGS)];
46 void* local[
sizeof...(ARGS)];
48 Params(
const ARGS&... args)
70 template<
typename... REST>
71 void _add(
unsigned pos, std::nullptr_t arg,
const REST&... rest)
77 _add(pos + 1, rest...);
81 template<
typename... REST>
82 void _add(
unsigned pos, int32_t arg,
const REST&... rest)
84 local[pos] = malloc(
sizeof(int32_t));
85 *(int32_t*)local[pos] = (int32_t)htonl((uint32_t)arg);
86 args[pos] = (
const char*)local[pos];
87 lengths[pos] =
sizeof(int32_t);
89 _add(pos + 1, rest...);
93 template<
typename... REST>
94 void _add(
unsigned pos, uint64_t arg,
const REST&... rest)
96 local[pos] = malloc(
sizeof(int64_t));
97 *(int64_t*)local[pos] = encode_int64_t(arg);
98 args[pos] = (
const char*)local[pos];
99 lengths[pos] =
sizeof(int64_t);
101 _add(pos + 1, rest...);
105 template<
typename... REST>
106 void _add(
unsigned pos,
const char* arg,
const REST&... rest)
108 local[pos] =
nullptr;
112 _add(pos + 1, rest...);
116 template<
typename... REST>
117 void _add(
unsigned pos,
const std::string& arg,
const REST&... rest)
119 local[pos] =
nullptr;
120 args[pos] = arg.data();
121 lengths[pos] = arg.size();
123 _add(pos + 1, rest...);
127 template<
typename... REST>
128 void _add(
unsigned pos,
const std::vector<uint8_t>& arg,
const REST&... rest)
130 local[pos] =
nullptr;
131 args[pos] = (
const char*)arg.data();
132 lengths[pos] = arg.size();
134 _add(pos + 1, rest...);
138 template<
typename... REST>
141 local[pos] = malloc(
sizeof(int64_t));
142 *(int64_t*)local[pos] = encode_datetime(arg);
143 args[pos] = (
const char*)local[pos];
144 lengths[pos] =
sizeof(int64_t);
146 _add(pos + 1, rest...);
155 Result() : res(
nullptr) {}
156 Result(PGresult* res) : res(res) {}
157 ~
Result() { PQclear(res); }
163 if (
this == &o)
return *
this;
170 operator bool()
const {
return res !=
nullptr; }
171 operator PGresult*() {
return res; }
172 operator const PGresult*()
const {
return res; }
187 unsigned rowcount()
const {
return PQntuples(res); }
190 bool is_null(
unsigned row,
unsigned col)
const
192 return PQgetisnull(res, row, col);
198 char* val = PQgetvalue(res, row, col);
203 uint16_t
get_int2(
unsigned row,
unsigned col)
const
205 char* val = PQgetvalue(res, row, col);
206 return ntohs(*(uint16_t*)val);
210 uint32_t
get_int4(
unsigned row,
unsigned col)
const
212 char* val = PQgetvalue(res, row, col);
213 return ntohl(*(uint32_t*)val);
217 uint64_t
get_int8(
unsigned row,
unsigned col)
const;
220 std::vector<uint8_t>
get_bytea(
unsigned row,
unsigned col)
const;
225 return PQgetvalue(res, row, col);
244 PGconn*
db =
nullptr;
247 void init_after_connect();
257 operator PGconn*() {
return db; }
265 void open_url(
const std::string& connection_string);
268 std::unique_ptr<Transaction>
transaction()
override;
271 void prepare(
const std::string& name,
const std::string& query);
275 if (profile) ++profile_query_count;
276 return PQexecParams(
db, query, 0,
nullptr,
nullptr,
nullptr,
nullptr, 1);
279 postgresql::Result exec_unchecked(
const std::string& query)
281 if (profile) ++profile_query_count;
282 return PQexecParams(
db, query.c_str(), 0,
nullptr,
nullptr,
nullptr,
nullptr, 1);
285 template<
typename STRING>
286 void exec_no_data(STRING query)
288 postgresql::Result res(exec_unchecked(query));
289 res.expect_no_data(query);
292 template<
typename STRING>
293 postgresql::Result exec(STRING query)
295 postgresql::Result res(exec_unchecked(query));
296 res.expect_result(query);
300 template<
typename STRING>
301 postgresql::Result exec_one_row(STRING query)
303 postgresql::Result res(exec_unchecked(query));
304 res.expect_one_row(query);
308 template<
typename ...ARGS>
309 postgresql::Result exec_unchecked(
const char* query, ARGS... args)
311 postgresql::Params<ARGS...> params(args...);
312 if (profile) ++profile_query_count;
313 return PQexecParams(
db, query, params.count,
nullptr, params.args, params.lengths, params.formats, 1);
316 template<
typename ...ARGS>
317 postgresql::Result exec_unchecked(
const std::string& query, ARGS... args)
319 postgresql::Params<ARGS...> params(args...);
320 if (profile) ++profile_query_count;
321 return PQexecParams(
db, query.c_str(), params.count,
nullptr, params.args, params.lengths, params.formats, 1);
324 template<
typename STRING,
typename ...ARGS>
325 void exec_no_data(STRING query, ARGS... args)
327 postgresql::Result res(exec_unchecked(query, args...));
328 res.expect_no_data(query);
331 template<
typename STRING,
typename ...ARGS>
332 postgresql::Result exec(STRING query, ARGS... args)
334 postgresql::Result res(exec_unchecked(query, args...));
335 res.expect_result(query);
339 template<
typename STRING,
typename ...ARGS>
340 postgresql::Result exec_one_row(STRING query, ARGS... args)
342 postgresql::Result res(exec_unchecked(query, args...));
343 res.expect_one_row(query);
347 postgresql::Result exec_prepared_unchecked(
const char* name)
349 if (profile) ++profile_query_count;
350 return PQexecPrepared(
db, name, 0,
nullptr,
nullptr,
nullptr, 1);
353 postgresql::Result exec_prepared_unchecked(
const std::string& name)
355 if (profile) ++profile_query_count;
356 return PQexecPrepared(
db, name.c_str(), 0,
nullptr,
nullptr,
nullptr, 1);
359 template<
typename STRING>
360 void exec_prepared_no_data(STRING name)
362 postgresql::Result res(exec_prepared_unchecked(name));
363 res.expect_no_data(name);
366 template<
typename STRING>
367 postgresql::Result exec_prepared(STRING name)
369 postgresql::Result res(exec_prepared_unchecked(name));
370 res.expect_result(name);
374 template<
typename STRING>
375 postgresql::Result exec_prepared_one_row(STRING name)
377 postgresql::Result res(exec_prepared_unchecked(name));
378 res.expect_one_row(name);
382 template<
typename ...ARGS>
383 postgresql::Result exec_prepared_unchecked(
const char* name, ARGS... args)
385 postgresql::Params<ARGS...> params(args...);
386 if (profile) ++profile_query_count;
387 return PQexecPrepared(
db, name, params.count, params.args, params.lengths, params.formats, 1);
390 template<
typename ...ARGS>
391 postgresql::Result exec_prepared_unchecked(
const std::string& name, ARGS... args)
393 postgresql::Params<ARGS...> params(args...);
394 if (profile) ++profile_query_count;
395 return PQexecPrepared(
db, name.c_str(), params.count, params.args, params.lengths, params.formats, 1);
398 template<
typename STRING,
typename ...ARGS>
399 void exec_prepared_no_data(STRING name, ARGS... args)
401 postgresql::Result res(exec_prepared_unchecked(name, args...));
402 res.expect_no_data(name);
405 template<
typename STRING,
typename ...ARGS>
406 postgresql::Result exec_prepared(STRING name, ARGS... args)
408 postgresql::Result res(exec_prepared_unchecked(name, args...));
409 res.expect_result(name);
413 template<
typename STRING,
typename ...ARGS>
414 postgresql::Result exec_prepared_one_row(STRING name, ARGS... args)
416 postgresql::Result res(exec_prepared_unchecked(name, args...));
417 res.expect_one_row(name);
427 bool has_table(const std::
string& name) override;
428 std::
string get_setting(const std::
string& key) override;
429 void set_setting(const std::
string& key, const std::
string& value) override;
431 void execute(const std::
string& query) override;
432 void explain(const std::
string& query, FILE* out) override;
443 void pqexec(const std::
string& query);
454 void run_single_row_mode(const std::
string& query_desc, std::function<
void(const postgresql::Result&)> dest);
463 void append_escaped(Querybuf& qb, const std::vector<uint8_t>& buf);
void open_url(const std::string &connection_string)
Connect to PostgreSQL using a connection URI.
Database connection.
Definition: postgresql.h:240
Result(Result &&o)
Implement move.
Definition: postgresql.h:160
void _add(unsigned pos, const char *arg, const REST &...rest)
Fill in the argument structures.
Definition: postgresql.h:106
void set_setting(const std::string &key, const std::string &value) override
Set a value in the settings table.
Argument list for PQexecParams built at compile time.
Definition: postgresql.h:40
void run_single_row_mode(const std::string &query_desc, std::function< void(const postgresql::Result &)> dest)
Retrieve query results in single row mode.
bool is_null(unsigned row, unsigned col) const
Check if a result value is null.
Definition: postgresql.h:190
std::unique_ptr< Transaction > transaction() override
Begin a transaction.
int changes()
Count the number of rows modified by the last query that was run.
void cancel_running_query_nothrow() noexcept
Send a cancellation command to the server.
bool has_table(const std::string &name) override
Check if the database contains a table.
void _add(unsigned pos)
Terminating condition for compile-time arg expansion.
Definition: postgresql.h:65
std::vector< uint8_t > get_bytea(unsigned row, unsigned col) const
Return a result value, transmitted in binary as an 8 bit integer.
uint64_t get_int8(unsigned row, unsigned col) const
Return a result value, transmitted in binary as an 8 bit integer.
void _add(unsigned pos, const Datetime &arg, const REST &...rest)
Fill in the argument structures.
Definition: postgresql.h:139
void prepare(const std::string &name, const std::string &query)
Precompile a query.
uint16_t get_int2(unsigned row, unsigned col) const
Return a result value, transmitted in binary as a 2 bit integer.
Definition: postgresql.h:203
void pqexec(const std::string &query)
Wrap PQexec.
void _add(unsigned pos, const std::vector< uint8_t > &arg, const REST &...rest)
Fill in the argument structures.
Definition: postgresql.h:128
Datetime get_timestamp(unsigned row, unsigned col) const
Return a result value, transmitted as a timestamp without timezone.
void pqexec_nothrow(const std::string &query) noexcept
Wrap PQexec but do not throw an exception in case of errors.
void expect_one_row(const std::string &query)
Check that the result successfully returned one row of data.
Wrap a PGresult, taking care of its memory management.
Definition: postgresql.h:151
void append_escaped(Querybuf &qb, const char *str)
Escape the string as a literal value and append it to qb.
bool get_bool(unsigned row, unsigned col) const
Return a result value, transmitted in binary as a byte (?)
Definition: postgresql.h:196
uint32_t get_int4(unsigned row, unsigned col) const
Return a result value, transmitted in binary as a 4 bit integer.
Definition: postgresql.h:210
void expect_result(const std::string &query)
Check that the result successfully returned some (possibly empty) data.
void expect_no_data(const std::string &query)
Check that the result successfully returned no data.
void drop_table_if_exists(const char *name)
Delete a table in the database if it exists, otherwise do nothing.
Report an PostgreSQL error.
Definition: postgresql.h:19
Common infrastructure for talking with SQL databases.
Error in case of failed database operations.
Definition: error.h:21
unsigned rowcount() const
Get the number of rows in the result.
Definition: postgresql.h:187
PGconn * db
Database connection.
Definition: postgresql.h:244
void discard_all_input_nothrow() noexcept
Discard all input from an asynchronous request.
void explain(const std::string &query, FILE *out) override
Format and print the EXPLAIN output for the query to the given file.
Date and time.
Definition: types.h:158
void _add(unsigned pos, std::nullptr_t arg, const REST &...rest)
Fill in the argument structures.
Definition: postgresql.h:71
void _add(unsigned pos, uint64_t arg, const REST &...rest)
Fill in the argument structures.
Definition: postgresql.h:94
void drop_settings() override
Drop the settings table.
#define WREPORT_THROWF_ATTRS(a, b)
const char * get_string(unsigned row, unsigned col) const
Return a result value, transmitted as a string.
Definition: postgresql.h:223
void execute(const std::string &query) override
Execute a query without reading its results.
void _add(unsigned pos, const std::string &arg, const REST &...rest)
Fill in the argument structures.
Definition: postgresql.h:117
std::string get_setting(const std::string &key) override
Get a value from the settings table.
void expect_success(const std::string &query)
Check that the result was successful.
void _add(unsigned pos, int32_t arg, const REST &...rest)
Fill in the argument structures.
Definition: postgresql.h:82