
#include "dbconn.h"
#include "fortress_skelet.h"

#ifdef _DEBUG
#ifndef DEBUG_NEW
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
#endif

/* indexy pro fortress classy - INSERT dotaz */
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_FORTRESSCLASSID               1
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_CLASSNAME                     2
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_CLASSTYPE                     3
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MINWEIGHT                     4
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXWEIGHT                     5
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MINTSI                        6
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXTSI                        7
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXLENGTH                     8
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXWIDTH                      9
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXHEIGHT                    10
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXDML                       11
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXLASERGUNS                 12
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXWEAPONSTOTAL              13
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_ACCPOINTSTOTAL               14
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_QRATING                      15
#define DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MRATING                      16

/* indexy pro fortress classy - SELECT dotaz */
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_FORTRESSCLASSID               1
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_CLASSNAME                     2
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_CLASSTYPE                     3
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MINWEIGHT                     4
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXWEIGHT                     5
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MINTSI                        6
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXTSI                        7
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXLENGTH                     8
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXWIDTH                      9
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXHEIGHT                    10
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXDML                       11
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXLASERGUNS                 12
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXWEAPONSTOTAL              13
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_ACCPOINTSTOTAL               14
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_QRATING                      15
#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MRATING                      16

#define DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_WHERE                         1

/* indexy pro fortress classy - DELETE dotaz */
#define DB_INDEX_FORTRESS_CLASS_DELETE_QUERY_WHERE                         1


/* indexy pro fortress classy - UPDATE dotaz */
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_FORTRESSCLASSID              16
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_CLASSNAME                     1
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_CLASSTYPE                     2
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MINWEIGHT                     3
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXWEIGHT                     4
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MINTSI                        5
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXTSI                        6
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXLENGTH                     7
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXWIDTH                      8
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXHEIGHT                     9
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXDML                       10
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXLASERGUNS                 11
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXWEAPONSTOTAL              12
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_ACCPOINTSTOTAL               13
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_QRATING                      14
#define DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MRATING                      15


// ################################

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

#define DB_FORTRESS_CLASSES_INITIALIZE_QUERY "SELECT FORTRESSCLASSID FROM FORTRESSCLASSES ORDER BY FORTRESSCLASSID DESC"

#define DB_INDEX_FORTRESS_CLASS_INITIALIZE_IDITEM                            1

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

void cacheFortressClassesListClass :: DB_Initialize()
{
    // priprav cache list a i nahraj data do indexu
    IBPP :: Database& db    = actx->db.getDatabase();
    IBPP :: Transaction& tr = actx->db.getTransaction();
    
    IBPP :: Statement s = IBPP :: StatementFactory(db, tr, DB_FORTRESS_CLASSES_INITIALIZE_QUERY);

	try
	{
        s->Execute();

        // nacteme, jak velky potrebujeme ten list
        if (s->Fetch())
        {
            int64_t d;
            s->Get(DB_INDEX_FORTRESS_CLASS_INITIALIZE_IDITEM, d);
            expand((unsigned long)d + 1, identifiersReusable());       
        
            // postupne nacitame polozky
            do
            {
                int64_t itemId;
                s->Get(DB_INDEX_FORTRESS_CLASS_INITIALIZE_IDITEM,    itemId);

                // pote oznacime polozku v listu, ze je v databazi
                data[(unsigned long)itemId].first = DATA_IN_DATABASE_ONLY;
                freeIDs.erase((dtIdentifier)itemId);
            } while (s->Fetch());
        }

        s->Close();
	}
	catch (std :: bad_alloc e)
	{
		errExceptionError(e.what());
	}   
	catch (IBPP :: Exception& e)
	{
		errExceptionError( e.what());
	}
}

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

void cacheFortressClassesListClass :: DB_Remove( unsigned long ID )
{
	/* smaz vsechny uzly uzivatele */
    IBPP :: Statement& s = actx->db.getFortressClassQueryDELETE();	
    s->Set(DB_INDEX_FORTRESS_CLASS_DELETE_QUERY_WHERE, (int64_t)ID);
	try
	{
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError( e.what());
	}
}

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

void cacheFortressClassesListClass :: DB_Load( unsigned long ID )
{
	// nahraj data itemu 
	IBPP :: Statement& s = actx->db.getFortressClassQuerySELECT();

	try
	{
        s->Set(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_WHERE, (int64_t)ID);
		s->Execute();

		// jestli je to v databazi, nahraj to, jinak oznam chybu 
		if (s->Fetch()) 
		{
            fortressClassClass* f = new fortressClassClass();
			f->setID(ID);
            f->DB_Load(ID);
            data[ID] = make_pair(DATA_SYNCHRONIZED, f);
		} else
		{
			errExceptionError( "Fortress load failed - no record %u in DB", (unsigned long long)ID);
		}
	}
	catch (std :: bad_alloc e)
	{
		errExceptionError(e.what());
	}   
	catch (IBPP :: Exception& e)
	{
		errExceptionError( e.what());
	}
}

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

