/* -*- 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 /* Commits: patientid | template | version | timestamp | uid | status Transactions: macro | version | timestamp | user | uid | cid Fields: transaction | name | value */ #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::vector fields; fields.push_back("visus.egen_korr.mangler.odxt"); fields.push_back("visus.egen_korr.kontrast.odxt"); fields.push_back("visus.egen_korr.snellen.odxt"); fields.push_back("visus.egen_korr.etdrs.odxt"); fields.push_back("visus.egen_korr.korr.sf.odxt"); fields.push_back("visus.egen_korr.korr.cyl.odxt"); fields.push_back("visus.egen_korr.korr.grader.odxt"); fields.push_back("visus.egen_korr.st_hul.odxt"); fields.push_back("visus.egen_korr.st_hul.snellen.odxt"); fields.push_back("visus.egen_korr.st_hul.etdrs.odxt"); fields.push_back("visus.egen_korr.mangler.osin"); fields.push_back("visus.egen_korr.kontrast.osin"); fields.push_back("visus.egen_korr.snellen.osin"); fields.push_back("visus.egen_korr.etdrs.osin"); fields.push_back("visus.egen_korr.korr.sf.osin"); fields.push_back("visus.egen_korr.korr.cyl.osin"); fields.push_back("visus.egen_korr.korr.grader.osin"); fields.push_back("visus.egen_korr.st_hul.osin"); fields.push_back("visus.egen_korr.st_hul.snellen.osin"); fields.push_back("visus.egen_korr.st_hul.etdrs.osin"); fields.push_back("visus.egen_korr.ou"); fields.push_back("visus.egen_korr.kontrast.ou"); fields.push_back("visus.egen_korr.snellen.ou"); fields.push_back("visus.egen_korr.etdrs.ou"); 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); 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*/