summaryrefslogtreecommitdiff
path: root/server/getdata/getdata.cc
blob: 717d1a9bd99ca8433baba06b04d442b86e4eed36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* -*- 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 <string>
#include <pqxx/pqxx>
#include <stdlib.h>
/*
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<std::string> 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<std::string, int> 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<std::string, int>::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<std::string> 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<std::string>::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*/