Go to the documentation of this file.
4 #ifndef DBALLE_SQL_POSTGRESQL_H
5 #define DBALLE_SQL_POSTGRESQL_H
12 #include <unordered_set>
29 const char* what()
const noexcept
override {
return msg.c_str(); }
35 namespace postgresql {
37 int64_t encode_datetime(
const Datetime& arg);
38 int64_t encode_int64_t(int64_t arg);
41 template<
typename... ARGS>
struct Params
43 static const int count =
sizeof...(ARGS);
44 const char* args[
sizeof...(ARGS)];
45 int lengths[
sizeof...(ARGS)];
46 int formats[
sizeof...(ARGS)];
47 void* local[
sizeof...(ARGS)];
49 Params(
const ARGS&... args)
71 template<
typename... REST>
72 void _add(
unsigned pos, std::nullptr_t arg,
const REST&... rest)
78 _add(pos + 1, rest...);
82 template<
typename... REST>
83 void _add(
unsigned pos, int32_t arg,
const REST&... rest)
85 local[pos] = malloc(
sizeof(int32_t));
86 *(int32_t*)local[pos] = (int32_t)htonl((uint32_t)arg);
87 args[pos] = (
const char*)local[pos];
88 lengths[pos] =
sizeof(int32_t);
90 _add(pos + 1, rest...);
94 template<
typename... REST>
95 void _add(
unsigned pos, uint64_t arg,
const REST&... rest)
97 local[pos] = malloc(
sizeof(int64_t));
98 *(int64_t*)local[pos] = encode_int64_t(arg);
99 args[pos] = (
const char*)local[pos];
100 lengths[pos] =
sizeof(int64_t);
102 _add(pos + 1, rest...);
106 template<
typename... REST>
107 void _add(
unsigned pos,
const char* arg,
const REST&... rest)
109 local[pos] =
nullptr;
113 _add(pos + 1, rest...);
117 template<
typename... REST>
118 void _add(
unsigned pos,
const std::string& arg,
const REST&... rest)
120 local[pos] =
nullptr;
121 args[pos] = arg.data();
122 lengths[pos] = arg.size();
124 _add(pos + 1, rest...);
128 template<
typename... REST>
129 void _add(
unsigned pos,
const std::vector<uint8_t>& arg,
const REST&... rest)
131 local[pos] =
nullptr;
132 args[pos] = (
const char*)arg.data();
133 lengths[pos] = arg.size();
135 _add(pos + 1, rest...);
139 template<
typename... REST>
142 local[pos] = malloc(
sizeof(int64_t));
143 *(int64_t*)local[pos] = encode_datetime(arg);
144 args[pos] = (
const char*)local[pos];
145 lengths[pos] =
sizeof(int64_t);
147 _add(pos + 1, rest...);
156 Result() : res(
nullptr) {}
157 Result(PGresult* res) : res(res) {}
158 ~
Result() { PQclear(res); }
164 if (
this == &o)
return *
this;
171 operator bool()
const {
return res !=
nullptr; }
172 operator PGresult*() {
return res; }
173 operator const PGresult*()
const {
return res; }
188 unsigned rowcount()
const {
return PQntuples(res); }
191 bool is_null(
unsigned row,
unsigned col)
const
193 return PQgetisnull(res, row, col);
199 char* val = PQgetvalue(res, row, col);
204 uint16_t
get_int2(
unsigned row,
unsigned col)
const
206 char* val = PQgetvalue(res, row, col);
207 return ntohs(*(uint16_t*)val);
211 uint32_t
get_int4(
unsigned row,
unsigned col)
const
213 char* val = PQgetvalue(res, row, col);
214 return ntohl(*(uint32_t*)val);
218 uint64_t
get_int8(
unsigned row,
unsigned col)
const;
221 std::vector<uint8_t>
get_bytea(
unsigned row,
unsigned col)
const;
226 return PQgetvalue(res, row, col);
245 PGconn*
db =
nullptr;
246 std::unordered_set<std::string> prepared_names;
249 void init_after_connect();
259 operator PGconn*() {
return db; }
267 void open_url(
const std::string& connection_string);
270 std::unique_ptr<Transaction>
transaction(
bool readonly=
false)
override;
273 void prepare(
const std::string& name,
const std::string& query);
277 return PQexecParams(
db, query, 0,
nullptr,
nullptr,
nullptr,
nullptr, 1);
280 postgresql::Result exec_unchecked(
const std::string& query)
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 return PQexecParams(
db, query, params.count,
nullptr, params.args, params.lengths, params.formats, 1);
315 template<
typename ...ARGS>
316 postgresql::Result exec_unchecked(
const std::string& query, ARGS... args)
318 postgresql::Params<ARGS...> params(args...);
319 return PQexecParams(
db, query.c_str(), params.count,
nullptr, params.args, params.lengths, params.formats, 1);
322 template<
typename STRING,
typename ...ARGS>
323 void exec_no_data(STRING query, ARGS... args)
325 postgresql::Result res(exec_unchecked(query, args...));
326 res.expect_no_data(query);
329 template<
typename STRING,
typename ...ARGS>
330 postgresql::Result exec(STRING query, ARGS... args)
332 postgresql::Result res(exec_unchecked(query, args...));
333 res.expect_result(query);
337 template<
typename STRING,
typename ...ARGS>
338 postgresql::Result exec_one_row(STRING query, ARGS... args)
340 postgresql::Result res(exec_unchecked(query, args...));
341 res.expect_one_row(query);
345 postgresql::Result exec_prepared_unchecked(
const char* name)
347 return PQexecPrepared(
db, name, 0,
nullptr,
nullptr,
nullptr, 1);
350 postgresql::Result exec_prepared_unchecked(
const std::string& name)
352 return PQexecPrepared(
db, name.c_str(), 0,
nullptr,
nullptr,
nullptr, 1);
355 template<
typename STRING>
356 void exec_prepared_no_data(STRING name)
358 postgresql::Result res(exec_prepared_unchecked(name));
359 res.expect_no_data(name);
362 template<
typename STRING>
363 postgresql::Result exec_prepared(STRING name)
365 postgresql::Result res(exec_prepared_unchecked(name));
366 res.expect_result(name);
370 template<
typename STRING>
371 postgresql::Result exec_prepared_one_row(STRING name)
373 postgresql::Result res(exec_prepared_unchecked(name));
374 res.expect_one_row(name);
378 template<
typename ...ARGS>
379 postgresql::Result exec_prepared_unchecked(
const char* name, ARGS... args)
381 postgresql::Params<ARGS...> params(args...);
382 return PQexecPrepared(
db, name, params.count, params.args, params.lengths, params.formats, 1);
385 template<
typename ...ARGS>
386 postgresql::Result exec_prepared_unchecked(
const std::string& name, ARGS... args)
388 postgresql::Params<ARGS...> params(args...);
389 return PQexecPrepared(
db, name.c_str(), params.count, params.args, params.lengths, params.formats, 1);
392 template<
typename STRING,
typename ...ARGS>
393 void exec_prepared_no_data(STRING name, ARGS... args)
395 postgresql::Result res(exec_prepared_unchecked(name, args...));
396 res.expect_no_data(name);
399 template<
typename STRING,
typename ...ARGS>
400 postgresql::Result exec_prepared(STRING name, ARGS... args)
402 postgresql::Result res(exec_prepared_unchecked(name, args...));
403 res.expect_result(name);
407 template<
typename STRING,
typename ...ARGS>
408 postgresql::Result exec_prepared_one_row(STRING name, ARGS... args)
410 postgresql::Result res(exec_prepared_unchecked(name, args...));
411 res.expect_one_row(name);
421 bool has_table(const std::
string& name) override;
422 std::
string get_setting(const std::
string& key) override;
423 void set_setting(const std::
string& key, const std::
string& value) override;
425 void execute(const std::
string& query) override;
426 void explain(const std::
string& query, FILE* out) override;
437 void pqexec(const std::
string& query);
448 void run_single_row_mode(const std::
string& query_desc, std::function<
void(const postgresql::Result&)> dest);
457 void append_escaped(Querybuf& qb, const std::vector<uint8_t>& buf);
void set_setting(const std::string &key, const std::string &value) override
Set a value in the settings table.
void _add(unsigned pos, const std::string &arg, const REST &... rest)
Fill in the argument structures.
Definition: postgresql.h:118
void open_url(const std::string &connection_string)
Connect to PostgreSQL using a connection URI.
int changes()
Count the number of rows modified by the last query that was run.
Error in case of failed database operations.
Definition: error.h:21
bool has_table(const std::string &name) override
Check if the database contains a table.
void expect_one_row(const std::string &query)
Check that the result successfully returned one row of data.
void drop_table_if_exists(const char *name)
Delete a table in the database if it exists, otherwise do nothing.
void prepare(const std::string &name, const std::string &query)
Precompile a query.
Report an PostgreSQL error.
Definition: postgresql.h:20
void expect_no_data(const std::string &query)
Check that the result successfully returned no data.
void run_single_row_mode(const std::string &query_desc, std::function< void(const postgresql::Result &)> dest)
Retrieve query results in single row mode.
void discard_all_input_nothrow() noexcept
Discard all input from an asynchronous request.
PGconn * db
Database connection.
Definition: postgresql.h:245
void append_escaped(Querybuf &qb, const char *str)
Escape the string as a literal value and append it to qb.
void execute(const std::string &query) override
Execute a query without reading its results.
void explain(const std::string &query, FILE *out) override
Format and print the EXPLAIN output for the query to the given file.
uint32_t get_int4(unsigned row, unsigned col) const
Return a result value, transmitted in binary as a 4 bit integer.
Definition: postgresql.h:211
void _add(unsigned pos, int32_t arg, const REST &... rest)
Fill in the argument structures.
Definition: postgresql.h:83
bool get_bool(unsigned row, unsigned col) const
Return a result value, transmitted in binary as a byte (?)
Definition: postgresql.h:197
void _add(unsigned pos)
Terminating condition for compile-time arg expansion.
Definition: postgresql.h:66
void _add(unsigned pos, uint64_t arg, const REST &... rest)
Fill in the argument structures.
Definition: postgresql.h:95
void expect_success(const std::string &query)
Check that the result was successful.
Datetime get_timestamp(unsigned row, unsigned col) const
Return a result value, transmitted as a timestamp without timezone.
void pqexec(const std::string &query)
Wrap PQexec.
void _add(unsigned pos, std::nullptr_t arg, const REST &... rest)
Fill in the argument structures.
Definition: postgresql.h:72
void expect_result(const std::string &query)
Check that the result successfully returned some (possibly empty) data.
void pqexec_nothrow(const std::string &query) noexcept
Wrap PQexec but do not throw an exception in case of errors.
void drop_settings() override
Drop the settings table.
void _add(unsigned pos, const Datetime &arg, const REST &... rest)
Fill in the argument structures.
Definition: postgresql.h:140
const char * get_string(unsigned row, unsigned col) const
Return a result value, transmitted as a string.
Definition: postgresql.h:224
Result(Result &&o)
Implement move.
Definition: postgresql.h:161
std::vector< uint8_t > get_bytea(unsigned row, unsigned col) const
Return a result value, transmitted in binary as an 8 bit integer.
#define WREPORT_THROWF_ATTRS(a, b)
Date and time.
Definition: types.h:164
Wrap a PGresult, taking care of its memory management.
Definition: postgresql.h:152
uint64_t get_int8(unsigned row, unsigned col) const
Return a result value, transmitted in binary as an 8 bit integer.
std::string get_setting(const std::string &key) override
Get a value from the settings table.
unsigned rowcount() const
Get the number of rows in the result.
Definition: postgresql.h:188
Database connection.
Definition: postgresql.h:241
void _add(unsigned pos, const std::vector< uint8_t > &arg, const REST &... rest)
Fill in the argument structures.
Definition: postgresql.h:129
std::unique_ptr< Transaction > transaction(bool readonly=false) override
Begin a transaction.
uint16_t get_int2(unsigned row, unsigned col) const
Return a result value, transmitted in binary as a 2 bit integer.
Definition: postgresql.h:204
bool is_null(unsigned row, unsigned col) const
Check if a result value is null.
Definition: postgresql.h:191
Argument list for PQexecParams built at compile time.
Definition: postgresql.h:41
void _add(unsigned pos, const char *arg, const REST &... rest)
Fill in the argument structures.
Definition: postgresql.h:107
void cancel_running_query_nothrow() noexcept
Send a cancellation command to the server.