
#include "context.h"
#include "space.h"
#include "dbconn.h"
#include "spaceindex.h"

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

/* indexy pro space objects - INSERT dotaz */
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_IDSPACEOBJECT                          1
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_PARENTOBJECT                           2
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_OBJTYPE                                3
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_IDFORTRESS                             4
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_POSX                                   5
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_POSY                                   6
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ARADIUS                                7
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ORADIUS                                8
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_WEIGHT                                 9
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_DISTANCEFROMPOBJ                      10
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_BROTANGLE                             11
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ANGULARVELOCITY                       12
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_SUNTYPE                               13
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_YEARPERIOD                            14
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_DAYPERIOD                             15
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_PLANETTYPE                            16
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_DAYTEMPERATURE                        17
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_NIGHTTEMPERATURE                      18
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_SEA                                   19
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_HWSOURCE                              20
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ZISOURCE                              21
#define DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ILSOURCE                              22

/* indexy pro space objects - SELECT dotaz */
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_IDSPACEOBJECT                          1
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_PARENTOBJECT                           2
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_OBJTYPE                                3
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_IDFORTRESS                             4
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_POSX                                   5
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_POSY                                   6
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_ARADIUS                                7
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_ORADIUS                                8
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_WEIGHT                                 9
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_DISTANCEFROMPOBJ                      10
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_BROTANGLE                             11
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_ANGULARVELOCITY                       12
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_SUNTYPE                               13
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_YEARPERIOD                            14
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_DAYPERIOD                             15
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_PLANETTYPE                            16
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_DAYTEMPERATURE                        17
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_NIGHTTEMPERATURE                      18
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_SEA                                   19
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_HWSOURCE                              20
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_ZISOURCE                              21
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_ILSOURCE                              22
#define DB_INDEX_SPACEOBJECTS_SELECT_QUERY_QUERY                                  1

/* indexy pro space objects - DELETE dotaz */
#define DB_INDEX_SPACEOBJECTS_DELETE_QUERY_IDSPACEOBJECT                          1

/* indexy pro space objects - UPDATE dotaz */
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_PARENTOBJECT                           1
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_OBJTYPE                                2
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_IDFORTRESS                             3
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_POSX                                   4
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_POSY                                   5
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ARADIUS                                6
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ORADIUS                                7
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_WEIGHT                                 8
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_DISTANCEFROMPOBJ                       9
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_BROTANGLE                             10
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ANGULARVELOCITY                       11
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_SUNTYPE                               12
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_YEARPERIOD                            13
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_DAYPERIOD                             14
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_PLANETTYPE                            15
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_DAYTEMPERATURE                        16
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_NIGHTTEMPERATURE                      17
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_SEA                                   18
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_HWSOURCE                              19
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ZISOURCE                              20
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ILSOURCE                              21
#define DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_IDSPACEOBJECT                         22


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

#define DB_SPACEOBJECTS_INITIALIZE_QUERY "SELECT IDSPACEOBJECT, OBJTYPE, POSX, POSY FROM SPACEOBJECTS ORDER BY IDSPACEOBJECT DESC"

#define DB_INDEX_SPACEOBJECTS_INITIALIZE_IDSPACEOBJECT                            1
#define DB_INDEX_SPACEOBJECTS_INITIALIZE_OBJTYPE                                  2
#define DB_INDEX_SPACEOBJECTS_INITIALIZE_POSX                                     3
#define DB_INDEX_SPACEOBJECTS_INITIALIZE_POSY                                     4

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

#define DB_INDEX_SPACEOBJECTS_SELSUB_IDSPACEOBJECT                                  1
#define DB_INDEX_SPACEOBJECTS_SELSUB_WHERE                                          1

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

