From: Chris Hofstaedtler Date: Wed, 3 Jun 2020 11:40:17 +0000 (+0200) Subject: spgsql: tidy up X-Git-Tag: dnsdist-1.5.0-rc3~12^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F9190%2Fhead;p=thirdparty%2Fpdns.git spgsql: tidy up --- diff --git a/modules/gpgsqlbackend/spgsql.cc b/modules/gpgsqlbackend/spgsql.cc index 870469c5ad..aeab4470b6 100644 --- a/modules/gpgsqlbackend/spgsql.cc +++ b/modules/gpgsqlbackend/spgsql.cc @@ -39,18 +39,8 @@ public: d_query = query; d_dolog = dolog; d_parent = db; - d_prepared = false; d_nparams = nparams; - d_res = NULL; - d_res_set = NULL; - paramValues = NULL; - paramLengths = NULL; d_nstatement = nstatement; - d_paridx = 0; - d_residx = 0; - d_resnum = 0; - d_fnum = 0; - d_cur_set = 0; } SSqlStatement* bind(const string& name, bool value) { return bind(name, string(value ? "t" : "f")); } @@ -82,9 +72,9 @@ public: d_dtime.set(); } if (!d_stmt.empty()) { - d_res_set = PQexecPrepared(d_db(), d_stmt.c_str(), d_nparams, paramValues, paramLengths, NULL, 0); + d_res_set = PQexecPrepared(d_db(), d_stmt.c_str(), d_nparams, paramValues, paramLengths, nullptr, 0); } else { - d_res_set = PQexecParams(d_db(), d_query.c_str(), d_nparams, NULL, paramValues, paramLengths, NULL, 0); + d_res_set = PQexecParams(d_db(), d_query.c_str(), d_nparams, nullptr, paramValues, paramLengths, nullptr, 0); } ExecStatusType status = PQresultStatus(d_res_set); if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK && status != PGRES_NONFATAL_ERROR) { @@ -103,10 +93,10 @@ public: } void nextResult() { - if (d_res_set == NULL) return; // no refcursor + if (d_res_set == nullptr) return; // no refcursor if (d_cur_set >= PQntuples(d_res_set)) { PQclear(d_res_set); - d_res_set = NULL; + d_res_set = nullptr; return; } // this code handles refcursors if they are returned @@ -116,7 +106,7 @@ public: #if PG_VERSION_NUM > 90000 // PQescapeIdentifier was added to libpq in postgresql 9.0 char *val = PQgetvalue(d_res_set, d_cur_set++, 0); - char *portal = PQescapeIdentifier(d_db(), val, strlen(val)); + char *portal = PQescapeIdentifier(d_db(), val, strlen(val)); string cmd = string("FETCH ALL FROM \"") + string(portal) + string("\""); PQfreemem(portal); #else @@ -124,17 +114,16 @@ public: string cmd = string("FETCH ALL FROM \"") + portal + string("\""); #endif // execute FETCH - if (d_dolog) + if (d_dolog) { g_log<= d_resnum) { PQclear(d_res); - d_res = NULL; + d_res = nullptr; nextResult(); } return this; @@ -173,7 +162,7 @@ public: SSqlStatement* getResult(result_t& result) { result.clear(); - if (d_res == NULL) return this; + if (d_res == nullptr) return this; result.reserve(d_resnum); row_t row; while(hasNextRow()) { nextRow(row); result.push_back(std::move(row)); } @@ -182,20 +171,26 @@ public: SSqlStatement* reset() { int i; - if (d_res) + if (d_res) { PQclear(d_res); - if (d_res_set) + } + if (d_res_set) { PQclear(d_res_set); - d_res_set = NULL; - d_res = NULL; + } + d_res_set = nullptr; + d_res = nullptr; d_paridx = d_residx = d_resnum = 0; - if (paramValues) - for(i=0;iusePrepared()) { // prepare a statement; name must be unique per session (using d_nstatement to ensure this). this->d_stmt = string("stmt") + std::to_string(d_nstatement); - PGresult* res = PQprepare(d_db(), d_stmt.c_str(), d_query.c_str(), d_nparams, NULL); + PGresult* res = PQprepare(d_db(), d_stmt.c_str(), d_query.c_str(), d_nparams, nullptr); ExecStatusType status = PQresultStatus(res); string errmsg(PQresultErrorMessage(res)); PQclear(res); @@ -234,16 +229,16 @@ private: throw SSqlException("Fatal error during prePQpreparepare: " + d_query + string(": ") + errmsg); } } - paramValues=NULL; - d_cur_set=d_paridx=d_residx=d_resnum=d_fnum=0; - paramLengths=NULL; - d_res=NULL; - d_res_set=NULL; + paramValues = nullptr; + paramLengths = nullptr; + d_cur_set = d_paridx = d_residx = d_resnum = 0; + d_res = nullptr; + d_res_set = nullptr; d_prepared = true; } void allocate() { - if (paramValues != NULL) return; + if (paramValues != nullptr) return; paramValues = new char*[d_nparams]; paramLengths = new int[d_nparams]; memset(paramValues, 0, sizeof(char*)*d_nparams); @@ -253,19 +248,18 @@ private: string d_query; string d_stmt; SPgSQL *d_parent; - PGresult *d_res_set; - PGresult *d_res; + PGresult *d_res_set{nullptr}; + PGresult *d_res{nullptr}; bool d_dolog; DTime d_dtime; // only used if d_dolog is set - bool d_prepared; + bool d_prepared{false}; int d_nparams; - int d_paridx; - char **paramValues; - int *paramLengths; - int d_residx; - int d_resnum; - int d_fnum; - int d_cur_set; + int d_paridx{0}; + char **paramValues{nullptr}; + int *paramLengths{nullptr}; + int d_residx{0}; + int d_resnum{0}; + int d_cur_set{0}; unsigned int d_nstatement; };