/*****************************************************************************
File: database.h
Author: Salas López, Jorge
Date:   31/03/2004
Description: Communication Database Library for postgres v6.4 | v7.2.
Invocation:  Nothing
*****************************************************************************/

/*****************************************************************************
   Table of Contents:
   Reusable Functions:
    void Connect(struct DBType *db);
    struct DBType *CreateDBType(char *name, char *options);
    void DestroyDB(struct DBType **db);
    void Disconnect(struct DBType *db);
    int ExecuteQuery(struct DBType *db, const char *query);
    int ExecuteTransaction(struct DBType *db, const char *update);
    int ExecuteUpdate(struct DBType *db, const char *update);
    char *GetCommandStatus(struct DBType *db);
    char *GetDatabaseName(struct DBType *db);
    char *GetErrorMessage(struct DBType *db);
    int GetFieldModifier(struct DBType *db, int index);
    char *GetFieldName(struct DBType *db, int index);
    int GetFieldSize(struct DBType *db, int index);
    int GetFieldType(struct DBType *db, int index);
    int GetNumberFields(struct DBType *db);
    int GetNumberTuples(struct DBType *db);
    char *GetResultValue(struct DBType *db, int tuple, int field);
    int GetValueLenght(struct DBType *db, int tuple, int field);
    int isFieldNull(struct DBType *db, int tuple, int field);
    int ProcessConectionPid(struct DBType *db);
   Intern Functions:
    ---
   Includes:
    libpq-fe.h;
   Source code in this file:
    All
*********************************************************************************/

/********************************************************************************
Import Libraries .
*********************************************************************************/
#include "libpq-fe.h"

/********
* TYPES *
*********/
struct PGSTATE{
  PGresult *result;
  char *message;
};

struct PGREPLDATA{
  int qid;

  char *database;
};

struct DBPG {
  // vars for postgres connections
  PGconn *connection;
  char *pghost;
  char *pgport;
  char *pgtty;
  char *pgoptions;
  struct PGSTATE state;
  struct PGREPLDATA repldata;
  int hasConnection;
};

struct DBType{
  struct DBPG pgdata;
};

/********************************************************************************
Reusable Function.
   void Connect(struct DBType *db);
   Purpose: Connect to database
   Limitations: Nothing
   Returns: if connection is established return -2
            else if an error occurs return -1
            else return 0
*********************************************************************************/
int Connect(struct DBType *db);

/********************************************************************************
Reusable Function.
   struct DBType *CreateDBType(char *name, char *options);
   Purpose: Create DBType
   Limitations: Nothing
   Returns: struct DBType
*********************************************************************************/
struct DBType *CreateDBType(char *name, char *options);

/********************************************************************************
Reusable Function.
   void DestroyDB(struct DBType **db);
   Purpose: Destroy DBType
   Limitations: Nothing
   Returns: struct DBType
*********************************************************************************/
void DestroyDB(struct DBType **db);

/********************************************************************************
Reusable Function.

   void Disconnect(struct DBType *db);
   Purpose: Disconnect to database
   Limitations: Nothing
   Returns: Nothing
*********************************************************************************/
void Disconnect(struct DBType *db);

/********************************************************************************
Reusable Function.
   int ExecuteQuery(struct DBType *db, const char *query);
   Purpose: Execute query;
   Limitations: Nothing
   Returns: if an error occurs return -1
            else return 0
*********************************************************************************/
int ExecuteQuery(struct DBType *db, const char *query);

/********************************************************************************
Reusable Function.
   int ExecuteTransaction(struct DBType *db, const char *update);
   Purpose: Execute {BEGIN | COMMIT | ABORT | END}
   Limitations: Nothing
   Returns: if an error occurs returns -1
            else return 0
*********************************************************************************/
int ExecuteTransaction(struct DBType *db, const char *update);

/********************************************************************************
Reusable Function.
   int ExecuteUpdate(struct DBType *db, const char *update);
   Purpose: Execute sql statement
   Limitations: Nothing
   Returns: if an error occurs returns -1
            else return 0
*********************************************************************************/
int ExecuteUpdate(struct DBType *db, const char *update, char type);

/********************************************************************************
Reusable Function.
   int ExistBinaryTuples(struct DBType *db);
   Purpose:
   Limitations: Nothing
   Returns: if an error exist return -1
            else return 1 if exist binary tuples
                        0 if not exist binary tuples
*********************************************************************************/
int ExistBinaryTuples(struct DBType *db);

/********************************************************************************
Reusable Function.
   int FreeResult(struct DBType *db);
   Purpose: Free memory of variable result
   Limitations: Nothing
   Returns: if an error exist return -1
            else return 0
*********************************************************************************/
int FreeResult(struct DBType *db);

/********************************************************************************
Reusable Function.
   char *GetCommandStatus(struct DBType *db);
   Purpose: Get the command status string associated with the SQL
            command that generated the PGresult
   Limitations: Nothing
   Returns: if an error exist return NULL
            else return message with command status
*********************************************************************************/
char *GetCommandStatus(struct DBType *db);