void cacheSpaceObjectListClass :: DB_Initialize( void )
{
    // 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_SPACEOBJECTS_INITIALIZE_QUERY);

	try
	{
        s->Execute();

        // nacteme, jak velky potrebujeme ten list
        if (s->Fetch())
        {
            int64_t d;
            s->Get(DB_INDEX_SPACEOBJECTS_INITIALIZE_IDSPACEOBJECT, d);
            expand((unsigned long)d + 1, identifiersReusable());       
        
            // postupne nacitame polozky
            do
            {
                int64_t objectId, objtype;
                int64_t posx, posy;
                s->Get(DB_INDEX_SPACEOBJECTS_INITIALIZE_IDSPACEOBJECT,  objectId);
                s->Get(DB_INDEX_SPACEOBJECTS_INITIALIZE_OBJTYPE,        objtype);
                s->Get(DB_INDEX_SPACEOBJECTS_INITIALIZE_POSX,           posx);
                s->Get(DB_INDEX_SPACEOBJECTS_INITIALIZE_POSY,           posy);

                // nejdrive vytvorime space index objekt, pokud je to hvezda
                if (objtype == SOT_STAR)
                {
                    spaceIndexItem* sii = new spaceIndexItem((const dtIdentifier)objectId, vector_dist_t((dist_t)posx, (dist_t)posy));
                    actx->spaceObjectIndex.addStar(sii);
                }

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

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

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

void cacheSpaceObjectListClass :: DB_IternalLoadFromQuery( unsigned long ID )
{
	// nahraj data itemu 
	IBPP :: Statement& s = actx->db.getSpaceObjQuerySELECT();

	try
	{
		dataListObjectClass* obj = NULL;

		int64_t spaceObjectType_;
		s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_OBJTYPE, spaceObjectType_);

		// rozlisime, ktery druh itemu nahrat, vytvorime ten objekt a nahrajeme ho
		switch ((spaceObjectType)spaceObjectType_)
		{
        case SOT_BASE:            obj = new spaceFortressObject();                   break;
        case SOT_STAR:            obj = new spaceSunObject();                        break;
        case SOT_MOON:            obj = new spacePlanetObject();                     break;
        case SOT_PLANET:          obj = new spacePlanetObject();                     break;
		default:
			obj = NULL;
		}

		if (obj != NULL)
		{
			obj->DB_Load(ID);
			data[ID] = make_pair(DATA_SYNCHRONIZED, obj);
		}
		else
		{
			errLogicalError( "Bad pattern index in items table");
		}
	}
	catch (std :: bad_alloc e)
	{
		errExceptionError(e.what());
	}
}

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

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

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

		// jestli je to v databazi, nahraj to, jinak oznam chybu 
		if (s->Fetch()) 
		{
			DB_IternalLoadFromQuery(ID);
		} else
		{
			errExceptionError("Item load failed - no record in DB");
		}
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

void cacheSpaceObjectListClass :: DB_Remove( unsigned long ID )
{
	/* smaz space objekt */
	IBPP :: Statement& s = actx->db.getSpaceObjQueryDELETE();	
	s->Set(DB_INDEX_SPACEOBJECTS_DELETE_QUERY_IDSPACEOBJECT, (int64_t)ID);
	try
	{
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

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

	setID(ID);

	// nacteme sva data
	int64_t data;
	s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_PARENTOBJECT,         data);          parentObject_ = (dtIdentifier) data;
}

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

void spaceObject :: DB_Save(unsigned long ID)
{
	// predpokladame, ze dotaz je pripraveny pro zapis dat
	IBPP :: Statement& s = actx->db.getSpaceObjQueryUPDATE();

	setID(ID);

	// zapiseme sva data
	s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_IDSPACEOBJECT,         (int64_t) ID);            
    s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_PARENTOBJECT,          (int64_t) parentObject_);  
    s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_OBJTYPE,               (int64_t) getType());
}

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

void spaceObject :: DB_Insert(unsigned long ID)
{
	// predpokladame, ze dotaz je pripraveny pro zapis dat
	IBPP :: Statement& s = actx->db.getSpaceObjQueryINSERT();

	setID(ID);

	// zapiseme sva data
	s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_IDSPACEOBJECT,         (int64_t) ID);            
	s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_PARENTOBJECT,          (int64_t) parentObject_);    
    s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_OBJTYPE,               (int64_t) getType());
}

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

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

	// nacteme sva data
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_POSX,                 position_.x);         
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_POSY,                 position_.y);    
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_ARADIUS,              areaRadius_);         
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_ORADIUS,              objectRadius_);   
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_WEIGHT,               objectWeight_);         
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_DISTANCEFROMPOBJ,     distanceFromParentObject_);    
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_BROTANGLE,            basicRotationAngle_);         
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_ANGULARVELOCITY,      angularVelocity_);  

    // nacteme podobjekty
    IBPP :: Statement& subObjects = actx->db.getSpaceObjQuerySELSUB();
    subObjects->Set(DB_INDEX_SPACEOBJECTS_SELSUB_WHERE, (int64_t)ID);
    
    subObjects->Execute();

    subObjects_.clear();
    while(subObjects->Fetch())
    {
        int64_t data;
        subObjects->Get(DB_INDEX_SPACEOBJECTS_SELSUB_IDSPACEOBJECT, data);
        subObjects_.insert((unsigned long)data);
    }

    
    spaceObject :: DB_Load(ID);
}

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

