// This is an include file for -*- C++ -*-
//
// Minimal library for gluing the POSTGRES LIBPQ cruft to C++,
// courtesy of Justin Smith (jsmith@mcs.drexel.edu), 17 Mar 1993
//
// Hacked to use stock POSTGRES headers - pma, 09 June 1993
//
// /usr/local/devel/postgres-4.2-devel/src/contrib/cplusplus/RCS/pg.h,v 1.2 1993/06/10 19:31:20 aoki Exp
//
// Changed 21/07/94 : Added some defines to allow new String class.
//                    Removed defines of 'true' and 'false' to allow
//                    them to become members of a Class.
//


#ifndef _pgdb_h
#define _pgdb_h

/*
#include <iostream.h>
*/
#include <string.h>

// anything included here that #include's C++ headers *may* have 
// problems with this...
extern "C" {
#define PROTOTYPES
#undef offsetof			// we define this in tmp/c.h ...
#define String PostgresString   // Change name of internal String class
#include "tmp/libpq-fe.h"
#undef String			// Allow a new String class.
#undef true			// Remove 'true' and 'false'
#undef false
    // These are for applications.
    extern char* PQhost;	// machine on which the backend is running
    extern char* PQport;	// comm. port with the postgres backend.
}

class pgdb {
    PortalBuffer*	portalbuf;
    char		res[PortalNameLength+2];// query result string
    char		dbname[NameLength+1];
    Parser		*sql_actor;
  
public:
    pgdb(char* name,Parser *InitActor) {
	(void) strncpy(dbname, name, NameLength);
	PQsetdb(name);
	sql_actor = InitActor;
    }
    
    ~pgdb() { PQfinish(); }
    
    // Sends a query (as a char string) to the database.
    // Ex: dbname << "retrieve(pg_user.all)";
    void operator<<(char*);
    
    // Gets the number of groups. 
    int ngroups();

    // Gets the number of fields for a given group (number) 
    int nfields(int);

    // Gets the number of tuples in a given group.
    int ntuples(int);

    // Gets the total number of tuples.
    int ntuples();
    
    // Gets the number of fields for records of group 0.
    int nfields() {
	return nfields(0);
    }
    
    // Gets the name of field par1 in group par2
    char* fieldname(int, int);
    
    // Gets the name of field par1 in group 0.
    char* fieldname(int);
    
    // Retrieves the data in the named field from the numbered tuple
    // as a character string.
    char* fielddata(int, char*);

    // Retrieves the length of data in the named field from the numbered
    // tuple.
    int fieldlength(int, char*);
};

#endif /* _pgdb_h */
