/////////////////////////////////////////////////////////////////////////////
// Name:        pgsqlcmd.cpp
// Author:      Stefan Liersch
// Contact:		stefan.liersch@ufz.de stefan.liersch@googlemail.com
// Created:     2007-02-05
/////////////////////////////////////////////////////////////////////////////

#include "pgdatabase.h"
#include "saga_gui/helper.h"			// using function MSG_General_Add()
#define MSG_ADD(s)	MSG_General_Add(s, true, true);

///////////////////////////////////////////////////////////////////////
//
//						SET DATESTYLE
//
///////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------

bool Cpgdatabase::SetDate_ISO()
{
	// creating the SQL query
	char *s ="SET datestyle TO ISO";
			 
	if (ExecSQL(s))
		return(true);
	else
		return(false);
}
//---------------------------------------------------------------------

bool Cpgdatabase::SetDate_SQL()
{
	// creating the SQL query
	char *s ="SET datestyle TO SQL";
			 
	if (ExecSQL(s))
		return(true);
	else
		return(false);
}
//---------------------------------------------------------------------

bool Cpgdatabase::SetDate_Postgres()
{
	// creating the SQL query
	char *s ="SET datestyle TO Postgres";
			 
	if (ExecSQL(s))
		return(true);
	else
		return(false);
}
//---------------------------------------------------------------------

bool Cpgdatabase::SetDate_German()
{
	// creating the SQL query
	char *s ="SET datestyle TO German";
			 
	if (ExecSQL(s))
		return(true);
	else
		return(false);
}
//---------------------------------------------------------------------

bool Cpgdatabase::SetDate_European()
{
	// creating the SQL query
	char *s ="SET datestyle TO European";
			 
	if (ExecSQL(s))
		return(true);
	else
		return(false);
}
//---------------------------------------------------------------------

bool Cpgdatabase::SetDate_NonEuropean()
{
	// creating the SQL query
	char *s ="SET datestyle TO NonEuropean";
			 
	if (ExecSQL(s))
		return(true);
	else
		return(false);
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------

bool Cpgdatabase::CreateUser_normal(string username, string password)
{
	string str;
	str = "CREATE USER " + username + " ";
	//str += "WITH SYSID uid ";
	str += "WITH PASSWORD 'normaluser' ";
	str += "NOCREATEDB";
	
	return(true);
}
//---------------------------------------------------------------------

bool Cpgdatabase::TableExists(const char* table)
{
	wxString sql = "SELECT relname FROM pg_class WHERE relname = '";
	sql += table;
	sql += "'";
	PGresult *res = ExecSQL_wx2(sql);

	if (PQntuples(res) > 0)
	{
		PQclear(res);
		return(true);
	} else {
		PQclear(res);
		return(false);
	}
}
//---------------------------------------------------------------------

bool Cpgdatabase::ExecSQL_wx(wxString SQL_Query)
{
	// executes any SQL query in the currently connected database
	// using the global connection variable 'conn'
	PGresult *res;
	try
	{
		res = PQexec(conn, SQL_Query.c_str());
	}
	catch(...)
	{
		return(false);
	}
	if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
	{
		MSG_ADD(PQresultErrorMessage(res), true);
		PQclear(res);
		return(false);
	} else {
		PQclear(res);
		return(true);
	}
}
//---------------------------------------------------------------------
bool Cpgdatabase::ExecSQL_wx1(wxString SQL_Query, wxString &error)
{
	// executes any SQL query in the currently connected database
	// using the global connection variable 'conn'
	// if something went wrong, wxString 'error' contains the corresponding error message
	PGresult *res;

	res = PQexec(conn, SQL_Query.c_str());
	if (PQresultStatus(res) != PGRES_TUPLES_OK)
	{
		error = PQresultErrorMessage(res),true;
		PQclear(res);
		return(false);
	} else {
		PQclear(res);
		return(true);
	}
	/*
	PGresult *res;
	try
	{
		res = PQexec(conn, SQL_Query.c_str());
	}
	catch(...)
	{
		wxString errorM(wxString(PQerrorMessage(conn)));
		error = errorM;
		return(false);
	}
	if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
	{
		PQclear(res);
		wxString errorM(wxString(PQerrorMessage(conn)));
		error = errorM;
		return(false);
	} else {
		PQclear(res);
		wxString errorM(wxString(PQerrorMessage(conn)));
		error = errorM;
		return(true);
	}
	*/
}

//---------------------------------------------------------------------
bool Cpgdatabase::ExecSQL_wx1(PGresult *res, wxString SQL_Query, wxString &error)
{
	// executes any SQL query in the currently connected database
	// using the global connection variable 'conn'
	// if something went wrong, wxString 'error' contains the corresponding error message
	
	// !!! NOTE: PGresult *res must be PQclear(ed) by calling function !!!
	if (PQresultStatus(res) != PGRES_TUPLES_OK)
	{
		error = PQresultErrorMessage(res),true;
		return(false);
	} else {
		//PQclear(res);
		return(true);
	}
}
//---------------------------------------------------------------------

PGresult* Cpgdatabase::ExecSQL_wx2(wxString SQL)
{
	return(PQexec(conn,SQL.c_str()));
}
//---------------------------------------------------------------------

bool Cpgdatabase::get_ResultStatus(PGresult* res)
{
	if (PQresultStatus(res) != PGRES_TUPLES_OK)
	{
		MSG_ADD(PQresultErrorMessage(res));
		PQclear(res);
		return(false);
	} else {
		return(true);
	}
}
