
#include "dbconn.h"
#include "error_handling.h"
#include "science_tree.h"
#include "items_patterns.h"
#include "context.h"
#include "cache_list.h"

#include <string>
#include <utility>
#include <sstream>

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

/* dotazy */
#define SCTREE_INSERT_QUERY "INSERT INTO SCTREE (IDUSER, IDNODE, ASSIGNEDPTS, INT1, "\
	                        "ICOMP1, DCOMP1, ICOMP2, DCOMP2, ICOMP3, DCOMP3, "\
							"ICOMP4, DCOMP4, ICOMP5, DCOMP5)"\
							"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
#define SCTREE_DELETE_QUERY "DELETE FROM SCTREE WHERE IDUSER = ?"
#define SCTREE_SELECT_QUERY "SELECT a.IDUSER, a.IDNODE, a.ASSIGNEDPTS, a.INT1,  "\
	                        "a.ICOMP1, a.DCOMP1, a.ICOMP2, a.DCOMP2, a.ICOMP3, a.DCOMP3, "\
							"a.ICOMP4, a.DCOMP4, a.ICOMP5, a.DCOMP5 FROM SCTREE a "\
							"WHERE a.IDUSER = ? AND a.IDNODE = ?"
#define SCTREE_UPDATE_QUERY "UPDATE SCTREE "\
	                        "SET ASSIGNEDPTS = ?, INT1 = ?, ICOMP1 = ?, "\
							"DCOMP1 = ?, ICOMP2 = ?, DCOMP2 = ?, ICOMP3 = ?, DCOMP3 = ?, "\
							"ICOMP4 = ?, DCOMP4 = ?, ICOMP5 = ?, DCOMP5 = ? "\
							"WHERE  IDUSER = ? AND IDNODE = ?"

#define ITEMPT_SELECT_QUERY "SELECT   a.DENSITY, a.TRANSPORT, a.CONSULREI, "\
	                        "a.CONSTITANIUM, a.CONSDEUTERIUM, a.CONSHEAVYWATER, a.REPAIRABILITY, "\
							"a.MOUNTING, a.MINWEIGHT, a.MAXWEIGHT, a.ITEM_NAME FROM ITEMPATTERNS a WHERE a.IDPATTERN = ?"



#define ITEMS__INSERT_QUERY "INSERT INTO ITEMS (IDITEM, PATTERN, WEIGHT, DAMAGE, WORKING, "\
	                        "DAMAGED, D1, I1, I2, I3, I4, I5, I6, SHADOWDAMAGE, SHADOWDAMAGED, "\
							" I7, I8, I9, I10, I11, I12, I13, I14, D2, D3, D4 ) "\
                            "VALUES (?, ?, ?, ?, ?, ?, "\
							"?, ?, ?, ?, ?, ?, ?, ?, ?, "\
                            "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"
#define ITEMS__SELECT_QUERY "SELECT a.IDITEM, a.PATTERN, a.WEIGHT, a.DAMAGE, a.WORKING, "\
	                        "a.DAMAGED, a.D1, a.I1, a.I2, a.I3, a.I4, a.I5, a.I6, a.SHADOWDAMAGE, a.SHADOWDAMAGED, "\
                            " a.I7, a.I8, a.I9, a.I10, a.I11, a.I12, a.I13, a.I14, a.D2, a.D3, a.D4 "\
                            "FROM ITEMS a WHERE IDITEM = ?"
#define ITEMS__DELETE_QUERY "DELETE FROM ITEMS WHERE IDITEM = ?"
#define ITEMS__UPDATE_QUERY "UPDATE ITEMS "\
	                        "SET PATTERN = ?, WEIGHT = ?, DAMAGE = ?, WORKING = ?, "\
							"DAMAGED = ?, D1 = ?, I1 = ?, I2 = ?, I3 = ?, I4 = ?, I5 = ?, I6 = ?, SHADOWDAMAGE = ?, SHADOWDAMAGED = ?, "\
							"I7 = ?, I8 = ?, I9 = ?, I10 = ?, I11 = ?, I12 = ?, I13 = ?, I14 = ?, D2 = ?, D3 = ?, D4 = ? "\
                            "WHERE IDITEM = ?"

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

