Skip to content

Commit

Permalink
Merge pull request #2095 from joto/modernize-copy
Browse files Browse the repository at this point in the history
Refactor: Modernize copy functions using std::string_view
  • Loading branch information
lonvia authored Oct 30, 2023
2 parents bf00c51 + 397e286 commit 7259990
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/middle-pgsql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static void send_id_list(pg_conn_t const &db_connection,
}

auto const sql = fmt::format("COPY {} FROM STDIN", table);
db_connection.copy_start(sql.c_str());
db_connection.copy_start(sql);
db_connection.copy_send(data, table);
db_connection.copy_end(table);
}
Expand Down
15 changes: 7 additions & 8 deletions src/pgsql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,24 @@ pg_result_t pg_conn_t::exec(std::string const &sql) const
return exec(sql.c_str());
}

void pg_conn_t::copy_start(char const *sql) const
void pg_conn_t::copy_start(std::string_view sql) const
{
assert(m_conn);

log_sql("(C{}) {}", m_connection_id, sql);
pg_result_t const res{PQexec(m_conn.get(), sql)};
pg_result_t const res{PQexec(m_conn.get(), sql.data())};
if (res.status() != PGRES_COPY_IN) {
throw fmt_error("Database error on COPY: {}", error_msg());
}
}

void pg_conn_t::copy_send(std::string const &data,
std::string const &context) const
void pg_conn_t::copy_send(std::string_view data, std::string_view context) const
{
assert(m_conn);

log_sql_data("(C{}) Copy data to '{}':\n{}", m_connection_id, context,
data);
int const r = PQputCopyData(m_conn.get(), data.c_str(), (int)data.size());
int const r = PQputCopyData(m_conn.get(), data.data(), (int)data.size());

switch (r) {
case 0: // need to wait for write ready
Expand All @@ -127,14 +126,14 @@ void pg_conn_t::copy_send(std::string const &data,
if (data.size() < 1100) {
log_error("Data: {}", data);
} else {
log_error("Data: {}\n...\n{}", std::string(data, 0, 500),
std::string(data, data.size() - 500));
log_error("Data: {}\n...\n{}", data.substr(0, 500),
data.substr(data.size() - 500, 500));
}

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

void pg_conn_t::copy_end(std::string const &context) const
void pg_conn_t::copy_end(std::string_view context) const
{
assert(m_conn);

Expand Down
6 changes: 3 additions & 3 deletions src/pgsql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ class pg_conn_t
*/
void set_config(char const *setting, char const *value) const;

void copy_start(char const *sql) const;
void copy_send(std::string const &data, std::string const &context) const;
void copy_end(std::string const &context) const;
void copy_start(std::string_view sql) const;
void copy_send(std::string_view data, std::string_view context) const;
void copy_end(std::string_view context) const;

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

0 comments on commit 7259990

Please sign in to comment.