<commit_before>#include "clientdb.h"

ClientDb::ClientDb(QString pathToFile, QObject *parent) : QObject(parent)
{
	sqlite3_open_v2(pathToFile.toStdString().c_str(), &mDbFile, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr);

	sqlite3_stmt * stmt;
	if (sqlite3_prepare_v2(mDbFile, "CREATE TABLE IF NOT EXISTS Clients (username TEXT UNIQUE, password TEXT, ip TEXT, port INTEGER, timeout INTEGER)", -1, &stmt, nullptr) != SQLITE_OK) throw DBException("Can't prepare statement for database creation!");
	if (sqlite3_step(stmt) != SQLITE_DONE)		throw DBException("Can't do statement step for database creation!");
	if (sqlite3_finalize(stmt) != SQLITE_OK)	throw DBException("Can't finalize statement for database creation!");
}

ClientDb::~ClientDb()
{
	sqlite3_close_v2(mDbFile);
}

bool ClientDb::addNewClient(const char *nick, const char *passwordHash)
{
	sqlite3_stmt * stmt;
	if(sqlite3_prepare_v2(mDbFile, "INSERT INTO Clients (username, password) VALUES(?, ?)", -1, &stmt, nullptr) != SQLITE_OK) throw DBException("Can't prepare statement in addNewClient!");
	if(sqlite3_bind_text(stmt, 1, nick,			-1, SQLITE_STATIC) != SQLITE_OK) throw DBException("Can't bind statement parameter 1 in addNewClient!");
	if(sqlite3_bind_text(stmt, 2, passwordHash, -1, SQLITE_STATIC) != SQLITE_OK) throw DBException("Can't bind statement parameter 2 in addNewClient!");
	if(sqlite3_step(stmt) != SQLITE_DONE)		throw DBException("Can't do statement step in addNewClient!");
	if (sqlite3_finalize(stmt) != SQLITE_OK)	throw DBException("Can't finalize statement in addNewClient!");
	return true;
}

bool ClientDb::verifyClient(const char *nick, const char *passwordHash) const
{
	bool valid = false;
	sqlite3_stmt * stmt;
	if(sqlite3_prepare_v2(mDbFile, "SELECT password FROM Clients WHERE username = ?", -1, &stmt, nullptr) != SQLITE_OK) throw DBException("Can't prepare statement in verifyClient!");
	if(sqlite3_bind_text(stmt, 1, nick, -1, SQLITE_STATIC) != SQLITE_OK) throw DBException("Can't bind statement parameter 1 in verifyClient!");
	if(sqlite3_step(stmt) != SQLITE_ROW) throw DBException("Can't do statement step in verifyClient!");
	if(!strcmp(reinterpret_cast<const char *>(passwordHash), reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)))) valid = true;
	if(sqlite3_finalize(stmt) != SQLITE_OK) throw DBException("Can't finalize statement in verifyClient!");
	return valid;
}

