From 9bb5689d48a2658f4d0b24ee093d09f733eec96c Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 30 Dec 2015 10:20:52 +0000 Subject: [PATCH] Remove query_void() methods. query() is just fine. --- src/delete/postgre_delete.cpp | 2 +- src/pq/connection.cpp | 16 ++++------------ src/pq/connection.hpp | 9 ++++----- src/scan/dbbackend.cpp | 8 ++++---- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/delete/postgre_delete.cpp b/src/delete/postgre_delete.cpp index b24f411..b37befc 100644 --- a/src/delete/postgre_delete.cpp +++ b/src/delete/postgre_delete.cpp @@ -70,6 +70,6 @@ namespace din { boost::copy(dele_ids | boost::adaptors::map_keys, infix_ostream_iterator(oss, " OR \"id\"=")); oss << ";\nCOMMIT;"; - conn.query_void(oss.str()); + conn.query(oss.str()); } } //namespace din diff --git a/src/pq/connection.cpp b/src/pq/connection.cpp index 7b7cf05..4425ce6 100644 --- a/src/pq/connection.cpp +++ b/src/pq/connection.cpp @@ -154,7 +154,7 @@ namespace pq { oss << "Unable to connect to database " << m_address << ':' << m_port << " as user \"" << m_username << '"'; throw DatabaseException(oss.str(), std::move(err), __FILE__, __LINE__); } - query_void("SET NAMES 'utf8'"); + this->query("SET NAMES 'utf8'"); PQinitTypes(m_localData->connection); //Init libpqtypes } @@ -184,16 +184,6 @@ namespace pq { return ResultSet(std::move(info)); } - void Connection::query_void (const std::string& parQuery) { - ResultInfo info(PQexec(m_localData->connection, parQuery.c_str())); - if (not info.result) - throw DatabaseException("Error running query", "Error allocating result object", __FILE__, __LINE__); - const int ress = PQresultStatus(info.result.get()); - if (ress != PGRES_TUPLES_OK && ress != PGRES_COMMAND_OK) { - throw DatabaseException("Error running query", error_message(), __FILE__, __LINE__); - } - } - std::string Connection::escaped_literal (const std::string& parString) { return this->escaped_literal(boost::string_ref(parString)); } @@ -205,7 +195,7 @@ namespace pq { return std::string(clean_str.get()); } - void Connection::query_void_params (const std::string& parQuery, PGParams& parParams) { + ResultSet Connection::query_params (const std::string& parQuery, PGParams& parParams) { int result_format = 1; assert(parParams.get()); ResultInfo info( @@ -225,6 +215,8 @@ namespace pq { if (ress != PGRES_TUPLES_OK && ress != PGRES_COMMAND_OK) { throw DatabaseException("Error running query", error_message(), __FILE__, __LINE__); } + + return ResultSet(std::move(info)); } auto Connection::make_params (const std::string* parTypes, ...) -> PGParams { diff --git a/src/pq/connection.hpp b/src/pq/connection.hpp index d6b5ea5..ecdd9a3 100644 --- a/src/pq/connection.hpp +++ b/src/pq/connection.hpp @@ -39,20 +39,19 @@ namespace pq { bool is_connected ( void ) const noexcept; void connect ( void ); void disconnect ( void ); - void query_void ( const std::string& parQuery ); ResultSet query ( const std::string& parQuery ); std::string escaped_literal ( const std::string& parString ); std::string escaped_literal ( boost::string_ref parString ); template - void query_void ( const std::string& parQuery, Args&&... parArgs ); + ResultSet query ( const std::string& parQuery, Args&&... parArgs ); private: struct LocalData; using PGParams = std::unique_ptr<::PGparam, void(*)(::PGparam*)>; - void query_void_params ( const std::string& parQuery, PGParams& parParams ); + ResultSet query_params ( const std::string& parQuery, PGParams& parParams ); PGParams make_params ( const std::string* parTypes, ... ); const std::string m_username; @@ -108,7 +107,7 @@ namespace pq { } //namespace implem template - void Connection::query_void (const std::string& parQuery, Args&&... parArgs) { + ResultSet Connection::query (const std::string& parQuery, Args&&... parArgs) { using std::remove_cv; using std::remove_reference; @@ -126,7 +125,7 @@ namespace pq { }; PGParams pgparams = make_pgparams(); - this->query_void_params(parQuery, pgparams); + return this->query_params(parQuery, pgparams); } } //namespace pq diff --git a/src/scan/dbbackend.cpp b/src/scan/dbbackend.cpp index 316b2af..56113e0 100644 --- a/src/scan/dbbackend.cpp +++ b/src/scan/dbbackend.cpp @@ -89,8 +89,8 @@ namespace din { pq::Connection conn(std::string(parDB.username), std::string(parDB.password), std::string(parDB.dbname), std::string(parDB.address), parDB.port); conn.connect(); - conn.query_void("BEGIN;"); - conn.query_void("INSERT INTO \"sets\" (\"desc\",\"type\") VALUES ($1, $2)", + conn.query("BEGIN;"); + conn.query("INSERT INTO \"sets\" (\"desc\",\"type\") VALUES ($1, $2)", parSetData.name, std::string(1, parSetData.type) ); @@ -102,7 +102,7 @@ namespace din { "($1, $2, $3, currval('\"sets_id_seq\"'), $4, $5, $6, $7, $8, $9, $10);"; const auto& itm = parData[z]; - conn.query_void(query, + conn.query(query, itm.path, tiger_to_string(itm.hash), itm.level, @@ -115,6 +115,6 @@ namespace din { itm.unreadable ); } - conn.query_void("COMMIT;"); + conn.query("COMMIT;"); } } //namespace din