void spaceRegularObject :: DB_Save(unsigned long ID)
{
	// predpokladame, ze dotaz je pripraveny pro zapis dat
	IBPP :: Statement& s = actx->db.getSpaceObjQueryUPDATE();

	// nacteme sva data
    s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_POSX,                 position_.x);         
    s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_POSY,                 position_.y);    
    s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ARADIUS,              areaRadius_);         
    s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ORADIUS,              objectRadius_);   
    s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_WEIGHT,               objectWeight_);         
    s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_DISTANCEFROMPOBJ,     distanceFromParentObject_);    
    s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_BROTANGLE,            basicRotationAngle_);         
    s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ANGULARVELOCITY,      angularVelocity_);   

    spaceObject :: DB_Save(ID);
}

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

void spaceRegularObject :: DB_Insert(unsigned long ID)
{
	// predpokladame, ze dotaz je pripraveny pro zapis dat
	IBPP :: Statement& s = actx->db.getSpaceObjQueryINSERT();

	// nacteme sva data
    s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_POSX,                 position_.x);         
    s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_POSY,                 position_.y);    
    s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ARADIUS,              areaRadius_);         
    s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ORADIUS,              objectRadius_);   
    s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_WEIGHT,               objectWeight_);         
    s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_DISTANCEFROMPOBJ,     distanceFromParentObject_);    
    s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_BROTANGLE,            basicRotationAngle_);         
    s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ANGULARVELOCITY,      angularVelocity_);   

    spaceObject :: DB_Insert(ID);
}

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

void spaceFortressObject :: DB_Load( unsigned long ID )
{
	/* nahraj data do patternu */
	IBPP :: Statement& s = actx->db.getSpaceObjQuerySELECT();

	int64_t data;
	s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_IDFORTRESS, data);    idFortress_  = (dtIdentifier)data;

    spaceObject :: DB_Load(ID);
}

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

void spaceFortressObject :: DB_Save(unsigned long ID)
{
	/* uloz data vesmirneho objektu */
	IBPP :: Statement& s = actx->db.getSpaceObjQueryUPDATE();

	try
	{
		s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_IDFORTRESS,          (int64_t)idFortress_);                  
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_POSX);    
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_POSY);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ARADIUS);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ORADIUS);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_WEIGHT);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_DISTANCEFROMPOBJ);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_BROTANGLE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ANGULARVELOCITY);
	    s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_SUNTYPE);    
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_YEARPERIOD);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_DAYPERIOD);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_PLANETTYPE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_DAYTEMPERATURE);
	    s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_NIGHTTEMPERATURE);    
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_SEA);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_HWSOURCE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ZISOURCE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ILSOURCE);

		spaceObject :: DB_Save(ID);
	
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

void spaceFortressObject :: DB_Insert(unsigned long ID)
{
	/* uloz data vesmirneho objektu */
	IBPP :: Statement& s = actx->db.getSpaceObjQueryINSERT();

	try
	{
		s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_IDFORTRESS,          (int64_t)idFortress_);                  
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_POSX);    
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_POSY);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ARADIUS);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ORADIUS);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_WEIGHT);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_DISTANCEFROMPOBJ);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_BROTANGLE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ANGULARVELOCITY);
	    s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_SUNTYPE);    
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_YEARPERIOD);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_DAYPERIOD);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_PLANETTYPE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_DAYTEMPERATURE);
	    s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_NIGHTTEMPERATURE);    
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_SEA);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_HWSOURCE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ZISOURCE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ILSOURCE);

		spaceObject :: DB_Insert(ID);
	
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

void spaceSunObject :: DB_Load( unsigned long ID )
{
	/* nahraj data do patternu */
	IBPP :: Statement& s = actx->db.getSpaceObjQuerySELECT();

	int64_t data;
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_SUNTYPE, data);    sunType_  = (spaceObjectSunType)data;

    spaceRegularObject :: DB_Load(ID);
}

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

