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

#ifndef HEADER_INCLUDED__pgdatabase_H
#define HEADER_INCLUDED__pgdatabase_H

//---------------------------------------------------------

//*********************************************************************
// ToDo
//---------------------------------------------------------------------
// - CreateTable(): wenn Sonderzeichen, oder "-" im Tablellennamen,
//   dann wird Tablelle nicht erstellt
//*********************************************************************
#include "wx/arrstr.h"
#include "libpq-fe.h"

#include "sqltable.h"
#include "simple_feature.h"

#include "saga_api/table.h"
//---------------------------------------------------------

class Cpgdatabase
{
	friend class CSimpleFeature;
public:
	Cpgdatabase		(void);
	Cpgdatabase		(char *pghost, char *pgport, char *pgoptions, char *pgtty,
					 char *dbname, char *user, char *password);

	~Cpgdatabase	(void);

	bool			b_conn;  // true if connection to database was successfull
	bool			b_trans; // true if transaction was successfull
	
	//-----------------------------------------------------
	// PUBLIC FUNCTIONS
	//-----------------------------------------------------
	bool			ConnectToDB(string pghost, string pgport,
								string pgoptions, string pgtty,
								string dbname, string user, string password);

	void			ExitDB();

	// Executes an user defined SQL command
	// Declaration in pgsqlcmd.cpp
	bool			ExecSQL_wx(wxString SQL_Query);
	bool			ExecSQL_wx1(wxString SQL_Query, wxString &error);

	//--------------------
	// Table Functions
	//--------------------
	int				GetNumberCols(const char *tablename);
	int				GetNumberRows(const char *tablename);

	// reads a PostgreSQL table
	bool			ReadTable(Csqltable *table);
	//bool			ReadSpatialTable(CSimpleFeature *SimpleFeature);

	// returns a string containing all user tables in database
	string			GetDBTables();
	wxArrayString	GetDBTablesArray();
	string			GetDBGeometryTables();

	bool			CreateTable(Csqltable *SQLTable);

	bool			DropTable(const char *tablename);

	bool			InsertIntoTable(const char *tablename, const char *record);

	//--------------------
	// SQL Commands
	// Declarations in pgsqlcmd.cpp
	//--------------------
	bool			SetDate_ISO();
	bool			SetDate_SQL();
	bool			SetDate_Postgres();
	bool			SetDate_German();
	bool			SetDate_European();
	bool			SetDate_NonEuropean();

	bool			CreateUser_normal(string username, string password);

	//--------------------
	// Connection Parameters
	//--------------------
	char			*GetDBName();	// returns database name
	char			*GetUser();		// returns user name
	char			*GetPW();		// returns password
	char			*GetHost();		// returns host name
	char			*GetPort();		// returns port
	char			*GetTTY();		// returns tty for debugging 
	char			*GetOptions();	// returns the connection configuration options

	PGconn			*GetConn();		// returns PGconn

	// returns SAGA TABLE_FIELDTYPE as string
	TSG_Table_Field_Type	SQLFieldTypeToSAGAFieldtype(int oid);

private:
	const char		*conninfo;
	PGconn			*conn;
	PGresult		*res;

	char			*pghost,
					*pgport,
					*pgoptions,
					*pgtty,
					*dbname,
					*user,
					*password;

	Csqltable		*SQLTable;
	CSimpleFeature	*SimpleFeature;

	//-----------------------------------------------------
	// PRIVATE FUNCTIONS
	//-----------------------------------------------------
	bool			ExecSQL(char *SQL_Query);

	bool			StartTransaction();
	void			CommitTransaction();

	bool			FetchRows(const char *tablename);

	bool			FetchAll();

	void			CloseCursor();

	// returns the 'field type' as string
	string			GetFieldTypeString(PGresult *res, int i);


}; // end class pgdatabase
#endif /* #ifndef HEADER_INCLUDED__pgdatabase */