From 070d2c7d55af1cac5f1c7b55186cb7cb3f69dab1 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 26 Feb 2014 14:57:37 +0100 Subject: Port from libpqxx to libpq. --- server/src/admin_export.cc | 77 +++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 24 deletions(-) (limited to 'server/src/admin_export.cc') 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 -#include + +#include "pgwork.h" #include "fieldnamescanner.h" #include "configuration.h" #define SEP "\t" -#ifdef USE_NEW_PQXX -#include -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 pos; std::vector 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 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; } -- cgit v1.2.3