#include "%%classItemName%%.h"

// %%classTableName%% //////////////////////////////////////////////////////////

BEGIN_STORE_TABLE(%%classItemName%%, dba::Storeable, "%%classTableName%%")
%%classItemBindings%%
END_STORE_TABLE()

%%classItemName%%* %%classItemName%%::GetBy%%pkName%%(wxdba::SQLArchive &ar, %%pkType%% val)
{
	if( ar.IsOpen() ) {
		%%classItemName%% *c = new %%classItemName%%();
		wxdba::SQLIStream istream = ar.GetIStream();
		istream.OpenFromQuery( *c, wxdba::SQL(wxT("SELECT * FROM %%classTableName%% WHERE %%pkName%% = (:d)")) << val );
		if( !istream.GetNext(c) ) {
			delete c;
			c = NULL;
		}
		istream.Destroy();
		
		return c;
	} else {
		return NULL;
	}
}

// %%classTableName%% collection ///////////////////////////////////////////////

%%classColName%%::%%classColName%%(wxdba::SQLArchive &ar)
{
	if( ar.IsOpen() ) {
		%%classItemName%% c;
		wxdba::SQLIStream istream = ar.GetIStream();
		istream.Open(c);
		while (istream.GetNext( &c )) {
			m_collection.push_back( c );
		}
		istream.Destroy();
	}
}

%%classColName%%::%%classColName%%(wxdba::SQLArchive &ar, const wxString &condition)
{
	if( ar.IsOpen() ) {
		%%classItemName%% c;
		wxdba::SQLIStream istream = ar.GetIStream();
		istream.OpenFromQuery( c, wxdba::SQL(wxT("SELECT * FROM %%classTableName%% WHERE ") + condition ));
		while (istream.GetNext( &c )) {
			m_collection.push_back( c );
		}
		istream.Destroy();
	}
}

%%classColName%%::~%%classColName%%()
{
	m_collection.clear();
}

%%classColName%%* %%classColName%%::Get(wxdba::SQLArchive &ar)
{
	return new %%classColName%%(ar);
}

%%classColName%%* %%classColName%%::Get(wxdba::SQLArchive &ar, const wxString &condition)
{		
	return new %%classColName%%(ar, condition);
}

void %%classColName%%::Fill(wxGrid* pGrid)
{
	if( pGrid ) {
		pGrid->DeleteCols(0,pGrid->GetNumberCols());
		pGrid->DeleteRows(0,pGrid->GetNumberRows());

		%%classColLabelFillGrid%%

		int i = 0;
		std::vector<%%classItemName%%>::iterator iter;
		for (iter = m_collection.begin(); iter != m_collection.end(); ++iter, ++i) {
			%%classItemName%% row = *iter;
			pGrid->AppendRows();
			
			%%classColDataFillGrid%%	
		}	
	}
}

// %%classTableName%% utilities ////////////////////////////////////////////////

int %%classUtilName%%::Create(wxdba::SQLArchive &ar
			, bool drop
			)
{
	if( ar.IsOpen() ) {
		wxString query = wxT(
			%%classUtilsCreateStatement%%
			);
		
		if( drop ) {
			query = wxT(
				%%classUtilsDropStatement%%
				) + query;
		}
		
		ar.GetOStream().SendUpdate( wxdba::SQL( query ) );
		return 0;
	} else {
		return -1;
	}
}
			
int %%classUtilName%%::Add(wxdba::SQLArchive &ar
			%%classUtilsAddParametersWithoutPK%%			
			)
{
	if( ar.IsOpen() && (ar.GetIdFetcher() != NULL) ) {
		%%classItemName%% c;
		%%classUtilsAddSetDebeaParams%%
		
		// write object into database
		wxdba::SQLOStream ostream = ar.GetOStream();
		ostream.Open();
		if( ostream.isOpen() ) { 
			ostream.Put(&c); // id of object is assigned here
			ostream.Destroy();
		}
		
		return 0;
	} else {
		return -1;
	}
}

int %%classUtilName%%::Edit(wxdba::SQLArchive &ar
			%%classUtilsAddParameters%%			
			)
{
	if( ar.IsOpen() && (ar.GetIdFetcher() != NULL) ) {
		%%classItemName%% *c = %%classItemName%%::GetBy%%pkName%%(ar, %%pkName%%);
		if( c ) {
			%%classUtilsEditDebeaStatement%%
			
			// update object in database
			wxdba::SQLOStream ostream = ar.GetOStream();
			ostream.Open();
			if( ostream.isOpen() ) { 
				ostream.Put(c); 
				ostream.Destroy();
			}
			
			return 0;
		}
	} 
	return -1;
}

int %%classUtilName%%::Delete(wxdba::SQLArchive &ar
			%%classUtilsDeleteParameters%%			
			)
{
	if( ar.IsOpen() ) {
		// delete object from database
		ar.GetOStream().SendUpdate( wxdba::SQL(wxT("DELETE FROM %%classTableName%% WHERE %%pkName%% = (:d)")) << %%pkName%% );
		
		return 0;
	} else {
		return -1;
	}
}