/* dotazy do tabulky FORTRESS */
#define FORTRESS_INSERT_QUERY  "INSERT INTO FORTRESSES (IDFORTRESS, IDREALM, FORTRESSNAME, WEIGHT, TSINTEGRITY, "\
                              "SINTEGRITY, SHIPLENGTH, SHIPWIDTH, SHIPHEIGHT, QGPLASMA, ULREA, "\
                              "HEAVYWATER, DEUTERIUM, ZCRYSTAL, ENERGY, ILMENITE, TITANIUM, "\
                              "QGPLASMAGRAD, ULREAGRAD, HEAVYWATERGRAD, DEUTERIUMGRAD, "\
                              "ZCRYSTALGRAD, ENERGYGRAD, ILMENITEGRAD, TITANIUMGRAD, "\
                              "UPDQGPLASMA, UPDULREA, UPDCONRES, UPDENERGY, QGPLASMATRIGGER, "\
                              "CRTRIGGER, ENERGYTRIGGER, TARGET, LTAIMTIME, "\
                              \
                              "ENE_ACCUMULATOR, ENE_ESC_ENERGY, ENE_ESC_TENERGY, "\
                              \
                              "DEF_SHIELDGENERATOR, DEF_ESC1_ENERGY, DEF_ESC1_TENERGY, DEF_EDC_PRIORITY, "\
                              "DEF_EDC_RATIO, DEF_EDC_MPD, DEF_EDC_SDOEL, "\
                              \
                              "WEP_ENERGYCIRCUIT, WEP_DRONEMISSILES, WEP_EDC_PRIORITY, WEP_EDC_RATIO, "\
                              "WEP_EDC_MPD, WEP_EDC_SDOEL, "\
                              \
                              "TBM_GENERATOR, TBM_ESC_ENERGY, TBM_ESC_TENERGY, TBM_EDC_PRIORITY, "\
                              "TBM_EDC_RATIO, TBM_EDC_MPD, TBM_EDC_SDOEL, "\
                              \
                              "MIN_MAGCOLLECTORS, MIN_FILTRATIONTANK, MIN_MININGMACHINE, MIN_DRILLINGMACHINE, "\
                              "MIN_QGQUARRYING, MIN_DEQUARRYING, MIN_ZCQUARRYING, MIN_ILQUARRYING, "\
                              \
                              "SEN_HIRESSENSORS, SEN_LONGRANGESENSORS, SEN_PSC1_STATE, SEN_PSC1_CHARGING, "\
                              "SEN_EDC1_PRIORITY, SEN_EDC1_RATIO, SEN_EDC1_MPD, SEN_EDC1_SDOEL, "\
                              "SEN_PSC2_STATE, SEN_PSC2_CHARGING, SEN_EDC2_PRIORITY, SEN_EDC2_RATIO, "\
                              "SEN_EDC2_MPD, SEN_EDC2_SDOEL, "\
                              \
                              "CLO_CLOAKINGDEVICE, CLO_PSC_STATE, CLO_PSC_CHARGING, CLO_EDC_PRIORITY, "\
                              "CLO_EDC_RATIO, CLO_EDC_MPD, CLO_EDC_SDOEL, "\
                              \
                              "CRE_CREW_CAPITAN, CRE_CREW_DOCTOR, CRE_CREW_OFFICER, CRE_CREW_ENGINEER, "\
                              "CRE_CREW_PILOT, CRE_CREW_SCIENTIST, CRE_CREW_PASSENGER, "\
                              \
                              "CRE_WOUNDED_CAPITAN, CRE_WOUNDED_DOCTOR, CRE_WOUNDED_OFFICER, "\
                              "CRE_WOUNDED_ENGINEER, CRE_WOUNDED_PILOT, CRE_WOUNDED_SCIENTIST, "\
                              "CRE_WOUNDED_PASSENGER, "\
                              \
                              "ACC_LUXURYAPARTMENTS, ACC_COMFORTABLEROOMS, ACC_COMMONCABINS, "\
                              "ACC_WAREHOUSES, "\
                              \
                              "MEC_ANDROIDS, MEC_PSC_STATE, MEC_PSC_CHARGING, MEC_EDC_PRIORITY, "\
                              "MEC_EDC_RATIO, MEC_EDC_MPD, MEC_EDC_SDOEL, "\
                              \
                              "WAR_MAGNETICALGRID, WAR_CONTAINER, WAR_ENERGETICCELL, "\
                              \
                              "OTH_PLASMAFILTER, OTH_LABS, OTH_LIFESUPPORT, OTH_TRANSMITTER, "\
                              "OTH_EDC_PRIORITY, OTH_EDC_RATIO, OTH_EDC_MPD, OTH_EDC_SDOEL, "\
                              \
                              "MAI_MAINFRAME, MAI_EDC_PRIORITY, MAI_EDC_RATIO, MAI_EDC_MPD, "\
                              "MAI_EDC_SDOEL, "\
                              \
                              "SUB_COMPUTER, SUB_QGCORE, SUB_BEACON, SUB_DAMPERS, "\
                              \
                              "IMP_JETENGINE, IMP_MECHDAMPERS, "\
                              \
                              "FAC_MAGNETICALFILTER, FAC_DEUTERIUMDECOMP, FAC_ZIRCONIUMREACTOR, "\
                              "FAC_ILMENITEFURNACE, FORTRESSTYPE, \n"\
                              "IMP_ACTUALFORCE, POSITIONX, POSITIONY, COURSEX, COURSEY, SPEEDNORM, "\
                              "ACCELERATION, SPOBJACTUALLYIN, NEARESTSTAR, WESPOBJ, MOV_STATE, "\
                              "MOV_WARP_TARGET_X, MOV_WARP_TARGET_Y, MOV_WARP_START, ENGINE_LIMIT, "\
                              "TARGETSPACEOBJECT, TARGETSHIP, TARGETPOSITIONX, TARGETPOSITIONY, "\
                              "LASTACTUALIZATION, SHIPMOVEACTION, SHIPMOVETARGET, FORTRESSCLASSCLASS, SUB_ACTUALSPEEDTOC, GRAVSPEEDNORM, "\
                              "MEC_MAX_WORK_ANDR, MEC_MAX_REPA_ANDR, MEC_ACT_REPA_ANDR, MEC_ACT_CONS_ANDR, STRUCTINTGRAD,"\
                              "FS_VOI_ANDR_PRIORITY, FS_VOI_ANDR_RATIO, FS_VOI_ANDR_ASSIGNED_DROIDS, FS_ENE_ANDR_PRIORITY, FS_ENE_ANDR_RATIO, FS_ENE_ANDR_ASSIGNED_DROIDS,"\
                              "FS_DEF_ANDR_PRIORITY, FS_DEF_ANDR_RATIO, FS_DEF_ANDR_ASSIGNED_DROIDS, FS_WEP_ANDR_PRIORITY, FS_WEP_ANDR_RATIO, FS_WEP_ANDR_ASSIGNED_DROIDS,"\
                              "FS_TBM_ANDR_PRIORITY, FS_TBM_ANDR_RATIO, FS_TBM_ANDR_ASSIGNED_DROIDS, FS_MIN_ANDR_PRIORITY, FS_MIN_ANDR_RATIO, FS_MIN_ANDR_ASSIGNED_DROIDS,"\
                              "FS_PRO_ANDR_PRIORITY, FS_PRO_ANDR_RATIO, FS_PRO_ANDR_ASSIGNED_DROIDS, FS_SEN_ANDR_PRIORITY, FS_SEN_ANDR_RATIO, FS_SEN_ANDR_ASSIGNED_DROIDS,"\
                              "FS_CLO_ANDR_PRIORITY, FS_CLO_ANDR_RATIO, FS_CLO_ANDR_ASSIGNED_DROIDS, FS_CRE_ANDR_PRIORITY, FS_CRE_ANDR_RATIO, FS_CRE_ANDR_ASSIGNED_DROIDS,"\
                              "FS_ACC_ANDR_PRIORITY, FS_ACC_ANDR_RATIO, FS_ACC_ANDR_ASSIGNED_DROIDS, FS_MEC_ANDR_PRIORITY, FS_MEC_ANDR_RATIO, FS_MEC_ANDR_ASSIGNED_DROIDS,"\
                              "FS_WAR_ANDR_PRIORITY, FS_WAR_ANDR_RATIO, FS_WAR_ANDR_ASSIGNED_DROIDS, FS_OTH_ANDR_PRIORITY, FS_OTH_ANDR_RATIO, FS_OTH_ANDR_ASSIGNED_DROIDS,"\
                              "FS_MAI_ANDR_PRIORITY, FS_MAI_ANDR_RATIO, FS_MAI_ANDR_ASSIGNED_DROIDS, FS_SUB_ANDR_PRIORITY, FS_SUB_ANDR_RATIO, FS_SUB_ANDR_ASSIGNED_DROIDS,"\
                              "FS_IMP_ANDR_PRIORITY, FS_IMP_ANDR_RATIO, FS_IMP_ANDR_ASSIGNED_DROIDS, FS_HAN_ANDR_PRIORITY, FS_HAN_ANDR_RATIO, FS_HAN_ANDR_ASSIGNED_DROIDS,"\
                              "FS_FAC_ANDR_PRIORITY, FS_FAC_ANDR_RATIO, FS_FAC_ANDR_ASSIGNED_DROIDS, CONTROLLABLE, BUILDINGPTS )"\
                              " VALUES (?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, "\
                              "?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, "\
                              \
                              "?, ?, ?, " /* ENE */ \
                              "?, ?, ?, ?, ?, ?, ?, " /* DEF */ \
                              "?, ?, ?, ?, ?, ?, " /* WEP */ \
                              "?, ?, ?, ?, ?, ?, ?, " /* TBM */ \
                              "?, ?, ?, ?, ?, ?, ?, ?, " /* MIN */ \
                              "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " /* SEN */ \
                              "?, ?, ?, ?, ?, ?, ?, " /* CLO */ \
                              "?, ?, ?, ?, ?, ?, ?, " /* CRE */ \
                              "?, ?, ?, ?, ?, ?, ?, " /* CRE WOUNDED */ \
                              "?, ?, ?, ?, " /* ACC */ \
                              "?, ?, ?, ?, ?, ?, ?, " /* MEC */ \
                              "?, ?, ?, " /* WAR */ \
                              "?, ?, ?, ?, ?, ?, ?, ?, " /* OTH */ \
                              "?, ?, ?, ?, ?, " /* MAI */ \
                              "?, ?, ?, ?, " /* SUB */ \
                              "?, ?, " /* IMP */ \
                              "?, ?, ?, ?, " /* FAC */ \
                              "?, " /* FORTRESSTYPE */ \
                              "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?, ?, "\
                              "?, ?, ?, ?, ?)" 
                        