bool ClientDb::loginClient(const char * nick, const char * address, int port)
{
	sqlite3_stmt * stmt;
	if (sqlite3_prepare_v2(mDbFile, "UPDATE Clients SET ip = ?, port = ?, timeout = strftime('%s','now','+10 minutes') WHERE username = ?", -1, &stmt, nullptr) != SQLITE_OK) throw DBException("Can't prepare statement in loginClient!");
	if (sqlite3_bind_text(stmt, 1, address, -1, SQLITE_STATIC)	!= SQLITE_OK) throw DBException("Can't bind statement parameter 1 in loginClient!");
	if (sqlite3_bind_int(stmt, 2, port)							!= SQLITE_OK) throw DBException("Can't bind statement parameter 2 in loginClient!");
	if (sqlite3_bind_text(stmt, 3, nick,	-1, SQLITE_STATIC)	!= SQLITE_OK) throw DBException("Can't bind statement parameter 3 in loginClient!");
	if (sqlite3_step(stmt) != SQLITE_DONE)		throw DBException("Can't do statement step in loginClient!");
	if (sqlite3_finalize(stmt) != SQLITE_OK)	throw DBException("Can't finalize statement in loginClient!");
<commit_msg>addNewClient returns false if client exists<commit_after>#include "clientdb.h"

ClientDb::ClientDb(QString pathToFile, QObject *parent) : QObject(parent)
{
	sqlite3_open_v2(pathToFile.toStdString().c_str(), &mDbFile, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr);

	sqlite3_stmt * stmt;
	if (sqlite3_prepare_v2(mDbFile, "CREATE TABLE IF NOT EXISTS Clients (username TEXT UNIQUE, password TEXT, ip TEXT, port INTEGER, timeout INTEGER)", -1, &stmt, nullptr) != SQLITE_OK) throw DBException("Can't prepare statement for database creation!");
	if (sqlite3_step(stmt) != SQLITE_DONE)		throw DBException("Can't do statement step for database creation!");
	if (sqlite3_finalize(stmt) != SQLITE_OK)	throw DBException("Can't finalize statement for database creation!");
}

ClientDb::~ClientDb()
{
	sqlite3_close_v2(mDbFile);
}

bool ClientDb::addNewClient(const char *nick, const char *passwordHash)
{
	bool ret = true;
	sqlite3_stmt * stmt;
	if (sqlite3_prepare_v2(mDbFile, "INSERT INTO Clients (username, password) VALUES(?, ?)", -1, &stmt, nullptr) != SQLITE_OK) throw DBException("Can't prepare statement in addNewClient!");
	if (sqlite3_bind_text(stmt, 1, nick,			-1, SQLITE_STATIC) != SQLITE_OK) throw DBException("Can't bind statement parameter 1 in addNewClient!");
	if (sqlite3_bind_text(stmt, 2, passwordHash, -1, SQLITE_STATIC) != SQLITE_OK) throw DBException("Can't bind statement parameter 2 in addNewClient!");
	int e = sqlite3_step(stmt);
	if (e == SQLITE_CONSTRAINT) ret = false;
	else if (e != SQLITE_DONE)	throw DBException("Can't do statement step in addNewClient!", e);
	
	e = sqlite3_finalize(stmt);
	if (e != SQLITE_OK && e != SQLITE_CONSTRAINT)	throw DBException("Can't finalize statement in addNewClient!");
	return true;
}

bool ClientDb::verifyClient(const char *nick, const char *passwordHash) const
{
	bool valid = false;
	sqlite3_stmt * stmt;
	if(sqlite3_prepare_v2(mDbFile, "SELECT password FROM Clients WHERE username = ?", -1, &stmt, nullptr) != SQLITE_OK) throw DBException("Can't prepare statement in verifyClient!");
	if(sqlite3_bind_text(stmt, 1, nick, -1, SQLITE_STATIC) != SQLITE_OK) throw DBException("Can't bind statement parameter 1 in verifyClient!");
	if(sqlite3_step(stmt) != SQLITE_ROW) throw DBException("Can't do statement step in verifyClient!");
	if(!strcmp(reinterpret_cast<const char *>(passwordHash), reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)))) valid = true;
	if(sqlite3_finalize(stmt) != SQLITE_OK) throw DBException("Can't finalize statement in verifyClient!");
	return valid;
}

bool ClientDb::loginClient(const char * nick, const char * address, int port)
{
	sqlite3_stmt * stmt;
	if (sqlite3_prepare_v2(mDbFile, "UPDATE Clients SET ip = ?, port = ?, timeout = strftime('%s','now','+10 minutes') WHERE username = ?", -1, &stmt, nullptr) != SQLITE_OK) throw DBException("Can't prepare statement in loginClient!");
	if (sqlite3_bind_text(stmt, 1, address, -1, SQLITE_STATIC)	!= SQLITE_OK) throw DBException("Can't bind statement parameter 1 in loginClient!");
	if (sqlite3_bind_int(stmt, 2, port)							!= SQLITE_OK) throw DBException("Can't bind statement parameter 2 in loginClient!");
	if (sqlite3_bind_text(stmt, 3, nick,	-1, SQLITE_STATIC)	!= SQLITE_OK) throw DBException("Can't bind statement parameter 3 in loginClient!");
	if (sqlite3_step(stmt) != SQLITE_DONE)		throw DBException("Can't do statement step in loginClient!");
	if (sqlite3_finalize(stmt) != SQLITE_OK)	throw DBException("Can't finalize statement in loginClient!");

{'fraction_non_alphanumeric': 0.12825738826612315, 'fraction_numerical': 0.016013975833454652, 'mean_word_length': 6.960602549246813, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4904800', 'n_tokens_mistral': 2361, 'n_tokens_neox': 2254, 'n_words': 642}