<commit_before>#include "mysql.hpp"

#include "CQuery.hpp"
#include "CConnection.hpp"
#include "CDispatcher.hpp"
#include "COptions.hpp"
#include "CLog.hpp"


CConnection::CConnection(const string &host, const string &user, const string &passw, const string &db,
	const COptions *options)
{
	CLog::Get()->Log(LOGLEVEL::DEBUG, 
		"CConnection::CConnection(this={}, host='{}', user='{}', passw='****', db='{}', options={})",
		static_cast<const void *>(this), host, user, passw, db, static_cast<const void *>(options));

	assert(options != nullptr);

	//initialize
	m_Connection = mysql_init(nullptr);
	if (m_Connection == nullptr)
	{
		CLog::Get()->Log(LOGLEVEL::ERROR, 
			"CConnection::CConnection - MySQL initialization failed (not enough memory available)");
		return;
	}
	
	//prepare connection flags through passed options
	unsigned long connect_flags = 0;
	if (options->GetOption<bool>(COptions::Type::MULTI_STATEMENTS) == true)
		connect_flags |= CLIENT_MULTI_STATEMENTS;

	//connect
	auto *result = mysql_real_connect(m_Connection, host.c_str(),
		user.c_str(), passw.c_str(), db.c_str(), 
		options->GetOption<unsigned int>(COptions::Type::SERVER_PORT), 
		nullptr, connect_flags);

	if (result == nullptr)
	{
		CLog::Get()->Log(LOGLEVEL::ERROR, 
			"CConnection::CConnection - establishing connection to MySQL database failed: #{} '{}'",
			mysql_errno(m_Connection), mysql_error(m_Connection));
		mysql_close(m_Connection);
		m_Connection = nullptr;
		return;
	}

	//set additional connection options
	my_bool reconnect = options->GetOption<bool>(COptions::Type::AUTO_RECONNECT);
	mysql_options(m_Connection, MYSQL_OPT_RECONNECT, &reconnect);

	CLog::Get()->Log(LOGLEVEL::DEBUG, "CConnection::CConnection - new connection = {}",
		static_cast<const void *>(m_Connection));
}

CConnection::~CConnection()
{
	CLog::Get()->Log(LOGLEVEL::DEBUG, "CConnection::~CConnection(this={}, connection={})",
		static_cast<const void *>(this), static_cast<const void *>(m_Connection));

	boost::lock_guard<boost::mutex> lock_guard(m_Mutex);
	if (IsConnected())
		mysql_close(m_Connection);
}


bool CConnection::EscapeString(const char *src, string &dest)
{
	CLog::Get()->Log(LOGLEVEL::DEBUG, "CConnection::EscapeString(src='{}', this={}, connection={})",
		src, static_cast<const void *>(this), static_cast<const void *>(m_Connection));

	if (IsConnected() == false || src == nullptr)
		return false;

	const size_t src_len = strlen(src);<commit_msg>bug fix: mysql_errno wasn't called internally<commit_after>#include "mysql.hpp"

#include "CQuery.hpp"
#include "CConnection.hpp"
#include "CDispatcher.hpp"
#include "COptions.hpp"
#include "CLog.hpp"


CConnection::CConnection(const string &host, const string &user, const string &passw, const string &db,
	const COptions *options)
{
	CLog::Get()->Log(LOGLEVEL::DEBUG, 
		"CConnection::CConnection(this={}, host='{}', user='{}', passw='****', db='{}', options={})",
		static_cast<const void *>(this), host, user, passw, db, static_cast<const void *>(options));

	assert(options != nullptr);

	//initialize
	m_Connection = mysql_init(nullptr);
	if (m_Connection == nullptr)
	{
		CLog::Get()->Log(LOGLEVEL::ERROR, 
			"CConnection::CConnection - MySQL initialization failed (not enough memory available)");
		return;
	}
	
	//prepare connection flags through passed options
	unsigned long connect_flags = 0;
	if (options->GetOption<bool>(COptions::Type::MULTI_STATEMENTS) == true)
		connect_flags |= CLIENT_MULTI_STATEMENTS;

	//connect
	auto *result = mysql_real_connect(m_Connection, host.c_str(),
		user.c_str(), passw.c_str(), db.c_str(), 
		options->GetOption<unsigned int>(COptions::Type::SERVER_PORT), 
		nullptr, connect_flags);

	if (result == nullptr)
	{
		CLog::Get()->Log(LOGLEVEL::ERROR, 
			"CConnection::CConnection - establishing connection to MySQL database failed: #{} '{}'",
			mysql_errno(m_Connection), mysql_error(m_Connection));
		return;
	}

	//set additional connection options
	my_bool reconnect = options->GetOption<bool>(COptions::Type::AUTO_RECONNECT);
	mysql_options(m_Connection, MYSQL_OPT_RECONNECT, &reconnect);

	CLog::Get()->Log(LOGLEVEL::DEBUG, "CConnection::CConnection - new connection = {}",
		static_cast<const void *>(m_Connection));
}

CConnection::~CConnection()
{
	CLog::Get()->Log(LOGLEVEL::DEBUG, "CConnection::~CConnection(this={}, connection={})",
		static_cast<const void *>(this), static_cast<const void *>(m_Connection));

	boost::lock_guard<boost::mutex> lock_guard(m_Mutex);
	if (IsConnected())
		mysql_close(m_Connection);
}


bool CConnection::EscapeString(const char *src, string &dest)
{
	CLog::Get()->Log(LOGLEVEL::DEBUG, "CConnection::EscapeString(src='{}', this={}, connection={})",
		src, static_cast<const void *>(this), static_cast<const void *>(m_Connection));

	if (IsConnected() == false || src == nullptr)
		return false;

	const size_t src_len = strlen(src);
{'fraction_non_alphanumeric': 0.1718332991172244, 'fraction_numerical': 0.0004105933073290905, 'mean_word_length': 6.733333333333333, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 43, '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': '2715230', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1599, 'n_words': 333}