diff options
author | randomdan <randomdan@localhost> | 2011-02-09 01:33:33 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-02-09 01:33:33 +0000 |
commit | cfa36aa5466d4c342c7de0f6cea7b8386984fd36 (patch) | |
tree | 17a13c13b3588101a146705839d5da6f280bddb9 /libpqpp/command.cpp | |
parent | Centralise the ICE splicer (diff) | |
download | libdbpp-postgresql-cfa36aa5466d4c342c7de0f6cea7b8386984fd36.tar.bz2 libdbpp-postgresql-cfa36aa5466d4c342c7de0f6cea7b8386984fd36.tar.xz libdbpp-postgresql-cfa36aa5466d4c342c7de0f6cea7b8386984fd36.zip |
Fix the build system to do dependencies properly
Break down libodbcpp into a set of base classes; libdbpp
Add a native PostgreSQL implementation of libdbpp; libpqpp
Extend project2 rdbms stuff to work with generic connectors
Update datasources to specify connector type
Build libmisc as .so
Diffstat (limited to 'libpqpp/command.cpp')
-rw-r--r-- | libpqpp/command.cpp | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/libpqpp/command.cpp b/libpqpp/command.cpp new file mode 100644 index 0000000..28d4f24 --- /dev/null +++ b/libpqpp/command.cpp @@ -0,0 +1,149 @@ +#include "command.h" +#include "connection.h" +#include <stdlib.h> +#include <string.h> + +static std::string addrStr(void * p, unsigned int no) { + std::string r; + r.resize(30); + r.resize(snprintf(const_cast<char *>(r.c_str()), 30, "pStatement-%u-%p", no, p)); + return r; +} + +PQ::Command::Command(const Connection * conn, const std::string & sql, unsigned int no) : + DB::Command(sql), + stmntName(addrStr(this, no)), + prepared(false), + c(conn) +{ +} + +PQ::Command::~Command() +{ + for (std::vector<char *>::const_iterator i = values.begin(); i != values.end(); i++) { + free(*i); + } +} + +void +PQ::Command::prepare() const +{ + if (!prepared) { + std::string psql; + psql.reserve(sql.length() + 20); + char buf[4]; + int p = 1; + for(std::string::const_iterator i = sql.begin(); i != sql.end(); i++) { + if (*i == '?') { + snprintf(buf, 4, "$%d", p++); + psql += buf; + } + else { + psql += *i; + } + } + c->checkResultFree(PQprepare( + c->conn, stmntName.c_str(), psql.c_str(), values.size(), NULL), PGRES_COMMAND_OK, __PRETTY_FUNCTION__); + prepared = true; + } +} + +void +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); + } + else { + free(values[n]); + } +} + +void +PQ::Command::bindParamI(unsigned int n, int v) +{ + paramsAtLeast(n); + lengths[n] = asprintf(&values[n], "%d", v); + formats[n] = 0; +} +void +PQ::Command::bindParamI(unsigned int n, long int v) +{ + paramsAtLeast(n); + lengths[n] = asprintf(&values[n], "%ld", v); + formats[n] = 0; +} +void +PQ::Command::bindParamI(unsigned int n, long long int v) +{ + paramsAtLeast(n); + lengths[n] = asprintf(&values[n], "%lld", v); + formats[n] = 0; +} +void +PQ::Command::bindParamI(unsigned int n, unsigned int v) +{ + paramsAtLeast(n); + lengths[n] = asprintf(&values[n], "%u", v); + formats[n] = 0; +} +void +PQ::Command::bindParamI(unsigned int n, long unsigned int v) +{ + paramsAtLeast(n); + lengths[n] = asprintf(&values[n], "%lu", v); + formats[n] = 0; +} +void +PQ::Command::bindParamI(unsigned int n, long long unsigned int v) +{ + paramsAtLeast(n); + lengths[n] = asprintf(&values[n], "%llu", v); + formats[n] = 0; +} +void +PQ::Command::bindParamF(unsigned int n, double v) +{ + paramsAtLeast(n); + lengths[n] = asprintf(&values[n], "%f", v); + formats[n] = 0; +} +void +PQ::Command::bindParamF(unsigned int n, float v) +{ + paramsAtLeast(n); + lengths[n] = asprintf(&values[n], "%f", v); + formats[n] = 0; +} +void +PQ::Command::bindParamS(unsigned int n, const Glib::ustring & s) +{ + paramsAtLeast(n); + values[n] = strndup(s.c_str(), s.length()); + formats[n] = 0; + lengths[n] = s.length(); +} +void +PQ::Command::bindParamT(unsigned int n, const tm * v) +{ + paramsAtLeast(n); + values[n] = static_cast<char *>(malloc(19)); + formats[n] = 0; + strftime(values[n], 19, "%F %T", v); + lengths[n] = 19; +} +void +PQ::Command::bindParamT(unsigned int n, time_t v) +{ + struct tm t; + gmtime_r(&v, &t); + bindParamT(n, &t); +} +void +PQ::Command::bindNull(unsigned int n) +{ + paramsAtLeast(n); +} + |