#define FORTRESS_SELECT_QUERY "SELECT IDFORTRESS, IDREALM, FORTRESSNAME, WEIGHT, TSINTEGRITY, "\
                              "SINTEGRITY, SHIPLENGTH, SHIPWIDTH, SHIPHEIGHT, QGPLASMA, ULREA, "\
                              "HEAVYWATER, DEUTERIUM, ZCRYSTAL, ENERGY, ILMENITE, TITANIUM, "\
                              "QGPLASMAGRAD, ULREAGRAD, HEAVYWATERGRAD, DEUTERIUMGRAD, "\
                              "ZCRYSTALGRAD, ENERGYGRAD, ILMENITEGRAD, TITANIUMGRAD, "\
                              "UPDQGPLASMA, UPDULREA, UPDCONRES, UPDENERGY, QGPLASMATRIGGER, "\
                              "CRTRIGGER, ENERGYTRIGGER, TARGET, LTAIMTIME, "\
                              \
                              "ENE_ACCUMULATOR, ENE_ESC_ENERGY, ENE_ESC_TENERGY, "\
                              \
                              "DEF_SHIELDGENERATOR, DEF_ESC1_ENERGY, DEF_ESC1_TENERGY, DEF_EDC_PRIORITY, "\
                              "DEF_EDC_RATIO, DEF_EDC_MPD, DEF_EDC_SDOEL, "\
                              \
                              "WEP_ENERGYCIRCUIT, WEP_DRONEMISSILES, WEP_EDC_PRIORITY, WEP_EDC_RATIO, "\
                              "WEP_EDC_MPD, WEP_EDC_SDOEL, "\
                              \
                              "TBM_GENERATOR, TBM_ESC_ENERGY, TBM_ESC_TENERGY, TBM_EDC_PRIORITY, "\
                              "TBM_EDC_RATIO, TBM_EDC_MPD, TBM_EDC_SDOEL, "\
                              \
                              "MIN_MAGCOLLECTORS, MIN_FILTRATIONTANK, MIN_MININGMACHINE, MIN_DRILLINGMACHINE, "\
                              "MIN_QGQUARRYING, MIN_DEQUARRYING, MIN_ZCQUARRYING, MIN_ILQUARRYING, "\
                              \
                              "SEN_HIRESSENSORS, SEN_LONGRANGESENSORS, SEN_PSC1_STATE, SEN_PSC1_CHARGING, "\
                              "SEN_EDC1_PRIORITY, SEN_EDC1_RATIO, SEN_EDC1_MPD, SEN_EDC1_SDOEL, "\
                              "SEN_PSC2_STATE, SEN_PSC2_CHARGING, SEN_EDC2_PRIORITY, SEN_EDC2_RATIO, "\
                              "SEN_EDC2_MPD, SEN_EDC2_SDOEL, "\
                              \
                              "CLO_CLOAKINGDEVICE, CLO_PSC_STATE, CLO_PSC_CHARGING, CLO_EDC_PRIORITY, "\
                              "CLO_EDC_RATIO, CLO_EDC_MPD, CLO_EDC_SDOEL, "\
                              \
                              "CRE_CREW_CAPITAN, CRE_CREW_DOCTOR, CRE_CREW_OFFICER, CRE_CREW_ENGINEER, "\
                              "CRE_CREW_PILOT, CRE_CREW_SCIENTIST, CRE_CREW_PASSENGER, "\
                              \
                              "CRE_WOUNDED_CAPITAN, CRE_WOUNDED_DOCTOR, CRE_WOUNDED_OFFICER, "\
                              "CRE_WOUNDED_ENGINEER, CRE_WOUNDED_PILOT, CRE_WOUNDED_SCIENTIST, "\
                              "CRE_WOUNDED_PASSENGER, "\
                              \
                              "ACC_LUXURYAPARTMENTS, ACC_COMFORTABLEROOMS, ACC_COMMONCABINS, "\
                              "ACC_WAREHOUSES, "\
                              \
                              "MEC_ANDROIDS, MEC_PSC_STATE, MEC_PSC_CHARGING, MEC_EDC_PRIORITY, "\
                              "MEC_EDC_RATIO, MEC_EDC_MPD, MEC_EDC_SDOEL, "\
                              \
                              "WAR_MAGNETICALGRID, WAR_CONTAINER, WAR_ENERGETICCELL, "\
                              \
                              "OTH_PLASMAFILTER, OTH_LABS, OTH_LIFESUPPORT, OTH_TRANSMITTER, "\
                              "OTH_EDC_PRIORITY, OTH_EDC_RATIO, OTH_EDC_MPD, OTH_EDC_SDOEL, "\
                              \
                              "MAI_MAINFRAME, MAI_EDC_PRIORITY, MAI_EDC_RATIO, MAI_EDC_MPD, "\
                              "MAI_EDC_SDOEL, "\
                              \
                              "SUB_COMPUTER, SUB_QGCORE, SUB_BEACON, SUB_DAMPERS, "\
                              \
                              "IMP_JETENGINE, IMP_MECHDAMPERS, "\
                              \
                              "FAC_MAGNETICALFILTER, FAC_DEUTERIUMDECOMP, FAC_ZIRCONIUMREACTOR, "\
                              "FAC_ILMENITEFURNACE, FORTRESSTYPE, \n"\
                              "IMP_ACTUALFORCE, POSITIONX, POSITIONY, COURSEX, COURSEY, SPEEDNORM, "\
                              "ACCELERATION, SPOBJACTUALLYIN, NEARESTSTAR, WESPOBJ, MOV_STATE, "\
                              "MOV_WARP_TARGET_X, MOV_WARP_TARGET_Y, MOV_WARP_START, ENGINE_LIMIT, "\
                              "TARGETSPACEOBJECT, TARGETSHIP, TARGETPOSITIONX, TARGETPOSITIONY, "\
                              "LASTACTUALIZATION, SHIPMOVEACTION, SHIPMOVETARGET, FORTRESSCLASSCLASS, SUB_ACTUALSPEEDTOC, GRAVSPEEDNORM, "\
                              "MEC_MAX_WORK_ANDR, MEC_MAX_REPA_ANDR, MEC_ACT_REPA_ANDR, MEC_ACT_CONS_ANDR, STRUCTINTGRAD, "\
                              "FS_VOI_ANDR_PRIORITY, FS_VOI_ANDR_RATIO, FS_VOI_ANDR_ASSIGNED_DROIDS, FS_ENE_ANDR_PRIORITY, FS_ENE_ANDR_RATIO, FS_ENE_ANDR_ASSIGNED_DROIDS,"\
                              "FS_DEF_ANDR_PRIORITY, FS_DEF_ANDR_RATIO, FS_DEF_ANDR_ASSIGNED_DROIDS, FS_WEP_ANDR_PRIORITY, FS_WEP_ANDR_RATIO, FS_WEP_ANDR_ASSIGNED_DROIDS,"\
                              "FS_TBM_ANDR_PRIORITY, FS_TBM_ANDR_RATIO, FS_TBM_ANDR_ASSIGNED_DROIDS, FS_MIN_ANDR_PRIORITY, FS_MIN_ANDR_RATIO, FS_MIN_ANDR_ASSIGNED_DROIDS,"\
                              "FS_PRO_ANDR_PRIORITY, FS_PRO_ANDR_RATIO, FS_PRO_ANDR_ASSIGNED_DROIDS, FS_SEN_ANDR_PRIORITY, FS_SEN_ANDR_RATIO, FS_SEN_ANDR_ASSIGNED_DROIDS,"\
                              "FS_CLO_ANDR_PRIORITY, FS_CLO_ANDR_RATIO, FS_CLO_ANDR_ASSIGNED_DROIDS, FS_CRE_ANDR_PRIORITY, FS_CRE_ANDR_RATIO, FS_CRE_ANDR_ASSIGNED_DROIDS,"\
                              "FS_ACC_ANDR_PRIORITY, FS_ACC_ANDR_RATIO, FS_ACC_ANDR_ASSIGNED_DROIDS, FS_MEC_ANDR_PRIORITY, FS_MEC_ANDR_RATIO, FS_MEC_ANDR_ASSIGNED_DROIDS,"\
                              "FS_WAR_ANDR_PRIORITY, FS_WAR_ANDR_RATIO, FS_WAR_ANDR_ASSIGNED_DROIDS, FS_OTH_ANDR_PRIORITY, FS_OTH_ANDR_RATIO, FS_OTH_ANDR_ASSIGNED_DROIDS,"\
                              "FS_MAI_ANDR_PRIORITY, FS_MAI_ANDR_RATIO, FS_MAI_ANDR_ASSIGNED_DROIDS, FS_SUB_ANDR_PRIORITY, FS_SUB_ANDR_RATIO, FS_SUB_ANDR_ASSIGNED_DROIDS,"\
                              "FS_IMP_ANDR_PRIORITY, FS_IMP_ANDR_RATIO, FS_IMP_ANDR_ASSIGNED_DROIDS, FS_HAN_ANDR_PRIORITY, FS_HAN_ANDR_RATIO, FS_HAN_ANDR_ASSIGNED_DROIDS,"\
                              "FS_FAC_ANDR_PRIORITY, FS_FAC_ANDR_RATIO, FS_FAC_ANDR_ASSIGNED_DROIDS, CONTROLLABLE, BUILDINGPTS "\
                              " FROM FORTRESSES WHERE IDFORTRESS = ? "

#define FORTRESS_DELETE_QUERY "DELETE FROM FORTRESSES WHERE IDFORTRESS = ? "

