/////////////////////////////////////////////////////////////////////////////
// Name:        MainFrame.h
// Author:      Stefan Liersch
// Contact:		stefan.liersch@ufz.de stliersch@freenet.de
// Created:     2007-02-05
/////////////////////////////////////////////////////////////////////////////

#include "pgdatabase.h"

///////////////////////////////////////////////////////////////////////
//
//						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::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)
	{
		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'
	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);
	}
}