summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-12-31 04:59:00 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2016-12-31 04:59:00 +0000
commit29269c1cf7d49dd4596cde49682e350875cb51f1 (patch)
tree21ea7c73a31caeafee3e5cd0799bfc2570ba3ceb
parentMore complex approach to setting tables to unlogged to handle foreign key dep... (diff)
downloadlibdbpp-postgresql-29269c1cf7d49dd4596cde49682e350875cb51f1.tar.bz2
libdbpp-postgresql-29269c1cf7d49dd4596cde49682e350875cb51f1.tar.xz
libdbpp-postgresql-29269c1cf7d49dd4596cde49682e350875cb51f1.zip
Do lots more work with compile time formatter instead runtime formatters
-rw-r--r--libpqpp/pq-command.cpp5
-rw-r--r--libpqpp/pq-connection.cpp6
-rw-r--r--libpqpp/pq-cursorselectcommand.cpp18
-rw-r--r--libpqpp/pq-mock.cpp13
4 files changed, 22 insertions, 20 deletions
diff --git a/libpqpp/pq-command.cpp b/libpqpp/pq-command.cpp
index 14efa77..84eaa43 100644
--- a/libpqpp/pq-command.cpp
+++ b/libpqpp/pq-command.cpp
@@ -2,12 +2,13 @@
#include "pq-connection.h"
#include <stdlib.h>
#include <string.h>
-#include <buffer.h>
+#include <compileTimeFormatter.h>
#include <boost/date_time/posix_time/posix_time.hpp>
+AdHocFormatter(PQCommondStatement, "pStatement_%?_%?");
PQ::Command::Command(Connection * conn, const std::string & sql, unsigned int no) :
DB::Command(sql),
- stmntName(stringbf("pStatement_%u_%p", no, this)),
+ stmntName(PQCommondStatement::get(no, this)),
c(conn)
{
}
diff --git a/libpqpp/pq-connection.cpp b/libpqpp/pq-connection.cpp
index 078272e..b7360e0 100644
--- a/libpqpp/pq-connection.cpp
+++ b/libpqpp/pq-connection.cpp
@@ -6,6 +6,7 @@
#include <unistd.h>
#include <poll.h>
#include <boost/assert.hpp>
+#include <compileTimeFormatter.h>
NAMEDFACTORY("postgresql", PQ::Connection, DB::ConnectionFactory);
@@ -137,12 +138,11 @@ PQ::Connection::checkResultFree(PGresult * res, int expected, int alt) const
PQclear(res);
}
+AdHocFormatter(PQConnectionCopyFrom, "COPY %? FROM STDIN %?");
void
PQ::Connection::beginBulkUpload(const char * table, const char * extra)
{
- char buf[BUFSIZ];
- snprintf(buf, BUFSIZ, "COPY %s FROM STDIN %s", table, extra);
- checkResultFree(PQexec(conn, buf), PGRES_COPY_IN);
+ checkResultFree(PQexec(conn, PQConnectionCopyFrom::get(table, extra).c_str()), PGRES_COPY_IN);
}
void
diff --git a/libpqpp/pq-cursorselectcommand.cpp b/libpqpp/pq-cursorselectcommand.cpp
index ab21f66..29e29f5 100644
--- a/libpqpp/pq-cursorselectcommand.cpp
+++ b/libpqpp/pq-cursorselectcommand.cpp
@@ -2,6 +2,7 @@
#include "pq-connection.h"
#include "pq-column.h"
#include "pq-error.h"
+#include <compileTimeFormatter.h>
PQ::CursorSelectCommand::CursorSelectCommand(Connection * conn, const std::string & sql, unsigned int no) :
DB::Command(sql),
@@ -29,32 +30,27 @@ PQ::CursorSelectCommand::~CursorSelectCommand()
}
}
+AdHocFormatter(PQCursorSelectDeclare, "DECLARE %? CURSOR FOR %?");
std::string
PQ::CursorSelectCommand::mkdeclare() const
{
std::string psql;
- psql.reserve(sql.length() + 40);
- psql += "DECLARE ";
- psql += stmntName;
- psql += " CURSOR FOR ";
prepareSql(psql, sql);
- return psql;
+ return PQCursorSelectDeclare::get(stmntName, psql);
}
+AdHocFormatter(PQCursorSelectFetch, "FETCH %? IN %?");
std::string
PQ::CursorSelectCommand::mkfetch() const
{
- char buf[BUFSIZ];
- snprintf(buf, sizeof(buf), "FETCH %d IN %s", fTuples, stmntName.c_str());
- return buf;
+ return PQCursorSelectFetch::get(fTuples, stmntName);
}
+AdHocFormatter(PQCursorSelectClose, "CLOSE %?");
std::string
PQ::CursorSelectCommand::mkclose() const
{
- char buf[BUFSIZ];
- snprintf(buf, sizeof(buf), "CLOSE %s", stmntName.c_str());
- return buf;
+ return PQCursorSelectClose::get(stmntName);
}
void
diff --git a/libpqpp/pq-mock.cpp b/libpqpp/pq-mock.cpp
index c90f84c..44c8cc9 100644
--- a/libpqpp/pq-mock.cpp
+++ b/libpqpp/pq-mock.cpp
@@ -1,6 +1,7 @@
#include "pq-mock.h"
#include "pq-connection.h"
-#include <buffer.h>
+#include <compileTimeFormatter.h>
+#include <modifycommand.h>
#include <selectcommand.h>
#include <selectcommandUtil.impl.h>
@@ -14,12 +15,14 @@ Mock::Mock(const std::string & masterdb, const std::string & name, const std::ve
SetTablesToUnlogged();
}
+AdHocFormatter(MockConnStr, "user=postgres dbname=%?");
DB::Connection *
Mock::openConnection() const
{
- return new Connection(stringbf("user=postgres dbname=%s", testDbName));
+ return new Connection(MockConnStr::get(testDbName));
}
+AdHocFormatter(MockSetUnlogged, "ALTER TABLE %?.%? SET UNLOGGED");
void
Mock::SetTablesToUnlogged() const
{
@@ -45,7 +48,7 @@ ORDER BY 1, 2)SQL");
do {
n = 0;
for (const auto & t : s->as<std::string, std::string>()) {
- c->execute("ALTER TABLE " + t.value<0>() + "." + t.value<1>() + " SET UNLOGGED");
+ c->execute(MockSetUnlogged::get(t.value<0>(), t.value<1>()));
n += 1;
}
} while(n);
@@ -59,7 +62,9 @@ Mock::~Mock()
void
Mock::DropDatabase() const
{
- master->execute("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '" + testDbName + "'");
+ auto t = master->modify("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = ?");
+ t->bindParamS(0, testDbName);
+ t->execute();
MockServerDatabase::DropDatabase();
}