#ifndef __TSAUTILS_DB_DRIVER_POSTGRES_H__
#define __TSAUTILS_DB_DRIVER_POSTGRES_H__

/***************************************************************************

    DBDriverPostgres.h

    DBDriverPostgres implements Postgres functionality

    Date        Author  Description
    20001114    MP      Created

 ***************************************************************************/

#include <postgres/libpq-fe.h>

#include "DBDriverGeneric.h"

TSAUTILS_NAMESPACE_BEGIN

/*
 ***** DBDriverPostgres
 */

class DBDriverPostgres : public DBDriverGeneric
{
    private:
        // Whether the last query returned rows
        bool    m_blDidReturnRows;

        // Connection to Postgres database
        PGconn *m_connection;

        // Result set from Postgres
        PGresult *m_resultSet;

        // Column names in the result set
        vector <string> m_columnNames;

        // Current row number
        long m_lCurrentRow;

        // Initialises result set data
        void _initResultSet();

        // Validates OK to access fields
        int _validateFieldAccess(const string &in_sFldName);
    public:
        // Constructor/destructor
        DBDriverPostgres(const char *pchHost, const char *pchDB, const char *pchUser, const char *pchPwd);
        virtual ~DBDriverPostgres();

		// Returns column names
		vector <string> colNames();

        // Retrieves field data
        bool        fldBool(const string &in_sFldName);
        double      fldDbl(const string &in_sFldName);
        Date        fldDT(const string &in_sFldName);
        float       fldFlt(const string &in_sFldName);
        int         fldInt(const string &in_sFldName);
        long        fldLong(const string &in_sFldName);
        string      fldStr(const string &in_sFldName);

        // Record navigation
        bool    didReturnRows();
        bool    recordNext();

        // SQL manipulation
        bool    sqlExec(const char *in_pchSQL);

        // Transaction control
        void    transCommit();
        void    transRollback();
        void    transStart();
};

TSAUTILS_NAMESPACE_END

#endif
