Skip to content

Commit 5dc24ac

Browse files
committed
Refactor call of PQfnumber function
Refactor the last place where a PQ* function was used directly outside pgsql.[ch]pp.
1 parent 1ef354e commit 5dc24ac

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/pgsql.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ class pg_result_t
6767
return std::string(get_value(row, col), get_length(row, col));
6868
}
6969

70+
/**
71+
* Get the column number from the name. Returns -1 if there is no column
72+
* of that name.
73+
*/
74+
int get_column_number(std::string const& name) const noexcept
75+
{
76+
return PQfnumber(m_result.get(), ('"' + name + '"').c_str());
77+
}
78+
7079
private:
7180
struct pg_result_deleter_t
7281
{

src/table.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ void table_t::start(std::string const &conninfo,
119119
} //appending
120120
else {
121121
//check the columns against those in the existing table
122-
auto res = m_sql_conn->query(
122+
auto const res = m_sql_conn->query(
123123
PGRES_TUPLES_OK, "SELECT * FROM {} LIMIT 0"_format(m_target->name));
124124
for (auto const &column : columns) {
125-
if (PQfnumber(res.get(), ('"' + column.name + '"').c_str()) < 0) {
125+
if (res.get_column_number(column.name) < 0) {
126126
fmt::print(stderr, "Adding new column \"{}\" to \"{}\"\n",
127127
column.name, m_target->name);
128128
m_sql_conn->exec("ALTER TABLE {} ADD COLUMN \"{}\" {}"_format(

0 commit comments

Comments
 (0)