Skip to content

Commit a1643d4

Browse files
committed
Make conn_t class in tests derive from pg_conn_t
Simplifies code and tests run on more production code this way.
1 parent e09e4ad commit a1643d4

1 file changed

Lines changed: 9 additions & 49 deletions

File tree

tests/common-pg.hpp

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <stdexcept>
66
#include <string>
77

8-
#include <libpq-fe.h>
9-
108
#include <boost/lexical_cast.hpp>
119

1210
#include "format.hpp"
@@ -23,51 +21,18 @@
2321
/// Helper classes for postgres connections
2422
namespace pg {
2523

26-
class conn_t
24+
class conn_t : public pg_conn_t
2725
{
2826
public:
29-
conn_t(char const *conninfo)
30-
{
31-
m_conn = PQconnectdb(conninfo);
32-
33-
if (PQstatus(m_conn) != CONNECTION_OK) {
34-
fprintf(stderr, "Could not connect to database '%s' because: %s\n",
35-
conninfo, PQerrorMessage(m_conn));
36-
PQfinish(m_conn);
37-
throw std::runtime_error("Database connection failed");
38-
}
39-
}
40-
41-
~conn_t() noexcept
42-
{
43-
if (m_conn) {
44-
PQfinish(m_conn);
45-
}
46-
}
47-
48-
void exec(std::string const &cmd, ExecStatusType expect = PGRES_COMMAND_OK)
49-
{
50-
pg_result_t res = query(cmd);
51-
if (res.status() != expect) {
52-
fprintf(stderr, "Query '%s' failed with: %s\n", cmd.c_str(),
53-
PQerrorMessage(m_conn));
54-
throw std::runtime_error("DB exec failed.");
55-
}
56-
}
57-
58-
pg_result_t query(std::string const &cmd) const
59-
{
60-
return PQexec(m_conn, cmd.c_str());
61-
}
27+
conn_t(std::string const &conninfo) : pg_conn_t(conninfo) {}
6228

6329
template <typename T>
6430
T require_scalar(std::string const &cmd) const
6531
{
66-
pg_result_t res = query(cmd);
67-
REQUIRE(res.status() == PGRES_TUPLES_OK);
32+
pg_result_t const res = query(PGRES_TUPLES_OK, cmd);
6833
REQUIRE(res.num_tuples() == 1);
6934

70-
std::string str = res.get_value(0, 0);
35+
std::string const str = res.get_value(0, 0);
7136
return boost::lexical_cast<T>(str);
7237
}
7338

@@ -78,16 +43,14 @@ class conn_t
7843

7944
void assert_null(std::string const &cmd) const
8045
{
81-
pg_result_t res = query(cmd);
82-
REQUIRE(res.status() == PGRES_TUPLES_OK);
46+
pg_result_t const res = query(PGRES_TUPLES_OK, cmd);
8347
REQUIRE(res.num_tuples() == 1);
8448
REQUIRE(res.is_null(0, 0));
8549
}
8650

8751
pg_result_t require_row(std::string const &cmd) const
8852
{
89-
pg_result_t res = query(cmd);
90-
REQUIRE(res.status() == PGRES_TUPLES_OK);
53+
pg_result_t res = query(PGRES_TUPLES_OK, cmd);
9154
REQUIRE(res.num_tuples() == 1);
9255

9356
return res;
@@ -108,9 +71,6 @@ class conn_t
10871

10972
REQUIRE(get_count("pg_catalog.pg_class", where) == 1);
11073
}
111-
112-
private:
113-
PGconn *m_conn = nullptr;
11474
};
11575

11676
class tempdb_t
@@ -146,15 +106,15 @@ class tempdb_t
146106
{
147107
if (!m_db_name.empty()) {
148108
try {
149-
conn_t conn("dbname=postgres");
150-
conn.query("DROP DATABASE IF EXISTS \"{}\""_format(m_db_name));
109+
conn_t conn{"dbname=postgres"};
110+
conn.exec("DROP DATABASE IF EXISTS \"{}\""_format(m_db_name));
151111
} catch (...) {
152112
fprintf(stderr, "DROP DATABASE failed. Ignored.\n");
153113
}
154114
}
155115
}
156116

157-
conn_t connect() const { return conn_t(conninfo().c_str()); }
117+
conn_t connect() const { return conn_t{conninfo()}; }
158118

159119
std::string conninfo() const { return "dbname=" + m_db_name; }
160120

0 commit comments

Comments
 (0)