Skip to content

Commit 397e286

Browse files
committed
Refactor: Modernize copy functions using std::string_view
1 parent bf00c51 commit 397e286

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

src/middle-pgsql.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static void send_id_list(pg_conn_t const &db_connection,
6464
}
6565

6666
auto const sql = fmt::format("COPY {} FROM STDIN", table);
67-
db_connection.copy_start(sql.c_str());
67+
db_connection.copy_start(sql);
6868
db_connection.copy_send(data, table);
6969
db_connection.copy_end(table);
7070
}

src/pgsql.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,24 @@ pg_result_t pg_conn_t::exec(std::string const &sql) const
9191
return exec(sql.c_str());
9292
}
9393

94-
void pg_conn_t::copy_start(char const *sql) const
94+
void pg_conn_t::copy_start(std::string_view sql) const
9595
{
9696
assert(m_conn);
9797

9898
log_sql("(C{}) {}", m_connection_id, sql);
99-
pg_result_t const res{PQexec(m_conn.get(), sql)};
99+
pg_result_t const res{PQexec(m_conn.get(), sql.data())};
100100
if (res.status() != PGRES_COPY_IN) {
101101
throw fmt_error("Database error on COPY: {}", error_msg());
102102
}
103103
}
104104

105-
void pg_conn_t::copy_send(std::string const &data,
106-
std::string const &context) const
105+
void pg_conn_t::copy_send(std::string_view data, std::string_view context) const
107106
{
108107
assert(m_conn);
109108

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

114113
switch (r) {
115114
case 0: // need to wait for write ready
@@ -127,14 +126,14 @@ void pg_conn_t::copy_send(std::string const &data,
127126
if (data.size() < 1100) {
128127
log_error("Data: {}", data);
129128
} else {
130-
log_error("Data: {}\n...\n{}", std::string(data, 0, 500),
131-
std::string(data, data.size() - 500));
129+
log_error("Data: {}\n...\n{}", data.substr(0, 500),
130+
data.substr(data.size() - 500, 500));
132131
}
133132

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

137-
void pg_conn_t::copy_end(std::string const &context) const
136+
void pg_conn_t::copy_end(std::string_view context) const
138137
{
139138
assert(m_conn);
140139

src/pgsql.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ class pg_conn_t
215215
*/
216216
void set_config(char const *setting, char const *value) const;
217217

218-
void copy_start(char const *sql) const;
219-
void copy_send(std::string const &data, std::string const &context) const;
220-
void copy_end(std::string const &context) const;
218+
void copy_start(std::string_view sql) const;
219+
void copy_send(std::string_view data, std::string_view context) const;
220+
void copy_end(std::string_view context) const;
221221

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

0 commit comments

Comments
 (0)