<commit_before>
#include "swganh/object/tangible/tangible_factory.h"

#include <cppconn/exception.h>
#include <cppconn/connection.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/sqlstring.h>
#include <glog/logging.h>

#include "anh/database/database_manager.h"
#include "swganh/object/tangible/tangible.h"
#include "swganh/object/exception.h"

using namespace std;
using namespace anh::database;
using namespace swganh::object;
using namespace swganh::object::tangible;

TangibleFactory::TangibleFactory(const shared_ptr<DatabaseManagerInterface>& db_manager)
    : db_manager_(db_manager)
{
}
void TangibleFactory::LoadTemplates()
{
    try {

        auto conn = db_manager_->getConnection("galaxy");
        auto statement = conn->prepareStatement("CALL sp_GetTangibleTemplates();");
        auto result = statement->executeQuery();

        while (result->next())
        {
            auto tangible = make_shared<Tangible>();
            tangible->SetPosition(move(glm::vec3(result->getDouble(1),result->getDouble(2), result->getDouble(3))));
            tangible->SetOrientation(move(glm::quat(result->getDouble(4),result->getDouble(5), result->getDouble(6), result->getDouble(7))));
            tangible->SetComplexity(result->getDouble(8));
            tangible->SetStfNameFile(move(result->getString(9)));
            tangible->SetStfNameString(move(result->getString(10)));
            string custom_string = result->getString(11);
            tangible->SetCustomName(move(wstring(begin(custom_string), end(custom_string))));
            tangible->SetVolume(result->getUInt(12));
            tangible->SetCustomization(move(result->getString(13)));
            tangible->SetOptionsMask(result->getUInt(14));
            tangible->SetIncapTimer(result->getUInt(15));
            tangible->SetConditionDamage(result->getUInt(16));
            uint8_t is_static = result->getInt(17);
            tangible->SetStatic(is_static == 1);
            tangible->SetTemplate(result->getString(18));
            
            tangible_templates_.insert(make_pair(tangible->GetTemplate(), move(tangible)));
        } while (statement->getMoreResults());
    }
    catch(sql::SQLException &e)
    {
        DLOG(ERROR) << "SQLException at " << __FILE__ << " (" << __LINE__ << ": " << __FUNCTION__ << ")";
        DLOG(ERROR) << "MySQL Error: (" << e.getErrorCode() << ": " << e.getSQLState() << ") " << e.what();
    }
}

bool TangibleFactory::HasTemplate(const string& template_name)
{
    auto found = GetTemplateIter_(template_name);
    return found != end(tangible_templates_);
}
unordered_map<string, shared_ptr<Tangible>>::iterator TangibleFactory::GetTemplateIter_(const string& template_name)
{
    auto found = find_if(begin(tangible_templates_), end(tangible_templates_), [&template_name] (pair<string, shared_ptr<Tangible>> template_pair) {
        return template_name == template_pair.first;
    });
    return found;
}
void TangibleFactory::PersistObject(const shared_ptr<Object>& object)
{
    if (object->IsDirty())
    {
        try 
        {
            auto conn = db_manager_->getConnection("galaxy");
            auto statement = conn->prepareStatement("CALL sp_PersistTangible(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);");
            // TODO: Add in values to persist
            statement->execute();
        }
            catch(sql::SQLException &e)
        {
            DLOG(ERROR) << "SQLException at " << __FILE__ << " (" << __LINE__ << ": " << __FUNCTION__ << ")";
            DLOG(ERROR) << "MySQL Error: (" << e.getErrorCode() << ": " << e.getSQLState() << ") " << e.what();
        }
    }
}

void TangibleFactory::DeleteObjectFromStorage(const shared_ptr<Object>& object)
{
    try 
    {
        auto conn = db_manager_->getConnection("galaxy");
        auto statement = conn->prepareStatement("CALL sp_DeleteTangible(?);");
        statement->setUInt64(1, object->GetObjectId());
        statement->execute();
    }
        catch(sql::SQLException &e)
    {
        DLOG(ERROR) << "SQLException at " << __FILE__ << " (" << __LINE__ << ": " << __FUNCTION__ << ")";
        DLOG(ERROR) << "MySQL Error: (" << e.getErrorCode() << ": " << e.getSQLState() << ") " << e.what();
    }
}

