Skip to content

Commit 8844a3b

Browse files
committed
Add asserts to check we are not operating on a closed db connection
1 parent 94a7fad commit 8844a3b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/pgsql.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "util.hpp"
66

77
#include <array>
8+
#include <cassert>
89
#include <cstdarg>
910
#include <cstdio>
1011

@@ -22,11 +23,15 @@ pg_conn_t::pg_conn_t(std::string const &conninfo)
2223

2324
char const *pg_conn_t::error_msg() const noexcept
2425
{
26+
assert(m_conn);
27+
2528
return PQerrorMessage(m_conn.get());
2629
}
2730

2831
pg_result_t pg_conn_t::query(ExecStatusType expect, char const *sql) const
2932
{
33+
assert(m_conn);
34+
3035
log_sql("{}", sql);
3136
pg_result_t res{PQexec(m_conn.get(), sql)};
3237
if (PQresultStatus(res.get()) != expect) {
@@ -69,6 +74,8 @@ void pg_conn_t::exec(std::string const &sql) const
6974
void pg_conn_t::copy_data(std::string const &sql,
7075
std::string const &context) const
7176
{
77+
assert(m_conn);
78+
7279
log_sql_data("Copy data to '{}':\n{}", context, sql);
7380
int const r = PQputCopyData(m_conn.get(), sql.c_str(), (int)sql.size());
7481

@@ -97,6 +104,8 @@ void pg_conn_t::copy_data(std::string const &sql,
97104

98105
void pg_conn_t::end_copy(std::string const &context) const
99106
{
107+
assert(m_conn);
108+
100109
if (PQputCopyEnd(m_conn.get(), nullptr) != 1) {
101110
throw std::runtime_error{"Ending COPY mode for '{}' failed: {}."_format(
102111
context, error_msg())};
@@ -113,6 +122,8 @@ pg_result_t
113122
pg_conn_t::exec_prepared_internal(char const *stmt, int num_params,
114123
char const *const *param_values) const
115124
{
125+
assert(m_conn);
126+
116127
if (get_logger().log_sql()) {
117128
std::string params;
118129
for (int i = 0; i < num_params; ++i) {

0 commit comments

Comments
 (0)