diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2017-01-06 02:11:57 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2017-01-08 19:33:31 +0000 |
commit | 9758d7cdc98f0d948581fb5630c33e98e4b52855 (patch) | |
tree | 08f603e35235f8c8fb8d0da022830138f952fddf /libpqpp/pq-connection.cpp | |
parent | Fix bug where prepared statement cache wasn't cleared on connection reset lea... (diff) | |
download | libdbpp-postgresql-9758d7cdc98f0d948581fb5630c33e98e4b52855.tar.bz2 libdbpp-postgresql-9758d7cdc98f0d948581fb5630c33e98e4b52855.tar.xz libdbpp-postgresql-9758d7cdc98f0d948581fb5630c33e98e4b52855.zip |
Use SQL hash for prepared statement names, increases reuse and avoids random reuse
Diffstat (limited to 'libpqpp/pq-connection.cpp')
-rw-r--r-- | libpqpp/pq-connection.cpp | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/libpqpp/pq-connection.cpp b/libpqpp/pq-connection.cpp index 547923f..643e021 100644 --- a/libpqpp/pq-connection.cpp +++ b/libpqpp/pq-connection.cpp @@ -28,8 +28,7 @@ PQ::ConnectionError::ConnectionError(const PGconn * conn) : } PQ::Connection::Connection(const std::string & info) : - conn(PQconnectdb(info.c_str())), - pstmntNo(0) + conn(PQconnectdb(info.c_str())) { if (PQstatus(conn) != CONNECTION_OK) { ConnectionError ce(conn); @@ -102,15 +101,15 @@ PQ::Connection::newSelectCommand(const std::string & sql, const DB::CommandOptio { auto pqco = dynamic_cast<const CommandOptions *>(opts); if (pqco && !pqco->useCursor) { - return new BulkSelectCommand(this, sql, pstmntNo++, opts); + return new BulkSelectCommand(this, sql, opts); } - return new CursorSelectCommand(this, sql, pstmntNo++, pqco); + return new CursorSelectCommand(this, sql, pqco, opts); } DB::ModifyCommand * PQ::Connection::newModifyCommand(const std::string & sql, const DB::CommandOptions * opts) { - return new ModifyCommand(this, sql, pstmntNo++, opts); + return new ModifyCommand(this, sql, opts); } bool @@ -176,10 +175,12 @@ PQ::Connection::bulkUploadData(const char * data, size_t len) const } } +static const std::string selectLastVal("SELECT lastval()"); +static const DB::CommandOptions selectLastValOpts(std::hash<std::string>()(selectLastVal)); int64_t PQ::Connection::insertId() { - BulkSelectCommand getId(this, "SELECT lastval()", pstmntNo++, NULL); + BulkSelectCommand getId(this, selectLastVal, &selectLastValOpts); int64_t id = -1; while (getId.fetch()) { getId[0] >> id; |