Commit 4ac2fce4 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Finalize first version of the resouce cache db schema

Differential revision: https://phabricator.kde.org/T8775
parent feccb93a
...@@ -107,7 +107,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location) ...@@ -107,7 +107,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
QStringList tables = QStringList() << "version_information" QStringList tables = QStringList() << "version_information"
<< "origin_types" << "origin_types"
<< "resource_types" << "resource_types"
<< "stores" << "storages"
<< "tags" << "tags"
<< "resources" << "resources"
<< "translations" << "translations"
...@@ -129,10 +129,9 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location) ...@@ -129,10 +129,9 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
if (dbTables.contains("version_information")) { if (dbTables.contains("version_information")) {
// Verify the version number // Verify the version number
QSqlQuery query; QFile f(":/get_version_information.sql");
query.exec("SELECT database_version, krita_version, creation_date" if (f.open(QFile::ReadOnly)) {
"FROM version_information" QSqlQuery query(f.readAll());
"ORDER BY id DESC LIMIT 1");
if (query.size() > 0) { if (query.size() > 0) {
query.first(); query.first();
QString schemaVersion = query.value(0).toString(); QString schemaVersion = query.value(0).toString();
...@@ -149,6 +148,10 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location) ...@@ -149,6 +148,10 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
} }
} }
} }
else {
return QSqlError("Error executing SQL", "Could not open get_version_information.sql", QSqlError::StatementError);
}
}
if (allTablesPresent && !schemaIsOutDated) { if (allTablesPresent && !schemaIsOutDated) {
infoResources << "All tables are present and up to date"; infoResources << "All tables are present and up to date";
...@@ -159,9 +162,8 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location) ...@@ -159,9 +162,8 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
Q_FOREACH(const QString &table, tables) { Q_FOREACH(const QString &table, tables) {
QFile f(":/create_" + table + ".sql"); QFile f(":/create_" + table + ".sql");
if (f.open(QFile::ReadOnly)) { if (f.open(QFile::ReadOnly)) {
QString sql(f.readAll());
QSqlQuery query; QSqlQuery query;
if (!query.exec(sql)) { if (!query.exec(f.readAll())) {
qWarning() << "Could not create table" << table; qWarning() << "Could not create table" << table;
return db.lastError(); return db.lastError();
} }
...@@ -184,8 +186,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location) ...@@ -184,8 +186,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
if (f.open(QFile::ReadOnly)) { if (f.open(QFile::ReadOnly)) {
QString sql = f.readAll(); QString sql = f.readAll();
Q_FOREACH(const QString &originType, originTypes) { Q_FOREACH(const QString &originType, originTypes) {
QSqlQuery query; QSqlQuery query(sql);
query.prepare(sql);
query.addBindValue(originType); query.addBindValue(originType);
if (!query.exec()) { if (!query.exec()) {
qWarning() << "Could not insert" << originType << db.lastError() << query.executedQuery(); qWarning() << "Could not insert" << originType << db.lastError() << query.executedQuery();
...@@ -210,8 +211,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location) ...@@ -210,8 +211,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
if (f.open(QFile::ReadOnly)) { if (f.open(QFile::ReadOnly)) {
QString sql = f.readAll(); QString sql = f.readAll();
Q_FOREACH(const QString &resourceType, resourceTypes) { Q_FOREACH(const QString &resourceType, resourceTypes) {
QSqlQuery query; QSqlQuery query(sql);
query.prepare(sql);
query.addBindValue(resourceType); query.addBindValue(resourceType);
if (!query.exec()) { if (!query.exec()) {
qWarning() << "Could not insert" << resourceType << db.lastError() << query.executedQuery(); qWarning() << "Could not insert" << resourceType << db.lastError() << query.executedQuery();
...@@ -235,7 +235,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location) ...@@ -235,7 +235,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
query.addBindValue(KritaVersionWrapper::versionString()); query.addBindValue(KritaVersionWrapper::versionString());
query.addBindValue(QDateTime::currentDateTimeUtc().toString()); query.addBindValue(QDateTime::currentDateTimeUtc().toString());
if (!query.exec()) { if (!query.exec()) {
qWarning() << "Could not insert the current version" << db.lastError() << query.executedQuery(); qWarning() << "Could not insert the current version" << db.lastError() << query.executedQuery() << query.boundValues();
return db.lastError(); return db.lastError();
} }
infoResources << "Filled version table"; infoResources << "Filled version table";
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
#include <kritaresources_export.h> #include <kritaresources_export.h>
/** /**
* @brief The KisResourceCacheDb class encapsulates the database that * @brief The KisResourceCacheDb class encapsulates the database that
* caches information about the resources available to the user. * caches information about the resources available to the user.
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<file alias="fill_version_information.sql">sql/fill_version_information.sql</file> <file alias="fill_version_information.sql">sql/fill_version_information.sql</file>
<file alias="create_origin_types.sql">sql/create_origin_types.sql</file> <file alias="create_origin_types.sql">sql/create_origin_types.sql</file>
<file alias="fill_origin_types.sql">sql/fill_origin_types.sql</file> <file alias="fill_origin_types.sql">sql/fill_origin_types.sql</file>
<file alias="create_stores.sql">sql/create_stores.sql</file> <file alias="create_storages.sql">sql/create_storages.sql</file>
<file alias="create_tags.sql">sql/create_tags.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="create_resource_types.sql">sql/create_resource_types.sql</file>
<file alias="fill_resource_types.sql">sql/fill_resource_types.sql</file> <file alias="fill_resource_types.sql">sql/fill_resource_types.sql</file>
...@@ -13,5 +13,6 @@ ...@@ -13,5 +13,6 @@
<file alias="create_translations.sql">sql/create_translations.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_versioned_resources.sql">sql/create_versioned_resources.sql</file>
<file alias="create_resource_tags.sql">sql/create_resource_tags.sql</file> <file alias="create_resource_tags.sql">sql/create_resource_tags.sql</file>
<file alias="get_version_information.sql">sql/get_version_information.sql</file>
</qresource> </qresource>
</RCC> </RCC>
CREATE TABLE IF NOT EXISTS origin_types ( CREATE TABLE IF NOT EXISTS origin_types (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY
name TEXT , name TEXT
) );
CREATE TABLE IF NOT EXISTS resource_tags ( CREATE TABLE IF NOT EXISTS resource_tags (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY
resource_id INTEGER, , resource_id INTEGER
tag_id INTEGER, , tag_id INTEGER
FOREIGN KEY(resource_id) REFERENCES resources(id) , FOREIGN KEY(resource_id) REFERENCES resources(id)
FOREIGN KEY(tag_id) REFERENCES tags(id) , FOREIGN KEY(tag_id) REFERENCES tags(id)
) );
CREATE TABLE IF NOT EXISTS resource_types ( CREATE TABLE IF NOT EXISTS resource_types (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY
name TEXT , name TEXT
) );
CREATE TABLE IF NOT EXISTS resources ( CREATE TABLE IF NOT EXISTS resources (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY
type TEXT , resource_type_id INTEGER
) , name TEXT
, tooltip TEXT
, icon BLOB
, FOREIGN KEY(resource_type_id) REFERENCES resource_types(id)
);
CREATE TABLE IF NOT EXISTS storages (
id INTEGER PRIMARY KEY
, origin_type_id INTEGER
, name TEXT
, location TEXT
, FOREIGN KEY(origin_type_id) REFERENCES origin_types(id)
);
CREATE TABLE IF NOT EXISTS stores (
id INTEGER PRIMARY KEY,
origin_type_id INTEGER,
FOREIGN KEY(origin_type_id) REFERENCES origin_types(id)
)
CREATE TABLE IF NOT EXISTS tags ( CREATE TABLE IF NOT EXISTS tags (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY
name TEXT , name TEXT
) , storage_id INTEGER
, FOREIGN KEY(storage_id) REFERENCES storages(id)
);
CREATE TABLE IF NOT EXISTS translations ( CREATE TABLE IF NOT EXISTS translations (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY
name TEXT , original_id INTEGER
) , locale TEXT
, translation TEXT
);
CREATE TABLE IF NOT EXISTS version_information ( CREATE TABLE IF NOT EXISTS version_information (
id INTEGER PRIMARY KEY AUTOINCREMENT id INTEGER PRIMARY KEY AUTOINCREMENT
, database_version TEXT , database_version TEXT
, krita_version TEXT , krita_version TEXT
, creation_date TEXT , creation_date TEXT
) );
CREATE TABLE IF NOT EXISTS versioned_resources ( CREATE TABLE IF NOT EXISTS versioned_resources (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY
name TEXT , resource_id INTEGER
) , storage_id INTEGER
, version INTEGER
, location TEXT
, datestamp TEXT
, FOREIGN KEY(resource_id) REFERENCES resources(id)
, FOREIGN KEY(storage_id) REFERENCES storages(id)
);
INSERT INTO version_information ( INSERT INTO version_information (
database_version database_version
, krita_version , krita_version
, creation_date , creation_date )
)
VALUES ( VALUES (
? ?
,? , ?
,? , ?
); );
SELECT (
database_version
, krita_version
, creation_date (
FROM version_information
ORDER BY id
DESC
LIMIT 1;
...@@ -46,7 +46,7 @@ void TestResourceCacheDb::testCreateDatabase() ...@@ -46,7 +46,7 @@ void TestResourceCacheDb::testCreateDatabase()
QStringList tables = QStringList() << "version_information" QStringList tables = QStringList() << "version_information"
<< "origin_types" << "origin_types"
<< "resource_types" << "resource_types"
<< "stores" << "storages"
<< "tags" << "tags"
<< "resources" << "resources"
<< "translations" << "translations"
......
...@@ -802,6 +802,12 @@ ...@@ -802,6 +802,12 @@
<extends>QPushButton</extends> <extends>QPushButton</extends>
<header>kis_color_button.h</header> <header>kis_color_button.h</header>
</customwidget> </customwidget>
<customwidget>
<class>KisFileNameRequester</class>
<extends>QWidget</extends>
<header location="global">kis_file_name_requester.h</header>
<container>1</container>
</customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections/> <connections/>
......
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