#define FORTRESS_UPDATE_QUERY "UPDATE FORTRESSES SET IDREALM = ?, FORTRESSNAME = ?, WEIGHT = ?, TSINTEGRITY = ?, "\
                              "SINTEGRITY = ?, SHIPLENGTH = ?, SHIPWIDTH = ?, SHIPHEIGHT = ?, QGPLASMA = ?, ULREA = ?, "\
                              "HEAVYWATER = ?, DEUTERIUM = ?, ZCRYSTAL = ?, ENERGY = ?, ILMENITE = ?, TITANIUM = ?, "\
                              "QGPLASMAGRAD = ?, ULREAGRAD = ?, HEAVYWATERGRAD = ?, DEUTERIUMGRAD = ?, "\
                              "ZCRYSTALGRAD = ?, ENERGYGRAD = ?, ILMENITEGRAD = ?, TITANIUMGRAD = ?, "\
                              "UPDQGPLASMA = ?, UPDULREA = ?, UPDCONRES = ?, UPDENERGY = ?, QGPLASMATRIGGER = ?, "\
                              "CRTRIGGER = ?, ENERGYTRIGGER = ?, TARGET = ?, LTAIMTIME = ?, "\
                              \
                              "ENE_ACCUMULATOR = ?, ENE_ESC_ENERGY = ?, ENE_ESC_TENERGY = ?, "\
                              \
                              "DEF_SHIELDGENERATOR = ?, DEF_ESC1_ENERGY = ?, DEF_ESC1_TENERGY = ?, DEF_EDC_PRIORITY = ?, "\
                              "DEF_EDC_RATIO = ?, DEF_EDC_MPD = ?, DEF_EDC_SDOEL = ?, "\
                              \
                              "WEP_ENERGYCIRCUIT = ?, WEP_DRONEMISSILES = ?, WEP_EDC_PRIORITY = ?, WEP_EDC_RATIO = ?, "\
                              "WEP_EDC_MPD = ?, WEP_EDC_SDOEL = ?, "\
                              \
                              "TBM_GENERATOR = ?, TBM_ESC_ENERGY = ?, TBM_ESC_TENERGY = ?, TBM_EDC_PRIORITY = ?, "\
                              "TBM_EDC_RATIO = ?, TBM_EDC_MPD = ?, TBM_EDC_SDOEL = ?, "\
                              \
                              "MIN_MAGCOLLECTORS = ?, MIN_FILTRATIONTANK = ?, MIN_MININGMACHINE = ?, MIN_DRILLINGMACHINE = ?, "\
                              "MIN_QGQUARRYING = ?, MIN_DEQUARRYING = ?, MIN_ZCQUARRYING = ?, MIN_ILQUARRYING = ?, "\
                              \
                              "SEN_HIRESSENSORS = ?, SEN_LONGRANGESENSORS = ?, SEN_PSC1_STATE = ?, SEN_PSC1_CHARGING = ?, "\
                              "SEN_EDC1_PRIORITY = ?, SEN_EDC1_RATIO = ?, SEN_EDC1_MPD = ?, SEN_EDC1_SDOEL = ?, "\
                              "SEN_PSC2_STATE = ?, SEN_PSC2_CHARGING = ?, SEN_EDC2_PRIORITY = ?, SEN_EDC2_RATIO = ?, "\
                              "SEN_EDC2_MPD = ?, SEN_EDC2_SDOEL = ?, "\
                              \
                              "CLO_CLOAKINGDEVICE = ?, CLO_PSC_STATE = ?, CLO_PSC_CHARGING = ?, CLO_EDC_PRIORITY = ?, "\
                              "CLO_EDC_RATIO = ?, CLO_EDC_MPD = ?, CLO_EDC_SDOEL = ?, "\
                              \
                              "CRE_CREW_CAPITAN = ?, CRE_CREW_DOCTOR = ?, CRE_CREW_OFFICER = ?, CRE_CREW_ENGINEER = ?, "\
                              "CRE_CREW_PILOT = ?, CRE_CREW_SCIENTIST = ?, CRE_CREW_PASSENGER = ?, "\
                              \
                              "CRE_WOUNDED_CAPITAN = ?, CRE_WOUNDED_DOCTOR = ?, CRE_WOUNDED_OFFICER = ?, "\
                              "CRE_WOUNDED_ENGINEER = ?, CRE_WOUNDED_PILOT = ?, CRE_WOUNDED_SCIENTIST = ?, "\
                              "CRE_WOUNDED_PASSENGER = ?, "\
                              \
                              "ACC_LUXURYAPARTMENTS = ?, ACC_COMFORTABLEROOMS = ?, ACC_COMMONCABINS = ?, "\
                              "ACC_WAREHOUSES = ?, "\
                              \
                              "MEC_ANDROIDS = ?, MEC_PSC_STATE = ?, MEC_PSC_CHARGING = ?, MEC_EDC_PRIORITY = ?, "\
                              "MEC_EDC_RATIO = ?, MEC_EDC_MPD = ?, MEC_EDC_SDOEL = ?, "\
                              \
                              "WAR_MAGNETICALGRID = ?, WAR_CONTAINER = ?, WAR_ENERGETICCELL = ?, "\
                              \
                              "OTH_PLASMAFILTER = ?, OTH_LABS = ?, OTH_LIFESUPPORT = ?, OTH_TRANSMITTER = ?, "\
                              "OTH_EDC_PRIORITY = ?, OTH_EDC_RATIO = ?, OTH_EDC_MPD = ?, OTH_EDC_SDOEL = ?, "\
                              \
                              "MAI_MAINFRAME = ?, MAI_EDC_PRIORITY = ?, MAI_EDC_RATIO = ?, MAI_EDC_MPD = ?, "\
                              "MAI_EDC_SDOEL = ?, "\
                              \
                              "SUB_COMPUTER = ?, SUB_QGCORE = ?, SUB_BEACON = ?, SUB_DAMPERS = ?, "\
                              \
                              "IMP_JETENGINE = ?, IMP_MECHDAMPERS = ?, "\
                              \
                              "FAC_MAGNETICALFILTER = ?, FAC_DEUTERIUMDECOMP = ?, FAC_ZIRCONIUMREACTOR = ?, "\
                              "FAC_ILMENITEFURNACE = ?, FORTRESSTYPE = ?, \n"\
                              "IMP_ACTUALFORCE = ?, POSITIONX = ?, POSITIONY = ?, COURSEX = ?, COURSEY = ?, SPEEDNORM = ?, "\
                              "ACCELERATION = ?, SPOBJACTUALLYIN = ?, NEARESTSTAR = ?, WESPOBJ = ?, MOV_STATE = ?, "\
                              "MOV_WARP_TARGET_X = ?, MOV_WARP_TARGET_Y = ?, MOV_WARP_START = ?, ENGINE_LIMIT = ?, "\
                              "TARGETSPACEOBJECT = ?, TARGETSHIP = ?, TARGETPOSITIONX = ?, TARGETPOSITIONY = ?, "\
                              "LASTACTUALIZATION = ?, SHIPMOVEACTION = ?, SHIPMOVETARGET = ?, FORTRESSCLASSCLASS = ?, SUB_ACTUALSPEEDTOC = ?, GRAVSPEEDNORM = ?, "\
                              "MEC_MAX_WORK_ANDR = ?, MEC_MAX_REPA_ANDR = ?, MEC_ACT_REPA_ANDR = ?, MEC_ACT_CONS_ANDR = ?, STRUCTINTGRAD = ?, "\
                              "FS_VOI_ANDR_PRIORITY = ?, FS_VOI_ANDR_RATIO = ?, FS_VOI_ANDR_ASSIGNED_DROIDS = ?, FS_ENE_ANDR_PRIORITY = ?, FS_ENE_ANDR_RATIO = ?, FS_ENE_ANDR_ASSIGNED_DROIDS = ?,"\
                              "FS_DEF_ANDR_PRIORITY = ?, FS_DEF_ANDR_RATIO = ?, FS_DEF_ANDR_ASSIGNED_DROIDS = ?, FS_WEP_ANDR_PRIORITY = ?, FS_WEP_ANDR_RATIO = ?, FS_WEP_ANDR_ASSIGNED_DROIDS = ?,"\
                              "FS_TBM_ANDR_PRIORITY = ?, FS_TBM_ANDR_RATIO = ?, FS_TBM_ANDR_ASSIGNED_DROIDS = ?, FS_MIN_ANDR_PRIORITY = ?, FS_MIN_ANDR_RATIO = ?, FS_MIN_ANDR_ASSIGNED_DROIDS = ?,"\
                              "FS_PRO_ANDR_PRIORITY = ?, FS_PRO_ANDR_RATIO = ?, FS_PRO_ANDR_ASSIGNED_DROIDS = ?, FS_SEN_ANDR_PRIORITY = ?, FS_SEN_ANDR_RATIO = ?, FS_SEN_ANDR_ASSIGNED_DROIDS = ?,"\
                              "FS_CLO_ANDR_PRIORITY = ?, FS_CLO_ANDR_RATIO = ?, FS_CLO_ANDR_ASSIGNED_DROIDS = ?, FS_CRE_ANDR_PRIORITY = ?, FS_CRE_ANDR_RATIO = ?, FS_CRE_ANDR_ASSIGNED_DROIDS = ?,"\
                              "FS_ACC_ANDR_PRIORITY = ?, FS_ACC_ANDR_RATIO = ?, FS_ACC_ANDR_ASSIGNED_DROIDS = ?, FS_MEC_ANDR_PRIORITY = ?, FS_MEC_ANDR_RATIO = ?, FS_MEC_ANDR_ASSIGNED_DROIDS = ?,"\
                              "FS_WAR_ANDR_PRIORITY = ?, FS_WAR_ANDR_RATIO = ?, FS_WAR_ANDR_ASSIGNED_DROIDS = ?, FS_OTH_ANDR_PRIORITY = ?, FS_OTH_ANDR_RATIO = ?, FS_OTH_ANDR_ASSIGNED_DROIDS = ?,"\
                              "FS_MAI_ANDR_PRIORITY = ?, FS_MAI_ANDR_RATIO = ?, FS_MAI_ANDR_ASSIGNED_DROIDS = ?, FS_SUB_ANDR_PRIORITY = ?, FS_SUB_ANDR_RATIO = ?, FS_SUB_ANDR_ASSIGNED_DROIDS = ?,"\
                              "FS_IMP_ANDR_PRIORITY = ?, FS_IMP_ANDR_RATIO = ?, FS_IMP_ANDR_ASSIGNED_DROIDS = ?, FS_HAN_ANDR_PRIORITY = ?, FS_HAN_ANDR_RATIO = ?, FS_HAN_ANDR_ASSIGNED_DROIDS = ?,"\
                              "FS_FAC_ANDR_PRIORITY = ?, FS_FAC_ANDR_RATIO = ?, FS_FAC_ANDR_ASSIGNED_DROIDS = ?, CONTROLLABLE = ?, BUILDINGPTS = ? "\
                              " WHERE IDFORTRESS = ? "\

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

/* dotazy do tabulky space objects */
#define SPACEOBJECTS_INSERT_QUERY "INSERT INTO SPACEOBJECTS (IDSPACEOBJECT, PARENTOBJECT, OBJTYPE,\n"\
                                  "IDFORTRESS, POSX, POSY, ARADIUS, ORADIUS, WEIGHT, \n"\
                                  "DISTANCEFROMPOBJ, BROTANGLE, ANGULARVELOCITY, SUNTYPE, \n"\
                                  "YEARPERIOD, DAYPERIOD, PLANETTYPE, DAYTEMPERATURE, \n"\
                                  "NIGHTTEMPERATURE, SEA, HWSOURCE, ZISOURCE, ILSOURCE)\n"\
                                  " VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"

#define SPACEOBJECTS_SELECT_QUERY "SELECT a.IDSPACEOBJECT, a.PARENTOBJECT, a.OBJTYPE, a.IDFORTRESS, \n"\
                                  "a.POSX, a.POSY, a.ARADIUS, a.ORADIUS, a.WEIGHT, a.DISTANCEFROMPOBJ, \n"\
                                  "a.BROTANGLE, a.ANGULARVELOCITY, a.SUNTYPE, a.YEARPERIOD, a.DAYPERIOD, \n"\
                                  "a.PLANETTYPE, a.DAYTEMPERATURE, a.NIGHTTEMPERATURE, a.SEA, a.HWSOURCE, \n"\
                                  "a.ZISOURCE, a.ILSOURCE FROM SPACEOBJECTS a WHERE a.IDSPACEOBJECT = ?"

#define SPACEOBJECTS_SELSUB_QUERY "SELECT IDSPACEOBJECT FROM SPACEOBJECTS WHERE PARENTOBJECT = ?"

#define SPACEOBJECTS_DELETE_QUERY "DELETE FROM SPACEOBJECTS WHERE IDSPACEOBJECT = ? "

