summaryrefslogtreecommitdiff
path: root/server/src/admin_export.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2014-02-26 14:57:37 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2014-02-26 14:57:37 +0100
commit070d2c7d55af1cac5f1c7b55186cb7cb3f69dab1 (patch)
tree6541607aea0154fa1a02e37423928d926a364da1 /server/src/admin_export.cc
parentd2715d7c3534f35de0ebdd30d8d770bd79ca7a23 (diff)
Port from libpqxx to libpq.
Diffstat (limited to 'server/src/admin_export.cc')
-rw-r--r--server/src/admin_export.cc77
1 files changed, 53 insertions, 24 deletions
diff --git a/server/src/admin_export.cc b/server/src/admin_export.cc
index 3bdfcf6..6f5ecba 100644
--- a/server/src/admin_export.cc
+++ b/server/src/admin_export.cc
@@ -34,20 +34,14 @@
#ifndef WITHOUT_DB
#include <stdlib.h>
-#include <pqxx/pqxx>
+
+#include "pgwork.h"
#include "fieldnamescanner.h"
#include "configuration.h"
#define SEP "\t"
-#ifdef USE_NEW_PQXX
-#include <pqxx/tuple.hxx>
-typedef pqxx::tuple tuple_t;
-#else
-typedef pqxx::result::tuple tuple_t;
-#endif
-
static std::string escape(std::string &str)
{
std::string out = "\"";
@@ -65,7 +59,7 @@ static std::string escape(std::string &str)
class File {
public:
- File(fieldnames_t &f, pqxx::work &w)
+ File(fieldnames_t &f, Work &w)
: work(w), fieldnames(f)
{
pos["id"] = 0;
@@ -93,16 +87,26 @@ public:
addcell("template", "Template");
{
- pqxx::result result =
+ result_t result =
work.exec("SELECT DISTINCT ON(name) name, caption FROM fieldnames"
" WHERE extract='true';");
- pqxx::result::const_iterator ri = result.begin();
+ result_t::const_iterator ri = result.begin();
+ /*
for(unsigned int r = 0; r < result.size(); r++) {
tuple_t tuple = result.at(r);
std::string name = tuple.at(0).c_str();
std::string caption = tuple.at(1).c_str();
addcell(name, caption);
}
+ */
+ while(ri != result.end()) {
+ tuple_t tuple = *ri;
+ std::string name = tuple.at(0).c_str();
+ std::string caption = tuple.at(1).c_str();
+ addcell(name, caption);
+ ri++;
+ }
+
}
endrow();
@@ -133,7 +137,7 @@ public:
std::string result;
private:
- pqxx::work &work;
+ Work &work;
std::map<std::string, int> pos;
std::vector<std::string> cells;
std::string name;
@@ -162,22 +166,39 @@ static std::string do_export(Environment &env, std::string templ, bool *ok,
if(passwd.size()) cs += " password=" + passwd;
cs += " dbname=" + (dbname.size() ? dbname : "pracro");
- pqxx::connection conn(cs);
- pqxx::work work(conn);
+ // pqxx::connection conn(cs);
+ PGconn *conn = PQconnectdb(cs.c_str());
+
+ if(conn == NULL || PQstatus(conn) == CONNECTION_BAD) {
+ ERR(db, "Postgresql init failed: %s\n", PQerrorMessage(conn));
+ conn = NULL;
+ return "Postgresql init failed";
+ }
+
+ Work work(conn);
std::set<std::string> filter;
{
- pqxx::result result =
+ result_t result =
work.exec("SELECT DISTINCT name FROM fieldnames"
" WHERE extract='true';");
- pqxx::result::const_iterator ri = result.begin();
+ result_t::const_iterator ri = result.begin();
+ /*
for(unsigned int r = 0; r < result.size(); r++) {
tuple_t tuple = result.at(r);
std::string name = tuple.at(0).c_str();
filter.insert(name);
//printf("filter: '%s'\n", name.c_str());
}
+ */
+ while(ri != result.end()) {
+ tuple_t tuple = *ri;
+ std::string name = tuple.at(0).c_str();
+ filter.insert(name);
+ //printf("filter: '%s'\n", name.c_str());
+ ri++;
+ }
}
templates_t t = scanfieldnames(env, filter);
@@ -213,10 +234,11 @@ static std::string do_export(Environment &env, std::string templ, bool *ok,
DEBUG(export, "QUERY: %s\n", q.c_str());
- pqxx::result result = work.exec(q);
- pqxx::result::const_iterator ri = result.begin();
- for(unsigned int r = 0; r < result.size(); r++) {
- tuple_t tuple = result.at(r);
+ result_t result = work.exec(q);
+ result_t::const_iterator ri = result.begin();
+ // for(unsigned int r = 0; r < result.size(); r++) {
+ while(ri != result.end()) {
+ tuple_t tuple = *ri;
std::string patientid = tuple.at(0).c_str();
std::string templ = tuple.at(1).c_str();
std::string version = tuple.at(2).c_str();
@@ -238,15 +260,16 @@ static std::string do_export(Environment &env, std::string templ, bool *ok,
timestamp.c_str(), patientid.c_str(), templ.c_str());
{
- pqxx::result result =
+ result_t result =
work.exec("SELECT f.name, f.value"
" FROM transactions t, fields f, fieldnames n"
" WHERE t.cid='"+uid+"' AND f.transaction=t.uid"
" AND f.name=n.name AND n.extract='true'"
" ORDER BY t.timestamp;");
- pqxx::result::const_iterator ri = result.begin();
- for(unsigned int r = 0; r < result.size(); r++) {
- tuple_t tuple = result.at(r);
+ result_t::const_iterator ri = result.begin();
+ //for(unsigned int r = 0; r < result.size(); r++) {
+ while(ri != result.end()) {
+ tuple_t tuple = *ri;
std::string name = tuple.at(0).c_str();
std::string value = tuple.at(1).c_str();
@@ -256,10 +279,16 @@ static std::string do_export(Environment &env, std::string templ, bool *ok,
}
file.endrow();
+ ri++;
}
}
*ok = true;
+
+ // Make sure we stop using the connection before we close the connection.
+ work.abort();
+ PQfinish(conn);
+
return file.result;
}