<commit_before>//  illarionserver - server for the game Illarion
//  Copyright 2011 Illarion e.V.
//
//  This file is part of illarionserver.
//
//  illarionserver is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  illarionserver is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with illarionserver.  If not, see <http://www.gnu.org/licenses/>.


#include "db/ConnectionManager.hpp"
#include "ArmorObjectTable.hpp"
#include <iostream>

ArmorObjectTable::ArmorObjectTable() : m_dataOK(false) {
    reload();
}

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

    try {
        ConnectionManager::TransactionHolder transaction = dbmgr->getTransaction();

        std::vector<TYPE_OF_ITEM_ID> ids;
        std::vector<TYPE_OF_BODYPARTS> bodyparts;
        std::vector<TYPE_OF_PUNCTUREARMOR> puncture;
        std::vector<TYPE_OF_STROKEARMOR> stroke;
        std::vector<TYPE_OF_THRUSTARMOR> thrust;
        std::vector<TYPE_OF_MAGICDISTURBANCE> magicdisturbance;
        std::vector<int16_t> absorb;
        std::vector<int16_t> stiffness;
        size_t rows = di::select_all<
                      di::Integer, di::Integer, di::Integer, di::Integer, di::Integer, di::Integer, di::Integer, di::Integer
                      >(transaction, ids, bodyparts, puncture, stroke, thrust, magicdisturbance, absorb, stiffness,
                        "SELECT arm_itemid, arm_bodyparts, arm_puncture, arm_stroke, arm_thrust, arm_magicdisturbance, arm_absorb, arm_stiffness FROM armor");

        if (rows > 0) {
            clearOldTable();
            ArmorStruct temprecord;

            for (size_t i = 0; i < rows; ++i) {
                temprecord.BodyParts = bodyparts[i];
                temprecord.PunctureArmor = puncture[i];
                temprecord.StrokeArmor = stroke[i];
                temprecord.ThrustArmor = thrust[i];
                temprecord.MagicDisturbance = magicdisturbance[i];
                temprecord.Absorb = absorb[i];
                temprecord.Stiffness = stiffness[i];
                m_table[ ids[i] ] = temprecord;
            }

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

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

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

bool ArmorObjectTable::find(TYPE_OF_ITEM_ID Id, ArmorStruct &ret) {
    TABLE::iterator iterator;
    iterator = m_table.find(Id);

    if (iterator == m_table.end()) {
        return false;
    } else {
        ret = (*iterator).second;
        return true;
    }
}



void ArmorObjectTable::clearOldTable() {
    m_table.clear();
}


ArmorObjectTable::~ArmorObjectTable() {
    clearOldTable();
}
<commit_msg>Implemented new database interface to the armor table loader.<commit_after>//  illarionserver - server for the game Illarion
//  Copyright 2011 Illarion e.V.
//
//  This file is part of illarionserver.
//
//  illarionserver is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  illarionserver is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with illarionserver.  If not, see <http://www.gnu.org/licenses/>.


#include "db/ConnectionManager.hpp"
#include "db/SchemaHelper.hpp"
#include "db/Query.hpp"
#include "db/Result.hpp"

#include "ArmorObjectTable.hpp"
#include "types.hpp"

#include <iostream>
#include <sstream>

ArmorObjectTable::ArmorObjectTable() : m_dataOK(false) {
    reload();
}

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

    try {
        Database::PConnection connection =
            Database::ConnectionManager::getInstance()->getConnection();

        std::stringstream ss;
        ss << "SELECT ";
        ss << "\"arm_itemid\", \"arm_bodyparts\", \"arm_puncture\", ";
        ss << "\"arm_stroke\", \"arm_thrust\", \"arm_magicdisturbance\", ";
        ss << "\"arm_absorb\", \"arm_stiffness\" ";
        ss << "FROM ";
        ss << Database::SchemaHelper::getServerSchema() << ".\"armor\";";
        
        Database::PQuery query = new Database::Query(connection, ss.str());
        Database::Result results = query->execute();

        if (!results.empty()) {
            clearOldTable();
            ArmorStruct temprecord;
            
            for (Database::Result::ConstIterator itr = results.begin();
                 itr != results.end(); ++itr) {
                     
                temprecord.BodyParts = (TYPE_OF_BODYPARTS) ((*itr)["arm_bodyparts"].as<int16_t>());
                temprecord.PunctureArmor = (TYPE_OF_PUNCTUREARMOR) ((*itr)["arm_puncture"].as<int16_t>());
                temprecord.StrokeArmor = (TYPE_OF_STROKEARMOR) ((*itr)["arm_stroke"].as<int16_t>());
                temprecord.ThrustArmor = (TYPE_OF_THRUSTARMOR) ((*itr)["arm_thrust"].as<int16_t>());
                temprecord.MagicDisturbance = (TYPE_OF_MAGICDISTURBANCE) (*itr)["arm_magicdisturbance"].as<int32_t>();
                temprecord.Absorb = (*itr)["arm_absorb"].as<int16_t>();
                temprecord.Stiffness = (*itr)["arm_stiffness"].as<int16_t>();

                m_table[(TYPE_OF_ITEM_ID) ((*itr)["arm_itemid"].as<TYPE_OF_ITEM_ID>())] = temprecord;
            }
            m_dataOK = true;
        } else {
            m_dataOK = false;
        }

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

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

bool ArmorObjectTable::find(TYPE_OF_ITEM_ID Id, ArmorStruct &ret) {
    TABLE::iterator iterator;
    iterator = m_table.find(Id);

    if (iterator == m_table.end()) {
        return false;
    } else {
        ret = (*iterator).second;
        return true;
    }
}



void ArmorObjectTable::clearOldTable() {
    m_table.clear();
}


ArmorObjectTable::~ArmorObjectTable() {
    clearOldTable();
}

{'fraction_non_alphanumeric': 0.12728592162554428, 'fraction_numerical': 0.0043541364296081275, 'mean_word_length': 2.741042345276873, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3187472', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2071, 'n_words': 512}