From ee34d25eae28e92cb3e49c2e4a0e9f4215d316c8 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Wed, 25 Jun 2025 22:18:35 +0200 Subject: [PATCH 1/2] Refactor: Use std::string not std::string_view in copy_start() Because std::string guarantees that there is a \0 at the end which we need for the C interface of the PostgreSQL library. Gets rid of clang-tidy warnings. --- src/db-copy.cpp | 2 +- src/pgsql.cpp | 4 ++-- src/pgsql.hpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/db-copy.cpp b/src/db-copy.cpp index f0fd0e33a..be2eee2e0 100644 --- a/src/db-copy.cpp +++ b/src/db-copy.cpp @@ -221,7 +221,7 @@ void db_copy_thread_t::thread_t::start_copy( } sql.push_back('\0'); - m_db_connection.copy_start(sql.data()); + m_db_connection.copy_start(to_string(sql)); m_inflight = target; } diff --git a/src/pgsql.cpp b/src/pgsql.cpp index 905bf51e7..9d01a88fc 100644 --- a/src/pgsql.cpp +++ b/src/pgsql.cpp @@ -136,12 +136,12 @@ pg_result_t pg_conn_t::exec(std::string const &sql) const return exec(sql.c_str()); } -void pg_conn_t::copy_start(std::string_view sql) const +void pg_conn_t::copy_start(std::string const &sql) const { assert(m_conn); log_sql("(C{}) {}", m_connection_id, sql); - pg_result_t const res{PQexec(m_conn.get(), sql.data())}; + pg_result_t const res{PQexec(m_conn.get(), sql.c_str())}; if (res.status() != PGRES_COPY_IN) { throw fmt_error("Database error on COPY: {}", error_msg()); } diff --git a/src/pgsql.hpp b/src/pgsql.hpp index 8b1a93322..d8a43c67a 100644 --- a/src/pgsql.hpp +++ b/src/pgsql.hpp @@ -241,7 +241,7 @@ class pg_conn_t */ void set_config(char const *setting, char const *value) const; - void copy_start(std::string_view sql) const; + void copy_start(std::string const &sql) const; void copy_send(std::string_view data, std::string_view context) const; void copy_end(std::string_view context) const; From ce0524d9cbfd294c5fbe64e53df59e25d445d748 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Wed, 25 Jun 2025 22:18:35 +0200 Subject: [PATCH 2/2] Refactor: Use std::string not std::string_view in prepare() Because std::string guarantees that there is a \0 at the end which we need for the C interface of the PostgreSQL library. Gets rid of clang-tidy warnings. --- src/gen/gen-base.cpp | 4 ++-- src/gen/gen-base.hpp | 4 ++-- src/middle-pgsql.cpp | 2 +- src/middle-pgsql.hpp | 2 +- src/pgsql.cpp | 6 +++--- src/pgsql.hpp | 5 +++-- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/gen/gen-base.cpp b/src/gen/gen-base.cpp index cdf553041..7a7123bb6 100644 --- a/src/gen/gen-base.cpp +++ b/src/gen/gen-base.cpp @@ -99,14 +99,14 @@ pg_result_t gen_base_t::dbexec(params_t const &tmp_params, return connection().exec(sql_template.render()); } -void gen_base_t::dbprepare(std::string_view stmt, std::string const &templ) +void gen_base_t::dbprepare(std::string const &stmt, std::string const &templ) { template_t sql_template{templ}; sql_template.set_params(get_params()); connection().prepare(stmt, fmt::runtime(sql_template.render())); } -void gen_base_t::dbprepare(std::string_view stmt, params_t const &tmp_params, +void gen_base_t::dbprepare(std::string const &stmt, params_t const &tmp_params, std::string const &templ) { template_t sql_template{templ}; diff --git a/src/gen/gen-base.hpp b/src/gen/gen-base.hpp index 875cbbac6..26588ba97 100644 --- a/src/gen/gen-base.hpp +++ b/src/gen/gen-base.hpp @@ -101,9 +101,9 @@ class gen_base_t pg_result_t dbexec(params_t const &tmp_params, std::string const &templ); - void dbprepare(std::string_view stmt, std::string const &templ); + void dbprepare(std::string const &stmt, std::string const &templ); - void dbprepare(std::string_view stmt, params_t const &tmp_params, + void dbprepare(std::string const &stmt, params_t const &tmp_params, std::string const &templ); void raster_table_preprocess(std::string const &table); diff --git a/src/middle-pgsql.cpp b/src/middle-pgsql.cpp index 8aa2194ea..a52aad1a4 100644 --- a/src/middle-pgsql.cpp +++ b/src/middle-pgsql.cpp @@ -93,7 +93,7 @@ void middle_pgsql_t::dbexec(std::string_view templ) const m_db_connection.exec(render_template(templ)); } -void middle_query_pgsql_t::prepare(std::string_view stmt, +void middle_query_pgsql_t::prepare(std::string const &stmt, std::string const &sql_cmd) const { m_db_connection.prepare(stmt, fmt::runtime(sql_cmd)); diff --git a/src/middle-pgsql.hpp b/src/middle-pgsql.hpp index 54164c39c..2ef73c887 100644 --- a/src/middle-pgsql.hpp +++ b/src/middle-pgsql.hpp @@ -71,7 +71,7 @@ class middle_query_pgsql_t : public middle_query_t bool relation_get(osmid_t id, osmium::memory::Buffer *buffer) const override; - void prepare(std::string_view stmt, std::string const &sql_cmd) const; + void prepare(std::string const &stmt, std::string const &sql_cmd) const; private: osmium::Location get_node_location_flatnodes(osmid_t id) const; diff --git a/src/pgsql.cpp b/src/pgsql.cpp index 9d01a88fc..8e9b906e3 100644 --- a/src/pgsql.cpp +++ b/src/pgsql.cpp @@ -194,15 +194,15 @@ void pg_conn_t::copy_end(std::string_view context) const } } -void pg_conn_t::prepare_internal(std::string_view stmt, - std::string_view sql) const +void pg_conn_t::prepare_internal(std::string const &stmt, + std::string const &sql) const { if (get_logger().log_sql()) { log_sql("(C{}) PREPARE {} AS {}", m_connection_id, stmt, sql); } pg_result_t const res{ - PQprepare(m_conn.get(), stmt.data(), sql.data(), 0, nullptr)}; + PQprepare(m_conn.get(), stmt.c_str(), sql.c_str(), 0, nullptr)}; if (res.status() != PGRES_COMMAND_OK) { throw fmt_error("Prepare failed for '{}': {}.", sql, error_msg()); } diff --git a/src/pgsql.hpp b/src/pgsql.hpp index d8a43c67a..be2d6f9ad 100644 --- a/src/pgsql.hpp +++ b/src/pgsql.hpp @@ -195,7 +195,7 @@ class pg_conn_t * status code PGRES_COMMAND_OK). */ template - void prepare(std::string_view stmt, fmt::format_string sql, + void prepare(std::string const &stmt, fmt::format_string sql, TArgs... params) const { std::string const query = @@ -252,7 +252,8 @@ class pg_conn_t void close(); private: - void prepare_internal(std::string_view stmt, std::string_view sql) const; + void prepare_internal(std::string const &stmt, + std::string const &sql) const; pg_result_t exec_prepared_internal(char const *stmt, int num_params, char const *const *param_values,