#define SPACEOBJECTS_UPDATE_QUERY "UPDATE SPACEOBJECTS SET PARENTOBJECT = ?, OBJTYPE = ?, IDFORTRESS = ?, \n"\
                                  "POSX = ?, POSY = ?, ARADIUS = ?, ORADIUS = ?, WEIGHT = ?, DISTANCEFROMPOBJ = ?, \n"\
                                  "BROTANGLE = ?, ANGULARVELOCITY = ?, SUNTYPE = ?, YEARPERIOD = ?, DAYPERIOD = ?, \n"\
                                  "PLANETTYPE = ?, DAYTEMPERATURE = ?, NIGHTTEMPERATURE = ?, SEA = ?, \n"\
                                  "HWSOURCE = ?, ZISOURCE = ?, ILSOURCE = ? WHERE IDSPACEOBJECT = ? \n"

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

#define FL_SELECT_QUERY(table)    "SELECT IDITEMOFLIST \nFROM " table "\nWHERE IDFORTRESS = ? ORDER BY IDITEMOFLIST ASC" 
#define FL_DELETE_QUERY(table)    "DELETE              \nFROM " table " \nWHERE IDFORTRESS = ? " 
#define FL_INSERT_QUERY(table)    "INSERT INTO " table "(IDFORTRESS, IDITEMOFLIST)\n VALUES (?, ?) " 

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

#define FLES_SELECT_QUERY(table)  "SELECT IDITEMOFLIST, ENERGY, TARGETENERGY \nFROM " table "\n WHERE IDFORTRESS = ? "\
                                  "\nORDER BY IDITEMOFLIST ASC\n"
#define FLES_DELETE_QUERY(table)  "DELETE              \nFROM " table " \n WHERE IDFORTRESS = ? "
#define FLES_INSERT_QUERY(table)  "INSERT INTO " table "(IDFORTRESS, IDITEMOFLIST, ENERGY, TARGETENERGY)\n"\
                                  "VALUES (?, ?, ?, ?)"

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

#define FORTRESSCLASS_INSERT_QUERY  "INSERT INTO FORTRESSCLASSES(FORTRESSCLASSID, CLASSNAME, CLASSTYPE,\n"\
                                    "MINWEIGHT, MAXWEIGHT, MINTSI, MAXTSI, MAXLENGTH, MAXWIDTH, MAXHEIGHT,\n"\
                                    "MAXDML, MAXLASERGUNS, MAXWEAPONSTOTAL, ACCPOINTSTOTAL, QRATING, MRATING) \n"\
                                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
#define FORTRESSCLASS_DELETE_QUERY  "DELETE FROM FORTRESSCLASSES WHERE FORTRESSCLASSID = ?"
#define FORTRESSCLASS_SELECT_QUERY  "SELECT a.FORTRESSCLASSID, a.CLASSNAME, a.CLASSTYPE, a.MINWEIGHT, \n"\
                                    "a.MAXWEIGHT, a.MINTSI, a.MAXTSI, a.MAXLENGTH, a.MAXWIDTH, a.MAXHEIGHT, \n"\
                                    "a.MAXDML, a.MAXLASERGUNS, a.MAXWEAPONSTOTAL, a.ACCPOINTSTOTAL, a.QRATING, a.MRATING\n "\
                                    "FROM FORTRESSCLASSES a WHERE FORTRESSCLASSID = ?"
#define FORTRESSCLASS_UPDATE_QUERY  "UPDATE FORTRESSCLASSES SET CLASSNAME = ?, CLASSTYPE = ?, MINWEIGHT = ?, \n"\
                                    "MAXWEIGHT = ?, MINTSI = ?, MAXTSI = ?, MAXLENGTH = ?, MAXWIDTH = ?, \n"\
                                    "MAXHEIGHT = ?, MAXDML = ?, MAXLASERGUNS = ?, \n"\
                                    "MAXWEAPONSTOTAL = ?, ACCPOINTSTOTAL = ?, QRATING = ?, MRATING = ?\n "\
                                    "WHERE FORTRESSCLASSID = ?\n"


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

#define PLAYERREALMS_INSERT_QUERY   "INSERT INTO PLAYERS(IDREALM, PLNAME, PLLOGIN, PLPASSWORD, HASBAN, BANLEVEL, BANDURATION, BANREASON, BANTIME)\n"\
                                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
#define PLAYERREALMS_DELETE_QUERY   "DELETE FROM PLAYERS WHERE IDREALM = ?"
#define PLAYERREALMS_SELECT_QUERY   "SELECT IDREALM, PLNAME, PLLOGIN, PLPASSWORD, HASBAN, BANLEVEL, BANDURATION, BANREASON, BANTIME \n"\
                                    "FROM PLAYERS  \n WHERE IDREALM = ?\n"
#define PLAYERREALMS_LOGIN_QUERY    "SELECT IDREALM, PLNAME, PLLOGIN, PLPASSWORD \n"\
                                    "FROM PLAYERS \n WHERE PLLOGIN = ?\n"
#define PLAYERREALMS_UPDATE_QUERY   "UPDATE PLAYERS SET PLNAME = ?, PLLOGIN = ?, PLPASSWORD = ?, HASBAN = ?, BANLEVEL = ?, BANDURATION = ?, BANREASON = ?, BANTIME = ? \n"\
                                    "WHERE IDREALM = ?"
#define PLAYERREALMS_BSLIST_QUERY   "SELECT IDFORTRESS, FORTRESSTYPE, CONTROLLABLE FROM FORTRESSES WHERE IDREALM = ?"


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

#define PL_SELECT_QUERY(table)    "SELECT IDITEMOFLIST \nFROM " table "\nWHERE IDREALM = ? ORDER BY IDITEMOFLIST ASC" 
#define PL_DELETE_QUERY(table)    "DELETE              \nFROM " table " \nWHERE IDREALM = ? " 
#define PL_INSERT_QUERY(table)    "INSERT INTO " table "(IDREALM, IDITEMOFLIST)\n VALUES (?, ?) " 

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

void dbFortressList :: prepare(const std :: string& sqlSELECT, const std :: string& sqlDELETE, 
        const std :: string& sqlINSERT, IBPP :: Database& db, IBPP :: Transaction& tr)
{
    fListSELECT_ = IBPP :: StatementFactory(db, tr);
    fListSELECT_->Prepare(sqlSELECT);

    fListDELETE_ = IBPP :: StatementFactory(db, tr);
    fListDELETE_->Prepare(sqlDELETE);

    fListINSERT_ = IBPP :: StatementFactory(db, tr);
    fListINSERT_->Prepare(sqlINSERT);
}

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

void dbFortressList :: close()
{
    fListSELECT_->Close();
    fListDELETE_->Close();
    fListINSERT_->Close();
}

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

void dbRealmList :: prepare(const std :: string& sqlSELECT, const std :: string& sqlDELETE, 
        const std :: string& sqlINSERT, IBPP :: Database& db, IBPP :: Transaction& tr)
{
    fListSELECT_ = IBPP :: StatementFactory(db, tr);
    fListSELECT_->Prepare(sqlSELECT);

    fListDELETE_ = IBPP :: StatementFactory(db, tr);
    fListDELETE_->Prepare(sqlDELETE);

    fListINSERT_ = IBPP :: StatementFactory(db, tr);
    fListINSERT_->Prepare(sqlINSERT);
}

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

void dbRealmList :: close()
{
    fListSELECT_->Close();
    fListDELETE_->Close();
    fListINSERT_->Close();
}

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

bool dbClient :: connect(const std :: string& server, const std :: string& database, 
		const std :: string& username, const std :: string& password)

