<commit_before>#include "commands/sql-worker.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlError>
#include <QtSql/QSqlField>
#include <QtSql/QSqlQuery>
#include "logger.h"


SqlWorker::SqlWorker(const QString &driver, const QString &host, const QString &user, const QString &password, const QString &database, QObject *parent)
	: QThread(parent), m_driver(driver), m_host(host), m_user(user), m_password(password), m_database(database)
{
	m_enabled = (m_driver == "QSQLITE" && !m_database.isEmpty())
			  || (!m_host.isEmpty() && !m_user.isEmpty() && !m_database.isEmpty());

	m_started = false;
}

bool SqlWorker::connect()
{
	if (!m_enabled || m_started)
		return true;

	QSqlDatabase db = QSqlDatabase::addDatabase(m_driver);
	db.setHostName(m_host);
	db.setDatabaseName(m_database);
	db.setUserName(m_user);
	db.setPassword(<PASSWORD>);

	if (!db.open())
	{
		log(QString("Error initializing commands: %1").arg(db.lastError().text()), Logger::Error);
		return false;
	}

	m_started = true;
	return true;
}

QString SqlWorker::escape(const QVariant &val)
{
	QSqlDriver *driver = QSqlDatabase::database().driver();
	if (driver == nullptr)
		return nullptr;

	QSqlField f;
	f.setType(val.type());
	f.setValue(val);

	return driver->formatValue(f);
}

bool SqlWorker::execute(const QString &sql)
{
	if (!m_enabled || !connect())
		return false;

	log(QString("SQL execution of \"%1\"").arg(sql));
	Logger::getInstance().logCommandSql(sql);

	QSqlQuery query;
	return query.exec(sql);
}
<commit_msg>Allow to pass a port to the commands database (fix #1258)<commit_after>#include "commands/sql-worker.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlError>
#include <QtSql/QSqlField>
#include <QtSql/QSqlQuery>
#include "logger.h"


SqlWorker::SqlWorker(const QString &driver, const QString &host, const QString &user, const QString &password, const QString &database, QObject *parent)
	: QThread(parent), m_driver(driver), m_host(host), m_user(user), m_password(password), m_database(database)
{
	m_enabled = (m_driver == "QSQLITE" && !m_database.isEmpty())
			  || (!m_host.isEmpty() && !m_user.isEmpty() && !m_database.isEmpty());

	m_started = false;
}

bool SqlWorker::connect()
{
	if (!m_enabled || m_started)
		return true;

	QSqlDatabase db = QSqlDatabase::addDatabase(m_driver);
	db.setDatabaseName(m_database);
	db.setUserName(m_user);
	db.setPassword(<PASSWORD>);

	int portSeparator = m_host.lastIndexOf(':');
	if (portSeparator > 0)
	{
		db.setHostName(m_host.left(portSeparator));
		db.setPort(m_host.mid(portSeparator + 1).toInt());
	}
	else
	{ db.setHostName(m_host); }

	if (!db.open())
	{
		log(QString("Error initializing commands: %1").arg(db.lastError().text()), Logger::Error);
		return false;
	}

	m_started = true;
	return true;
}

QString SqlWorker::escape(const QVariant &val)
{
	QSqlDriver *driver = QSqlDatabase::database().driver();
	if (driver == nullptr)
		return nullptr;

	QSqlField f;
	f.setType(val.type());
	f.setValue(val);

	return driver->formatValue(f);
}

bool SqlWorker::execute(const QString &sql)
{
	if (!m_enabled || !connect())
		return false;

	log(QString("SQL execution of \"%1\"").arg(sql));
	Logger::getInstance().logCommandSql(sql);

	QSqlQuery query;
	return query.exec(sql);
}

{'fraction_non_alphanumeric': 0.1662621359223301, 'fraction_numerical': 0.003033980582524272, 'mean_word_length': 5.897489539748954, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 18, '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': '2112636', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1171, 'n_words': 238}