<commit_before>#include "Album.h"
#include "Artist.h"
#include "Debug.h"
#include "Match.h"
#include "Metatrack.h"
#include "PostgreSQL.h"
#include "Track.h"

using namespace std;

/* constructors/destructor */
PostgreSQL::PostgreSQL(const string connection) : Database(), pg_result(NULL), got_result(false) {
	pg_connection = PQconnectdb(connection.c_str());
	if (PQstatus(pg_connection) != CONNECTION_OK) {
		Debug::error("Unable to connect to the database");
		exit(1);
	}
	album_cache_lifetime = loadSettingInt(ALBUM_CACHE_LIFETIME_KEY, ALBUM_CACHE_LIFETIME_VALUE, ALBUM_CACHE_LIFETIME_DESCRIPTION);
	metatrack_cache_lifetime = loadSettingInt(METATRACK_CACHE_LIFETIME_KEY, METATRACK_CACHE_LIFETIME_VALUE, METATRACK_CACHE_LIFETIME_DESCRIPTION);
	puid_cache_lifetime = loadSettingInt(PUID_CACHE_LIFETIME_KEY, PUID_CACHE_LIFETIME_VALUE, PUID_CACHE_LIFETIME_DESCRIPTION);
}

PostgreSQL::~PostgreSQL() {
	clear();
	PQfinish(pg_connection);
}

/* methods */
bool PostgreSQL::loadAlbum(Album *album) {
	/* fetch album from cache */
	if (album == NULL)
		return false;
	if (album->mbid.size() != 36 || album->mbid[8] != '-' || album->mbid[13] != '-' || album->mbid[18] != '-' || album->mbid[23] != '-') {
		string msg = "Unable to load album from cache. Illegal MusicBrainz ID: ";
		msg.append(album->mbid);
		Debug::notice(msg);
		return false;
	}
	ostringstream query;
	query << "SELECT * FROM v_daemon_load_album WHERE album_mbid = '" << escapeString(album->mbid) << "' AND album_last_updated + INTERVAL '" << album_cache_lifetime << " months' > now()";
	if (!doQuery(query.str()) || getRows() <= 0) {
		/* album not in cache */
		string msg = "Unable to load album from cache. MusicBrainz ID not found or cache is too old: ";
		msg.append(album->mbid);
		Debug::notice(msg);
		return false;
	}
	/* album data */
	album->mbid = getString(0, 1);
	album->type = getString(0, 2);
	album->title = getString(0, 3);
	album->released = getString(0, 4);
	/* artist data */
	album->artist->mbid = getString(0, 7);
	album->artist->name = getString(0,8);
	album->artist->sortname = getString(0, 9);
	/* track data */
	int trackcount = getRows();
	album->tracks.resize(trackcount);
	for (int t = 0; t < trackcount; ++t) {
		int trackindex = getInt(t, 14) - 1;
		if (trackindex < 0 || trackindex >= (int) album->tracks.capacity()) {
			/* this really shouldn't happen.
			 * seemingly we're missing entries in the track table */
			string msg = "Tracknumber either exceed album track count or is less than 1. Did you mess with the track table? MusicBrainz Album ID: ";
			msg.append(album->mbid);
			Debug::warning(msg);
			return false;
		}
		album->tracks[trackindex] = new Track(album);
		/* track data */
		album->tracks[trackindex]->mbid = getString(t, 11);
		album->tracks[trackindex]->title = getString(t, 12);
		album->tracks[trackindex]->duration = getInt(t, 13);
		album->tracks[trackindex]->tracknumber = trackindex + 1;
		/* track artist data */
		album->tracks[trackindex]->artist->mbid = getString(t, 16);
		album->tracks[trackindex]->artist->name = getString(t, 17);
		album->tracks[trackindex]->artist->sortname = getString(t, 18);
	}
	return true;
}

