<commit_before>#include <postgresql/libpq-fe.h>
#include <string>
#include <stdlib.h>

class DBResult {
	PGresult *res;
	public:
		DBResult(PGresult* inRes) : res(inRes) {}
		DBResult(PGconn* con, std::string query) {
			res = PQexec(con, query.c_str());
		}
		DBResult(DBResult&& other) { //move constructor
			res = other.res;
			other.res = nullptr;
		}
		~DBResult() { PQclear(res); }
		DBResult& operator=(DBResult&& other) { //move assignment operator
			if (res)
				PQclear(res);
			res = other.res;
			other.res = nullptr;
			return *this;
		}
		operator PGresult*() const { return res; }

		bool commandOK() const {
			ExecStatusType rc = PQresultStatus(res);
			return PGRES_COMMAND_OK == rc;
		}
		bool dataReturned() const {
			ExecStatusType rc = PQresultStatus(res);
			return PGRES_TUPLES_OK == rc;
		}
		const char *errorMessage() {
			return PQresultErrorMessage(res);
		}
		int rowsAffected() const {
			return atoi(PQcmdTuples(res));
		}
};

class DBTransaction {
	PGconn *con_;
	bool open_;
	public:
		DBTransaction(PGconn *con) : con_(con), open_(true) {
			DBResult res(con_, "begin");
		}
		~DBTransaction() {
			if (open_)
				DBResult(con_, "rollback");
		}
		DBResult commit() {
			DBResult res(con_, "commit");
			open_ = !res.commandOK();
			return res;
		}
};<commit_msg>added function to fetch a long from a simple query<commit_after>#pragma once

#include <postgresql/libpq-fe.h>
#include <string>
#include <stdlib.h>

class DBResult {
	PGresult *res;
	public:
		DBResult(PGresult* inRes) : res(inRes) {}
		DBResult(PGconn* con, std::string query) {
			res = PQexec(con, query.c_str());
		}
		DBResult(DBResult&& other) { //move constructor
			res = other.res;
			other.res = nullptr;
		}
		~DBResult() { PQclear(res); }
		DBResult& operator=(DBResult&& other) { //move assignment operator
			if (res)
				PQclear(res);
			res = other.res;
			other.res = nullptr;
			return *this;
		}
		operator PGresult*() const { return res; }

		bool commandOK() const {
			ExecStatusType rc = PQresultStatus(res);
			return PGRES_COMMAND_OK == rc;
		}
		bool dataReturned() const {
			ExecStatusType rc = PQresultStatus(res);
			return PGRES_TUPLES_OK == rc;
		}
		const char *errorMessage() {
			return PQresultErrorMessage(res);
		}
		int rowsAffected() const {
			return atoi(PQcmdTuples(res));
		}
};

class DBTransaction {
	PGconn *con_;
	bool open_;
	public:
		DBTransaction(PGconn *con) : con_(con), open_(true) {
			DBResult res(con_, "begin");
		}
		~DBTransaction() {
			if (open_)
				DBResult(con_, "rollback");
		}
		DBResult commit() {
			DBResult res(con_, "commit");
			open_ = !res.commandOK();
			return res;
		}
};

inline long DBLongFromQuery(PGconn *db, const char *query) 
{
	long value = 0;
	DBResult res(PQexec(db, query));
	if (res.dataReturned()) {
		value = atol(PQgetvalue(res, 0, 0));
	} else {
		throw std::runtime_error(res.errorMessage());
	}
	return value;
}
{'fraction_non_alphanumeric': 0.1449673651666094, 'fraction_numerical': 0.0010305736860185502, 'mean_word_length': 3.7504078303425774, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6505122', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1060, 'n_words': 249}