void spaceSunObject :: DB_Save(unsigned long ID)
{
	/* uloz data vesmirneho objektu */
	IBPP :: Statement& s = actx->db.getSpaceObjQueryUPDATE();

	try
	{
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_IDFORTRESS);                  
        s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_SUNTYPE,               (int64_t)sunType_);    
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_YEARPERIOD);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_DAYPERIOD);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_PLANETTYPE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_DAYTEMPERATURE);
	    s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_NIGHTTEMPERATURE);    
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_SEA);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_HWSOURCE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ZISOURCE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ILSOURCE);

		spaceRegularObject :: DB_Save(ID);
	
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

void spaceSunObject :: DB_Insert(unsigned long ID)
{
	/* uloz data vesmirneho objektu */
	IBPP :: Statement& s = actx->db.getSpaceObjQueryINSERT();

	try
	{
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_IDFORTRESS);                  
        s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_SUNTYPE,               (int64_t)sunType_);       
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_YEARPERIOD);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_DAYPERIOD);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_PLANETTYPE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_DAYTEMPERATURE);
	    s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_NIGHTTEMPERATURE);    
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_SEA);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_HWSOURCE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ZISOURCE);
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ILSOURCE);

		spaceRegularObject :: DB_Insert(ID);
	
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

void spacePlanetObject :: DB_Load( unsigned long ID )
{
	/* nahraj data do patternu */
	IBPP :: Statement& s = actx->db.getSpaceObjQuerySELECT();

	int64_t data;
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_OBJTYPE,          data);    type_             = (spaceObjectType)data;
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_YEARPERIOD,       data);    yearPeriod_       = (long long)data;
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_DAYPERIOD,        data);    dayPeriod_        = (long long)data;
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_PLANETTYPE,       data);    planetType_       = (spaceObjectPlanetType)data;
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_DAYTEMPERATURE,   data);    dayTemperature_   = (long)data;
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_NIGHTTEMPERATURE, data);    nightTemperature_ = (long)data;
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_SEA,              data);    sea_              = (long)data;
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_HWSOURCE,         data);    heavyWaterSource_ = (long)data;
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_ZISOURCE,         data);    zirconiumSource_  = (long)data;
    s->Get(DB_INDEX_SPACEOBJECTS_SELECT_QUERY_ILSOURCE,         data);    ilmeniteSource_   = (long)data;

    spaceRegularObject :: DB_Load(ID);
}

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

void spacePlanetObject :: DB_Save(unsigned long ID)
{
	/* uloz data vesmirneho objektu */
	IBPP :: Statement& s = actx->db.getSpaceObjQueryUPDATE();

	try
	{
		s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_IDFORTRESS);                  
        s->SetNull(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_SUNTYPE);    
		s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_YEARPERIOD,           (int64_t)yearPeriod_);
		s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_DAYPERIOD,            (int64_t)dayPeriod_);
		s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_PLANETTYPE,           (int64_t)planetType_);
		s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_DAYTEMPERATURE,       (int64_t)dayTemperature_);
	    s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_NIGHTTEMPERATURE,     (int64_t)nightTemperature_);    
		s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_SEA,                  (int64_t)sea_);
		s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_HWSOURCE,             (int64_t)heavyWaterSource_);
		s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ZISOURCE,             (int64_t)zirconiumSource_);
		s->Set(DB_INDEX_SPACEOBJECTS_UPDATE_QUERY_ILSOURCE,             (int64_t)ilmeniteSource_);

		spaceRegularObject :: DB_Save(ID);
	
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

void spacePlanetObject :: DB_Insert(unsigned long ID)
{
	/* uloz data vesmirneho objektu */
	IBPP :: Statement& s = actx->db.getSpaceObjQueryINSERT();

	try
	{
		s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_IDFORTRESS);                  
        s->SetNull(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_SUNTYPE);    
		s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_YEARPERIOD,           (int64_t)yearPeriod_);
		s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_DAYPERIOD,            (int64_t)dayPeriod_);
		s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_PLANETTYPE,           (int64_t)planetType_);
		s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_DAYTEMPERATURE,       (int64_t)dayTemperature_);
	    s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_NIGHTTEMPERATURE,     (int64_t)nightTemperature_);    
		s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_SEA,                  (int64_t)sea_);
		s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_HWSOURCE,             (int64_t)heavyWaterSource_);
		s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ZISOURCE,             (int64_t)zirconiumSource_);
		s->Set(DB_INDEX_SPACEOBJECTS_INSERT_QUERY_ILSOURCE,             (int64_t)ilmeniteSource_);

		spaceRegularObject :: DB_Insert(ID);
	
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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