void fortressClassClass :: DB_Load(unsigned long ID)
{
	// predpokladame, ze dotaz je pripraveny pro cteni dat
	IBPP :: Statement& s = actx->db.getFortressClassQuerySELECT();
    setID(ID);

    try
    {
        int64_t data;
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_CLASSNAME, className_);
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_CLASSTYPE,          data);           classType_                      = (fortressesTypes)data;
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MINWEIGHT,          data);           minWeight_                      = data;
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXWEIGHT,          data);           maxWeight_                      = data;
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MINTSI,             data);           minTotalStructuralIntegrity_    = data;
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXTSI,             data);           maxTotalStructuralIntegrity_    = data;
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXLENGTH,          maxLength_);
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXWIDTH,           maxWidth_);
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXHEIGHT,          maxHeight_);
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXDML,             data);           maxDroneMissileLaunchers_       = (unsigned long)data;
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXLASERGUNS,       data);           maxLaserGuns_                   = (unsigned long)data;
        s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MAXWEAPONSTOTAL,    data);           maxWeaponsTotal_                = (unsigned long)data;
		s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_ACCPOINTSTOTAL,     data);           accomodationPointsTotal_        = (unsigned long)data;
		s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_QRATING,            qualityRating_);
		s->Get(DB_INDEX_FORTRESS_CLASS_SELECT_QUERY_MRATING,            massRating_);
    }
	catch (IBPP :: Exception& e)
	{
		errExceptionError( e.what());
	}
};

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

void fortressClassClass :: DB_Save(unsigned long ID)
{
	/* uloz data uzlu */
	IBPP :: Statement& s = actx->db.getFortressClassQueryUPDATE();

	try
	{
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_FORTRESSCLASSID,    (int64_t)ID);
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_CLASSNAME,          className_);
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_CLASSTYPE,          (int64_t)classType_);                             
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MINWEIGHT,          (int64_t)minWeight_);                             
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXWEIGHT,          (int64_t)maxWeight_);                             
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MINTSI,             (int64_t)minTotalStructuralIntegrity_);         
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXTSI,             (int64_t)maxTotalStructuralIntegrity_);        
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXLENGTH,          maxLength_);
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXWIDTH,           maxWidth_);
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXHEIGHT,          maxHeight_);
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXDML,             (int64_t)maxDroneMissileLaunchers_);                
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXLASERGUNS,       (int64_t)maxLaserGuns_);  
        s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MAXWEAPONSTOTAL,    (int64_t)maxWeaponsTotal_);                           
		s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_ACCPOINTSTOTAL,     (int64_t)accomodationPointsTotal_);                   
		s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_QRATING,            qualityRating_);
		s->Set(DB_INDEX_FORTRESS_CLASS_UPDATE_QUERY_MRATING,            massRating_);
	
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError( e.what());
	}
}

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

void fortressClassClass :: DB_Insert(unsigned long ID)
{
	/* uloz data uzlu */
	IBPP :: Statement& s = actx->db.getFortressClassQueryINSERT();

	try
	{
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_FORTRESSCLASSID,    (int64_t)ID);
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_CLASSNAME,          className_);
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_CLASSTYPE,          (int64_t)classType_);                             
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MINWEIGHT,          (int64_t)minWeight_);                             
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXWEIGHT,          (int64_t)maxWeight_);                             
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MINTSI,             (int64_t)minTotalStructuralIntegrity_);         
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXTSI,             (int64_t)maxTotalStructuralIntegrity_);        
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXLENGTH,          maxLength_);
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXWIDTH,           maxWidth_);
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXHEIGHT,          maxHeight_);
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXDML,             (int64_t)maxDroneMissileLaunchers_);                
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXLASERGUNS,       (int64_t)maxLaserGuns_); 
        s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MAXWEAPONSTOTAL,    (int64_t)maxWeaponsTotal_);                           
		s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_ACCPOINTSTOTAL,     (int64_t)accomodationPointsTotal_);                   
		s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_QRATING,            qualityRating_);
		s->Set(DB_INDEX_FORTRESS_CLASS_INSERT_QUERY_MRATING,            massRating_);
	
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError( e.what());
	}
}

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