From f4216f207e539cc10fb882fa49cbe294ebe4ee63 Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 18 Jan 2011 10:54:06 +0000 Subject: Initial shot at a data extraction tool. --- server/getdata/Makefile.am | 21 ++++++ server/getdata/getdata.cc | 176 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 server/getdata/Makefile.am create mode 100644 server/getdata/getdata.cc (limited to 'server') diff --git a/server/getdata/Makefile.am b/server/getdata/Makefile.am new file mode 100644 index 0000000..8cd960f --- /dev/null +++ b/server/getdata/Makefile.am @@ -0,0 +1,21 @@ +bin_PROGRAMS = getdata + +getdata_LDADD = $(LD_EFENCE) $(PQXX_LIBS) $(CONFIG_LIBS) + +getdata_CXXFLAGS = $(PQXX_CXXFLAGS) $(CONFIG_CXXFLAGS) + +getdata_SOURCES = \ + getdata.cc + +EXTRA_DIST = + +################ +# Test Section # +################ + +TEST_SOURCE_DEPS = ${pracrod_SOURCES} ${EXTRA_DIST} +TEST_SCRIPT_DIR = $(top_srcdir)/../tools + +include ${TEST_SCRIPT_DIR}/Makefile.am.test + +include Makefile.am.test \ No newline at end of file diff --git a/server/getdata/getdata.cc b/server/getdata/getdata.cc new file mode 100644 index 0000000..b051de0 --- /dev/null +++ b/server/getdata/getdata.cc @@ -0,0 +1,176 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * getdata.cc + * + * Tue Jan 18 08:11:53 CET 2011 + * Copyright 2011 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Pracro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include +#include +#include + +enum { + PATIENTID = 0, + MACRO = 1, + VERSION = 2, + TIMESTAMP = 3, + USER = 4, + UID = 5, + TEMPLATE = 6 +}; + +enum { + TRANSACTION = 0, + NAME = 1, + VALUE = 2 +}; + +#define SEP "\t" + +std::string escape(std::string str) +{ + std::string out = "\""; + std::string::iterator i = str.begin(); + while(i != str.end()) { + if(*i == '\"') out += "''"; + else out += *i; + i++; + } + out += "\""; + return out; +} + +int main() +{ + std::string like = "ref"; + + std::string host = "localhost"; + std::string port = "5432"; + std::string user = "pracro"; + std::string password = "pracro"; + std::string dbname = "pracro"; + + std::string cs = "host="+host+" port="+port+ + " user="+user+" password="+password+" dbname="+dbname; + + pqxx::connection conn(cs); + pqxx::work W(conn); + + // transactions: + // patientid | macro | version | timestamp | user | uid | template + + // fields: + // transaction | name | value + + std::map fieldnames; + int idx = 0; + pqxx::result Rf = W.exec("SELECT DISTINCT name FROM fields ORDER BY name;"); + pqxx::result::const_iterator rfi = Rf.begin(); + for(unsigned int r = 0; r < Rf.size(); r++) { + pqxx::result::tuple tuple = Rf.at(r); + std::string name = tuple.at(0).c_str(); + fieldnames[name] = idx; + idx++; + } + + printf("patientid"SEP); + printf("template"SEP); + printf("macro"SEP); + printf("version"SEP); + printf("timestamp"SEP); + printf("user"SEP); + printf("uid"SEP); + std::map::iterator i = fieldnames.begin(); + while(i != fieldnames.end()) { + printf("%s"SEP, i->first.c_str()); + i++; + } + printf("\n"); + + pqxx::result R = W.exec("SELECT * FROM transactions WHERE template LIKE '"+like+"%';"); + + pqxx::result::const_iterator ri = R.begin(); + for(unsigned int r = 0; r < R.size(); r++) { + + pqxx::result::tuple tuple = R.at(r); + + std::string patientid = tuple.at(PATIENTID).c_str(); + std::string macro = tuple.at(MACRO).c_str(); + std::string version = tuple.at(VERSION).c_str(); + std::string timestamp = tuple.at(TIMESTAMP).c_str(); + std::string user = tuple.at(USER).c_str(); + std::string uid = tuple.at(UID).c_str(); + std::string templ = tuple.at(TEMPLATE).c_str(); + + pqxx::result Rf = W.exec("SELECT * FROM fields WHERE transaction="+uid+";"); + + std::vector values; + values.insert(values.begin(), fieldnames.size(), ""); + pqxx::result::const_iterator rfi = Rf.begin(); + for(unsigned int r = 0; r < Rf.size(); r++) { + pqxx::result::tuple tuple = Rf.at(r); + + std::string name = tuple.at(NAME).c_str(); + std::string value = tuple.at(VALUE).c_str(); + values[fieldnames[name]] = value; + } + + printf("%s"SEP, escape(patientid).c_str()); + printf("%s"SEP, escape(templ).c_str()); + printf("%s"SEP, escape(macro).c_str()); + printf("%s"SEP, escape(version).c_str()); + time_t t = atol(timestamp.c_str()); + printf("%s"SEP, escape(ctime(&t)).c_str()); + printf("%s"SEP, escape(user).c_str()); + printf("%s"SEP, escape(uid).c_str()); + std::vector::iterator vi = values.begin(); + while(vi != values.end()) { + printf("%s"SEP, escape(*vi).c_str()); + vi++; + } + printf("\n"); + + } + + return 0; +} + + + + + + + +#ifdef TEST_GETDATA +//deps: +//cflags: +//libs: +#include "test.h" + +TEST_BEGIN; + +// TODO: Put some testcode here (see test.h for usable macros). + +TEST_END; + +#endif/*TEST_GETDATA*/ -- cgit v1.2.3