/********************************************************************************
Reusable Function.
   char *GetDatabaseName(struct DBType *db);
   Purpose: Get the name of database
   Limitations: Nothing
   Returns: if an error exist return NULL
            else return database's name
*********************************************************************************/
char *GetDatabaseName(struct DBType *db);

/********************************************************************************
Reusable Function.
   char *GetErrorMessage(struct DBType *db);
   Purpose: Get status message
   Limitations: Nothing
   Returns: Status error message
*********************************************************************************/
char *GetErrorMessage(struct DBType *db);

/********************************************************************************
Reusable Function.
   char *GetFieldMetadata(struct DBType *db);
   Purpose: Get field (attribute) metadata.
   Limitations: Nothing
   Returns: if an error occurs return NULL
            else return value of field
*********************************************************************************/
char *GetFieldMetadata(struct DBType *db, int field);

/********************************************************************************
Reusable Function.
   char *GetFieldMetadata(struct DBType *db);
   Purpose: Get field (attribute) metadata.
   Limitations: Nothing
   Returns: if an error occurs return NULL
            else return value of field
*********************************************************************************/
char *GetFieldsMetadata(struct DBType *db);

/********************************************************************************
Reusable Function.
   int GetFieldModifier(struct DBType *db, int index);
   Purpose: Get the type-specific modification data of the field
            associated with the given field index.
            Field indices start at 0.
   Limitations: Nothing
   Returns: if an error occurs return -1
            else return modifier
*********************************************************************************/
int GetFieldModifier(struct DBType *db, int index);

/********************************************************************************
Reusable Function.
   char *GetFieldName(struct DBType *db, int index);
   Purpose: Get the field (attribute) name associated
            with the given field index. Field indices start at 0.
   Limitations: Nothing
   Returns: if an error exist return NULL
            else return the name of field (attribute)
*********************************************************************************/
char *GetFieldName(struct DBType *db, int index);

/********************************************************************************
Reusable Function.
   int GetFieldSize(struct DBType *db, int index);
   Purpose: Get the size in bytes of the field
            associated with the given field index.
            Field indices start at 0.
   Limitations: Nothing
   Returns: Return the space allocated for this field in a database tuple,
             in other words the size of the server's binary representation
             of the data type.
            Return -1 if the field is variable size or if an error occurs.
*********************************************************************************/
int GetFieldSize(struct DBType *db, int index);

/********************************************************************************
Reusable Function.
   int GetFieldType(struct DBType *db, int index);
   Purpose: Get the field type associated with the given field index.
            The integer returned is an internal coding of the type.
            Field indices start at 0.
   Limitations: Nothing
   Returns: if an error exist return -1
            else return the type of field (attribute)
*********************************************************************************/
int GetFieldType(struct DBType *db, int index);

/********************************************************************************
Reusable Function.
   int GetNumberFields(struct DBType *db);
   Purpose: Get the number of fields (attributes)
           in each tuple of the query result
   Limitations: Nothing
   Returns: if an error exist return -1
            else return the number of fields (attributes)
*********************************************************************************/
int GetNumberFields(struct DBType *db);

/********************************************************************************
Reusable Function.
   int GetNumberTuples(struct DBType *db);
   Purpose: Get number of tuples
   Limitations: Nothing
   Returns: if an error exist return -1
            else return number of tuples
*********************************************************************************/
int GetNumberTuples(struct DBType *db);

/********************************************************************************
Reusable Function.
   char *GetResultValue(struct DBType *db, int tuple, int field);
   Purpose: Get a single field (attribute) value of one tuple.
            Tuple and field indices start at 0.
   Limitations: Nothing
   Returns: if an error occurs return NULL
            else return value of field
*********************************************************************************/
char *GetResultValue(struct DBType *db, int tuple, int field);

/********************************************************************************
Reusable Function.
   char *GetRowsAffected(struct DBType *db);
   Purpose: Get number of rows affected in the last update
   Limitations: Nothing
   Returns: if an error exist return NULL
            else return number of rows affected
*********************************************************************************/
char *GetRowsAffected(struct DBType *db);

/********************************************************************************
Reusable Function.
   int GetValueLenght(struct DBType *db, int tuple, int field);
   Purpose: Returns the length of a field (attribute) in bytes.
            Tuple and field indices start at 0.
   Limitations: Nothing
   Returns: if an error occurs return -1
            else return lenght of data
*********************************************************************************/
int GetValueLenght(struct DBType *db, int tuple, int field);

/********************************************************************************
Reusable Function.
   int isFieldNull(struct DBType *db, int tuple, int field);
   Purpose: Get number of rows affected in the last update
   Limitations: Nothing

   Returns: if an error exist return -1
            else if data in field is null return 1
            else return 0
*********************************************************************************/
int isFieldNull(struct DBType *db, int tuple, int field);

/********************************************************************************
Reusable Function.
   int ProcessConectionPid(struct DBType *db);
   Purpose: Get the process ID of the backend server handling this connection.
   Limitations: Nothing
   Returns: if an error exist return -1
            else pid
*********************************************************************************/
int ProcessConectionPid(struct DBType *db);
