/***************************************************************************
                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.
***************************************************************************/

#include "Database.h"

#include <string.h>
#include <stdlib.h>


DataBase::~DataBase()
{
	if(m_PGconn != NULL) {
		PgCloseDB();
	}
}

PGconn *DataBase::PgOpenDB(string &strHost, int Port, string &strDBName, string &strAcct, string &strPasswd)
{
	char szPort[12];
	
	::memset(szPort, 0, sizeof(szPort));
	::snprintf(szPort, sizeof(szPort), "%d", Port);

	m_PGconn = PQsetdbLogin(strHost.c_str(), szPort, NULL, NULL, strDBName.c_str(), strAcct.c_str(), strPasswd.c_str());
	if(PQstatus(m_PGconn) == CONNECTION_BAD) {
		return NULL;
	}
	return m_PGconn;
}

PGconn *DataBase::PgOpenDB(const char *pszDBName)
{
	m_PGconn = PQsetdb(NULL, NULL, NULL, NULL, pszDBName);
	if(PQstatus(m_PGconn) == CONNECTION_BAD) {
		return NULL;
	}
	return m_PGconn;
}

void DataBase::PgCloseDB()
{
	if(m_PGconn != NULL) 
	{
		PQfinish(m_PGconn);
		m_PGconn = NULL;
	}
}

int DataBase::PgResult(CFLAG flag)
{
	int fRet = 0;
	int Result;

	Result = PQresultStatus(m_pRes);
	switch(Result) {
		case  PGRES_EMPTY_QUERY :
									fRet = -1;
									break;
		case  PGRES_BAD_RESPONSE :
									fRet = -2;
									break;
		case  PGRES_NONFATAL_ERROR :
									fRet = -3;
									break;
		case  PGRES_FATAL_ERROR :
									fRet = -4;
									break;
	    case  PGRES_TUPLES_OK :
									fRet = 1;
									break;
		case  PGRES_COMMAND_OK :
									fRet = 2;
									break;
	}
	if(fRet < 0) {
		m_ErrorMessage = PQresultErrorMessage(m_pRes);
	}
	m_ResultCode = Result;
	if(flag == CLEAR) {
		PQclear(m_pRes);
	}
	return fRet;
}

string &DataBase::GetErrorMessage()
{
	return m_ErrorMessage;
}

int DataBase::GetCmdTuples()
{
	//fprintf(stderr, "DataBase::GetCmdTuples PQcmdTuples %s\n", PQcmdTuples(m_pRes));
	return (int)atoi(PQcmdTuples(m_pRes));
}

int DataBase::GetNoTuples()
{
	return PQntuples(m_pRes);
}

int DataBase::GetNoFields()
{
	return PQnfields(m_pRes);
}

PGresult *DataBase::GetRes()
{
	return m_pRes;
}

void DataBase::PgClear()
{
	PQclear(m_pRes);
}

char *DataBase::GetValue(int tuple, int field)
{
	return PQgetvalue(m_pRes, tuple, field);
}

int DataBase::PgDoExec(char *pszQuery)
{
	return this->PgDoExec(pszQuery, NOT_CLEAR);
}

int DataBase::PgDoExec(string &strQuery)
{
	return this->PgDoExec((char *)strQuery.c_str(), NOT_CLEAR);
}

int DataBase::PgDoExec(char *pszQuery, CFLAG flag)
{
	if(m_PGconn == NULL) return -1;
	m_pRes = PQexec(m_PGconn, pszQuery);
	return PgResult(flag);
}

int DataBase::PgEscapeString(char *to, const char *from, size_t length)
{
	int retval = 0;

	//PQescapeStringConn(m_PGconn, to, from, length, &retval);
	PQescapeString(to, from, length);
	return retval;
}
