<commit_before>#include "Database.h"
#include "Defines.h"

#include <QDebug>
#include <QSqlError>

/** Database constructor
 * Constructs the local database.
 * Currently no interface to handle remote databases, just creates one in the
 * current working directory.
 */
Database::Database(QObject* parent)
{
    this->path = QDir(CONFIG_FOLDER).filePath("horizon.db");
}

Database::Database(QString path, QObject* parent)
{
    this->path = path;
}

/** Initialize the actual database, if it hasn't been done already.
 * \return Success/failure of the operation.
*/
bool Database::init()
{
    db = QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
    db.setHostName("localhost");
    db.setDatabaseName(this->path);

    bool status = db.open();
    qDebug() << db.lastError();
    if (!status)
    {
        qDebug("Couldn't connect to the database!");
        return false;
    }

    QSqlQuery createQuery(db);

    bool rtn = createQuery.exec("CREATE TABLE IF NOT EXISTS games(ID INTEGER PRIMARY KEY ASC, GAMENAME TEXT NOT NULL, GAMEDIRECTORY TEXT NOT NULL, GAMEEXECUTABLE TEXT NOT NULL, ARGUMENTS TEXT NOT NULL, DRM INT DEFAULT 0);");

    if (rtn)
    {
        emit dbChanged();
    }

    return rtn;
}

/** Remove every table in the database.
 * \return Success/failure of the operation.
*/
bool Database::reset()
{
    QSqlQuery query(db);
    bool rtn = query.exec("DROP TABLE IF EXISTS games");

    if (rtn)
    {
        emit dbChanged();
    }

    return rtn;
}

/** Add a game to the database and repopulate the games list.
 * \param gameName The name of the game.
 * \param gameDirectory Working directory of the game.
 * \param executablePath The location of the executable on the filesystem.
 * \param arguments List of arguments to launch with.
 * \param drm The DRM the game came from, where 0 = None, 1 = Steam, 2 = Origin, 3 = uPlay
 * \return Success/failure of the operation.
*/
bool Database::addGame(QString gameName, QString gameDirectory, QString executablePath, QString arguments, int drm)
{
    QSqlQuery query(db);
    query.prepare("INSERT OR IGNORE INTO GAMES(GAMENAME, GAMEDIRECTORY, GAMEEXECUTABLE, ARGUMENTS, DRM) VALUES (:gameName, :gameDirectory, :executablePath, :arguments, :drm);");
    query.bindValue(":gameName", gameName);
    query.bindValue(":gameDirectory", gameDirectory);
    query.bindValue(":executablePath", executablePath);
    query.bindValue(":arguments", arguments);
    query.bindValue(":drm", drm);
    bool rtn = query.exec();

    if (rtn)
    {<commit_msg>Proper database reset by recreating the 'games' table after dropping it<commit_after>#include "Database.h"
#include "Defines.h"

#include <QDebug>
#include <QSqlError>

/** Database constructor
 * Constructs the local database.
 * Currently no interface to handle remote databases, just creates one in the
 * current working directory.
 */
Database::Database(QObject* parent)
{
    this->path = QDir(CONFIG_FOLDER).filePath("horizon.db");
}

Database::Database(QString path, QObject* parent)
{
    this->path = path;
}

/** Initialize the actual database, if it hasn't been done already.
 * \return Success/failure of the operation.
*/
bool Database::init()
{
    db = QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
    db.setHostName("localhost");
    db.setDatabaseName(this->path);

    bool status = db.open();
    qDebug() << db.lastError();
    if (!status)
    {
        qDebug("Couldn't connect to the database!");
        return false;
    }

    QSqlQuery createQuery(db);

    bool rtn = createQuery.exec("CREATE TABLE IF NOT EXISTS games(ID INTEGER PRIMARY KEY ASC, GAMENAME TEXT NOT NULL, GAMEDIRECTORY TEXT NOT NULL, GAMEEXECUTABLE TEXT NOT NULL, ARGUMENTS TEXT NOT NULL, DRM INT DEFAULT 0);");

    if (rtn)
    {
        emit dbChanged();
    }

    return rtn;
}

/** Remove every table in the database.
 * \return Success/failure of the operation.
*/
bool Database::reset()
{
    QSqlQuery query(db);
    bool rtn = query.exec("DROP TABLE IF EXISTS games") && query.exec("CREATE TABLE IF NOT EXISTS games(ID INTEGER PRIMARY KEY ASC, GAMENAME TEXT NOT NULL, GAMEDIRECTORY TEXT NOT NULL, GAMEEXECUTABLE TEXT NOT NULL, ARGUMENTS TEXT NOT NULL, DRM INT DEFAULT 0);");

    if (rtn)
    {
        emit dbChanged();
    }

    return rtn;
}

/** Add a game to the database and repopulate the games list.
 * \param gameName The name of the game.
 * \param gameDirectory Working directory of the game.
 * \param executablePath The location of the executable on the filesystem.
 * \param arguments List of arguments to launch with.
 * \param drm The DRM the game came from, where 0 = None, 1 = Steam, 2 = Origin, 3 = uPlay
 * \return Success/failure of the operation.
*/
bool Database::addGame(QString gameName, QString gameDirectory, QString executablePath, QString arguments, int drm)
{
    QSqlQuery query(db);
    query.prepare("INSERT OR IGNORE INTO GAMES(GAMENAME, GAMEDIRECTORY, GAMEEXECUTABLE, ARGUMENTS, DRM) VALUES (:gameName, :gameDirectory, :executablePath, :arguments, :drm);");
    query.bindValue(":gameName", gameName);
    query.bindValue(":gameDirectory", gameDirectory);
    query.bindValue(":executablePath", executablePath);
    query.bindValue(":arguments", arguments);
    query.bindValue(":drm", drm);
    bool rtn = query.exec();

    if (rtn)
    {
{'fraction_non_alphanumeric': 0.11652861445783133, 'fraction_numerical': 0.0020707831325301206, 'mean_word_length': 3.7908025247971144, 'pattern_counts': {'":': 10, '<': 11, '<?xml version=': 0, '>': 13, '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': '466517', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1575, 'n_words': 540}