{
	/* pripoj se k databazi */
	try
	{
		db_ = IBPP :: DatabaseFactory(server, database, username, password);
		db_->Connect();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
        errExceptionError("Failed to connect to %S:%S with username %S", server, database, username);
		return false;
	}

	/* nastartuj transakci */
	try
	{
		tr_ = IBPP :: TransactionFactory(db_, IBPP :: amWrite, IBPP :: ilReadDirty, IBPP :: lrWait);
		tr_->Start();
	}
	catch (IBPP :: Exception& e)
	{
		disconnect();
		errExceptionError(e.what());
		return false;
	}

	/* priprav dotazy */
	try
	{
		/* SCIENCE TREE DOTAZY */
		scTreeQueryINSERT_ = IBPP :: StatementFactory(db_, tr_);
		scTreeQueryINSERT_->Prepare(SCTREE_INSERT_QUERY);

		scTreeQueryDELETE_ = IBPP :: StatementFactory(db_, tr_);
		scTreeQueryDELETE_->Prepare(SCTREE_DELETE_QUERY);

		scTreeQuerySELECT_ = IBPP :: StatementFactory(db_, tr_);
		scTreeQuerySELECT_->Prepare(SCTREE_SELECT_QUERY);

		scTreeQueryUPDATE_ = IBPP :: StatementFactory(db_, tr_);
		scTreeQueryUPDATE_->Prepare(SCTREE_UPDATE_QUERY);

		/* ITEM PATTERNS DOTAZY */
		itemPatternQuerySELECT_ = IBPP :: StatementFactory(db_, tr_);
		itemPatternQuerySELECT_->Prepare(ITEMPT_SELECT_QUERY);

		/* ITEMS DOTAZY */
		itemsQueryINSERT_ = IBPP :: StatementFactory(db_, tr_);
		itemsQueryINSERT_->Prepare(ITEMS__INSERT_QUERY);

		itemsQueryDELETE_ = IBPP :: StatementFactory(db_, tr_);
		itemsQueryDELETE_->Prepare(ITEMS__DELETE_QUERY);

		itemsQuerySELECT_ = IBPP :: StatementFactory(db_, tr_);
		itemsQuerySELECT_->Prepare(ITEMS__SELECT_QUERY);

		itemsQueryUPDATE_ = IBPP :: StatementFactory(db_, tr_);
		itemsQueryUPDATE_->Prepare(ITEMS__UPDATE_QUERY);

        /* FORTRESS DOTAZY */
        fortressQueryINSERT_ = IBPP :: StatementFactory(db_, tr_);
        fortressQueryINSERT_->Prepare(FORTRESS_INSERT_QUERY);

        fortressQueryDELETE_ = IBPP :: StatementFactory(db_, tr_);
        fortressQueryDELETE_->Prepare(FORTRESS_DELETE_QUERY);

        fortressQuerySELECT_ = IBPP :: StatementFactory(db_, tr_);
        fortressQuerySELECT_->Prepare(FORTRESS_SELECT_QUERY);

        fortressQueryUPDATE_ = IBPP :: StatementFactory(db_, tr_);
        fortressQueryUPDATE_->Prepare(FORTRESS_UPDATE_QUERY);

        /* SPACEOBJECTS DOTAZY */
        spaceObjQueryINSERT_ = IBPP :: StatementFactory(db_, tr_);
        spaceObjQueryINSERT_->Prepare(SPACEOBJECTS_INSERT_QUERY);

        spaceObjQueryDELETE_ = IBPP :: StatementFactory(db_, tr_);
        spaceObjQueryDELETE_->Prepare(SPACEOBJECTS_DELETE_QUERY);

        spaceObjQuerySELECT_ = IBPP :: StatementFactory(db_, tr_);
        spaceObjQuerySELECT_->Prepare(SPACEOBJECTS_SELECT_QUERY);

        spaceObjQueryUPDATE_ = IBPP :: StatementFactory(db_, tr_);
        spaceObjQueryUPDATE_->Prepare(SPACEOBJECTS_UPDATE_QUERY);

        spaceObjQuerySELSUB_ = IBPP :: StatementFactory(db_, tr_);
        spaceObjQuerySELSUB_->Prepare(SPACEOBJECTS_SELSUB_QUERY);

        /* FORTRESS CLASSES DOTAZY */
        fortressClassQueryINSERT_ = IBPP :: StatementFactory(db_, tr_);
        fortressClassQueryINSERT_->Prepare(FORTRESSCLASS_INSERT_QUERY);

        fortressClassQueryDELETE_ = IBPP :: StatementFactory(db_, tr_);
        fortressClassQueryDELETE_->Prepare(FORTRESSCLASS_DELETE_QUERY);

        fortressClassQuerySELECT_ = IBPP :: StatementFactory(db_, tr_);
        fortressClassQuerySELECT_->Prepare(FORTRESSCLASS_SELECT_QUERY);

        fortressClassQueryUPDATE_ = IBPP :: StatementFactory(db_, tr_);
        fortressClassQueryUPDATE_->Prepare(FORTRESSCLASS_UPDATE_QUERY);

        /* PLAYER REALMS DOTAZY */
        playerRealmQueryINSERT_ = IBPP :: StatementFactory(db_, tr_);
        playerRealmQueryINSERT_->Prepare(PLAYERREALMS_INSERT_QUERY);

        playerRealmQueryDELETE_ = IBPP :: StatementFactory(db_, tr_);
        playerRealmQueryDELETE_->Prepare(PLAYERREALMS_DELETE_QUERY);

        playerRealmQuerySELECT_ = IBPP :: StatementFactory(db_, tr_);
        playerRealmQuerySELECT_->Prepare(PLAYERREALMS_SELECT_QUERY);

        playerRealmQueryUPDATE_ = IBPP :: StatementFactory(db_, tr_);
        playerRealmQueryUPDATE_->Prepare(PLAYERREALMS_UPDATE_QUERY);

        playerRealmQueryLOGIN_ = IBPP :: StatementFactory(db_, tr_);
        playerRealmQueryLOGIN_->Prepare(PLAYERREALMS_LOGIN_QUERY);

        playerRealmQueryBSLIST_ = IBPP :: StatementFactory(db_, tr_);
        playerRealmQueryBSLIST_->Prepare(PLAYERREALMS_BSLIST_QUERY);

        playerRealmDS_.prepare(PL_SELECT_QUERY("DISCOVERED_STARS"), PL_DELETE_QUERY("DISCOVERED_STARS"),
            PL_INSERT_QUERY("DISCOVERED_STARS"), db_, tr_);

        /* priprava fortress list dotazu */
        F_HAN_HANGARS_.prepare(FL_SELECT_QUERY("F_HAN_HANGARS"), FL_DELETE_QUERY("F_HAN_HANGARS"),
            FL_INSERT_QUERY("F_HAN_HANGARS"), db_, tr_);
        F_PRO_PROBES_.prepare(FL_SELECT_QUERY("F_PRO_PROBES"), FL_DELETE_QUERY("F_PRO_PROBES"),
            FL_INSERT_QUERY("F_PRO_PROBES"), db_, tr_);
        F_WAR_STORAGE_.prepare(FL_SELECT_QUERY("F_WAR_STORAGE"), FL_DELETE_QUERY("F_WAR_STORAGE"),
            FL_INSERT_QUERY("F_WAR_STORAGE"), db_, tr_);
        F_WEP_DRONEMISSILELAUNCHERS_.prepare(FLES_SELECT_QUERY("F_WEP_DRONEMISSILELAUNCHERS"),
            FLES_DELETE_QUERY("F_WEP_DRONEMISSILELAUNCHERS"), FLES_INSERT_QUERY("F_WEP_DRONEMISSILELAUNCHERS"),
            db_, tr_);
        F_WEP_LASERGUNS_.prepare(FLES_SELECT_QUERY("F_WEP_LASERGUNS"),
            FLES_DELETE_QUERY("F_WEP_LASERGUNS"), FLES_INSERT_QUERY("F_WEP_LASERGUNS"),
            db_, tr_);
	}
	catch (IBPP :: Exception& e)
	{
		disconnect();
		errExceptionError(e.what());
		return false;
	}

	return true;
}

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

