diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-02-28 17:22:24 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-02-28 17:22:24 +0000 |
commit | 9228b13ad8b8a42277af00bbcd645aa65e0c9f56 (patch) | |
tree | 1800183cd6053d9c2703934c2a8a978ed33aa260 /libpqpp/pq-modifycommand.cpp | |
parent | Prepare statement upfront during construction (diff) | |
download | libdbpp-postgresql-9228b13ad8b8a42277af00bbcd645aa65e0c9f56.tar.bz2 libdbpp-postgresql-9228b13ad8b8a42277af00bbcd645aa65e0c9f56.tar.xz libdbpp-postgresql-9228b13ad8b8a42277af00bbcd645aa65e0c9f56.zip |
Actually, don't prepare statement upfront during construction as it might not be valid (e.g. uncreated temp tables)libdbpp-postgresql-1.0.3
Diffstat (limited to 'libpqpp/pq-modifycommand.cpp')
-rw-r--r-- | libpqpp/pq-modifycommand.cpp | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/libpqpp/pq-modifycommand.cpp b/libpqpp/pq-modifycommand.cpp index e140f0a..caad5c6 100644 --- a/libpqpp/pq-modifycommand.cpp +++ b/libpqpp/pq-modifycommand.cpp @@ -7,7 +7,7 @@ PQ::ModifyCommand::ModifyCommand(Connection * conn, const std::string & sql, uns DB::Command(sql), DB::ModifyCommand(sql), PQ::Command(conn, sql, no), - pstmt(prepare()->second) + pstmt(nullptr) { } @@ -15,26 +15,29 @@ PQ::ModifyCommand::~ModifyCommand() { } -PQ::Connection::PreparedStatements::const_iterator +const char * PQ::ModifyCommand::prepare() const { + if (pstmt) { + return pstmt; + } auto hash(std::hash<std::string>()(sql)); auto i = c->preparedStatements.find(hash); if (i != c->preparedStatements.end()) { - return i; + return (pstmt = i->second.c_str()); } std::string psql; psql.reserve(sql.length() + 20); prepareSql(psql, sql); c->checkResultFree(PQprepare( c->conn, stmntName.c_str(), psql.c_str(), values.size(), NULL), PGRES_COMMAND_OK); - return c->preparedStatements.insert({hash, stmntName}).first; + return (pstmt = c->preparedStatements.insert({hash, stmntName}).first->second.c_str()); } unsigned int PQ::ModifyCommand::execute(bool anc) { - PGresult * res = PQexecPrepared(c->conn, pstmt.c_str(), values.size(), &values.front(), &lengths.front(), NULL, 0); + PGresult * res = PQexecPrepared(c->conn, prepare(), values.size(), &values.front(), &lengths.front(), NULL, 0); c->checkResult(res, PGRES_COMMAND_OK, PGRES_TUPLES_OK); unsigned int rows = atoi(PQcmdTuples(res)); PQclear(res); |