summaryrefslogtreecommitdiff
path: root/libpqpp/pq-modifycommand.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libpqpp/pq-modifycommand.cpp')
-rw-r--r--libpqpp/pq-modifycommand.cpp13
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);