void dbClient :: commit(void)
{
	try
	{
		tr_->CommitRetain();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

void dbClient :: disconnect(void)
{
	/* napred commituj databazi */
	try
	{
		tr_->Commit();

        // odpreparuj dotazy

	    /* Scientific tree */
	    scTreeQueryINSERT_->Close();
	    scTreeQueryDELETE_->Close();
	    scTreeQuerySELECT_->Close();
	    scTreeQueryUPDATE_->Close();

	    /* Item patterns */
	    itemPatternQuerySELECT_->Close();

	    /* Items */
	    itemsQueryINSERT_->Close();
	    itemsQueryDELETE_->Close();
	    itemsQuerySELECT_->Close();
	    itemsQueryUPDATE_->Close();

        /* Fortress */
        fortressQueryINSERT_->Close();
        fortressQueryDELETE_->Close();
        fortressQuerySELECT_->Close();
        fortressQueryUPDATE_->Close();

        /* Space objects */
        spaceObjQueryINSERT_->Close();
        spaceObjQueryDELETE_->Close();
        spaceObjQuerySELECT_->Close();
        spaceObjQueryUPDATE_->Close();
        spaceObjQuerySELSUB_->Close();

        /* Fortress classes */
        fortressClassQueryINSERT_->Close();
        fortressClassQueryDELETE_->Close();
        fortressClassQuerySELECT_->Close();
        fortressClassQueryUPDATE_->Close();

        /* Player realms */
        playerRealmQueryINSERT_->Close();
        playerRealmQueryDELETE_->Close();
        playerRealmQuerySELECT_->Close();
        playerRealmQueryUPDATE_->Close();
        playerRealmQueryLOGIN_->Close();
        playerRealmQueryBSLIST_->Close();

        playerRealmDS_.close();

        /* fortress listy */
        F_HAN_HANGARS_.close();
        F_PRO_PROBES_.close();
        F_WAR_STORAGE_.close();
        F_WEP_DRONEMISSILELAUNCHERS_.close();
        F_WEP_LASERGUNS_.close();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}

	/* potom zkus uzavrit pripojeni */
	try
	{
		db_->Disconnect();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

/* SCIENTIFIC TREE db operace */

#define DB_SCTREE_IDUSER_IDX                      1
#define DB_SCTREE_IDNODE_IDX                      2
#define DB_SCTREE_ASSIGNEDPTS_IDX                 3
#define DB_SCTREE_INTEGER1_IDX                    4
#define DB_SCTREE_ICOMP1_STARTINDEX               5
#define DB_SCTREE_DCOMP1_STARTINDEX               6
#define DB_SCTREE_STEP 2
#define DB_SCTREE_END_IDX                        14

#define DB_SCTREE_SAVE_IDUSER_IDX                     13
#define DB_SCTREE_SAVE_IDNODE_IDX                     14
#define DB_SCTREE_SAVE_ASSIGNEDPTS_IDX                 1
#define DB_SCTREE_SAVE_INTEGER1_IDX                    2
#define DB_SCTREE_SAVE_ICOMP1_STARTINDEX               3
#define DB_SCTREE_SAVE_DCOMP1_STARTINDEX               4
#define DB_SCTREE_SAVE_STEP 2
#define DB_SCTREE_SAVE_END_IDX                        14

#define DB_SCTREE_SAVE_TREE_EMPTY_START               (DB_SCTREE_SAVE_ASSIGNEDPTS_IDX + 1)
#define DB_SCTREE_SAVE_TREE_EMPTY_END                 (DB_SCTREE_SAVE_IDUSER_IDX - 1)


void scientificTree :: DBInsert(dtIdentifier userId)
{
	/* uloz data uzlu */
	IBPP :: Statement& s = actx->db.getScTreeQueryINSERT();

	try
	{
		s->Set(DB_SCTREE_IDUSER_IDX,           (int64_t)userId);
		s->Set(DB_SCTREE_IDNODE_IDX,           (int64_t)0);
		s->Set(DB_SCTREE_ASSIGNEDPTS_IDX,      (int64_t)freeTokens_);
		for (int i = DB_SCTREE_ASSIGNEDPTS_IDX + 1; i <= DB_SCTREE_END_IDX; i++)
		{
			s->SetNull(i);
		}
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}

	/* uloz uzly */
	scienceTotal_->DBInsert(userId);
}

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

void scientificTree :: DBSave(dtIdentifier userId)
{
	/* uloz data uzlu */
	IBPP :: Statement& s = actx->db.getScTreeQueryUPDATE();

	try
	{
		s->Set(DB_SCTREE_SAVE_IDUSER_IDX,           (int64_t)userId);
		s->Set(DB_SCTREE_SAVE_IDNODE_IDX,           (int64_t)0);
		s->Set(DB_SCTREE_SAVE_ASSIGNEDPTS_IDX,      (int64_t)freeTokens_);
		for (int i = DB_SCTREE_SAVE_TREE_EMPTY_START; i <= DB_SCTREE_SAVE_TREE_EMPTY_END; i++)
		{
			s->SetNull(i);
		}
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}

	/* uloz uzly */
	scienceTotal_->DBSave(userId);
}

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

#define DB_SCTREE_LOAD_IDUSER        1
#define DB_SCTREE_LOAD_IDNODE        2
#define DB_SCTREE_LOAD_ASSIGNEDPTS   3

void scientificTree :: DBLoad(dtIdentifier userId)
{
	/* nahraj data do scientific tree */
	IBPP :: Statement& s = actx->db.getScTreeQuerySELECT();

	try
	{
		s->Set(DB_SCTREE_LOAD_IDUSER, (int64_t)userId );
		s->Set(DB_SCTREE_LOAD_IDNODE, (int64_t)0      );
		s->Execute();

		/* jestli je to v databazi, nahraj to, jinak oznam chybu */
		if (s->Fetch()) 
		{
			int64_t data;
			s->Get(DB_SCTREE_LOAD_ASSIGNEDPTS, data);          freeTokens_ = (unsigned short)data;
		} else
		{
			errExceptionError( "Science tree load failed - no record in DB");
		}
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}

	/* nahraj vsechny uzly */
	scienceTotal_->DBLoad(userId);
}

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

#define DB_SCTREE_DELETE_ID 1

void scientificTree :: DBDelete(dtIdentifier userId)
{
	/* smaz vsechny uzly uzivatele */
	IBPP :: Statement& s = actx->db.getScTreeQueryDELETE();	
	s->Set(DB_SCTREE_DELETE_ID, (int64_t)userId);
	try
	{
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

#define DB_SCTREE_ARRAY_MAX_SIZE 5

void treeLeaf :: DBInsert(dtIdentifier userId)
{
	/* uloz data uzlu */
	IBPP :: Statement& s = actx->db.getScTreeQueryINSERT();

	try
	{
		s->Set(DB_SCTREE_IDUSER_IDX,              (int64_t)userId                     );
		s->Set(DB_SCTREE_IDNODE_IDX,              (int64_t)nodeIndex_                 );
    	s->Set(DB_SCTREE_ASSIGNEDPTS_IDX,         (int64_t)assignedPts_               );
		s->Set(DB_SCTREE_INTEGER1_IDX,            (int64_t)freePtsInNode_             );

		/* priprav pole na ulozeni - vynuluj ho */
		pair<unsigned short, double> vData[DB_SCTREE_ARRAY_MAX_SIZE];
		for (unsigned int i = 0; i < DB_SCTREE_ARRAY_MAX_SIZE; i++)
		{
			vData[i] = std :: pair<unsigned short, double>(0, 0.0);
		}

		/* premisti data do pole */
		for (unsigned int i = 0; i < itemComponents_.size(); i++)
		{
			vData[i] = itemComponents_[i];
		}

		/* naloz toto pole do databaze */
		for (unsigned int i = 0; i < DB_SCTREE_ARRAY_MAX_SIZE; i++)
		{
			s->Set(DB_SCTREE_ICOMP1_STARTINDEX + i * DB_SCTREE_STEP, vData[i].first);
			s->Set(DB_SCTREE_DCOMP1_STARTINDEX + i * DB_SCTREE_STEP, vData[i].second);
		}

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

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

void treeLeaf :: DBSave(dtIdentifier userId)
{
	/* uloz data uzlu */
	IBPP :: Statement& s = actx->db.getScTreeQueryUPDATE();

	try
	{
		s->Set(DB_SCTREE_SAVE_IDUSER_IDX,              (int64_t)userId                     );
		s->Set(DB_SCTREE_SAVE_IDNODE_IDX,              (int64_t)nodeIndex_                 );
    	s->Set(DB_SCTREE_SAVE_ASSIGNEDPTS_IDX,         (int64_t)assignedPts_               );
		s->Set(DB_SCTREE_SAVE_INTEGER1_IDX,            (int64_t)freePtsInNode_             );

		/* priprav pole na ulozeni - vynuluj ho */
		pair<unsigned short, double> vData[DB_SCTREE_ARRAY_MAX_SIZE];
		for (unsigned int i = 0; i < DB_SCTREE_ARRAY_MAX_SIZE; i++)
		{
			vData[i] = std :: pair<unsigned short, double>(0, 0.0);
		}

		/* premisti data do pole */
		for (unsigned int i = 0; i < itemComponents_.size(); i++)
		{
			vData[i] = itemComponents_[i];
		}

		/* naloz toto pole do databaze */
		for (unsigned int i = 0; i < DB_SCTREE_ARRAY_MAX_SIZE; i++)
		{
			s->Set(DB_SCTREE_SAVE_ICOMP1_STARTINDEX + i * DB_SCTREE_SAVE_STEP, vData[i].first);
			s->Set(DB_SCTREE_SAVE_DCOMP1_STARTINDEX + i * DB_SCTREE_SAVE_STEP, vData[i].second);
		}

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

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

#define DB_SCTREE_LOAD_IDUSER_IDX                      1
#define DB_SCTREE_LOAD_IDNODE_IDX                      2
#define DB_SCTREE_LOAD_ASSIGNEDPTS_IDX                 3
#define DB_SCTREE_LOAD_INTEGER1_IDX                    4
#define DB_SCTREE_LOAD_ICOMP1_STARTINDEX               5
#define DB_SCTREE_LOAD_DCOMP1_STARTINDEX               6
#define DB_SCTREE_LOAD_STEP 2
#define DB_SCTREE_LOAD_END_IDX                        14

void treeLeaf :: DBLoad(dtIdentifier userId)
{
	/* nahraj data do scientific tree */
	IBPP :: Statement& s = actx->db.getScTreeQuerySELECT();

	try
	{
		s->Set(DB_SCTREE_LOAD_IDUSER, (int64_t)userId          );
		s->Set(DB_SCTREE_LOAD_IDNODE, (int64_t)nodeIndex_      );
		s->Execute();

		/* jestli je to v databazi, nahraj to, jinak oznam chybu */
		if (s->Fetch()) 
		{
			int64_t data;
			s->Get(DB_SCTREE_LOAD_ASSIGNEDPTS,                      data  ); assignedPts_   = (unsigned short)data;
			s->Get(DB_SCTREE_LOAD_INTEGER1_IDX,                     data  ); freePtsInNode_ = (unsigned short)data;

			/* priprav pole na nacteni */
			pair<unsigned short, double> vData[DB_SCTREE_ARRAY_MAX_SIZE];

			/* nacti pole z databaze */
			for (unsigned int i = 0; i < DB_SCTREE_ARRAY_MAX_SIZE; i++)
			{
				int64_t f;
				s->Get(DB_SCTREE_LOAD_ICOMP1_STARTINDEX + i * DB_SCTREE_LOAD_STEP,               f); vData[i].first = (unsigned short)f;
				s->Get(DB_SCTREE_LOAD_DCOMP1_STARTINDEX + i * DB_SCTREE_LOAD_STEP, vData[i].second);
			}

			/* premisti data z pole */
			for (unsigned int i = 0; i < itemComponents_.size(); i++)
			{
				itemComponents_[i] = vData[i];
			}
		} else
		{
			errExceptionError("Science tree node load failed - no record in DB");
		}
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

void treeInnerNode :: DBInsert(dtIdentifier userId)
{
	/* uloz data potomku */
	for (treeNodePVector :: iterator iter = descendants_.begin(); iter != descendants_.end(); iter++)
	{
		(*iter)->DBInsert(userId);
	}

	/* uloz data uzlu */
	IBPP :: Statement& s = actx->db.getScTreeQueryINSERT();

	try
	{
		s->Set(DB_SCTREE_IDUSER_IDX,              (int64_t)userId                     );
		s->Set(DB_SCTREE_IDNODE_IDX,              (int64_t)nodeIndex_                 );
    	s->Set(DB_SCTREE_ASSIGNEDPTS_IDX,         (int64_t)assignedPts_               );

		for (int i = DB_SCTREE_ASSIGNEDPTS_IDX + 1; i <= DB_SCTREE_END_IDX; i++)
		{
			s->SetNull(i);
		}
	
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

void treeInnerNode :: DBSave(dtIdentifier userId)
{
	/* uloz data potomku */
	for (treeNodePVector :: iterator iter = descendants_.begin(); iter != descendants_.end(); iter++)
	{
		(*iter)->DBSave(userId);
	}

	/* uloz data uzlu */
	IBPP :: Statement& s = actx->db.getScTreeQueryUPDATE();

	try
	{
		s->Set(DB_SCTREE_SAVE_IDUSER_IDX,              (int64_t)userId                     );
		s->Set(DB_SCTREE_SAVE_IDNODE_IDX,              (int64_t)nodeIndex_                 );
    	s->Set(DB_SCTREE_SAVE_ASSIGNEDPTS_IDX,         (int64_t)assignedPts_               );

		for (int i = DB_SCTREE_SAVE_TREE_EMPTY_START; i <= DB_SCTREE_SAVE_TREE_EMPTY_END; i++)
		{
			s->SetNull(i);
		}
	
		s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

void treeInnerNode :: DBLoad(dtIdentifier userId)
{
    /* nahraj potomky z databaze */
	for (treeNodePVector :: iterator iter = descendants_.begin(); iter != descendants_.end(); iter++)
	{
		(*iter)->DBLoad(userId);
	}

	/* nahraj data do scientific tree */
	IBPP :: Statement& s = actx->db.getScTreeQuerySELECT();

	try
	{
		s->Set(DB_SCTREE_LOAD_IDUSER, (int64_t)userId          );
		s->Set(DB_SCTREE_LOAD_IDNODE, (int64_t)nodeIndex_      );
		s->Execute();

		/* jestli je to v databazi, nahraj to, jinak oznam chybu */
		if (s->Fetch()) 
		{
			int64_t data;
			s->Get(DB_SCTREE_LOAD_ASSIGNEDPTS, data);          assignedPts_ = (unsigned short)data;
		} else
		{
			errExceptionError("Science tree node load failed - no record in DB");
		}
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

/* ITEM PATTERNS db operace */

#define DB_ITEM_PATTERN_DENSITY_IDX                     1
#define DB_ITEM_PATTERN_TRANSPORT_IDX                   2
#define DB_ITEM_PATTERN_CONSULREI_IDX                   3
#define DB_ITEM_PATTERN_CONSTITANIUM_IDX                4
#define DB_ITEM_PATTERN_CONSDEUTERIUM_IDX               5
#define DB_ITEM_PATTERN_CONSHEAVYWATER_IDX              6
#define DB_ITEM_PATTERN_REPAIRABILITY_IDX               7
#define DB_ITEM_PATTERN_MOUNTING_IDX                    8
#define DB_ITEM_PATTERN_MINWEIGHT_IDX                   9
#define DB_ITEM_PATTERN_MAXWEIGHT_IDX                  10
#define DB_ITEM_PATTERN_ITEM_NAME_IDX                  11

#define DB_ITEM_PATTERN_ID_IDX                          1

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

#define DB_ITEM_PATTERN_INITIALIZE_QUERY "SELECT IDPATTERN FROM ITEMPATTERNS ORDER BY IDPATTERN DESC"

#define DB_INDEX_ITEM_PATTERN_INITIALIZE_IDPATTERN                           1


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

void cacheItemsPatternsListClass :: 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_ITEM_PATTERN_INITIALIZE_QUERY);

	try
	{
        s->Execute();

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

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

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

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

void cacheItemsPatternsListClass :: DB_Load( unsigned long ID )
{
	try {
	    itemsPatternClass *newItem = new itemsPatternClass();
	    newItem->DB_Load(ID);
    	data[ID] = make_pair(DATA_SYNCHRONIZED, newItem);
	}
	catch (std :: bad_alloc a)
	{
        errExceptionError("Not enough memory to load pattern");
	}
}

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

void itemsPatternClass :: DB_Load( unsigned long ID )
{
	setID(ID);

	/* nahraj data do patternu */
    IBPP :: Statement& s = actx->db.getItemPatternQuerySELECT();

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

		/* jestli je to v databazi, nahraj to, jinak oznam chybu */
		if (s->Fetch()) 
		{
			int64_t data;
			s->Get(DB_ITEM_PATTERN_DENSITY_IDX, data);                density_                  = (unsigned long)data;
			s->Get(DB_ITEM_PATTERN_TRANSPORT_IDX, data);              transport_                =                data;
			s->Get(DB_ITEM_PATTERN_CONSULREI_IDX, data);              constructionUlrei_        =                data;
			s->Get(DB_ITEM_PATTERN_CONSTITANIUM_IDX, data);           constructionTitanium_     =                data;
			s->Get(DB_ITEM_PATTERN_CONSDEUTERIUM_IDX, data);          constructionDeuterium_    =                data;
			s->Get(DB_ITEM_PATTERN_CONSHEAVYWATER_IDX, data);         constructionHeavyWater_   =                data;
			s->Get(DB_ITEM_PATTERN_REPAIRABILITY_IDX, data);          repairability_            = (unsigned long)data;
			s->Get(DB_ITEM_PATTERN_MOUNTING_IDX, data);               mounting_                 = (unsigned long)data;
            s->Get(DB_ITEM_PATTERN_MINWEIGHT_IDX, data);              minWeight_                = (unsigned long)data;
            s->Get(DB_ITEM_PATTERN_MAXWEIGHT_IDX, data);              maxWeight_                = (unsigned long)data;
            s->Get(DB_ITEM_PATTERN_ITEM_NAME_IDX, itemName_);
		} else
		{
			errExceptionError("Item pattern load failed - no record in DB");
		}
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

const std :: string makeSettingsFmt(const settings st)
{
    std :: stringstream ss;
    ss << "SELECT a.DOUBLEITEM, a.INTEGERITEM " <<
        "  FROM SETTINGS a " <<
        " WHERE a.IDENTIFICATOR = " << (int)st;
    return ss.str();
}

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

const std :: string makeSettingsWriteDoubleFmt(const settings st)
{
    std :: stringstream ss;

    ss << "UPDATE SETTINGS SET DOUBLEITEM = ? WHERE IDENTIFICATOR = " << (int) st;

    return ss.str();
}

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

const std :: string makeSettingsWriteIntegerFmt(const settings st)
{
    std :: stringstream ss;

    ss << "UPDATE SETTINGS SET INTEGERITEM = ? WHERE IDENTIFICATOR = " << (int) st;

    return ss.str();
}

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

#define DB_SETTINGS_LOAD_INDEX_DOUBLE           1
#define DB_SETTINGS_LOAD_INDEX_INTEGER          2

#define DB_SETTINGS_SAVE_INDEX_DOUBLE           1
#define DB_SETTINGS_SAVE_INDEX_INTEGER          1

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

double dbClient :: getDoubleSettings(const settings st)
{
    double data = 0;

	/* nahraj data z nastaveni */
	try
	{
	    IBPP :: Statement s = IBPP :: StatementFactory(db_, tr_);
        s->Execute(makeSettingsFmt(st));

		/* jestli je to v databazi, nahraj to, jinak oznam chybu */
		if (s->Fetch()) 
		{        
            s->Get(DB_SETTINGS_LOAD_INDEX_DOUBLE, data);
		} else
		{
            errExceptionError("Settings {double, %u} loading failed!", (unsigned long long) st);
		}
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}

    return data;
}

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

signed long long dbClient :: getIntegerSettings(const settings st)
{
    signed long long data = 0;

	/* nahraj data z nastaveni */
	try
	{
	    IBPP :: Statement s = IBPP :: StatementFactory(db_, tr_);
        s->Execute(makeSettingsFmt(st));

		/* jestli je to v databazi, nahraj to, jinak oznam chybu */
		if (s->Fetch()) 
		{        
            s->Get(DB_SETTINGS_LOAD_INDEX_INTEGER, data);
		} else
		{
            errExceptionError("Settings {double, %u} loading failed!", (unsigned long long) st);
		}
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}

    return data;
}

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

void dbClient :: setDoubleSettings(const settings st, double value)
{
    try
    {
	    IBPP :: Statement s = IBPP :: StatementFactory(db_, tr_);
        s->Prepare(makeSettingsWriteDoubleFmt(st));
        s->Set(DB_SETTINGS_SAVE_INDEX_DOUBLE, value);
        s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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

void dbClient :: setIntegerSettings(const settings st, signed long long value)
{
    try
    {
	    IBPP :: Statement s = IBPP :: StatementFactory(db_, tr_);
        s->Prepare(makeSettingsWriteIntegerFmt(st));
        s->Set(DB_SETTINGS_SAVE_INDEX_INTEGER, value);
        s->Execute();
	}
	catch (IBPP :: Exception& e)
	{
		errExceptionError(e.what());
	}
}

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