summaryrefslogtreecommitdiff
path: root/server/src/pracrodaopgsql.cc
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/pracrodaopgsql.cc')
-rw-r--r--server/src/pracrodaopgsql.cc104
1 files changed, 55 insertions, 49 deletions
diff --git a/server/src/pracrodaopgsql.cc b/server/src/pracrodaopgsql.cc
index 3c7040a..2192031 100644
--- a/server/src/pracrodaopgsql.cc
+++ b/server/src/pracrodaopgsql.cc
@@ -43,9 +43,13 @@
#ifndef WITHOUT_DB
#include <stdlib.h>
+#include <list>
+#include <sstream>
#include "debug.h"
+#include "pgwork.h"
+
PracroDAOPgsql::PracroDAOPgsql(std::string _host, std::string _port,
std::string _user, std::string _passwd,
std::string _dbname)
@@ -58,11 +62,13 @@ PracroDAOPgsql::PracroDAOPgsql(std::string _host, std::string _port,
if(user.size()) cs += " user=" + user;
if(passwd.size()) cs += " password=" + passwd;
cs += " dbname=" + (dbname.size() ? dbname : "pracro");
- try {
- conn = new pqxx::connection(cs);
- } catch(std::exception &e) {
- ERR_LOG(db, "Postgresql init failed: %s\n", e.what());
+
+ conn = PQconnectdb(cs.c_str());
+
+ if(conn == NULL || PQstatus(conn) == CONNECTION_BAD) {
+ ERR_LOG(db, "Postgresql init failed: %s\n", PQerrorMessage(conn));
conn = NULL;
+ return;
}
DEBUG(db, "Pgsql connection %p (%s)\n", conn, cs.c_str());
@@ -71,7 +77,7 @@ PracroDAOPgsql::PracroDAOPgsql(std::string _host, std::string _port,
PracroDAOPgsql::~PracroDAOPgsql()
{
if(conn) {
- delete conn;
+ PQfinish(conn);
}
}
@@ -83,9 +89,9 @@ std::string PracroDAOPgsql::newSessionId()
}
try {
- pqxx::work W(*conn);
- pqxx::result R = W.exec("SELECT nextval('sessionseq');");
- pqxx::result::const_iterator ri = R.begin();
+ Work W(conn);
+ result_t R = W.exec("SELECT nextval('sessionseq');");
+ result_t::const_iterator ri = R.begin();
if(ri != R.end()) {
DEBUG(db, "New session id: %s\n", (*ri)[0].c_str());
return (*ri)[0].c_str();
@@ -118,7 +124,7 @@ void PracroDAOPgsql::commitTransaction(std::string sessionid,
if(commit.fields.size() == 0) return;
- pqxx::work W(*conn);
+ Work W(conn);
std::string version = _macro.version;
std::string macro = _macro.name;
@@ -128,7 +134,7 @@ void PracroDAOPgsql::commitTransaction(std::string sessionid,
try {
ts = "SELECT status FROM commits WHERE uid='"+sessionid+"';";
- pqxx::result R = W.exec(ts);
+ result_t R = W.exec(ts);
if(!R.size()) {
ts = "INSERT INTO commits (patientid, template, version,"
" \"timestamp\", uid, status) VALUES ("
@@ -141,10 +147,10 @@ void PracroDAOPgsql::commitTransaction(std::string sessionid,
");"
;
DEBUG(sql, "Query: %s\n", ts.c_str());
- pqxx::result R = W.exec(ts);
+ result_t R = W.exec(ts);
} else {
- pqxx::result::const_iterator ri = R.begin();
+ result_t::const_iterator ri = R.begin();
if(ri != R.end()) {
std::string status = (*ri)[0].c_str();
if(status == "committed") {
@@ -156,7 +162,7 @@ void PracroDAOPgsql::commitTransaction(std::string sessionid,
ts = "UPDATE commits SET status='active' WHERE uid="+sessionid+";";
DEBUG(sql, "Query: %s\n", ts.c_str());
- /*pqxx::result R = */W.exec(ts);
+ /*result_t R = */W.exec(ts);
}
} catch(std::exception &e) {
ERR_LOG(db, "Query failed: %s: %s\n", e.what(), ts.c_str());
@@ -175,7 +181,7 @@ void PracroDAOPgsql::commitTransaction(std::string sessionid,
");"
;
DEBUG(sql, "Query: %s\n", ts.c_str());
- pqxx::result R = W.exec(ts);
+ result_t R = W.exec(ts);
if(commit.fields.size() > 0) {
// field table lookup
@@ -191,11 +197,11 @@ void PracroDAOPgsql::commitTransaction(std::string sessionid,
DEBUG(sql, "Query: %s\n", ts.c_str());
R = W.exec(ts);
- DEBUG(db, "input fields: %d, output fields: %lu\n",
- commit.fields.size(), R.size());
+ DEBUG(db, "input fields: %d, output fields: %d\n",
+ commit.fields.size(), (int)R.size());
// Store known fields
- pqxx::result::const_iterator ri = R.begin();
+ result_t::const_iterator ri = R.begin();
if(ri != R.end()) {
std::string name = (*ri)[0].c_str();
DEBUG(db, "Storing: %s with value %s\n",
@@ -253,15 +259,15 @@ Values PracroDAOPgsql::getLatestValues(std::string sessionid,
std::stringstream soldest; soldest << oldest;
try {
{
- pqxx::work W(*conn);
+ Work W(conn);
query = "UPDATE commits SET status='active' WHERE status='idle'"
" AND uid="+sessionid+";";
DEBUG(sql, "Query: %s\n", query.c_str());
- /*pqxx::result R = */W.exec(query);
+ /*result_t R = */W.exec(query);
W.commit();
}
- pqxx::work W(*conn);
+ Work W(conn);
#ifdef NEW
@@ -288,16 +294,16 @@ Values PracroDAOPgsql::getLatestValues(std::string sessionid,
" \"timestamp\">="+soldest.str()+" AND"
" (status='committed' OR uid="+sessionid+");";
DEBUG(sql, "Query: %s\n", query.c_str());
- pqxx::result commits = W.exec(query);
- pqxx::result::const_iterator ci = commits.begin();
+ result_t commits = W.exec(query);
+ result_t::const_iterator ci = commits.begin();
while(ci != commits.end()) {
std::string cid = (*ci)[0].c_str();
query = "SELECT uid, \"timestamp\" FROM transactions WHERE cid="+cid+
macros+";";
DEBUG(sql, "Query: %s\n", query.c_str());
- pqxx::result transactions = W.exec(query);
- pqxx::result::const_iterator ti = transactions.begin();
+ result_t transactions = W.exec(query);
+ result_t::const_iterator ti = transactions.begin();
while(ti != transactions.end()) {
std::string tid = (*ti)[0].c_str();
time_t timestamp = atol((*ti)[1].c_str());
@@ -305,9 +311,9 @@ Values PracroDAOPgsql::getLatestValues(std::string sessionid,
query = "SELECT name, value FROM fields WHERE"
" transaction="+tid+" AND ("+ names +");";
DEBUG(sql, "Query: %s\n", query.c_str());
- pqxx::result fields = W.exec(query);
+ result_t fields = W.exec(query);
DEBUG(sql, "Results: %lu\n", fields.size());
- pqxx::result::const_iterator fi = fields.begin();
+ result_t::const_iterator fi = fields.begin();
while(fi != fields.end()) {
std::string name = (*fi)[0].c_str();
if(values.find(name) == values.end() ||
@@ -374,8 +380,8 @@ Values PracroDAOPgsql::getLatestValues(std::string sessionid,
}
DEBUG(sql, "Query: %s\n", query.c_str());
- pqxx::result R = W.exec(query);
- pqxx::result::const_iterator ri = R.begin();
+ result_t R = W.exec(query);
+ result_t::const_iterator ri = R.begin();
while(ri != R.end()) {
Value v;
v.value = (*ri)[1].c_str();
@@ -407,7 +413,7 @@ unsigned PracroDAOPgsql::nrOfCommits(std::string sessionid,
std::string query;
std::stringstream soldest; soldest << oldest;
try {
- pqxx::work W(*conn);
+ Work W(conn);
query = "SELECT count(*) FROM commits c, transactions f"
" WHERE c.patientid = '" + W.esc(patientid) + "' AND c.uid = f.cid";
//if(!uncom) query += " AND (c.status='committed' OR c.uid="+sessionid+")";
@@ -416,7 +422,7 @@ unsigned PracroDAOPgsql::nrOfCommits(std::string sessionid,
//" AND f.timestamp >= " + soldest.str()
;
DEBUG(sql, "Query: %s\n", query.c_str());
- pqxx::result R = W.exec(query);
+ result_t R = W.exec(query);
if(R.size() != 1) {
ERR_LOG(db, "No result set; expected one row with one column\n");
return 0;
@@ -443,10 +449,10 @@ void PracroDAOPgsql::addFieldname(std::string name, std::string description)
std::string ts;
try {
- pqxx::work W(*conn);
+ Work W(conn);
ts = "SELECT name FROM fieldnames WHERE name='"+W.esc(name)+"';";
- pqxx::result Rc = W.exec(ts);
+ result_t Rc = W.exec(ts);
if(Rc.size()) {
ts = "UPDATE fieldnames SET "
" description='" + W.esc(description) + "', "
@@ -461,7 +467,7 @@ void PracroDAOPgsql::addFieldname(std::string name, std::string description)
;
}
DEBUG(sql, "Query: %s\n", ts.c_str());
- pqxx::result R = W.exec(ts);
+ result_t R = W.exec(ts);
W.commit();
} catch (std::exception &e) {
ERR_LOG(db, "Query failed: %s: %s\n", e.what(), ts.c_str());
@@ -477,11 +483,11 @@ void PracroDAOPgsql::delFieldname(std::string name)
std::string ts;
try {
- pqxx::work W(*conn);
+ Work W(conn);
ts = "DELETE FROM fieldnames WHERE name="
"'" + W.esc(name) + "' ";
DEBUG(sql, "Query: %s\n", ts.c_str());
- pqxx::result R = W.exec(ts);
+ result_t R = W.exec(ts);
W.commit();
} catch (std::exception &e) {
ERR_LOG(db, "Query failed: %s: %s\n", e.what(), ts.c_str());
@@ -499,11 +505,11 @@ std::vector<Fieldname> PracroDAOPgsql::getFieldnames()
std::string query;
try {
- pqxx::work W(*conn);
+ Work W(conn);
query = "SELECT * FROM fieldnames";
DEBUG(sql, "Query: %s\n", query.c_str());
- pqxx::result R = W.exec(query);
- pqxx::result::const_iterator ri = R.begin();
+ result_t R = W.exec(query);
+ result_t::const_iterator ri = R.begin();
while(ri != R.end()) {
Fieldname f;
f.name = (*ri)[0].c_str();
@@ -528,9 +534,9 @@ void PracroDAOPgsql::commit(std::string sessionid)
std::string ts;
try {
- pqxx::work W(*conn);
+ Work W(conn);
ts = "UPDATE commits SET status='committed' WHERE uid="+sessionid+";";
- /*pqxx::result R = */W.exec(ts);
+ /*result_t R = */W.exec(ts);
W.commit();
} catch (std::exception &e) {
@@ -547,9 +553,9 @@ void PracroDAOPgsql::nocommit(std::string sessionid)
std::string ts;
try {
- pqxx::work W(*conn);
+ Work W(conn);
ts = "UPDATE commits SET status='idle' WHERE uid="+sessionid+";";
- /*pqxx::result R = */W.exec(ts);
+ /*result_t R = */W.exec(ts);
W.commit();
} catch (std::exception &e) {
@@ -566,9 +572,9 @@ void PracroDAOPgsql::discard(std::string sessionid)
std::string ts;
try {
- pqxx::work W(*conn);
+ Work W(conn);
ts = "DELETE FROM commits WHERE uid="+sessionid+";";
- /*pqxx::result R = */W.exec(ts);
+ /*result_t R = */W.exec(ts);
W.commit();
} catch (std::exception &e) {
ERR_LOG(db, "Abort (rollback) failed: %s: %s\n", e.what(), ts.c_str());
@@ -584,9 +590,9 @@ bool PracroDAOPgsql::idle(std::string sessionid)
std::string ts = "SELECT status FROM commits WHERE uid='"+sessionid+"';";
try {
- pqxx::work W(*conn);
- pqxx::result R = W.exec(ts);
- pqxx::result::const_iterator ri = R.begin();
+ Work W(conn);
+ result_t R = W.exec(ts);
+ result_t::const_iterator ri = R.begin();
if(ri != R.end()) {
std::string status = (*ri)[0].c_str();
return status == "idle";
@@ -607,7 +613,7 @@ void PracroDAOPgsql::setIdle(std::string sessionid, bool idle)
std::string ts;
try {
- pqxx::work W(*conn);
+ Work W(conn);
if(idle) {
ts = "UPDATE commits SET status='idle' WHERE uid="+sessionid+
" AND status='active';";
@@ -615,7 +621,7 @@ void PracroDAOPgsql::setIdle(std::string sessionid, bool idle)
ts = "UPDATE commits SET status='active' WHERE uid="+sessionid+
" AND status='idle';";
}
- /*pqxx::result R = */W.exec(ts);
+ /*result_t R = */W.exec(ts);
W.commit();
} catch (std::exception &e) {