/* -*- 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>

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<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*/