#include <windows.h>
#include <libpq-fe.h>

#ifndef __DATABASECLASS
#define __DATABASECLASS

#define DATABASE_TYPE_NOTYPE	0
#define DATABASE_TYPE_NUMBER	1
#define DATABASE_TYPE_STRING	2
#define	DATABASE_TYPE_DATE		3
#define DATABASE_TYPE_UNKNOWN	4
#define DATABASE_TYPE_ERROR		5		//napr. pri zistovani typu datumu moze hodit chybu

struct DB_TABLE_ATTRIBUTE
{
	char *name;
	BYTE type;//number, string alebo date
};

struct DB_TABLE
{
	char *name;
	DWORD nAttributes;//pocet atributov
	DB_TABLE_ATTRIBUTE *attributes;//jednotlive atributy
};

class CDbStructure
{
	public:
		DWORD nTables;//pocet tabuliek
		DB_TABLE *tables;

		CDbStructure();
		~CDbStructure();
};

class CSQLresult
{
	private:
		PGresult	*res;
		char *error;
	public:
		ExecStatusType GetResult();
		const char *GetErrorCode();//vrati SQL chybu
		char *GetError();//vrati SQL textovu chybu
		int GetnTuples();//pocet riadkov vo vysledku selectu
		const char *Value(int row, int column);
		CSQLresult();
		~CSQLresult();

		bool ok;//prebehlo vykonanie v poriadku?

	friend class CDatabase;
};

class CDatabase
{
	private:
		PGconn* connection;
		CDbStructure* structure;

		static BOOL CALLBACK DialogProc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp);
		static DWORD GetType(const char *type);//vrati typ, ktory reprezentuje SQL type

	public:
		CDatabase();
		~CDatabase();
		void UpdateStructure();//nacita strukturu do CDatabase::CDbStructure
		const CDbStructure* GetStructure();//vrati pointer na strukturu
		void FillTreeViewWithAtoms(HWND hwnd);
		bool Connect();
		void Disconnect();
		bool TableExists(const char* tableName);//existuje tabulka s takymto nazvom?
		void SetSettings();//zobrazi dialogbox s nastavenim pre pripojenie

		static BYTE IsNumericType(const char *type);//vrati nenulovy, ak SQL typ type je cislo
		static BYTE IsCharacterType(const char *type);//vrati nenulovy, ak SQL typ type je string alebo datum (specifikuje, ci ide o string alebo datum)
		static const char *GetType(DWORD type);//vrati slovny typ

		static char *typeNumeric;
		static char *typeString;
		static char *typeDate;

		CSQLresult* ExecSQL(const char *command);
};

#endif