<commit_before>/****************************************************************************
**
** Copyright (C) 2013 Jolla Ltd.
** Contact: <NAME> <<EMAIL>>
**
****************************************************************************/

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "dbworker.h"

#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlResult>
#include <QSqlDriver>
#include <QDebug>
#include <QDesktopServices>
#include <QDir>
#include <QFile>
#include <QDateTime>

#ifndef DEBUG_LOGS
#define DEBUG_LOGS 0
#endif

#define DB_USER_VERSION 1

#define QUOTE(arg) #arg
#define STR(arg) QUOTE(arg)

static const char * const create_table_tab =
        "CREATE TABLE tab (tab_id INTEGER PRIMARY KEY,\n"
        "tab_history_id INTEGER\n"
        ");\n";

static const char * const create_table_link =
        "CREATE TABLE link (link_id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
        "url TEXT,\n"
        "title TEXT,\n"
        "thumb_path TEXT\n"
        ");\n";

static const char * const create_table_browser_history =
        "CREATE TABLE browser_history (id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
        "url TEXT UNIQUE,\n"
        "title TEXT,\n"
        "favorite_icon TEXT,\n"
        "visited_count INTEGER DEFAULT 1,\n"
        "date INTEGER"
        ");\n";

static const char * const create_table_tab_history =
        "CREATE TABLE tab_history (id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
        "tab_id INTEGER,\n"
        "link_id INTEGER,\n"
        "date INT"
        ");\n";

static const char * const create_table_settings =
        "CREATE TABLE settings (name TEXT PRIMARY KEY,\n"
        "value TEXT\n"
        ");\n";

static const char * const set_user_version =
        "PRAGMA user_version=" STR(DB_USER_VERSION) ";\n";

static const char *db_schema[] = {
    create_table_tab,
    create_table_tab_history,
    create_table_link,
    create_table_browser_history,
    create_table_settings,
    set_user_version
};
static int db_schema_count = sizeof(db_schema) / sizeof(*db_schema);

DBWorker::DBWorker(QObject *parent) :
    QObject(parent)
{
}

void DBWorker::init()
{
    QString databaseDir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
    const QString dbFileName = QLatin1String(DB_NAME);
    QDir dir(databaseDir);

    if(!dir.mkpath(databaseDir)) {
        qWarning() << "Can't create directory "+ databaseDir;
        return;
    }

    m_database = QSqlDatabase::addDatabase("QSQLITE");
    m_database.setDatabaseName(dir.absoluteFilePath(dbFileName));
    bool dbCreated = dir.exists(dbFileName);
    bool ok = m_database.open();

    if (!ok)
        qWarning() << "Failed to open database " << m_database.databaseName();

    if (!dbCreated) {
        //TODO check transaction & rollback/commit
        for (int i = 0; i < db_schema_count; ++i) {
            QSqlQuery query = prepare(db_schema[i]);
            execute(query);
        }
    }

    // check current schema version and migrate if needed
    QSqlQuery schemaQuery = prepare("PRAGMA user_version;");
    if (execute(schemaQuery) && schemaQuery.next()) {
        int userVersion = schemaQuery.value(0).toInt();
        if (userVersion == 0) {
            migrateTo_1();
        }
    } else {
        qWarning() << "Failed to check schema version";
    }
<commit_msg>[sailfish-browser] Limit history size to 2000 entries<commit_after>/****************************************************************************
**
** Copyright (C) 2013 Jolla Ltd.
** Contact: <NAME> <<EMAIL>>
**
****************************************************************************/

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "dbworker.h"

#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlResult>
#include <QSqlDriver>
#include <QDebug>
#include <QDesktopServices>
#include <QDir>
#include <QFile>
#include <QDateTime>

#ifndef DEBUG_LOGS
#define DEBUG_LOGS 0
#endif

#define DB_USER_VERSION 1

#define QUOTE(arg) #arg
#define STR(arg) QUOTE(arg)

#define MAX_BROWSER_HISTORY_SIZE 2000

static const char * const create_table_tab =
        "CREATE TABLE tab (tab_id INTEGER PRIMARY KEY,\n"
        "tab_history_id INTEGER\n"
        ");\n";

static const char * const create_table_link =
        "CREATE TABLE link (link_id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
        "url TEXT,\n"
        "title TEXT,\n"
        "thumb_path TEXT\n"
        ");\n";

static const char * const create_table_browser_history =
        "CREATE TABLE browser_history (id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
        "url TEXT UNIQUE,\n"
        "title TEXT,\n"
        "favorite_icon TEXT,\n"
        "visited_count INTEGER DEFAULT 1,\n"
        "date INTEGER"
        ");\n";

static const char * const create_table_tab_history =
        "CREATE TABLE tab_history (id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
        "tab_id INTEGER,\n"
        "link_id INTEGER,\n"
        "date INT"
        ");\n";

static const char * const create_table_settings =
        "CREATE TABLE settings (name TEXT PRIMARY KEY,\n"
        "value TEXT\n"
        ");\n";

static const char * const set_user_version =
        "PRAGMA user_version=" STR(DB_USER_VERSION) ";\n";

static const char *db_schema[] = {
    create_table_tab,
    create_table_tab_history,
    create_table_link,
    create_table_browser_history,
    create_table_settings,
    set_user_version
};
static int db_schema_count = sizeof(db_schema) / sizeof(*db_schema);

DBWorker::DBWorker(QObject *parent) :
    QObject(parent)
{
}

void DBWorker::init()
{
    QString databaseDir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
    const QString dbFileName = QLatin1String(DB_NAME);
    QDir dir(databaseDir);

    if(!dir.mkpath(databaseDir)) {
        qWarning() << "Can't create directory "+ databaseDir;
        return;
    }

    m_database = QSqlDatabase::addDatabase("QSQLITE");
    m_database.setDatabaseName(dir.absoluteFilePath(dbFileName));
    bool dbCreated = dir.exists(dbFileName);
    bool ok = m_database.open();

    if (!ok)
        qWarning() << "Failed to open database " << m_database.databaseName();

    if (!dbCreated) {
        //TODO check transaction & rollback/commit
        for (int i = 0; i < db_schema_count; ++i) {
            QSqlQuery query = prepare(db_schema[i]);
            execute(query);
        }
    } else {
        // Limit history size to 2000 entries
        QSqlQuery cleanupHistory = prepare("DELETE FROM browser_history WHERE id NOT IN (SELECT id from browser_history"\
                                           " ORDER BY date DESC LIMIT " STR(MAX_BROWSER_HISTORY_SIZE) ");");
        if (!execute(cleanupHistory)) {
            qWarning() << "Failed to clear older history items";
        }
    }

    // check current schema version and migrate if needed
    QSqlQuery schemaQuery = prepare("PRAGMA user_version;");
    if (execute(schemaQuery) && schemaQuery.next()) {
        int userVersion = schemaQuery.value(0).toInt();
        if (userVersion == 0) {
            migrateTo_1();
        }
    } else {
        qWarning() << "Failed to check schema version";
    }

{'fraction_non_alphanumeric': 0.15098877605558525, 'fraction_numerical': 0.005879208979155532, 'mean_word_length': 3.146814404432133, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2922434', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2225, 'n_words': 614}