Commit 0e7683d3 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Add begin of the resource cache db implementation

parent df37e1db
......@@ -20,4 +20,4 @@ add_subdirectory( libkis )
if (NOT APPLE AND HAVE_QT_QUICK)
add_subdirectory( libqml )
endif()
add_subdirectory( resources )
set(kritaresources_LIB_SRCS
KisResourceCacheDb.cpp
)
qt5_add_resources(kritawidgetutils_LIB_SRCS sql.qrc)
# help plugin trader find installed plugins when run from uninstalled tests
if (BUILD_TESTING)
add_definitions(-DCMAKE_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}")
endif()
add_library(kritaresources SHARED ${kritaresources_LIB_SRCS})
generate_export_header(kritaresources BASE_NAME kritaresources)
target_link_libraries(kritaresources
PUBLIC
Qt5::Core
PRIVATE
Qt5::Sql
KF5::ConfigCore
KF5::CoreAddons
KF5::I18n
)
set_target_properties(kritaresources PROPERTIES
VERSION ${GENERIC_KRITA_LIB_VERSION} SOVERSION ${GENERIC_KRITA_LIB_SOVERSION}
)
install(TARGETS kritaresources ${INSTALL_TARGETS_DEFAULT_ARGS} )
/*
* Copyright (C) 2018 Boudewijn Rempt <boud@valdyas.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KisResourceCacheDb.h"
#include <QtSql>
#include <QStandardPaths>
#include <kconfiggroup.h>
#include <ksharedconfig.h>
const QString dbDriver = "QSQLITE";
class KisResourceCacheDb::Private
{
public:
QSqlError initDb();
};
KisResourceCacheDb::KisResourceCacheDb(QObject *parent)
: QObject(parent)
, d(new Private())
{
QSqlError err = d->initDb();
}
KisResourceCacheDb::~KisResourceCacheDb()
{
}
QSqlError KisResourceCacheDb::Private::initDb()
{
QSqlDatabase db = QSqlDatabase::addDatabase(dbDriver);
const KConfigGroup group(KSharedConfig::openConfig(), "ResourceManagement");
QString dbFile(group.readEntry<QString>("cachedb", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/resourcecache"));
db.setDatabaseName(dbFile);
if (!db.open()) {
return db.lastError();
}
QStringList tables = QStringList() << "origin_types"
<< "resource_types"
<< "stores"
<< "tags"
<< "resources"
<< "translations"
<< "versioned_resources"
<< "resource_tags";
QSqlQuery query;
Q_FOREACH(const QString &table, tables) {
QFile f(":/create_" + table + ".sql");
Q_ASSERT(f.exists());
if (f.open(QFile::ReadOnly)) {
QString sql(f.readAll());
if (!query.exec(sql)) {
return db.lastError();
}
}
}
return QSqlError();
}
/*
* Copyright (C) 2018 Boudewijn Rempt <boud@valdyas.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KISRESOURCECACHEDB_H
#define KISRESOURCECACHEDB_H
#include <QObject>
#include <QScopedPointer>
#include <kritaresources_export.h>
/**
* @brief The KisResourceCacheDb class encapsulates the database that
* caches information about the resources available to the user.
*/
class KisResourceCacheDb : public QObject
{
Q_OBJECT
public:
explicit KisResourceCacheDb(QObject *parent = nullptr);
~KisResourceCacheDb();
private:
class Private;
QScopedPointer<Private> d;
};
#endif // KISRESOURCECACHEDB_H
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource>
<file alias="create_origin_types.sql">sql/create_origin_types.sql</file>
<file alias="fill_origin_types.sql">sql/create_origin_types.sql</file>
<file alias="create_stores.sql">sql/create_stores.sql</file>
<file alias="create_tags.sql">sql/create_tags.sql</file>
<file alias="create_resource_types.sql">sql/create_resource_types.sql</file>
<file alias="fill_resource_types.sql">sql/create_resource_types.sql</file>
<file alias="create_resources.sql">sql/create_resources.sql</file>
<file alias="create_translations.sql">sql/create_translations.sql</file>
<file alias="create_versioned_resources.sql">sql/create_versioned_resources.sql</file>
<file alias="create_resource_tags.sql">sql/create_resource_tags.sql</file>
</qresource>
</RCC>
CREATE TABLE IF NOT EXISTS origin_types (
id INTEGER PRIMARY KEY,
name TEXT
)
CREATE TABLE IF NOT EXISTS resource_tags (
id INTEGER PRIMARY KEY,
resource_id INTEGER,
tag_id INTEGER,
FOREIGN KEY(resource_id) REFERENCES resources(id)
FOREIGN KEY(tag_id) REFERENCES tags(id)
)
CREATE TABLE IF NOT EXISTS resource_types (
id INTEGER PRIMARY KEY,
name TEXT
)
CREATE TABLE IF NOT EXISTS resources (
id INTEGER PRIMARY KEY,
type TEXT
)
CREATE TABLE IF NOT EXISTS stores (
id INTEGER PRIMARY KEY,
origin_type_id INTEGER,
FOREIGN KEY(origin_type_id) REFERENCES origin_types(id)
)
INSERT into origin_types (id, type) VALUES(?,?);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment