Skip to content

Commit c8f7de5

Browse files
committed
Clean up pgsql code related to COPY command
Use consistent and somewhat clearer naming, use special function for copy_start().
1 parent 4facd1a commit c8f7de5

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

src/db-copy.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ void db_copy_thread_t::thread_t::write_to_db(db_cmd_copy_t *buffer)
184184
start_copy(buffer->target);
185185
}
186186

187-
m_conn->copy_data(buffer->buffer, buffer->target->name);
187+
m_conn->copy_send(buffer->buffer, buffer->target->name);
188188
}
189189

190190
void db_copy_thread_t::thread_t::start_copy(
@@ -205,15 +205,15 @@ void db_copy_thread_t::thread_t::start_copy(
205205
}
206206

207207
sql.push_back('\0');
208-
m_conn->query(PGRES_COPY_IN, sql.data());
208+
m_conn->copy_start(sql.data());
209209

210210
m_inflight = target;
211211
}
212212

213213
void db_copy_thread_t::thread_t::finish_copy()
214214
{
215215
if (m_inflight) {
216-
m_conn->end_copy(m_inflight->name);
216+
m_conn->copy_end(m_inflight->name);
217217
m_inflight.reset();
218218
}
219219
}

src/pgsql.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,25 @@ void pg_conn_t::exec(std::string const &sql) const
8080
}
8181
}
8282

83-
void pg_conn_t::copy_data(std::string const &sql,
83+
void pg_conn_t::copy_start(char const *sql) const
84+
{
85+
assert(m_conn);
86+
87+
log_sql("{}", sql);
88+
pg_result_t const res{PQexec(m_conn.get(), sql)};
89+
if (res.status() != PGRES_COPY_IN) {
90+
throw std::runtime_error{
91+
"Database error on COPY: {}"_format(error_msg())};
92+
}
93+
}
94+
95+
void pg_conn_t::copy_send(std::string const &data,
8496
std::string const &context) const
8597
{
8698
assert(m_conn);
8799

88-
log_sql_data("Copy data to '{}':\n{}", context, sql);
89-
int const r = PQputCopyData(m_conn.get(), sql.c_str(), (int)sql.size());
100+
log_sql_data("Copy data to '{}':\n{}", context, data);
101+
int const r = PQputCopyData(m_conn.get(), data.c_str(), (int)data.size());
90102

91103
switch (r) {
92104
case 0: // need to wait for write ready
@@ -101,17 +113,17 @@ void pg_conn_t::copy_data(std::string const &sql,
101113
break;
102114
}
103115

104-
if (sql.size() < 1100) {
105-
log_error("Data: {}", sql);
116+
if (data.size() < 1100) {
117+
log_error("Data: {}", data);
106118
} else {
107-
log_error("Data: {}\n...\n{}", std::string(sql, 0, 500),
108-
std::string(sql, sql.size() - 500));
119+
log_error("Data: {}\n...\n{}", std::string(data, 0, 500),
120+
std::string(data, data.size() - 500));
109121
}
110122

111123
throw std::runtime_error{"COPYing data to Postgresql."};
112124
}
113125

114-
void pg_conn_t::end_copy(std::string const &context) const
126+
void pg_conn_t::copy_end(std::string const &context) const
115127
{
116128
assert(m_conn);
117129

src/pgsql.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ class pg_conn_t
237237
void exec(char const *sql) const;
238238
void exec(std::string const &sql) const;
239239

240-
void copy_data(std::string const &sql, std::string const &context) const;
241-
242-
void end_copy(std::string const &context) const;
240+
void copy_start(char const *sql) const;
241+
void copy_send(std::string const &data, std::string const &context) const;
242+
void copy_end(std::string const &context) const;
243243

244244
/// Return the latest generated error message on this connection.
245245
char const *error_msg() const noexcept;

0 commit comments

Comments
 (0)