<commit_before>/************************************ ABOUT ***********************************\
| This function class is responsible for reading the settings data from the    |
| internal settings database. It also has some functions for updating the      |
| rules inside the databse.                                                    |
\******************************************************************************/
// This file should be auto generated by the generate schema script //

#include <sqlite3.h>
#include <string>

#include "./DomainSettings.h"

/******************************** SETTINGS API ********************************\
| This class handles all the data to and from the database. It involves        |
| getting a single setting from the databsae as well as updating the database  |
| from the desired source                                                      |
\******************************************************************************/
class SettingsAPI {
  private:
    sqlite3 *database;  // What is the SQLite variable?
    bool isOpened;
    DomainSettings SQLToSettings(sqlite3_stmt *statement);
  public:
    SettingsAPI();
    ~SettingsAPI();
    bool IsOpen();  // Check to see if the current database opened sucessfully
    DomainSettings GetSettings(std::string domain);  // get the settings for a domain
    DomainSettings GetSettings(std::string domain, int version);
    void Disconnect();  // This might not be needed / this should be done in cleanup
};

/************************ SETTINGS API :: INITILIZATION ***********************\
| This function instanciates a connection to the sqlite databse. If the        |
| database is not found at the given file it will be created                   |
\******************************************************************************/
SettingsAPI::SettingsAPI() {
    // Open the database
    int rc = sqlite3_open_v2("./passworddatabse", &(this->database), SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (rc) {
        this->isOpened = false;
    }
    else {
        this->isOpened = true;
    }
}


// close the database on deconstruct
SettingsAPI::~SettingsAPI() {
    sqlite3_close(this->database);
}

/*************************** SETTINGS API :: IS OPEN **************************\
| This function is a quick check to see if the sqlite file is open or if some  |
| error has happened durring a previous step. The variable isOpened maintains  |
| the status of the file. There may be a better way to do this using the       |
| sqlite3 functions to check to see if the file is still active or not         |
\******************************************************************************/
bool SettingsAPI::IsOpen() {
    return (this->isOpened);
}

/************************ SETTINGS API :: GET SETTINGS ************************\
| This function takes in a string and an integer. It gets a single set of      |
| settings for a domian. If the integer is specified then that version of the  |
| setting is returned. If the integer is zero then the newest version of the   |
| setting is obtained                                                          |
\******************************************************************************/
DomainSettings SettingsAPI::GetSettings(std::string domain) { return GetSettings(domain, 0); }
DomainSettings SettingsAPI::GetSettings(std::string domain, int version) {
    std::string rawSQL = "SELECT * FROM rules";

    if (version == 0) { 
        rawSQL = "SELECT * FROM rules WHERE (version) IN (SELECT MAX(version) FROM rules WHERE domain = ? GROUP BY domain)";
    }
    // prepare statement
    sqlite3_stmt *statement;
    const char *tailPointer;  // return value for the last
    int returnCode = sqlite3_prepare_v2(this->database, rawSQL.c_str(), rawSQL.length(),  &statement, &tailPointer);
    sqlite3_bind_text (statement, 1, domain.c_str(), domain.length(), NULL);
    if (returnCode) {
        fprintf(stderr, "Error with qeurry:%i: %s\n", returnCode, sqlite3_errmsg(this->database));
        // If the error code is 1 (which i think is "no such table") then create the table
        if (returnCode == 1) {
            // Create the database using the autogenerated query in DomainSettings
            int dbCreateResultCode = sqlite3_exec(this->database, DomainSettings::CreateTableSQL().c_str(), NULL, NULL, NULL);
            if (dbCreateResultCode) {
                fprintf(stderr, "Error Creating Table: %i: %s\n", dbCreateResultCode, sqlite3_errmsg(this->database));
            }
            // if the table was sucessfully created then run this command again to get the data from the table
            else {
                return GetSettings(domain, version);
            }
        }
        return (DomainSettings(domain));
    }

    // Step to the first element?
    int stepResult = sqlite3_step(statement);
    if (stepResult = SQLITE_DONE) {
        return DomainSettings(domain);
    }
    // Get the results
    DomainSettings settings = DomainSettings(statement);

    // finalize query
    sqlite3_finalize(statement);

    // Return the final settings object
    return settings;
}<commit_msg>added a second indetical bind to the rule lookup query This allows the actual row of the database to be found instead of pulling every row with the same version as the max version of the row we are actually looking for;<commit_after>/************************************ ABOUT ***********************************\
| This function class is responsible for reading the settings data from the    |
| internal settings database. It also has some functions for updating the      |
| rules inside the databse.                                                    |
\******************************************************************************/
// This file should be auto generated by the generate schema script //

#include <sqlite3.h>
#include <string>

#include "./DomainSettings.h"

/******************************** SETTINGS API ********************************\
| This class handles all the data to and from the database. It involves        |
| getting a single setting from the databsae as well as updating the database  |
| from the desired source                                                      |
\******************************************************************************/
class SettingsAPI {
  private:
    sqlite3 *database;  // What is the SQLite variable?
    bool isOpened;
    DomainSettings SQLToSettings(sqlite3_stmt *statement);
  public:
    SettingsAPI();
    ~SettingsAPI();
    bool IsOpen();  // Check to see if the current database opened sucessfully
    DomainSettings GetSettings(std::string domain);  // get the settings for a domain
    DomainSettings GetSettings(std::string domain, int version);
    void Disconnect();  // This might not be needed / this should be done in cleanup
};

/************************ SETTINGS API :: INITILIZATION ***********************\
| This function instanciates a connection to the sqlite databse. If the        |
| database is not found at the given file it will be created                   |
\******************************************************************************/
SettingsAPI::SettingsAPI() {
    // Open the database
    int rc = sqlite3_open_v2("./passworddatabse", &(this->database), SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (rc) {
        this->isOpened = false;
    }
    else {
        this->isOpened = true;
    }
}


// close the database on deconstruct
SettingsAPI::~SettingsAPI() {
    sqlite3_close(this->database);
}

/*************************** SETTINGS API :: IS OPEN **************************\
| This function is a quick check to see if the sqlite file is open or if some  |
| error has happened durring a previous step. The variable isOpened maintains  |
| the status of the file. There may be a better way to do this using the       |
| sqlite3 functions to check to see if the file is still active or not         |
\******************************************************************************/
bool SettingsAPI::IsOpen() {
    return (this->isOpened);
}

/************************ SETTINGS API :: GET SETTINGS ************************\
| This function takes in a string and an integer. It gets a single set of      |
| settings for a domian. If the integer is specified then that version of the  |
| setting is returned. If the integer is zero then the newest version of the   |
| setting is obtained                                                          |
\******************************************************************************/
DomainSettings SettingsAPI::GetSettings(std::string domain) { return GetSettings(domain, 0); }
DomainSettings SettingsAPI::GetSettings(std::string domain, int version) {
    std::string rawSQL = "SELECT * FROM rules";

    if (version == 0) { 
        rawSQL = "SELECT * FROM rules WHERE domain = ? AND version IN (SELECT MAX(version) FROM rules WHERE domain = ? GROUP BY domain)";
    }
    // prepare statement
    sqlite3_stmt *statement;
    const char *tailPointer;  // return value for the last
    int returnCode = sqlite3_prepare_v2(this->database, rawSQL.c_str(), rawSQL.length(),  &statement, &tailPointer);
    sqlite3_bind_text (statement, 1, domain.c_str(), domain.length(), NULL);
    sqlite3_bind_text (statement, 2, domain.c_str(), domain.length(), NULL);

    if (returnCode) {
        fprintf(stderr, "Error with qeurry:%i: %s\n", returnCode, sqlite3_errmsg(this->database));
        // If the error code is 1 (which i think is "no such table") then create the table
        if (returnCode == 1) {
            // Create the database using the autogenerated query in DomainSettings
            int dbCreateResultCode = sqlite3_exec(this->database, DomainSettings::CreateTableSQL().c_str(), NULL, NULL, NULL);
            if (dbCreateResultCode) {
                fprintf(stderr, "Error Creating Table: %i: %s\n", dbCreateResultCode, sqlite3_errmsg(this->database));
            }
            // if the table was sucessfully created then run this command again to get the data from the table
            else {
                return GetSettings(domain, version);
            }
        }
        return (DomainSettings(domain));
    }

    // Step to the first element?
    int stepResult = sqlite3_step(statement);
    if (stepResult = SQLITE_DONE) {
        return DomainSettings(domain);
    }
    // Get the results
    DomainSettings settings = DomainSettings(statement);

    // finalize query
    sqlite3_finalize(statement);

    // Return the final settings object
    return settings;
}
{'fraction_non_alphanumeric': 0.20253640206669798, 'fraction_numerical': 0.004133395960544857, 'mean_word_length': 3.15696993361968, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3725749', 'n_tokens_mistral': 2625, 'n_tokens_neox': 2496, 'n_words': 987}