diff options
author | randomdan <randomdan@localhost> | 2011-02-17 20:42:53 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-02-17 20:42:53 +0000 |
commit | c5f116af8c044883edf44311173a0c6c017b7d59 (patch) | |
tree | 1ec836764f704d6b39ad52bdddda7fcd35d642f5 /libpqpp/modifycommand.cpp | |
parent | Fixes to compile with all gcc warnings as errors (diff) | |
download | libdbpp-postgresql-c5f116af8c044883edf44311173a0c6c017b7d59.tar.bz2 libdbpp-postgresql-c5f116af8c044883edf44311173a0c6c017b7d59.tar.xz libdbpp-postgresql-c5f116af8c044883edf44311173a0c6c017b7d59.zip |
Add check function for when a connection is finished with, but you don't want to close it
Use prepared statements only for modifications, use fetch instead for selects (doesn't load an entire record set)
Support varchar oid
Diffstat (limited to 'libpqpp/modifycommand.cpp')
-rw-r--r-- | libpqpp/modifycommand.cpp | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/libpqpp/modifycommand.cpp b/libpqpp/modifycommand.cpp index ae7abeb..c14ff74 100644 --- a/libpqpp/modifycommand.cpp +++ b/libpqpp/modifycommand.cpp @@ -6,7 +6,8 @@ PQ::ModifyCommand::ModifyCommand(const Connection * conn, const std::string & sql, unsigned int no) : DB::Command(sql), DB::ModifyCommand(sql), - PQ::Command(conn, sql, no) + PQ::Command(conn, sql, no), + prepared(false) { } @@ -17,7 +18,29 @@ PQ::ModifyCommand::~ModifyCommand() unsigned int PQ::ModifyCommand::execute(bool anc) { - prepare(); + if (!prepared) { + std::string psql; + psql.reserve(sql.length() + 20); + char buf[4]; + int p = 1; + bool inquote = false; + for(std::string::const_iterator i = sql.begin(); i != sql.end(); i++) { + if (*i == '?' && !inquote) { + snprintf(buf, 4, "$%d", p++); + psql += buf; + } + else if (*i == '\'') { + inquote = !inquote; + psql += *i; + } + else { + psql += *i; + } + } + c->checkResultFree(PQprepare( + c->conn, stmntName.c_str(), psql.c_str(), values.size(), NULL), PGRES_COMMAND_OK); + prepared = true; + } PGresult * res = PQexecPrepared(c->conn, stmntName.c_str(), values.size(), &values.front(), &lengths.front(), &formats.front(), 0); c->checkResult(res, PGRES_COMMAND_OK); unsigned int rows = atoi(PQcmdTuples(res)); |