////////////////////////////////////////////////////////////////
// Creating the menubar 'Database'
//
// Author:
// Stefan Liersch: stliersch@freenet.de, stefan.liersch@ufz.de
// 2006-10-15
////////////////////////////////////////////////////////////////

#ifndef __MENU_DB_H__
#define __MENU_DB_H__

#include <wx/wx.h>

#include "saga_api/saga_api.h"

#include "PGSQL/pgdatabase.h"
#include "PGSQL/sqltable.h"
#include "PGSQL/simple_feature.h"

//---------------------------------------------------------------------
struct TGeometryAttributes
{
	string	geometry_columnt_name;
	string	geometry_type;
	int		geometry_dimension;
	int		SRID;
};
//---------------------------------------------------------------------

class Cmenu_db
{
public:
	Cmenu_db(wxMenu *pMenu);
	~Cmenu_db(void);

	wxMenu			*m_pDB_Menu;
	Csqltable		*m_psqltable;
	CSimpleFeature	*m_pSimpleFeature;

	void			Create_DB_Menu(class wxMenu *m_pDB_Menu);

	// --------------------------------------------------------------
	// Event handler funcions
	void			On_ConnectDB(void);
	void			On_DeConnectDB(void);
	void			On_DB_ImportTable(void);
	void			On_DB_ExportTable(void);
	void			On_DB_CreateTable(void);
	void			On_DB_DeleteTable(void);
	void			On_DB_ModifyTable(void);
	void			On_DB_ImportSimpleFeature(void);
	void			On_DB_ImportAsShape(void);
	void			On_DB_ExportAsShape(void);
	void			On_DB_ImportAsGML(void);
	void			On_DB_ExportAsGML(void);
	void			On_DB_ExportAsSimpleFeature(void);
	// --------------------------------------------------------------

private:

	TGeometryAttributes	GeoAttributes;

	// --------------------------------------------------------------
	// functions are implemented in menu_db_tools.cpp
	void			_On_ConnectDB(void);
	void			_On_DeConnectDB(void);
	void			_On_DB_ImportTable(void);
	void			_On_DB_ExportTable(void);
	void			_On_DB_DeleteTable(void);
	// returns SQL field type corresponding to SAGA table field types
	string			Get_FieldTypeFromSAGA(TSG_Table_Field_Type SG_ft);
	bool			is_numeric(TSG_Table_Field_Type SG_FT);

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



	bool			DLG_ConnectDB();

	// create a table in SAGA workspace from database query
	void			CreateSAGATable(Csqltable *table, CSG_Table *table_in);
	
	CSG_Table		*SelectSAGATable();

	// --------------------------------------------------------------
	// implementation in 'menu_db_vector_data.cpp'
	bool			Create_Shape_From_SF(CSG_Shapes *pShapes);
	bool			Create_SF_From_Shape(CSG_Shapes *pShapes);
	void			AssignGeoAttributes(CSG_Shapes *pShapes);
	string			Get_Reduced_ShapeName(string &ShapeName);
	string			Get_SQL_Create(CSG_Shapes *pShapes, string ShapeName, bool b_create_pk, int pk_col);
	string			Get_SQL_SELECT_GEOM(CSG_Shapes *pShapes, string ShapeName, TGeometryAttributes GeoAttributes);
	string			Get_SQL_INSERT_INTO(CSG_Shapes *pShapes, string ShapeName,
										int row, bool b_create_pk, int pk_col,
										TGeometryAttributes GeoAttributes);
	// --------------------------------------------------------------

	// --------------------------------------------------------------
	// Database connection parameters
	CSG_String		host;
	CSG_String		port;
	CSG_String		options;
	CSG_String		tty;
	CSG_String		dbname;
	CSG_String		user;
	CSG_String		password;
	// --------------------------------------------------------------

};
//-------------------------------------

#endif __MENU_DB_H__