#include "./DataBase.hh"
#include "./DataBaseJournal.hh"
#include <pgsql/libpq-fe.h>
#include <stdlib.h>
#include <string.h>
#include "Memory.h"

DataBaseJournal::DataBaseJournal(char* H, char* P) : DataBase()
{
 host = strdup(H);
 port    = strdup(P);
 dbName   = strdup("Journal");

 //index = new DataBase(H, P, "Keyword");

 DataBase::SetDataBaseInfo(host, port, dbName);
 //ConnectToDataBase();
 //index->ConnectToDataBase);

}

//db = new DataBaseJounarl(info.Host, info.Port, info.DataBase)

void DataBaseJournal::SetKey(char* PDBcord,char* Residues)
{
  char* sql;
  sql = (char*)memoryAllocate(sizeof(char)*300, "DataBaseJournal::SetKey");

//  sprintf(sql,"SELECT * FROM journal,keyword WHERE journal.ID=keyword.ID ");
//  printf("%s\n",sql);
//  Exec(sql);
//  DisplayAll(res);
//  if((PQgetvalue(res,PQntuples(res)-1 , PQnfields(res)-1))==NULL){

//  ResClear();

  int mode=0;
  printf("1:Show   2:Delete ---->>");
  scanf("%d",&mode);
  switch(mode){
  case 1:

    sprintf(sql, "SELECT pdbcord,residues,title,journal,authors,volum FROM journal,keyword WHERE journal.ID=keyword.ID and pdbcord ~ '%s' and residues ~ '%s'",PDBcord,Residues);

    printf("/PDBCord             /Residues /Title                                                  /Journal                       /Authors                                      /Volume             \n");
    Exec(sql);
    DisplayAll(res);
    ResClear();

    break;
  case 2:

    sprintf(sql, "DELETE * FROM journal,keyword WHERE pdbcord ~ '%s' and residues ~ '%s'",PDBcord,Residues);

    Exec(sql);
    ResClear();

    break;

  default:

    break;

  }
}



