diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-02-19 21:25:02 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-02-19 21:25:02 +0000 |
commit | 0bf706059f8a6df882cac3d6e7e976f4432b4271 (patch) | |
tree | 69d6641af24d31227ed616bf9c0c9ee1f27ea167 /libpqpp/pq-command.cpp | |
parent | Build most things by default (diff) | |
download | libdbpp-postgresql-0bf706059f8a6df882cac3d6e7e976f4432b4271.tar.bz2 libdbpp-postgresql-0bf706059f8a6df882cac3d6e7e976f4432b4271.tar.xz libdbpp-postgresql-0bf706059f8a6df882cac3d6e7e976f4432b4271.zip |
Refactor to use std::string as buffer and increase test coverage
Diffstat (limited to 'libpqpp/pq-command.cpp')
-rw-r--r-- | libpqpp/pq-command.cpp | 92 |
1 files changed, 47 insertions, 45 deletions
diff --git a/libpqpp/pq-command.cpp b/libpqpp/pq-command.cpp index 9b336bd..790c7f7 100644 --- a/libpqpp/pq-command.cpp +++ b/libpqpp/pq-command.cpp @@ -14,8 +14,13 @@ PQ::Command::Command(Connection * conn, const std::string & sql, unsigned int no PQ::Command::~Command() { - for (std::vector<char *>::const_iterator i = values.begin(); i != values.end(); ++i) { - free(*i); + for (auto i = values.size(); i-- > 0;) { + if (bufs[i]) { + delete bufs[i]; + } + else { + free(values[i]); + } } } @@ -46,102 +51,99 @@ PQ::Command::paramsAtLeast(unsigned int n) if (values.size() <= n) { values.resize(n + 1, NULL); lengths.resize(n + 1, 0); - formats.resize(n + 1, 0); + bufs.resize(n + 1, NULL); } else { - free(values[n]); + if (bufs[n]) { + delete bufs[n]; + bufs[n] = nullptr; + } + else { + free(values[n]); + } values[n] = NULL; } } +template<typename ... T> void -PQ::Command::bindParamI(unsigned int n, int v) +PQ::Command::paramSet(unsigned int n, const char * fmt, const T & ... v) { paramsAtLeast(n); - lengths[n] = asprintf(&values[n], "%d", v); - formats[n] = 0; + lengths[n] = asprintf(&values[n], fmt, v...); + delete bufs[n]; + bufs[n] = nullptr; } + void -PQ::Command::bindParamI(unsigned int n, long int v) +PQ::Command::paramSet(unsigned int n, const std::string & b) { paramsAtLeast(n); - lengths[n] = asprintf(&values[n], "%ld", v); - formats[n] = 0; + delete bufs[n]; + bufs[n] = new std::string(b); + lengths[n] = b.length(); + values[n] = const_cast<char *>(bufs[n]->data()); +} + +void +PQ::Command::bindParamI(unsigned int n, int v) +{ + paramSet(n, "%d", v); +} +void +PQ::Command::bindParamI(unsigned int n, long int v) +{ + paramSet(n, "%ld", v); } void PQ::Command::bindParamI(unsigned int n, long long int v) { - paramsAtLeast(n); - lengths[n] = asprintf(&values[n], "%lld", v); - formats[n] = 0; + paramSet(n, "%lld", v); } void PQ::Command::bindParamI(unsigned int n, unsigned int v) { - paramsAtLeast(n); - lengths[n] = asprintf(&values[n], "%u", v); - formats[n] = 0; + paramSet(n, "%u", v); } void PQ::Command::bindParamI(unsigned int n, long unsigned int v) { - paramsAtLeast(n); - lengths[n] = asprintf(&values[n], "%lu", v); - formats[n] = 0; + paramSet(n, "%lu", v); } void PQ::Command::bindParamI(unsigned int n, long long unsigned int v) { - paramsAtLeast(n); - lengths[n] = asprintf(&values[n], "%llu", v); - formats[n] = 0; + paramSet(n, "%llu", v); } void PQ::Command::bindParamB(unsigned int n, bool v) { - paramsAtLeast(n); - lengths[n] = asprintf(&values[n], "%s", v ? "true" : "false"); - formats[n] = 0; + paramSet(n, "%s", v ? "true" : "false"); } void PQ::Command::bindParamF(unsigned int n, double v) { - paramsAtLeast(n); - lengths[n] = asprintf(&values[n], "%g", v); - formats[n] = 0; + paramSet(n, "%g", v); } void PQ::Command::bindParamF(unsigned int n, float v) { - paramsAtLeast(n); - lengths[n] = asprintf(&values[n], "%g", v); - formats[n] = 0; + paramSet(n, "%g", v); } void PQ::Command::bindParamS(unsigned int n, const Glib::ustring & s) { - paramsAtLeast(n); - values[n] = strndup(s.c_str(), s.bytes()); - formats[n] = 0; - lengths[n] = s.bytes(); + paramSet(n, s); } void PQ::Command::bindParamT(unsigned int n, const boost::posix_time::time_duration & v) { - paramsAtLeast(n); - auto buf = boost::posix_time::to_simple_string(v); - values[n] = strdup(buf.c_str()); - formats[n] = 0; - lengths[n] = buf.length(); + paramSet(n, boost::posix_time::to_simple_string(v)); } void PQ::Command::bindParamT(unsigned int n, const boost::posix_time::ptime & v) { - paramsAtLeast(n); - auto buf = boost::posix_time::to_iso_extended_string(v); - values[n] = strdup(buf.c_str()); - formats[n] = 0; - lengths[n] = buf.length(); + paramSet(n, boost::posix_time::to_iso_extended_string(v)); } void PQ::Command::bindNull(unsigned int n) |