<commit_before>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */

#include "NativeSimpleDBInterface.h"

#include "NativeException.h"
#include "NativeLoggerObject.h"
#include "NativeMessageStrings.h"

#include <QSqlError>
#include <QSqlQuery>

using namespace std;


NativeSimpleDBInterface::NativeSimpleDBInterface(string dbPath, string table)
    : db_(QSqlDatabase::addDatabase("QSQLITE"))
    , dbPath_(QString::fromUtf8(dbPath.c_str()))
    , table_(QString::fromUtf8(table.c_str()))
{
    db_.setDatabaseName(QString::fromUtf8(dbPath.c_str()));
}

NativeSimpleDBInterface::~NativeSimpleDBInterface()
{
    if (db_.isOpen())
    {
        db_.close();
    }
}

void NativeSimpleDBInterface::open()
{
    if (db_.isOpen())
    {
        return;
    }

    if (!db_.open())
    {
        throw NativeException(QString(Native::Msg::Failed_to_open_database).arg(dbPath_, db_.lastError().text()).toStdString());
    }

    QSqlQuery query(db_);
    query.exec("CREATE TABLE IF NOT EXISTS " + table_ + " (simpleKey VARCHAR(255) PRIMARY KEY, simpleValue TEXT)");
    QSqlError sqlError = query.lastError();
    if (sqlError.isValid())
    {
        throw NativeException(QString(Native::Msg::Create_table_failed).arg(dbPath_, table_, sqlError.text()).toStdString());
    }
}

string NativeSimpleDBInterface::get(const string& key)
{
    open();
    QSqlQuery query(db_);
    query.prepare("SELECT simpleValue FROM " + table_ + " WHERE simpleKey = :simpleKey");
    QString keyQString = QString::fromUtf8(key.c_str());
    query.bindValue(":simpleKey", keyQString);
    query.exec();
    QSqlError sqlError = query.lastError();
    if (sqlError.isValid())
    {
        throw NativeException(QString(Native::Msg::Failed_to_get_key).arg(dbPath_, keyQString, sqlError.text()).toStdString());
    }

    if (query.next())
    {
        return query.value(0).toString().toStdString();
    }
    return "";
}

void NativeSimpleDBInterface::set(const string& key, const string& value)
{
    open();
    QSqlQuery query(db_);
    query.prepare("INSERT INTO " + table_ + " (simpleKey, simpleValue) VALUES (:simpleKey, :simpleValue)");
    QString keyQString = QString::fromUtf8(key.c_str());
    query.bindValue(":simpleKey", keyQString);
    query.bindValue(":simpleValue", QString::fromUtf8(value.c_str()));
    query.exec();
    QSqlError sqlError = query.lastError();
    if (sqlError.isValid())
    {
        throw NativeException(QString(Native::Msg::Failed_to_set_key).arg(dbPath_, keyQString, sqlError.text()).toStdString());
    }
}

bool NativeSimpleDBInterface::contains(const string& key)
{
    open();
    QSqlQuery query(db_);
    query.prepare("SELECT simpleValue FROM " + table_ + " WHERE simpleKey = :simpleKey");
    QString keyQString = QString::fromUtf8(key.c_str());
    query.bindValue(":simpleKey", keyQString);
    query.exec();
    QSqlError sqlError = query.lastError();
    if (sqlError.isValid())
    {
        throw NativeException(QString(Native::Msg::Failed_to_check_for_key).arg(dbPath_, keyQString, sqlError.text()).toStdString());
    }
<commit_msg>TIMOB-10163: BlackBerry: Add BlackBerry to anvil test harness app<commit_after>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */

#include "NativeSimpleDBInterface.h"

#include "NativeException.h"
#include "NativeLoggerObject.h"
#include "NativeMessageStrings.h"

#include <QSqlError>
#include <QSqlQuery>

using namespace std;


NativeSimpleDBInterface::NativeSimpleDBInterface(string dbPath, string table)
    : db_(QSqlDatabase::addDatabase("QSQLITE"))
    , dbPath_(QString::fromUtf8(dbPath.c_str()))
    , table_(QString::fromUtf8(table.c_str()))
{
    db_.setDatabaseName(QString::fromUtf8(dbPath.c_str()));
}

NativeSimpleDBInterface::~NativeSimpleDBInterface()
{
    if (db_.isOpen())
    {
        db_.close();
    }
}

void NativeSimpleDBInterface::open()
{
    if (db_.isOpen())
    {
        return;
    }

    if (!db_.open())
    {
        throw NativeException(QString(Native::Msg::Failed_to_open_database).arg(dbPath_, db_.lastError().text()).toStdString());
    }

    QSqlQuery query(db_);
    query.exec("CREATE TABLE IF NOT EXISTS " + table_ + " (simpleKey VARCHAR(255) PRIMARY KEY, simpleValue TEXT)");
    QSqlError sqlError = query.lastError();
    if (sqlError.isValid())
    {
        throw NativeException(QString(Native::Msg::Create_table_failed).arg(dbPath_, table_, sqlError.text()).toStdString());
    }
}

string NativeSimpleDBInterface::get(const string& key)
{
    open();
    QSqlQuery query(db_);
    query.prepare("SELECT simpleValue FROM " + table_ + " WHERE simpleKey = :simpleKey");
    QString keyQString = QString::fromUtf8(key.c_str());
    query.bindValue(":simpleKey", keyQString);
    query.exec();
    QSqlError sqlError = query.lastError();
    if (sqlError.isValid())
    {
        throw NativeException(QString(Native::Msg::Failed_to_get_key).arg(dbPath_, keyQString, sqlError.text()).toStdString());
    }

    if (query.next())
    {
        return query.value(0).toString().toStdString();
    }
    return "";
}

void NativeSimpleDBInterface::set(const string& key, const string& value)
{
    open();
    QSqlQuery query(db_);
    query.prepare("REPLACE INTO " + table_ + " (simpleKey, simpleValue) VALUES (:simpleKey, :simpleValue)");
    QString keyQString = QString::fromUtf8(key.c_str());
    query.bindValue(":simpleKey", keyQString);
    query.bindValue(":simpleValue", QString::fromUtf8(value.c_str()));
    query.exec();
    QSqlError sqlError = query.lastError();
    if (sqlError.isValid())
    {
        throw NativeException(QString(Native::Msg::Failed_to_set_key).arg(dbPath_, keyQString, sqlError.text()).toStdString());
    }
}

bool NativeSimpleDBInterface::contains(const string& key)
{
    open();
    QSqlQuery query(db_);
    query.prepare("SELECT simpleValue FROM " + table_ + " WHERE simpleKey = :simpleKey");
    QString keyQString = QString::fromUtf8(key.c_str());
    query.bindValue(":simpleKey", keyQString);
    query.exec();
    QSqlError sqlError = query.lastError();
    if (sqlError.isValid())
    {
        throw NativeException(QString(Native::Msg::Failed_to_check_for_key).arg(dbPath_, keyQString, sqlError.text()).toStdString());
    }

{'fraction_non_alphanumeric': 0.14563993303911124, 'fraction_numerical': 0.0065439050372850405, 'mean_word_length': 4.334415584415584, 'pattern_counts': {'":': 8, '<': 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': '5972413', 'n_tokens_mistral': 2105, 'n_tokens_neox': 2077, 'n_words': 388}