shared_ptr<Object> TangibleFactory::CreateObjectFromStorage(uint64_t object_id)
{
    auto tangible = make_shared<Tangible>();
    try {
        auto conn = db_manager_->getConnection("galaxy");
        auto statement = conn->prepareStatement("CALL sp_GetTangible(?);");
        statement->setUInt64(1, object_id);
        auto result = statement->executeQuery();

        while (result->next())
        {
            tangible->SetPosition(move(glm::vec3(result->getDouble(1),result->getDouble(2), result->getDouble(3))));
            tangible->SetOrientation(move(glm::quat(result->getDouble(4),result->getDouble(5), result->getDouble(6), result->getDouble(7))));
            tangible->SetComplexity(result->getDouble(8));
            tangible->SetStfNameFile(move(result->getString(9)));
            tangible->SetStfNameString(move(result->getString(10)));
            string custom_string = result->getString(11);
            tangible->SetCustomName(move(wstring(begin(custom_string), end(custom_string))));
            tangible->SetVolume(result->getUInt(12));
            tangible->SetCustomization(move(result->getString(13)));
            tangible->SetOptionsMask(result->getUInt(14));
            tangible->SetIncapTimer(result->getUInt(15));
            tangible->SetConditionDamage(result->getUInt(16));
            uint8_t is_static = result->getInt(17);
            tangible->SetStatic(is_static == 1);
            tangible->SetTemplate(result->getString(18));
        }
    }
    catch(sql::SQLException &e)
    {
        DLOG(ERROR) << "SQLException at " << __FILE__ << " (" << __LINE__ << ": " << __FUNCTION__ << ")";
        DLOG(ERROR) << "MySQL Error: (" << e.getErrorCode() << ": " << e.getSQLState() << ") " << e.what();
    }
    return tangible;
}