bool PostgreSQL::loadMetafile(Metafile *metafile) {
	if (metafile == NULL)
		return false;
	if (metafile->filename.size() <= 0) {
		Debug::notice("Length of filename is 0 or less? Can't load that from cache");
		return false;
	}
	string e_filename = escapeString(metafile->filename);
	ostringstream query;
	query << "SELECT * FROM v_daemon_load_metafile WHERE filename = '" << e_filename << "'";
	if (!doQuery(query.str()) || getRows() <= 0) {
		string msg = "Didn't find file in database: ";
		msg.append(metafile->filename);
		Debug::notice(msg);
		return false;
	}
	metafile->duration = getInt(0, 2);
	metafile->channels = getInt(0, 3);
	metafile->bitrate = getInt(0, 4);
	metafile->samplerate = getInt(0, 5);
	metafile->puid = getString(0, 6);
	metafile->album = getString(0, 7);
	metafile->albumartist = getString(0, 8);
	metafile->albumartistsort = getString(0, 9);
	metafile->artist = getString(0, 10);
	metafile->artistsort = getString(0, 11);
	metafile->musicbrainz_albumartistid = getString(0, 12);
	metafile->musicbrainz_albumid = getString(0, 13);
	metafile->musicbrainz_artistid = getString(0, 14);
	metafile->musicbrainz_trackid = getString(0, 15);
	metafile->title = getString(0, 16);
	metafile->tracknumber = getString(0, 17);
	metafile->released = getString(0, 18);
	metafile->genre = getString(0, 19);
	metafile->pinned = getBool(0, 20);
	metafile->force_save = getBool(0, 21);
	return true;
}

vector<Metafile> PostgreSQL::loadMetafiles(const string &filename_pattern) {
	string e_filename_pattern = escapeString(filename_pattern);
	vector<Metafile> files;
	files.clear();
	ostringstream query;
	query << "SELECT * FROM v_daemon_load_metafile WHERE filename LIKE '" << e_filename_pattern << "%'";
	if (!doQuery(query.str()) || getRows() <= 0)
		return files;
	for (int r = 0; r < getRows(); ++r) {
		Metafile metafile(getString(r, 0));
		metafile.duration = getInt(r, 2);
		metafile.channels = getInt(r, 3);
		metafile.bitrate = getInt(r, 4);
		metafile.samplerate = getInt(r, 5);
		metafile.puid = getString(r, 6);
		metafile.album = getString(r, 7);
		metafile.albumartist = getString(r, 8);
		metafile.albumartistsort = getString(r, 9);
		metafile.artist = getString(r, 10);
		metafile.artistsort = getString(r, 11);
		metafile.musicbrainz_albumartistid = getString(r, 12);
		metafile.musicbrainz_albumid = getString(r, 13);
		metafile.musicbrainz_artistid = getString(r, 14);
		metafile.musicbrainz_trackid = getString(r, 15);
		metafile.title = getString(r, 16);
		metafile.tracknumber = getString(r, 17);
		metafile.released = getString(r, 18);
		metafile.genre = getString(r, 19);
		metafile.pinned = getBool(r, 20);
		metafile.force_save = getBool(r, 21);
		files.push_back(metafile);
	}
	return files;
}

bool PostgreSQL::loadSettingBool(const string &key, bool default_value, const string &description) {
	string def_val = (default_value ? "true" : "false");
	return (loadSettingString(key, def_val, description) == "true");
}

double PostgreSQL::loadSettingDouble(const string &key, double default_value, const string &description) {
	ostringstream def_val;
	def_val << default_value;
	return atof(loadSettingString(key, def_val.str(), description).c_str());
}

int PostgreSQL::loadSettingInt(const string &key, int default_value, const string &description) {
	ostringstream def_val;
	def_val << default_value;
	return atoi(loadSettingString(key, def_val.str(), description).c_str());
}

