/***************************************************************************
                Database Class
                -----------------------------------------
	begin      : 2013/09/16
	copyright  : (C) 2005 Solbox Inc.
	author     : Development 1 Team
					- 2013/09/16
	email      : dev1@solbox.com
	version    : 3.2.0

	CopyRight(C) 2005 Solbox Inc. All Rights reserved.
	Redistribution and use in source and binary forms, with or with out
	modification, are not permitted in outside of Solbox Inc.
***************************************************************************/
#ifndef __DATABASE_H__
#define __DATABASE_H__

#include <iostream>
#include <string>

#include "libpq-fe.h"
#define MaxSizeOfDBQuery		1024*9

using namespace std;
class DataBase
{
public:
	enum CFLAG { CLEAR = 1, NOT_CLEAR };
	DataBase() { m_PGconn = NULL; m_pRes = NULL;}
	~DataBase();
	PGconn *PgOpenDB(string &strHost, int Port, string &strDBName, string &strAcct, string &strPasswd);
	PGconn *PgOpenDB(const char *pszDBName);
	void PgCloseDB();
	int PgResult(CFLAG flag);
	PGconn *GetPgConn();
	PGresult *GetRes();
	int GetCmdTuples();
	int GetNoTuples();
	int GetNoFields();
	int GetResultCode() { return m_ResultCode; }
	char *GetValue(int tuple, int field);
	void PgClear();
	int PgDoExec(string &strQuery);
	int PgDoExec(char *pszQuery);
	int PgDoExec(char *pszQuery, CFLAG flag);
	int PgEscapeString(char *to, const char *from, size_t length);
	string &GetErrorMessage();
private:
	int m_ResultCode;
	PGconn *m_PGconn;
	PGresult *m_pRes;
	string m_ErrorMessage;
};

#endif // ~__DATABASE_H__
