Skip to content

Commit 0569cdf

Browse files
authored
Merge pull request #1823 from joto/pgsql-code-cleanup
Various cleanups of pgsql code
2 parents 677b647 + f0b68a6 commit 0569cdf

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

src/pgsql.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pg_result_t pg_conn_t::query(ExecStatusType expect, char const *sql) const
4343

4444
log_sql("{}", sql);
4545
pg_result_t res{PQexec(m_conn.get(), sql)};
46-
if (PQresultStatus(res.get()) != expect) {
46+
if (res.status() != expect) {
4747
throw std::runtime_error{"Database error: {}"_format(error_msg())};
4848
}
4949
return res;
@@ -121,7 +121,7 @@ void pg_conn_t::end_copy(std::string const &context) const
121121
}
122122

123123
pg_result_t const res{PQgetResult(m_conn.get())};
124-
if (PQresultStatus(res.get()) != PGRES_COMMAND_OK) {
124+
if (res.status() != PGRES_COMMAND_OK) {
125125
throw std::runtime_error{fmt::format(
126126
"Ending COPY mode for '{}' failed: {}.", context, error_msg())};
127127
}
@@ -156,11 +156,11 @@ pg_conn_t::exec_prepared_internal(char const *stmt, int num_params,
156156
}
157157
pg_result_t res{PQexecPrepared(m_conn.get(), stmt, num_params, param_values,
158158
nullptr, nullptr, 0)};
159-
if (PQresultStatus(res.get()) != PGRES_TUPLES_OK) {
159+
if (res.status() != PGRES_TUPLES_OK) {
160160
log_error("SQL command failed: EXECUTE {}({})", stmt,
161161
concat_params(num_params, param_values));
162-
throw std::runtime_error{"Database error: {} ({})"_format(
163-
error_msg(), PQresultStatus(res.get()))};
162+
throw std::runtime_error{
163+
"Database error: {} ({})"_format(error_msg(), res.status())};
164164
}
165165

166166
return res;

src/pgsql.hpp

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@
3737
class pg_result_t
3838
{
3939
public:
40-
pg_result_t() = default;
40+
pg_result_t() noexcept = default;
4141

4242
explicit pg_result_t(PGresult *result) noexcept : m_result(result) {}
4343

44-
/// Get a pointer to the underlying PGresult object.
45-
PGresult *get() const noexcept { return m_result.get(); }
46-
4744
/// Get the status of this result.
4845
ExecStatusType status() const noexcept
4946
{
@@ -56,27 +53,42 @@ class pg_result_t
5653
/// The number of tuples (rows) in this result.
5754
int num_tuples() const noexcept { return PQntuples(m_result.get()); }
5855

59-
/// Does the field at (row, col) has the NULL value?
56+
/**
57+
* Does the field at (row, col) has the NULL value?
58+
*
59+
* \pre 0 <= row < num_tuples() and 0 <= col < num_fields()
60+
*/
6061
bool is_null(int row, int col) const noexcept
6162
{
62-
assert(row < num_tuples() && col < num_fields());
63+
assert(row >= 0 && row < num_tuples() && col >= 0 &&
64+
col < num_fields());
6365
return PQgetisnull(m_result.get(), row, col) != 0;
6466
}
6567

66-
/// The length of the field at (row, col) in bytes.
68+
/**
69+
* The length of the field at (row, col) in bytes.
70+
*
71+
* \pre 0 <= row < num_tuples() and 0 <= col < num_fields()
72+
*/
6773
int get_length(int row, int col) const noexcept
6874
{
69-
assert(row < num_tuples() && col < num_fields());
75+
assert(row >= 0 && row < num_tuples() && col >= 0 &&
76+
col < num_fields());
7077
return PQgetlength(m_result.get(), row, col);
7178
}
7279

7380
/**
7481
* Get value of the field at (row, col) as char pointer. The string is
7582
* null-terminated. Only valid as long as the pg_result_t is in scope.
83+
*
84+
* When the result is NULL, an empty string is returned.
85+
*
86+
* \pre 0 <= row < num_tuples() and 0 <= col < num_fields()
7687
*/
7788
char const *get_value(int row, int col) const noexcept
7889
{
79-
assert(row < num_tuples() && col < num_fields());
90+
assert(row >= 0 && row < num_tuples() && col >= 0 &&
91+
col < num_fields());
8092
return PQgetvalue(m_result.get(), row, col);
8193
}
8294

@@ -100,7 +112,7 @@ class pg_result_t
100112
}
101113

102114
/// Return true if this holds an actual result.
103-
explicit operator bool() { return m_result.get(); }
115+
explicit operator bool() const noexcept { return m_result.get(); }
104116

105117
private:
106118
struct pg_result_deleter_t
@@ -207,6 +219,10 @@ class pg_conn_t
207219
pg_result_t query(ExecStatusType expect, char const *sql) const;
208220
pg_result_t query(ExecStatusType expect, std::string const &sql) const;
209221

222+
/**
223+
* Update a PostgreSQL setting (like with the SET command). Will silently
224+
* ignore settings that are not available or any other errors.
225+
*/
210226
void set_config(char const *setting, char const *value) const;
211227

212228
/**
@@ -223,6 +239,7 @@ class pg_conn_t
223239

224240
void end_copy(std::string const &context) const;
225241

242+
/// Return the latest generated error message on this connection.
226243
char const *error_msg() const noexcept;
227244

228245
/// Close database connection.
@@ -253,15 +270,15 @@ std::string tablespace_clause(std::string const &name);
253270
std::string qualified_name(std::string const &schema, std::string const &name);
254271

255272
/**
256-
* Check that the string confirms to the identifier syntax we accept.
257-
* Throws a runtime exception if an invalid character is found.
273+
* Check that the string conforms to the identifier syntax we accept.
258274
*
259275
* Note that PostgreSQL accepts any character in a quoted identifier.
260-
* This function checks for some characters that are problematic in the
261-
* internal functions that create SQL statements.
276+
* This function checks for some characters that are potentially problematic
277+
* in our internal functions that create SQL statements.
262278
*
263-
* \param name Identifier to check.
264-
* \param in Name of the identifier. Only used to create a human-readable error.
279+
* \param name Identifier to check.
280+
* \param in Name of the identifier. Only used to create a human-readable error.
281+
* \throws runtime_exception If an invalid character is found in the name.
265282
*/
266283
void check_identifier(std::string const &name, char const *in);
267284

0 commit comments

Comments
 (0)