void ScheduledScriptsTable::reload() {
#ifdef DataConnect_DEBUG
    std::cout << "ScheduledScriptsTable: reload" << std::endl;
#endif

    try {
        Database::SelectQuery query;
        query.addColumn("scheduledscripts", "sc_scriptname");
        query.addColumn("scheduledscripts", "sc_mincycletime");
        query.addColumn("scheduledscripts", "sc_maxcycletime");
        query.addColumn("scheduledscripts", "sc_functionname");
        query.addServerTable("scheduledscripts");

        Database::Result results = query.execute();

        if (!results.empty()) {
            clearOldTable();
            ScriptData tmpRecord;

            for (Database::ResultConstIterator itr = results.begin();
                 itr != results.end(); ++itr) {
                tmpRecord.minCycleTime = (uint32_t)((*itr)["sc_mincycletime"].as<uint32_t>());
                tmpRecord.maxCycleTime = (uint32_t)((*itr)["sc_maxcycletime"].as<uint32_t>());
                tmpRecord.nextCycleTime = 0;

                if (!((*itr)["sc_scriptname"].is_null()) && !((*itr)["sc_functionname"].is_null())) {
                    tmpRecord.functionName = ((*itr)["sc_functionname"].as<std::string>());
                    tmpRecord.scriptName = ((*itr)["sc_scriptname"].as<std::string>());

                    try {
                        boost::shared_ptr<LuaScheduledScript> tmpScript(new LuaScheduledScript(tmpRecord.scriptName));
                        tmpRecord.scriptptr = tmpScript;
                        addData(tmpRecord);
                    } catch (ScriptException &e) {
                        Logger::writeError("scripts", "Error while loading scheduled script: " + tmpRecord.scriptName + ":\n" + e.what() + "\n");
                    }
                }
            }
        }

        m_dataOk = true;
    } catch (std::exception &e) {
        std::cerr << "exception: " << e.what() << std::endl;
        m_dataOk = false;
    }
}
void ScheduledScriptsTable::reload() {
    try {
        Database::SelectQuery query;
        query.addColumn("scheduledscripts", "sc_scriptname");
        query.addColumn("scheduledscripts", "sc_mincycletime");
        query.addColumn("scheduledscripts", "sc_maxcycletime");
        query.addColumn("scheduledscripts", "sc_functionname");
        query.addServerTable("scheduledscripts");

        Database::Result results = query.execute();

        if (!results.empty()) {
            clearOldTable();
            ScriptData tmpRecord;

            for (const auto &row : results) {
                tmpRecord.minCycleTime = row["sc_mincycletime"].as<uint32_t>();
                tmpRecord.maxCycleTime = row["sc_maxcycletime"].as<uint32_t>();
                tmpRecord.nextCycleTime = 0;

                if (!row["sc_scriptname"].is_null() && !row["sc_functionname"].is_null()) {
                    tmpRecord.functionName = row["sc_functionname"].as<std::string>();
                    tmpRecord.scriptName = row["sc_scriptname"].as<std::string>();

                    try {
                        std::shared_ptr<LuaScheduledScript> tmpScript = std::make_shared<LuaScheduledScript>(tmpRecord.scriptName);
                        tmpRecord.scriptptr = tmpScript;
                        addData(tmpRecord);
                    } catch (const ScriptException &e) {
                        Logger::error(LogFacility::Script) << "Error while loading scheduled script: " << tmpRecord.scriptName << ": " << e.what() << Log::end;
                    }
                }
            }
        }

        m_dataOk = true;
    } catch (std::exception &e) {
        Logger::error(LogFacility::Other) << "Exception in ScheduledScriptsTable::reload: " << e.what() << Log::end;
        m_dataOk = false;
    }
}
void NamesObjectTable::reload() {
#ifdef DataConnect_DEBUG
    std::cout << "NamesObjectTable: reload" << std::endl;
#endif

    try {
        Database::SelectQuery query;
        query.addColumn("itemname", "itn_itemid");
        query.addColumn("itemname", "itn_german");
        query.addColumn("itemname", "itn_english");
        query.addServerTable("itemname");

        Database::Result results = query.execute();

        if (!results.empty()) {
            clearOldTable();
            NamesStruct temprecord;

            for (Database::ResultConstIterator itr = results.begin();
                 itr != results.end(); ++itr) {
                temprecord.German = (TYPE_OF_GERMAN)((*itr)["itn_german"].as<std::string>());
                temprecord.English = (TYPE_OF_ENGLISH)((*itr)["itn_english"].as<std::string>());
                m_table[(TYPE_OF_ITEM_ID)((*itr)["itn_itemid"].as<int32_t>())] = temprecord;
            }

            m_dataOK = true;
        } else {
            m_dataOK = false;
        }


#ifdef DataConnect_DEBUG
        std::cout << "loaded " << rows << " rows into NamesObjectTable" << std::endl;
#endif

    } catch (...) {
        m_dataOK = false;
    }

}
void ContainerObjectTable::reload() {
#ifdef DataConnect_DEBUG
    std::cout << "ContainerObjectTable: reload" << std::endl;
#endif

    try {
        Database::SelectQuery query;
        query.addColumn("container", "con_itemid");
        query.addColumn("container", "con_volume");
        query.addServerTable("container");

        Database::Result results = query.execute();

        if (!results.empty()) {
            clearOldTable();
            ContainerStruct temprecord;

            for (Database::ResultConstIterator itr = results.begin();
                 itr != results.end(); ++itr) {
                temprecord.ContainerVolume = (TYPE_OF_VOLUME)((*itr)["con_volume"].as<int32_t>());
                m_table[(TYPE_OF_ITEM_ID)((*itr)["con_itemid"].as<TYPE_OF_ITEM_ID>())] = temprecord;
            }

            m_dataOK = true;
        } else {
            m_dataOK = false;
        }

#ifdef DataConnect_DEBUG
        std::cout << "loaded " << rows << " rows into ContainerObjectTable" << std::endl;
#endif

    } catch (...) {
        m_dataOK = false;
    }


}
MonsterTable::MonsterTable() {
    Logger::info(LogFacility::Other) << "MonsterTable::constructor" << Log::end;

    try {
        using namespace Database;
        PConnection connection = ConnectionManager::getInstance().getConnection();
        connection->beginTransaction();

        SelectQuery monquery(connection);
        monquery.addColumn("monster", "mob_monsterid");
        monquery.addColumn("monster", "mob_name_de");
        monquery.addColumn("monster", "mob_name_en");
        monquery.addColumn("monster", "mob_race");
        monquery.addColumn("monster", "mob_hitpoints");
        monquery.addColumn("monster", "mob_movementtype");
        monquery.addColumn("monster", "mob_canattack");
        monquery.addColumn("monster", "mob_canhealself");
        monquery.addColumn("monster", "script");
        monquery.addColumn("monster", "mob_minsize");
        monquery.addColumn("monster", "mob_maxsize");
        monquery.addServerTable("monster");
        monquery.addOrderBy("monster", "mob_monsterid", Database::SelectQuery::ASC);

        Database::Result monresults = monquery.execute();

        if (!monresults.empty()) {
            auto questNodes = QuestNodeTable::getInstance().getMonsterNodes();
            auto questItr = questNodes.first;
            auto questEnd = questNodes.second;

            SelectQuery monLootDataQuery(connection);
            monLootDataQuery.addColumn("monster_drop_data", "mdd_id");
            monLootDataQuery.addColumn("monster_drop_data", "mdd_key");
            monLootDataQuery.addColumn("monster_drop_data", "mdd_value");
            monLootDataQuery.addServerTable("monster_drop_data");
            auto lootDataResults = monLootDataQuery.execute();

            std::map<uint32_t, std::map<std::string, std::string>> lootData;

            for (const auto &row: lootDataResults) {
                auto &lootDataEntry = lootData[row["mdd_id"].as<uint32_t>()];
                lootDataEntry.insert(std::make_pair(row["mdd_key"].as<std::string>(),
                                                    row["mdd_value"].as<std::string>()));
            }

            for (const auto &row : monresults) {
                MonsterStruct temprecord;
                const uint32_t id = row["mob_monsterid"].as<uint32_t>();
                temprecord.nameDe = row["mob_name_de"].as<std::string>();
                temprecord.nameEn = row["mob_name_en"].as<std::string>();
                temprecord.race = row["mob_race"].as<TYPE_OF_RACE_ID>();
                temprecord.hitpoints = row["mob_hitpoints"].as<uint16_t>();
                temprecord.canselfheal = row["mob_canhealself"].as<bool>();
                temprecord.canattack = row["mob_canattack"].as<bool>();
                temprecord.minsize = row["mob_minsize"].as<uint16_t>();
                temprecord.maxsize = row["mob_maxsize"].as<uint16_t>();

                const std::string movementType = row["mob_movementtype"].as<std::string>("");

                if (movementType == "walk") {
                    temprecord.movement = movement_type::walk;
                } else if (movementType == "fly") {
                    temprecord.movement = movement_type::fly;
                } else if (movementType == "crawl") {
                    temprecord.movement = movement_type::crawl;
                } else {
                    //TODO: Some proper error handling for invalid data
                    temprecord.movement = movement_type::walk;
                }

                if (!row["script"].is_null()) {
                    const std::string scriptname = row["script"].as<std::string>();

                    try {
                        std::shared_ptr<LuaMonsterScript> script(new LuaMonsterScript(scriptname));
                        temprecord.script = script;
                    } catch (ScriptException &e) {
                        Logger::error(LogFacility::Script) << "Error while loading monster script: " << scriptname << ": " << e.what() << Log::end;
                    }
                } else if (questItr != questEnd && questItr->first == id) {
                    std::shared_ptr<LuaMonsterScript> tmpScript = std::make_shared<LuaMonsterScript>();
                    temprecord.script = tmpScript;
                }

                while (questItr != questEnd && questItr->first == id) {
                    temprecord.script->addQuestScript(questItr->second.entrypoint, questItr->second.script);
                    ++questItr;
                }

                SelectQuery monAttrQuery(connection);
                monAttrQuery.addColumn("monster_attributes", "mobattr_name");
                monAttrQuery.addColumn("monster_attributes", "mobattr_min");
                monAttrQuery.addColumn("monster_attributes", "mobattr_max");
                monAttrQuery.addEqualCondition("monster_attributes", "mobattr_monsterid", id);
                monAttrQuery.addServerTable("monster_attributes");

                Database::Result monAttrResults = monAttrQuery.execute();

                for (const auto &row : monAttrResults) {
                    const std::string attribute = row["mobattr_name"].as<std::string>("");
                    uint16_t minValue = row["mobattr_min"].as<uint16_t>();
                    uint16_t maxValue = row["mobattr_max"].as<uint16_t>();

                    if (attribute == "luck") {
                        temprecord.attributes.luck = std::make_pair(minValue, maxValue);
                    } else if (attribute == "strength") {
                        temprecord.attributes.strength = std::make_pair(minValue, maxValue);
                    } else if (attribute == "dexterity") {
                        temprecord.attributes.dexterity = std::make_pair(minValue, maxValue);
                    } else if (attribute == "constitution") {
                        temprecord.attributes.constitution = std::make_pair(minValue, maxValue);
                    } else if (attribute == "agility") {
                        temprecord.attributes.agility = std::make_pair(minValue, maxValue);
                    } else if (attribute == "intelligence") {
                        temprecord.attributes.intelligence = std::make_pair(minValue, maxValue);
                    } else if (attribute == "perception") {
                        temprecord.attributes.perception = std::make_pair(minValue, maxValue);
                    } else if (attribute == "willpower") {
                        temprecord.attributes.willpower = std::make_pair(minValue, maxValue);
                    } else if (attribute == "essence") {
                        temprecord.attributes.essence = std::make_pair(minValue, maxValue);
                    } else {
                        Logger::error(LogFacility::Other) << "Unknown attribute type for monster " << id << ": " << attribute << Log::end;
                    }
                }

                SelectQuery monSkillQuery(connection);
                monSkillQuery.addColumn("monster_skills", "mobsk_skill_id");
                monSkillQuery.addColumn("monster_skills", "mobsk_minvalue");
                monSkillQuery.addColumn("monster_skills", "mobsk_maxvalue");
                monSkillQuery.addEqualCondition("monster_skills", "mobsk_monsterid", id);
                monSkillQuery.addServerTable("monster_skills");

                Database::Result monSkillResults = monSkillQuery.execute();

                for (const auto &row : monSkillResults) {
                    TYPE_OF_SKILL_ID skill = TYPE_OF_SKILL_ID(row["mobsk_skill_id"].as<uint16_t>());
                    uint16_t minValue = row["mobsk_minvalue"].as<uint16_t>();
                    uint16_t maxValue = row["mobsk_maxvalue"].as<uint16_t>();

                    temprecord.skills[skill] = std::make_pair(minValue, maxValue);
                }

                SelectQuery monItemQuery(connection);
                monItemQuery.addColumn("monster_items", "mobit_itemid");
                monItemQuery.addColumn("monster_items", "mobit_position");
                monItemQuery.addColumn("monster_items", "mobit_mincount");
                monItemQuery.addColumn("monster_items", "mobit_maxcount");
                monItemQuery.addEqualCondition("monster_items", "mobit_monsterid", id);
                monItemQuery.addServerTable("monster_items");

                Database::Result monItemResults = monItemQuery.execute();

                for (const auto &row : monItemResults) {
                    itemdef_t tempitem;
                    tempitem.itemid = row["mobit_itemid"].as<TYPE_OF_ITEM_ID>();
                    tempitem.amount = std::make_pair(
                                          row["mobit_mincount"].as<uint16_t>(),
                                          row["mobit_maxcount"].as<uint16_t>());

                    const std::string position = row["mobit_position"].as<std::string>("");
                    uint16_t location;

                    if (position == "head") {
                        location = 1;
                    } else if (position == "neck") {
                        location = 2;
                    } else if (position == "breast") {
                        location = 3;
                    } else if (position == "hands") {
                        location = 4;
                    } else if (position == "left hand") {
                        location = 5;
                    } else if (position == "right hand") {
                        location = 6;
                    } else if (position == "left finger") {
                        location = 7;
                    } else if (position == "right finger") {
                        location = 8;
                    } else if (position == "legs") {
                        location = 9;
                    } else if (position == "feet") {
                        location = 10;
                    } else if (position == "coat") {
                        location = 11;
                    } else if (position == "belt1") {
                        location = 12;
                    } else if (position == "belt2") {
                        location = 13;
                    } else if (position == "belt3") {
                        location = 14;
                    } else if (position == "belt4") {
                        location = 15;
                    } else if (position == "belt5") {
                        location = 16;
                    } else if (position == "belt6") {
                        location = 17;
                    } else {
                        Logger::error(LogFacility::Other) << "Invalid itemslot for monster " << id << ": " << position << Log::end;
                        location = 99;
                    }

                    const auto &tempCommon = Data::CommonItems[tempitem.itemid];

                    if (location < 99 && tempCommon.isValid()) {
                        tempitem.AgeingSpeed = tempCommon.AgeingSpeed;
                        temprecord.items[location].push_back(tempitem);
                    } else if (location < 99) {
                        Logger::error(LogFacility::Other) << "Invalid item for monster " << id << ": " << tempitem.itemid << Log::end;
                    }
                }

                SelectQuery monLootQuery(connection);
                monLootQuery.addColumn("monster_drop", "md_id");
                monLootQuery.addColumn("monster_drop", "md_category");
                monLootQuery.addColumn("monster_drop", "md_probability");
                monLootQuery.addColumn("monster_drop", "md_itemid");
                monLootQuery.addColumn("monster_drop", "md_amount_min");
                monLootQuery.addColumn("monster_drop", "md_amount_max");
                monLootQuery.addColumn("monster_drop", "md_quality_min");
                monLootQuery.addColumn("monster_drop", "md_quality_max");
                monLootQuery.addColumn("monster_drop", "md_durability_min");
                monLootQuery.addColumn("monster_drop", "md_durability_max");
                monLootQuery.addEqualCondition("monster_drop", "md_monsterid", id);
                monLootQuery.addServerTable("monster_drop");

                const auto monLootResults = monLootQuery.execute();

                for (const auto &row : monLootResults) {
                    auto lootId = row["md_id"].as<uint32_t>();
                    auto categoryId = row["md_category"].as<uint16_t>();

                    auto &category = temprecord.loot[categoryId];
                    auto &lootItem = category[lootId];

                    lootItem.itemId = row["md_itemid"].as<TYPE_OF_ITEM_ID>();
                    lootItem.probability = row["md_probability"].as<double>();
                    lootItem.amount = std::make_pair(row["md_amount_min"].as<uint16_t>(),
                                                     row["md_amount_max"].as<uint16_t>());
                    lootItem.quality = std::make_pair(row["md_quality_min"].as<uint16_t>(),
                                                      row["md_quality_max"].as<uint16_t>());
                    lootItem.durability = std::make_pair(row["md_durability_min"].as<uint16_t>(),
                                                         row["md_durability_max"].as<uint16_t>());
                    lootItem.data = std::move(lootData[lootId]);
                }

                



                table[id] = temprecord;
                dataOK = true;
            }
        }

        connection->commitTransaction();
        Logger::info(LogFacility::Other) << "Loaded " << table.size() << " monsters!" << Log::end;
    } catch (std::exception &e) {
        Logger::error(LogFacility::Other) << "Exception in MonsterTable::reload: " << e.what() << Log::end;
        dataOK = false;
    }

}
void MonsterTable::reload() {
#ifdef DataConnect_DEBUG
    std::cout << "MonsterTable: reload" << std::endl;
#endif

    try {
        using namespace Database;
        PConnection connection = ConnectionManager::getInstance().getConnection();
        connection->beginTransaction();

        SelectQuery monquery(connection);
        monquery.addColumn("monster", "mob_monsterid");
        monquery.addColumn("monster", "mob_name_de");
        monquery.addColumn("monster", "mob_name_en");
        monquery.addColumn("monster", "mob_race");
        monquery.addColumn("monster", "mob_hitpoints");
        monquery.addColumn("monster", "mob_movementtype");
        monquery.addColumn("monster", "mob_canattack");
        monquery.addColumn("monster", "mob_canhealself");
        monquery.addColumn("monster", "script");
        monquery.addColumn("monster", "mob_minsize");
        monquery.addColumn("monster", "mob_maxsize");
        monquery.addServerTable("monster");
        monquery.addOrderBy("monster", "mob_monsterid", Database::SelectQuery::ASC);

        Database::Result monresults = monquery.execute();

        if (!monresults.empty()) {
            clearOldTable();

            auto questNodes = QuestNodeTable::getInstance()->getMonsterNodes();
            auto questItr = questNodes.first;
            auto questEnd = questNodes.second;

            for (Database::ResultConstIterator itr = monresults.begin();
                 itr != monresults.end(); ++itr) {
                MonsterStruct temprecord;
                std::string movementType;
                std::string scriptname;
                uint32_t id;

                id = (*itr)["mob_monsterid"].as<uint32_t>();
                temprecord.nameDe = (*itr)["mob_name_de"].as<std::string>();
                temprecord.nameEn = (*itr)["mob_name_en"].as<std::string>();
                temprecord.race = (Character::race_type)((*itr)["mob_race"].as<uint16_t>());
                temprecord.hitpoints = (*itr)["mob_hitpoints"].as<uint16_t>();
                temprecord.canselfheal = (*itr)["mob_canhealself"].as<bool>();
                temprecord.canattack = (*itr)["mob_canattack"].as<bool>();
                temprecord.minsize = (*itr)["mob_minsize"].as<uint16_t>();
                temprecord.maxsize = (*itr)["mob_maxsize"].as<uint16_t>();

                movementType = (*itr)["mob_movementtype"].as<std::string>();

                if (movementType == "walk") {
                    temprecord.movement = Character::walk;
                } else if (movementType == "fly") {
                    temprecord.movement = Character::fly;
                } else if (movementType == "crawl") {
                    temprecord.movement = Character::crawl;
                } else {
                    //TODO: Some proper error handling for invalid data
                    temprecord.movement = Character::walk;
                }

                if (!(*itr)["script"].is_null()) {
                    scriptname = (*itr)["script"].as<std::string>();

                    try {
                        boost::shared_ptr<LuaMonsterScript> script(new LuaMonsterScript(scriptname));
                        temprecord.script = script;
                    } catch (ScriptException &e) {
                        Logger::writeError("scripts", "Error while loading monster script: " + scriptname + ":\n" + e.what() + "\n");
                    }
                } else if (questItr != questEnd && questItr->first == id) {
                    boost::shared_ptr<LuaMonsterScript> tmpScript(new LuaMonsterScript());
                    temprecord.script = tmpScript;
                }

                while (questItr != questEnd && questItr->first == id) {
                    temprecord.script->addQuestScript(questItr->second.entrypoint, questItr->second.script);
                    ++questItr;
                }

                SelectQuery monAttrQuery(connection);
                monAttrQuery.addColumn("monster_attributes", "mobattr_name");
                monAttrQuery.addColumn("monster_attributes", "mobattr_min");
                monAttrQuery.addColumn("monster_attributes", "mobattr_max");
                monAttrQuery.addEqualCondition("monster_attributes", "mobattr_monsterid", id);
                monAttrQuery.addServerTable("monster_attributes");

                Database::Result monAttrResults = monAttrQuery.execute();

                if (!monAttrResults.empty()) {
                    std::string attribute;
                    uint16_t minValue, maxValue;

                    for (Database::ResultConstIterator itr2 = monAttrResults.begin();
                         itr2 != monAttrResults.end(); ++itr2) {
                        attribute = (*itr2)["mobattr_name"].as<std::string>();
                        minValue = (*itr2)["mobattr_min"].as<uint16_t>();
                        maxValue = (*itr2)["mobattr_max"].as<uint16_t>();

                        if (attribute == "luck") {
                            temprecord.attributes.luck = std::make_pair(minValue, maxValue);
                        } else if (attribute == "strength") {
                            temprecord.attributes.strength = std::make_pair(minValue, maxValue);
                        } else if (attribute == "dexterity") {
                            temprecord.attributes.dexterity = std::make_pair(minValue, maxValue);
                        } else if (attribute == "constitution") {
                            temprecord.attributes.constitution = std::make_pair(minValue, maxValue);
                        } else if (attribute == "agility") {
                            temprecord.attributes.agility = std::make_pair(minValue, maxValue);
                        } else if (attribute == "intelligence") {
                            temprecord.attributes.intelligence = std::make_pair(minValue, maxValue);
                        } else if (attribute == "perception") {
                            temprecord.attributes.perception = std::make_pair(minValue, maxValue);
                        } else if (attribute == "willpower") {
                            temprecord.attributes.willpower = std::make_pair(minValue, maxValue);
                        } else if (attribute == "essence") {
                            temprecord.attributes.essence = std::make_pair(minValue, maxValue);
                        } else {
                            std::cerr << "unknown attribute type: "<< attribute << std::endl;
                        }
                    } // for (Database::ResultConstIterator itr2 = monAttrResults...
                } // if (!monAttrResults.empty())

                SelectQuery monSkillQuery(connection);
                monSkillQuery.addColumn("monster_skills", "mobsk_skill_id");
                monSkillQuery.addColumn("monster_skills", "mobsk_minvalue");
                monSkillQuery.addColumn("monster_skills", "mobsk_maxvalue");
                monSkillQuery.addEqualCondition("monster_skills", "mobsk_monsterid", id);
                monSkillQuery.addServerTable("monster_skills");

                Database::Result monSkillResults = monSkillQuery.execute();

                if (!monSkillResults.empty()) {
                    TYPE_OF_SKILL_ID skill;
                    uint16_t minValue, maxValue;

                    for (Database::ResultConstIterator itr2 = monSkillResults.begin();
                         itr2 != monSkillResults.end(); ++itr2) {
                        skill = (TYPE_OF_SKILL_ID)((*itr2)["mobsk_skill_id"].as<uint16_t>());
                        minValue = (*itr2)["mobsk_minvalue"].as<uint16_t>();
                        maxValue = (*itr2)["mobsk_maxvalue"].as<uint16_t>();

                        temprecord.skills[skill] = std::make_pair(minValue, maxValue);
                    } // for (Database::ResultConstIterator itr2 = monSkillResults...
                } // if (!monSkillResults.empty())

                SelectQuery monItemQuery(connection);
                monItemQuery.addColumn("monster_items", "mobit_itemid");
                monItemQuery.addColumn("monster_items", "mobit_position");
                monItemQuery.addColumn("monster_items", "mobit_mincount");
                monItemQuery.addColumn("monster_items", "mobit_maxcount");
                monItemQuery.addEqualCondition("monster_items", "mobit_monsterid", id);
                monItemQuery.addServerTable("monster_items");

                Database::Result monItemResults = monItemQuery.execute();

                if (!monItemResults.empty()) {
                    itemdef_t tempitem;
                    uint16_t location;
                    std::string position;

                    for (Database::ResultConstIterator itr2 = monItemResults.begin();
                         itr2 != monItemResults.end(); ++itr2) {
                        tempitem.itemid = (*itr2)["mobit_itemid"].as<TYPE_OF_ITEM_ID>();
                        tempitem.amount = std::make_pair(
                                              (*itr2)["mobit_mincount"].as<uint16_t>(),
                                              (*itr2)["mobit_maxcount"].as<uint16_t>());

                        position = (*itr2)["mobit_position"].as<std::string>();

                        if (position == "head") {
                            location = 1;
                        } else if (position == "neck") {
                            location = 2;
                        } else if (position == "breast") {
                            location = 3;
                        } else if (position == "hands") {
                            location = 4;
                        } else if (position == "left hand") {
                            location = 5;
                        } else if (position == "right hand") {
                            location = 6;
                        } else if (position == "left finger") {
                            location = 7;
                        } else if (position == "right finger") {
                            location = 8;
                        } else if (position == "legs") {
                            location = 9;
                        } else if (position == "feet") {
                            location = 10;
                        } else if (position == "coat") {
                            location = 11;
                        } else if (position == "belt1") {
                            location = 12;
                        } else if (position == "belt2") {
                            location = 13;
                        } else if (position == "belt3") {
                            location = 14;
                        } else if (position == "belt4") {
                            location = 15;
                        } else if (position == "belt5") {
                            location = 16;
                        } else if (position == "belt6") {
                            location = 17;
                        } else {
                            std::cerr << "specified invalid itemslot: " <<  position << " for monster " << temprecord.nameEn << std::endl;
                            location = 99;
                        }

                        CommonStruct tempCommon;

                        if (location < 99 && CommonItems->find(tempitem.itemid, tempCommon)) {
                            tempitem.AgeingSpeed = tempCommon.AgeingSpeed;
                            temprecord.items[location].push_back(tempitem);
                        } else if (location < 99) {
                            std::cerr << "couldn't find item: " <<  tempitem.itemid << " for monster " << temprecord.nameEn << std::endl;
                        }
                    } // for (Database::ResultConstIterator itr2 = monItemResults...
                } // if (!monItemResults.empty())

                m_table[id] = temprecord;
                m_dataOK = true;
            } // for (Database::ResultConstIterator itr = monresults...
        } // if (!monresults.empty())

        connection->commitTransaction();
#ifdef DataConnect_DEBUG
        std::cout << "loaded " << m_table.size() << " monsters!" << std::endl;
#endif
    } catch (std::exception &e) {
        std::cerr << "exception: " << e.what() << std::endl;
        m_dataOK = false;
    }

}
void WeaponObjectTable::reload() {
#ifdef DataConnect_DEBUG
    std::cout << "WeaponObjectTable: reload" << std::endl;
#endif

    try {
        Database::SelectQuery query;
        query.addColumn("weapon", "wp_itemid");
        query.addColumn("weapon", "wp_attack");
        query.addColumn("weapon", "wp_defence");
        query.addColumn("weapon", "wp_accuracy");
        query.addColumn("weapon", "wp_range");
        query.addColumn("weapon", "wp_weapontype");
        query.addColumn("weapon", "wp_ammunitiontype");
        query.addColumn("weapon", "wp_actionpoints");
        query.addColumn("weapon", "wp_magicdisturbance");
        query.addColumn("weapon", "wp_poison");
        query.addColumn("weapon", "wp_fightingscript");
        query.addServerTable("weapon");

        Database::Result results = query.execute();

        if (!results.empty()) {
            clearOldTable();
            WeaponStruct temprecord;
            std::string scriptname;
            TYPE_OF_ITEM_ID weaponId;

            for (Database::ResultConstIterator itr = results.begin();
                 itr != results.end(); ++itr) {
                weaponId = (*itr)["wp_itemid"].as<TYPE_OF_ITEM_ID>();
                temprecord.Attack = (TYPE_OF_ATTACK)((*itr)["wp_attack"].as<uint16_t>());
                temprecord.Defence = (TYPE_OF_DEFENCE)((*itr)["wp_defence"].as<uint16_t>());
                temprecord.Accuracy = (TYPE_OF_ACCURACY)((*itr)["wp_accuracy"].as<uint16_t>());
                temprecord.Range = (TYPE_OF_RANGE)((*itr)["wp_range"].as<uint16_t>());
                temprecord.WeaponType = (TYPE_OF_WEAPONTYPE)((*itr)["wp_weapontype"].as<uint16_t>());
                temprecord.AmmunitionType = (TYPE_OF_AMMUNITIONTYPE)((*itr)["wp_ammunitiontype"].as<uint16_t>());
                temprecord.ActionPoints = (TYPE_OF_ACTIONPOINTS)((*itr)["wp_actionpoints"].as<uint16_t>());
                temprecord.MagicDisturbance = (TYPE_OF_MAGICDISTURBANCE)((*itr)["wp_magicdisturbance"].as<uint16_t>());
                temprecord.PoisonStrength = (TYPE_OF_POISONSTRENGTH)((*itr)["wp_poison"].as<uint16_t>());

                if (!(*itr)["wp_fightingscript"].is_null()) {
                    scriptname = (*itr)["wp_fightingscript"].as<std::string>();

                    if (!scriptname.empty()) {
                        try {
                            boost::shared_ptr<LuaWeaponScript> tmpScript(new LuaWeaponScript(scriptname));
                            temprecord.script = tmpScript;
                        } catch (ScriptException &e) {
                            Logger::writeError("scripts", "Error while loading weapon script: " + scriptname + ":\n" + e.what() + "\n");
                        }
                    }
                }

                m_table[weaponId] = temprecord;
            }

            m_dataOK = true;
        } else {
            m_dataOK = false;
        }


#ifdef DataConnect_DEBUG
        std::cout << "loaded " << rows << " rows into WeaponObjectTable" << std::endl;
#endif

    } catch (...) {
        m_dataOK = false;
    }

}
void NPCTable::reload() {
    try {
        Database::SelectQuery query;
        query.addColumn("npc", "npc_id");
        query.addColumn("npc", "npc_type");
        query.addColumn("npc", "npc_posx");
        query.addColumn("npc", "npc_posy");
        query.addColumn("npc", "npc_posz");
        query.addColumn("npc", "npc_name");
        query.addColumn("npc", "npc_faceto");
        query.addColumn("npc", "npc_is_healer");
        query.addColumn("npc", "npc_script");
        query.addColumn("npc", "npc_sex");
        query.addColumn("npc", "npc_hair");
        query.addColumn("npc", "npc_beard");
        query.addColumn("npc", "npc_hairred");
        query.addColumn("npc", "npc_hairgreen");
        query.addColumn("npc", "npc_hairblue");
        query.addColumn("npc", "npc_skinred");
        query.addColumn("npc", "npc_skingreen");
        query.addColumn("npc", "npc_skinblue");
        query.addServerTable("npc");
        query.addOrderBy("npc", "npc_id", Database::SelectQuery::ASC);

        Database::Result results = query.execute();

        if (!results.empty()) {
            auto questNodes = QuestNodeTable::getInstance().getNpcNodes();
            auto questItr = questNodes.first;
            auto questEnd = questNodes.second;

            for (const auto &row : results) {
                NPC *newNPC = nullptr;
                TYPE_OF_CHARACTER_ID npcID = 0;
                std::string npcName;

                try {
                    npcID = row["npc_id"].as<TYPE_OF_CHARACTER_ID>();

                    const position pos(
                        row["npc_posx"].as<int16_t>(),
                        row["npc_posy"].as<int16_t>(),
                        row["npc_posz"].as<int16_t>()
                    );

                    npcName = row["npc_name"].as<std::string>();

                    Character::appearance appearance;
                    appearance.hairtype = uint8_t(row["npc_hair"].as<int16_t>());
                    appearance.beardtype = uint8_t(row["npc_hair"].as<int16_t>());
                    appearance.hair = {
                        uint8_t(row["npc_hairred"].as<int16_t>()),
                        uint8_t(row["npc_hairgreen"].as<int16_t>()),
                        uint8_t(row["npc_hairblue"].as<int16_t>())
                    };
                    appearance.skin = {
                        (uint8_t)(row["npc_skinred"].as<int16_t>()),
                        (uint8_t)(row["npc_skingreen"].as<int16_t>()),
                        (uint8_t)(row["npc_skinblue"].as<int16_t>())
                    };

                    newNPC = new NPC(
                        npcID, npcName,
                        row["npc_type"].as<TYPE_OF_RACE_ID>(),
                        pos,
                        Character::face_to(row["npc_faceto"].as<uint32_t>()),
                        row["npc_is_healer"].as<bool>(),
                        Character::sex_type(row["npc_sex"].as<uint32_t>()),
                        appearance);

                    // add npc to npc list
                    _world->Npc.insert(newNPC);

                    // set field to occupied
                    Field *tempf;

                    if (_world->GetPToCFieldAt(tempf, pos)) {
                        tempf->setChar();
                    }

                    if (!row["npc_script"].is_null()) {
                        const std::string scriptname = row["npc_script"].as<std::string>();

                        try {
                            std::shared_ptr<LuaNPCScript> script(new LuaNPCScript(scriptname, newNPC));

                            while (questItr != questEnd && questItr->first == npcID) {
                                script->addQuestScript(questItr->second.entrypoint, questItr->second.script);
                                ++questItr;
                            }

                            newNPC->setScript(script);
                        } catch (ScriptException &e) {
                            Logger::error(LogFacility::Script) << "Error while loading npc script: " << scriptname << ": " << e.what() << Log::end;
                        }
                    }

                    newNPC = nullptr;
                } catch (NoSpace &s) {
                    Logger::error(LogFacility::Other) << "No space available for NPC " << npcName << "(" << npcID << "): " << s.what() << Log::end;
                }

                delete newNPC;
            }

        }

        m_dataOK = true;
    } catch (...) {
        Logger::error(LogFacility::Other) << "Error while loading NPCs" << Log::end;
        m_dataOK = false;
    }
}