From ccc75eafc7ddb7760130ec2ec0bb41086ba8ea79 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Tue, 11 Jan 2022 07:34:17 +0100 Subject: [PATCH] Remove obsolete pointer indirection and some warnings --- include/sqlpp11/postgresql/connection.h | 22 +++++----- .../postgresql/detail/connection_handle.h | 41 +++++++++---------- .../detail/prepared_statement_handle.h | 16 ++++---- include/sqlpp11/postgresql/result.h | 3 +- include/sqlpp11/postgresql/result_field.h | 2 +- 5 files changed, 41 insertions(+), 43 deletions(-) diff --git a/include/sqlpp11/postgresql/connection.h b/include/sqlpp11/postgresql/connection.h index 760c6a88..2528c519 100644 --- a/include/sqlpp11/postgresql/connection.h +++ b/include/sqlpp11/postgresql/connection.h @@ -443,17 +443,17 @@ namespace sqlpp inline size_t connection::insert_impl(const std::string& stmt) { - return execute(stmt)->result.affected_rows(); + return static_cast(execute(stmt)->result.affected_rows()); } inline size_t connection::update_impl(const std::string& stmt) { - return execute(stmt)->result.affected_rows(); + return static_cast(execute(stmt)->result.affected_rows()); } inline size_t connection::remove_impl(const std::string& stmt) { - return execute(stmt)->result.affected_rows(); + return static_cast(execute(stmt)->result.affected_rows()); } // prepared execution @@ -474,28 +474,28 @@ namespace sqlpp { validate_connection_handle(); execute_prepared_statement(*_handle, *prep._handle.get()); - return prep._handle->result.affected_rows(); + return static_cast(prep._handle->result.affected_rows()); } inline size_t connection::run_prepared_insert_impl(prepared_statement_t& prep) { validate_connection_handle(); execute_prepared_statement(*_handle, *prep._handle.get()); - return prep._handle->result.affected_rows(); + return static_cast(prep._handle->result.affected_rows()); } inline size_t connection::run_prepared_update_impl(prepared_statement_t& prep) { validate_connection_handle(); execute_prepared_statement(*_handle, *prep._handle.get()); - return prep._handle->result.affected_rows(); + return static_cast(prep._handle->result.affected_rows()); } inline size_t connection::run_prepared_remove_impl(prepared_statement_t& prep) { validate_connection_handle(); execute_prepared_statement(*_handle, *prep._handle.get()); - return prep._handle->result.affected_rows(); + return static_cast(prep._handle->result.affected_rows()); } inline void connection::set_default_isolation_level(isolation_level level) @@ -561,7 +561,7 @@ namespace sqlpp result.resize((s.size() * 2) + 1); int err; - size_t length = PQescapeStringConn(_handle->postgres, &result[0], s.c_str(), s.size(), &err); + size_t length = PQescapeStringConn(_handle->native(), &result[0], s.c_str(), s.size(), &err); result.resize(length); return result; } @@ -662,7 +662,7 @@ namespace sqlpp inline uint64_t connection::last_insert_id(const std::string& table, const std::string& fieldname) { std::string sql = "SELECT currval('" + table + "_" + fieldname + "_seq')"; - PGresult* res = PQexec(_handle->postgres, sql.c_str()); + PGresult* res = PQexec(_handle->native(), sql.c_str()); if (PQresultStatus(res) != PGRES_TUPLES_OK) { std::string err{PQresultErrorMessage(res)}; @@ -673,12 +673,12 @@ namespace sqlpp // Parse the number and return. std::string in{PQgetvalue(res, 0, 0)}; PQclear(res); - return std::stoi(in); + return std::stoul(in); } inline ::PGconn* connection::native_handle() { - return _handle->postgres; + return _handle->native(); } inline std::string context_t::escape(const std::string& arg) diff --git a/include/sqlpp11/postgresql/detail/connection_handle.h b/include/sqlpp11/postgresql/detail/connection_handle.h index c8d96b45..b1b3bfe3 100644 --- a/include/sqlpp11/postgresql/detail/connection_handle.h +++ b/include/sqlpp11/postgresql/detail/connection_handle.h @@ -53,28 +53,34 @@ namespace sqlpp namespace detail { + inline void handle_cleanup(PGconn* postgres) + { + PQfinish(postgres); + } + struct DLL_LOCAL connection_handle { - const std::shared_ptr config; - PGconn* postgres{nullptr}; - std::set prepared_statement_names; + std::shared_ptr config; + std::unique_ptr postgres; + std::set prepared_statement_names; connection_handle(const std::shared_ptr& config); - ~connection_handle(); connection_handle(const connection_handle&) = delete; - connection_handle(connection_handle&&) = delete; + connection_handle(connection_handle&&) = default; connection_handle& operator=(const connection_handle&) = delete; - connection_handle& operator=(connection_handle&&) = delete; + connection_handle& operator=(connection_handle&&) = default; + ~connection_handle(); PGconn* native() const { - return postgres; + return postgres.get(); } void deallocate_prepared_statement(const std::string& name); }; - inline connection_handle::connection_handle(const std::shared_ptr& conf) : config(conf) + inline connection_handle::connection_handle(const std::shared_ptr& conf) + : config(conf), postgres{nullptr, handle_cleanup} { #ifdef SQLPP_DYNAMIC_LOADING init_pg(""); @@ -194,18 +200,15 @@ namespace sqlpp { conninfo.append(" service=" + config->service); } - if (this->postgres) - return; - this->postgres = PQconnectdb(conninfo.c_str()); + postgres.reset(PQconnectdb(conninfo.c_str())); - if (!this->postgres) + if (!postgres) throw std::bad_alloc(); - if (PQstatus(this->postgres) != CONNECTION_OK) + if (PQstatus(postgres.get()) != CONNECTION_OK) { - std::string msg(PQerrorMessage(this->postgres)); - PQfinish(this->postgres); + std::string msg(PQerrorMessage(postgres.get())); throw broken_connection(std::move(msg)); } } @@ -217,18 +220,12 @@ namespace sqlpp { std::cerr << "PostgreSQL debug: closing database connection." << std::endl; } - - // Close connection - if (this->postgres) - { - PQfinish(this->postgres); - } } inline void connection_handle::deallocate_prepared_statement(const std::string& name) { std::string cmd = "DEALLOCATE \"" + name + "\""; - PGresult* result = PQexec(postgres, cmd.c_str()); + PGresult* result = PQexec(postgres.get(), cmd.c_str()); PQclear(result); prepared_statement_names.erase(name); } diff --git a/include/sqlpp11/postgresql/detail/prepared_statement_handle.h b/include/sqlpp11/postgresql/detail/prepared_statement_handle.h index 72cf72ed..e05dc1cb 100644 --- a/include/sqlpp11/postgresql/detail/prepared_statement_handle.h +++ b/include/sqlpp11/postgresql/detail/prepared_statement_handle.h @@ -65,9 +65,9 @@ namespace sqlpp // ctor statement_handle_t(detail::connection_handle& _connection); statement_handle_t(const statement_handle_t&) = delete; - statement_handle_t(statement_handle_t&&) = default; + statement_handle_t(statement_handle_t&&) = delete; statement_handle_t& operator=(const statement_handle_t&) = delete; - statement_handle_t& operator=(statement_handle_t&&) = default; + statement_handle_t& operator=(statement_handle_t&&) = delete; virtual ~statement_handle_t(); bool operator!() const; @@ -89,9 +89,9 @@ namespace sqlpp // ctor prepared_statement_handle_t(detail::connection_handle& _connection, std::string stmt, const size_t& paramCount); prepared_statement_handle_t(const prepared_statement_handle_t&) = delete; - prepared_statement_handle_t(prepared_statement_handle_t&&) = default; + prepared_statement_handle_t(prepared_statement_handle_t&&) = delete; prepared_statement_handle_t& operator=(const prepared_statement_handle_t&) = delete; - prepared_statement_handle_t& operator=(prepared_statement_handle_t&&) = default; + prepared_statement_handle_t& operator=(prepared_statement_handle_t&&) = delete; virtual ~prepared_statement_handle_t(); @@ -153,10 +153,10 @@ namespace sqlpp inline void prepared_statement_handle_t::execute() { - int size = static_cast(paramValues.size()); + const size_t size = paramValues.size(); std::vector values; - for (int i = 0; i < size; i++) + for (size_t i = 0u; i < size; i++) values.push_back(nullValues[i] ? nullptr : const_cast(paramValues[i].c_str())); // Execute prepared statement with the parameters. @@ -164,7 +164,7 @@ namespace sqlpp valid = false; count = 0; totalCount = 0; - result = PQexecPrepared(connection.postgres, _name.data(), size, values.data(), nullptr, nullptr, 0); + result = PQexecPrepared(connection.native(), _name.data(), static_cast(size), values.data(), nullptr, nullptr, 0); /// @todo validate result? is it really valid valid = true; } @@ -188,7 +188,7 @@ namespace sqlpp inline void prepared_statement_handle_t::prepare(std::string stmt) { // Create the prepared statement - result = PQprepare(connection.postgres, _name.c_str(), stmt.c_str(), 0, nullptr); + result = PQprepare(connection.native(), _name.c_str(), stmt.c_str(), 0, nullptr); valid = true; } } diff --git a/include/sqlpp11/postgresql/result.h b/include/sqlpp11/postgresql/result.h index 78e8fbe4..3672b9c1 100644 --- a/include/sqlpp11/postgresql/result.h +++ b/include/sqlpp11/postgresql/result.h @@ -76,10 +76,10 @@ namespace sqlpp T t(0); auto txt = std::string(getPqValue(m_result, record, field)); if(txt != "") - { t = std::stold(txt); } + return t; } @@ -87,6 +87,7 @@ namespace sqlpp { return m_query; } + std::string& query() { return m_query; diff --git a/include/sqlpp11/postgresql/result_field.h b/include/sqlpp11/postgresql/result_field.h index 462aa18b..8a59f114 100644 --- a/include/sqlpp11/postgresql/result_field.h +++ b/include/sqlpp11/postgresql/result_field.h @@ -40,7 +40,7 @@ namespace sqlpp { namespace postgresql { - struct connection; + class connection; } namespace detail