/* test3.cpp
joris esch 3.10.1998
*/
// compile with:
// c++ test3.cpp -o test3 -lpq -lpq++

#include "test3.h"

void main()
{
          
  bool verbose = true;
  char* dbname= "template1";        
 

  /* make a connection to the database */
 
  if (verbose) {
    cout << "Trying to connect to db " << dbname  << '\n';;  // << " on host " << pghost;
  } ;
  
  // PgDatabase inherits from PgConnection
  PgDatabase *conn=new PgDatabase(dbname);

  // give error messages if conn is not ok

  if (conn->Status()!=0){
    cerr << "Connection status: " << conn->Status() <<'\n';
    cerr << "Connection failed." << '\n';
    cerr << "Database name: " << conn->DBName() << '\n';
    cerr << conn->ErrorMessage() << '\n';
    //end execution here... or use exception handling...cfr. ch8
    exit(1); //Stroustrup page 116 0== okidoki, 1!=okidoki
    //throw "Connection failed";
  }; 
  
  if (verbose) {cout <<  "Connection succeeded.\n";};
  cout << "You are currently connected to db: " << conn->DBName() << '\n';
  //cout << "Let's do a query";
  //see if i can do  query...
  if (conn->ExecTuplesOk("DECLARE mycursor BINARY CURSOR FOR select * from pg_database")){// 1 means Ok
    int no_t=conn->Tuples();
    int no_f=conn->Fields();
    if (verbose) {
      cout << "The number of tuples in the query: " << conn->Tuples() << '\n';
      cout << "The number of fields in the query: " << conn->Fields() << '\n';
    }
    cout << "The query results: \n";
  
    for(int i=0;i < no_t; i++){
      for(int j=0;j < no_f; j++){
	cout << conn->GetValue(i,j) << '\t';
	//cout << conn->GetValue(i,j) << '\n';
      }
      cout << '\n';
    }
  } else{
    cout << "Execution of query failed\n";//conn
  }

  //conn->Exec("DECLARE mycursor BINARY CURSOR FOR select * from test");
  
  //return 0;
  
}