shared_ptr<Object> TangibleFactory::CreateObjectFromTemplate(const string& template_name)
{<commit_msg>removed unecessary move() - because it's a temporary without a name the compiler does the right thing.<commit_after>
#include "swganh/object/tangible/tangible_factory.h"

#include <cppconn/exception.h>
#include <cppconn/connection.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/sqlstring.h>
#include <glog/logging.h>

#include "anh/database/database_manager.h"
#include "swganh/object/tangible/tangible.h"
#include "swganh/object/exception.h"

using namespace std;
using namespace anh::database;
using namespace swganh::object;
using namespace swganh::object::tangible;

TangibleFactory::TangibleFactory(const shared_ptr<DatabaseManagerInterface>& db_manager)
    : db_manager_(db_manager)
{
}
void TangibleFactory::LoadTemplates()
{
    try {

        auto conn = db_manager_->getConnection("galaxy");
        auto statement = conn->prepareStatement("CALL sp_GetTangibleTemplates();");
        auto result = statement->executeQuery();

        while (result->next())
        {
            auto tangible = make_shared<Tangible>();
            tangible->SetPosition(glm::vec3(result->getDouble(1),result->getDouble(2), result->getDouble(3)));
            tangible->SetOrientation(glm::quat(result->getDouble(4),result->getDouble(5), result->getDouble(6), result->getDouble(7)));
            tangible->SetComplexity(result->getDouble(8));
            tangible->SetStfNameFile(result->getString(9));
            tangible->SetStfNameString(result->getString(10));
            string custom_string = result->getString(11);
            tangible->SetCustomName(wstring(begin(custom_string), end(custom_string)));
            tangible->SetVolume(result->getUInt(12));
            tangible->SetCustomization(result->getString(13));
            tangible->SetOptionsMask(result->getUInt(14));
            tangible->SetIncapTimer(result->getUInt(15));
            tangible->SetConditionDamage(result->getUInt(16));
            uint8_t is_static = result->getInt(17);
            tangible->SetStatic(is_static == 1);
            tangible->SetTemplate(result->getString(18));
            
            tangible_templates_.insert(make_pair(tangible->GetTemplate(), move(tangible)));
        } while (statement->getMoreResults());
    }
    catch(sql::SQLException &e)
    {
        DLOG(ERROR) << "SQLException at " << __FILE__ << " (" << __LINE__ << ": " << __FUNCTION__ << ")";
        DLOG(ERROR) << "MySQL Error: (" << e.getErrorCode() << ": " << e.getSQLState() << ") " << e.what();
    }
}

bool TangibleFactory::HasTemplate(const string& template_name)
{
    auto found = GetTemplateIter_(template_name);
    return found != end(tangible_templates_);
}
unordered_map<string, shared_ptr<Tangible>>::iterator TangibleFactory::GetTemplateIter_(const string& template_name)
{
    auto found = find_if(begin(tangible_templates_), end(tangible_templates_), [&template_name] (pair<string, shared_ptr<Tangible>> template_pair) {
        return template_name == template_pair.first;
    });
    return found;
}
void TangibleFactory::PersistObject(const shared_ptr<Object>& object)
{
    if (object->IsDirty())
    {
        try 
        {
            auto conn = db_manager_->getConnection("galaxy");
            auto statement = conn->prepareStatement("CALL sp_PersistTangible(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);");
            // TODO: Add in values to persist
            statement->execute();
        }
            catch(sql::SQLException &e)
        {
            DLOG(ERROR) << "SQLException at " << __FILE__ << " (" << __LINE__ << ": " << __FUNCTION__ << ")";
            DLOG(ERROR) << "MySQL Error: (" << e.getErrorCode() << ": " << e.getSQLState() << ") " << e.what();
        }
    }
}

void TangibleFactory::DeleteObjectFromStorage(const shared_ptr<Object>& object)
{
    try 
    {
        auto conn = db_manager_->getConnection("galaxy");
        auto statement = conn->prepareStatement("CALL sp_DeleteTangible(?);");
        statement->setUInt64(1, object->GetObjectId());
        statement->execute();
    }
        catch(sql::SQLException &e)
    {
        DLOG(ERROR) << "SQLException at " << __FILE__ << " (" << __LINE__ << ": " << __FUNCTION__ << ")";
        DLOG(ERROR) << "MySQL Error: (" << e.getErrorCode() << ": " << e.getSQLState() << ") " << e.what();
    }
}

shared_ptr<Object> TangibleFactory::CreateObjectFromStorage(uint64_t object_id)
{
    auto tangible = make_shared<Tangible>();
    try {
        auto conn = db_manager_->getConnection("galaxy");
        auto statement = conn->prepareStatement("CALL sp_GetTangible(?);");
        statement->setUInt64(1, object_id);
        auto result = statement->executeQuery();

        while (result->next())
        {
            tangible->SetPosition(glm::vec3(result->getDouble(1),result->getDouble(2), result->getDouble(3)));
            tangible->SetOrientation(glm::quat(result->getDouble(4),result->getDouble(5), result->getDouble(6), result->getDouble(7)));
            tangible->SetComplexity(result->getDouble(8));
            tangible->SetStfNameFile(result->getString(9));
            tangible->SetStfNameString(result->getString(10));
            string custom_string = result->getString(11);
            tangible->SetCustomName(wstring(begin(custom_string), end(custom_string)));
            tangible->SetVolume(result->getUInt(12));
            tangible->SetCustomization(result->getString(13));
            tangible->SetOptionsMask(result->getUInt(14));
            tangible->SetIncapTimer(result->getUInt(15));
            tangible->SetConditionDamage(result->getUInt(16));
            uint8_t is_static = result->getInt(17);
            tangible->SetStatic(is_static == 1);
            tangible->SetTemplate(result->getString(18));
        }
    }
    catch(sql::SQLException &e)
    {
        DLOG(ERROR) << "SQLException at " << __FILE__ << " (" << __LINE__ << ": " << __FUNCTION__ << ")";
        DLOG(ERROR) << "MySQL Error: (" << e.getErrorCode() << ": " << e.getSQLState() << ") " << e.what();
    }
    return tangible;
}

shared_ptr<Object> TangibleFactory::CreateObjectFromTemplate(const string& template_name)
{
{'fraction_non_alphanumeric': 0.16549095921511392, 'fraction_numerical': 0.011027325062839537, 'mean_word_length': 3.4446846846846846, 'pattern_counts': {'":': 16, '<': 247, '<?xml version=': 0, '>': 203, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6337253', 'n_tokens_mistral': 3776, 'n_tokens_neox': 3753, 'n_words': 527}