string PostgreSQL::loadSettingString(const string &key, const string &default_value, const string &description) {<commit_msg>fixed bug when loading metafiles from cache<commit_after>#include "Album.h"
#include "Artist.h"
#include "Debug.h"
#include "Match.h"
#include "Metatrack.h"
#include "PostgreSQL.h"
#include "Track.h"

using namespace std;

/* constructors/destructor */
PostgreSQL::PostgreSQL(const string connection) : Database(), pg_result(NULL), got_result(false) {
	pg_connection = PQconnectdb(connection.c_str());
	if (PQstatus(pg_connection) != CONNECTION_OK) {
		Debug::error("Unable to connect to the database");
		exit(1);
	}
	album_cache_lifetime = loadSettingInt(ALBUM_CACHE_LIFETIME_KEY, ALBUM_CACHE_LIFETIME_VALUE, ALBUM_CACHE_LIFETIME_DESCRIPTION);
	metatrack_cache_lifetime = loadSettingInt(METATRACK_CACHE_LIFETIME_KEY, METATRACK_CACHE_LIFETIME_VALUE, METATRACK_CACHE_LIFETIME_DESCRIPTION);
	puid_cache_lifetime = loadSettingInt(PUID_CACHE_LIFETIME_KEY, PUID_CACHE_LIFETIME_VALUE, PUID_CACHE_LIFETIME_DESCRIPTION);
}

PostgreSQL::~PostgreSQL() {
	clear();
	PQfinish(pg_connection);
}

/* methods */
bool PostgreSQL::loadAlbum(Album *album) {
	/* fetch album from cache */
	if (album == NULL)
		return false;
	if (album->mbid.size() != 36 || album->mbid[8] != '-' || album->mbid[13] != '-' || album->mbid[18] != '-' || album->mbid[23] != '-') {
		string msg = "Unable to load album from cache. Illegal MusicBrainz ID: ";
		msg.append(album->mbid);
		Debug::notice(msg);
		return false;
	}
	ostringstream query;
	query << "SELECT * FROM v_daemon_load_album WHERE album_mbid = '" << escapeString(album->mbid) << "' AND album_last_updated + INTERVAL '" << album_cache_lifetime << " months' > now()";
	if (!doQuery(query.str()) || getRows() <= 0) {
		/* album not in cache */
		string msg = "Unable to load album from cache. MusicBrainz ID not found or cache is too old: ";
		msg.append(album->mbid);
		Debug::notice(msg);
		return false;
	}
	/* album data */
	album->mbid = getString(0, 1);
	album->type = getString(0, 2);
	album->title = getString(0, 3);
	album->released = getString(0, 4);
	/* artist data */
	album->artist->mbid = getString(0, 7);
	album->artist->name = getString(0,8);
	album->artist->sortname = getString(0, 9);
	/* track data */
	int trackcount = getRows();
	album->tracks.resize(trackcount);
	for (int t = 0; t < trackcount; ++t) {
		int trackindex = getInt(t, 14) - 1;
		if (trackindex < 0 || trackindex >= (int) album->tracks.capacity()) {
			/* this really shouldn't happen.
			 * seemingly we're missing entries in the track table */
			string msg = "Tracknumber either exceed album track count or is less than 1. Did you mess with the track table? MusicBrainz Album ID: ";
			msg.append(album->mbid);
			Debug::warning(msg);
			return false;
		}
		album->tracks[trackindex] = new Track(album);
		/* track data */
		album->tracks[trackindex]->mbid = getString(t, 11);
		album->tracks[trackindex]->title = getString(t, 12);
		album->tracks[trackindex]->duration = getInt(t, 13);
		album->tracks[trackindex]->tracknumber = trackindex + 1;
		/* track artist data */
		album->tracks[trackindex]->artist->mbid = getString(t, 16);
		album->tracks[trackindex]->artist->name = getString(t, 17);
		album->tracks[trackindex]->artist->sortname = getString(t, 18);
	}
	return true;
}

bool PostgreSQL::loadMetafile(Metafile *metafile) {
	if (metafile == NULL)
		return false;
	if (metafile->filename.size() <= 0) {
		Debug::notice("Length of filename is 0 or less? Can't load that from cache");
		return false;
	}
	string e_filename = escapeString(metafile->filename);
	ostringstream query;
	query << "SELECT * FROM v_daemon_load_metafile WHERE filename = '" << e_filename << "'";
	if (!doQuery(query.str()) || getRows() <= 0) {
		string msg = "Didn't find file in database: ";
		msg.append(metafile->filename);
		Debug::notice(msg);
		return false;
	}
	metafile->duration = getInt(0, 1);
	metafile->channels = getInt(0, 2);
	metafile->bitrate = getInt(0, 3);
	metafile->samplerate = getInt(0, 4);
	metafile->puid = getString(0, 5);
	metafile->album = getString(0, 6);
	metafile->albumartist = getString(0, 7);
	metafile->albumartistsort = getString(0, 8);
	metafile->artist = getString(0, 9);
	metafile->artistsort = getString(0, 10);
	metafile->musicbrainz_albumartistid = getString(0, 11);
	metafile->musicbrainz_albumid = getString(0, 12);
	metafile->musicbrainz_artistid = getString(0, 13);
	metafile->musicbrainz_trackid = getString(0, 14);
	metafile->title = getString(0, 15);
	metafile->tracknumber = getString(0, 16);
	metafile->released = getString(0, 17);
	metafile->genre = getString(0, 18);
	metafile->pinned = getBool(0, 19);
	metafile->force_save = getBool(0, 20);
	return true;
}

vector<Metafile> PostgreSQL::loadMetafiles(const string &filename_pattern) {
	string e_filename_pattern = escapeString(filename_pattern);
	vector<Metafile> files;
	files.clear();
	ostringstream query;
	query << "SELECT * FROM v_daemon_load_metafile WHERE filename LIKE '" << e_filename_pattern << "%'";
	if (!doQuery(query.str()) || getRows() <= 0)
		return files;
	for (int r = 0; r < getRows(); ++r) {
		Metafile metafile(getString(r, 0));
		metafile.duration = getInt(r, 1);
		metafile.channels = getInt(r, 2);
		metafile.bitrate = getInt(r, 3);
		metafile.samplerate = getInt(r, 4);
		metafile.puid = getString(r, 5);
		metafile.album = getString(r, 6);
		metafile.albumartist = getString(r, 7);
		metafile.albumartistsort = getString(r, 8);
		metafile.artist = getString(r, 9);
		metafile.artistsort = getString(r, 10);
		metafile.musicbrainz_albumartistid = getString(r, 11);
		metafile.musicbrainz_albumid = getString(r, 12);
		metafile.musicbrainz_artistid = getString(r, 13);
		metafile.musicbrainz_trackid = getString(r, 14);
		metafile.title = getString(r, 15);
		metafile.tracknumber = getString(r, 16);
		metafile.released = getString(r, 17);
		metafile.genre = getString(r, 18);
		metafile.pinned = getBool(r, 19);
		metafile.force_save = getBool(r, 20);
		files.push_back(metafile);
	}
	return files;
}

bool PostgreSQL::loadSettingBool(const string &key, bool default_value, const string &description) {
	string def_val = (default_value ? "true" : "false");
	return (loadSettingString(key, def_val, description) == "true");
}

double PostgreSQL::loadSettingDouble(const string &key, double default_value, const string &description) {
	ostringstream def_val;
	def_val << default_value;
	return atof(loadSettingString(key, def_val.str(), description).c_str());
}

int PostgreSQL::loadSettingInt(const string &key, int default_value, const string &description) {
	ostringstream def_val;
	def_val << default_value;
	return atoi(loadSettingString(key, def_val.str(), description).c_str());
}

string PostgreSQL::loadSettingString(const string &key, const string &default_value, const string &description) {
{'fraction_non_alphanumeric': 0.15258530770376993, 'fraction_numerical': 0.019818208910743554, 'mean_word_length': 5.27536231884058, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 135, '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': '2809623', 'n_tokens_mistral': 5133, 'n_tokens_neox': 4708, 'n_words': 1054}