[graphics/krita] libs/resources: Rename versioned_resources.location to versioned_resources.version

Halla Rempt null at kde.org
Thu Jan 28 12:58:13 GMT 2021


Git commit 3e0bf9412cb2363576f6751700bfd58411d7a2f1 by Halla Rempt.
Committed on 28/01/2021 at 12:53.
Pushed by rempt into branch 'master'.

Rename versioned_resources.location to versioned_resources.version

And remove resources.version.

This updates the database schema version.

CCMAIL:kimageshop at kde.org

M  +16   -18   libs/resources/KisResourceCacheDb.cpp
M  +3    -3    libs/resources/KisResourceLocator.cpp
M  +0    -9    libs/resources/KisResourceModel.cpp
M  +18   -15   libs/resources/KisResourceQueryMapper.cpp
M  +0    -1    libs/resources/sql/create_resources.sql
M  +1    -1    libs/resources/sql/create_versioned_resources.sql

https://invent.kde.org/graphics/krita/commit/3e0bf9412cb2363576f6751700bfd58411d7a2f1

diff --git a/libs/resources/KisResourceCacheDb.cpp b/libs/resources/KisResourceCacheDb.cpp
index 8591de2058..ef630fa162 100644
--- a/libs/resources/KisResourceCacheDb.cpp
+++ b/libs/resources/KisResourceCacheDb.cpp
@@ -33,7 +33,7 @@ const QString dbDriver = "QSQLITE";
 
 const QString KisResourceCacheDb::dbLocationKey { "ResourceCacheDbDirectory" };
 const QString KisResourceCacheDb::resourceCacheDbFilename { "resourcecache.sqlite" };
-const QString KisResourceCacheDb::databaseVersion { "0.0.4" };
+const QString KisResourceCacheDb::databaseVersion { "0.0.5" };
 QStringList KisResourceCacheDb::storageTypes { QStringList() };
 QStringList KisResourceCacheDb::disabledBundles { QStringList() << "Krita_3_Default_Resources.bundle" };
 
@@ -331,11 +331,11 @@ int KisResourceCacheDb::resourceIdForResource(const QString &resourceName, const
                    ",      versioned_resources\n"
                    ",      storages\n"
                    "WHERE  resources.resource_type_id = resource_types.id\n"    // join resources and resource_types by resource id
-                   "AND    versioned_resources.resource_id = resources.id\n"   // join versioned_resources and resources by resource id
+                   "AND    versioned_resources.resource_id = resources.id\n"    // join versioned_resources and resources by resource id
                    "AND    storages.id = versioned_resources.storage_id\n"      // join storages and versioned_resources by storage id
                    "AND    storages.location = :storage_location\n"             // storage location must be the same as asked for
                    "AND    resource_types.name = :resource_type\n"              // resource type must be the same as asked for
-                   "AND    versioned_resources.location = :filename\n")) {       // location must be the same as asked for
+                   "AND    versioned_resources.filename = :filename\n")) {      // filename must be the same as asked for
         qWarning() << "Could not read and prepare resourceIdForResource (in versioned resources)" << q.lastError();
         return -1;
     }
@@ -422,14 +422,14 @@ bool KisResourceCacheDb::addResourceVersionImpl(int resourceId, QDateTime timest
 
     QSqlQuery q;
     r = q.prepare("INSERT INTO versioned_resources \n"
-                  "(resource_id, storage_id, version, location, timestamp, md5sum)\n"
+                  "(resource_id, storage_id, version, filename, timestamp, md5sum)\n"
                   "VALUES\n"
                   "( :resource_id\n"
                   ", (SELECT id \n"
                   "   FROM   storages \n"
                   "   WHERE  location = :storage_location)\n"
                   ", :version\n"
-                  ", :location\n"
+                  ", :filename\n"
                   ", :timestamp\n"
                   ", :md5sum\n"
                   ");");
@@ -442,7 +442,7 @@ bool KisResourceCacheDb::addResourceVersionImpl(int resourceId, QDateTime timest
     q.bindValue(":resource_id", resourceId);
     q.bindValue(":storage_location", KisResourceLocator::instance()->makeStorageLocationRelative(storage->location()));
     q.bindValue(":version", resource->version());
-    q.bindValue(":location", QFileInfo(resource->filename()).fileName());
+    q.bindValue(":filename", QFileInfo(resource->filename()).fileName());
     q.bindValue(":timestamp", timestamp.toSecsSinceEpoch());
     KIS_SAFE_ASSERT_RECOVER_NOOP(!resource->md5().isEmpty());
     q.bindValue(":md5sum", resource->md5().toHex());
@@ -522,7 +522,7 @@ bool KisResourceCacheDb::updateResourceTableForResourceIfNeeded(int resourceId,
     QString maxVersionFilename;
     {
         QSqlQuery q;
-        r = q.prepare("SELECT location\n"
+        r = q.prepare("SELECT filename\n"
                       "FROM   versioned_resources\n"
                       "WHERE  resource_id = :resource_id\n"
                       "AND    version = :version;");
@@ -550,9 +550,9 @@ bool KisResourceCacheDb::updateResourceTableForResourceIfNeeded(int resourceId,
     int currentVersion = -1;
     {
         QSqlQuery q;
-        r = q.prepare("SELECT version\n"
-                      "FROM   resources\n"
-                      "WHERE  id = :resource_id;");
+        r = q.prepare("SELECT MAX(versioned_resources.version)\n"
+                      "FROM   versioned_resources\n"
+                      "WHERE  versioned_resources.id = :resource_id;");
         if (!r) {
             qWarning() << "Could not prepare findMaxVersion statement" << q.lastError();
             return r;
@@ -708,7 +708,7 @@ bool KisResourceCacheDb::addResource(KisResourceStorageSP storage, QDateTime tim
 
     QSqlQuery q;
     r = q.prepare("INSERT INTO resources \n"
-                  "(storage_id, resource_type_id, name, filename, tooltip, thumbnail, status, temporary, version) \n"
+                  "(storage_id, resource_type_id, name, filename, tooltip, thumbnail, status, temporary) \n"
                   "VALUES \n"
                   "((SELECT id "
                   "  FROM storages "
@@ -721,8 +721,7 @@ bool KisResourceCacheDb::addResource(KisResourceStorageSP storage, QDateTime tim
                   ", :tooltip\n"
                   ", :thumbnail\n"
                   ", :status\n"
-                  ", :temporary\n"
-                  ", :version);");
+                  ", :temporary)\n");
 
     if (!r) {
         qWarning() << "Could not prepare addResource statement" << q.lastError();
@@ -734,7 +733,6 @@ bool KisResourceCacheDb::addResource(KisResourceStorageSP storage, QDateTime tim
     q.bindValue(":name", resource->name());
     q.bindValue(":filename", QFileInfo(resource->filename()).fileName());
     q.bindValue(":tooltip", i18n(resource->name().toUtf8()));
-    q.bindValue(":version", resource->version());
 
     QByteArray ba;
     QBuffer buf(&ba);
@@ -763,13 +761,13 @@ bool KisResourceCacheDb::addResource(KisResourceStorageSP storage, QDateTime tim
 
     // Then add a new version
     r = q.prepare("INSERT INTO versioned_resources\n"
-                  "(resource_id, storage_id, version, location, timestamp, md5sum)\n"
+                  "(resource_id, storage_id, version, filename, timestamp, md5sum)\n"
                   "VALUES\n"
                   "(:resource_id\n"
                   ",    (SELECT id FROM storages\n"
                   "      WHERE location = :storage_location)\n"
                   ", :version\n"
-                  ", :location\n"
+                  ", :filename\n"
                   ", :timestamp\n"
                   ", :md5sum\n"
                   ");");
@@ -782,7 +780,7 @@ bool KisResourceCacheDb::addResource(KisResourceStorageSP storage, QDateTime tim
     q.bindValue(":resource_id", resourceId);
     q.bindValue(":storage_location", KisResourceLocator::instance()->makeStorageLocationRelative(storage->location()));
     q.bindValue(":version", resource->version());
-    q.bindValue(":location", QFileInfo(resource->filename()).fileName());
+    q.bindValue(":filename", QFileInfo(resource->filename()).fileName());
     q.bindValue(":timestamp", timestamp.toSecsSinceEpoch());
     KIS_SAFE_ASSERT_RECOVER_NOOP(!resource->md5().isEmpty());
     if (resource->md5().isEmpty()) {
@@ -1364,7 +1362,7 @@ bool KisResourceCacheDb::synchronizeStorage(KisResourceStorageSP storage)
 
         QSqlQuery q;
         q.setForwardOnly(true);
-        if (!q.prepare("SELECT versioned_resources.resource_id, versioned_resources.location, versioned_resources.version, versioned_resources.timestamp\n"
+        if (!q.prepare("SELECT versioned_resources.resource_id, versioned_resources.filename, versioned_resources.version, versioned_resources.timestamp\n"
                        "FROM   versioned_resources\n"
                        ",      resource_types\n"
                        ",      resources\n"
diff --git a/libs/resources/KisResourceLocator.cpp b/libs/resources/KisResourceLocator.cpp
index 6f15448daf..8c1153a116 100644
--- a/libs/resources/KisResourceLocator.cpp
+++ b/libs/resources/KisResourceLocator.cpp
@@ -198,7 +198,7 @@ KoResourceSP KisResourceLocator::resource(QString storageLocation, const QString
     if (resource->resourceId() < 0 || resource->version() < 0) {
         QSqlQuery q;
         if (!q.prepare("SELECT resources.id\n"
-                       ",      resources.version\n"
+                       ",      versioned_resources.version\n"
                        ",      versioned_resources.md5sum\n"
                        "FROM   resources\n"
                        ",      storages\n"
@@ -210,7 +210,7 @@ KoResourceSP KisResourceLocator::resource(QString storageLocation, const QString
                        "AND    resource_types.name = :resource_type\n"
                        "AND    resources.filename  = :filename\n"
                        "AND    versioned_resources.resource_id = resources.id\n"
-                       "AND    versioned_resources.version = resources.version")) {
+                       "AND    versioned_resources.version = (SELECT MAX(version) FROM versioned_resources WHERE versioned_resources.resource_id = resources.id)")) {
             qWarning() << "Could not prepare id/version query" << q.lastError();
 
         }
@@ -220,7 +220,7 @@ KoResourceSP KisResourceLocator::resource(QString storageLocation, const QString
         q.bindValue(":filename", filename);
 
         if (!q.exec()) {
-            qWarning() << "Could not execute id/version quert" << q.lastError() << q.boundValues();
+            qWarning() << "Could not execute id/version query" << q.lastError() << q.boundValues();
         }
 
         if (!q.first()) {
diff --git a/libs/resources/KisResourceModel.cpp b/libs/resources/KisResourceModel.cpp
index 9cfbdcfb57..1101087f16 100644
--- a/libs/resources/KisResourceModel.cpp
+++ b/libs/resources/KisResourceModel.cpp
@@ -50,7 +50,6 @@ KisAllResourcesModel::KisAllResourcesModel(const QString &resourceType, QObject
                                        ",      resources.thumbnail\n"
                                        ",      resources.status\n"
                                        ",      storages.location\n"
-                                       ",      resources.version\n"
                                        ",      resource_types.name as resource_type\n"
                                        ",      resources.status as resource_active\n"
                                        ",      storages.active as storage_active\n"
@@ -467,26 +466,18 @@ int KisAllResourcesModel::rowCount(const QModelIndex &) const
 
 void KisAllResourcesModel::addStorage(const QString &/*location*/)
 {
-    qDebug() << "KisAllResourcesModel::addStorage" << rowCount();
-
     beginResetModel();
     resetQuery();
     endResetModel();
-
-    qDebug() << "\t\t" << rowCount();
 }
 
 
 
 void KisAllResourcesModel::removeStorage(const QString &/*location*/)
 {
-    qDebug() << "KisAllResourcesModel::removeStorage" << rowCount();
-
     beginResetModel();
     resetQuery();
     endResetModel();
-
-    qDebug() << "\t\t" << rowCount();
 }
 
 struct KisResourceModel::Private
diff --git a/libs/resources/KisResourceQueryMapper.cpp b/libs/resources/KisResourceQueryMapper.cpp
index ab20126db4..6f21e292ef 100644
--- a/libs/resources/KisResourceQueryMapper.cpp
+++ b/libs/resources/KisResourceQueryMapper.cpp
@@ -155,21 +155,24 @@ QVariant KisResourceQueryMapper::variantFromResourceQueryById(int resourceId, in
 
     QSqlQuery q;
     if (!q.prepare("SELECT resources.id\n"
-              ",      resources.storage_id\n"
-              ",      resources.name\n"
-              ",      resources.filename\n"
-              ",      resources.tooltip\n"
-              ",      resources.thumbnail\n"
-              ",      resources.status\n"
-              ",      storages.location\n"
-              ",      resources.version\n"
-              ",      resource_types.name as resource_type\n"
-              ",      resources.status as resource_active\n"
-              ",      storages.active as storage_active\n"
-              "FROM   resources\n"
-              ",      resource_types\n"
-              ",      storages\n"
-             "WHERE  resources.id = :resource_id"))
+                   ",      resources.storage_id\n"
+                   ",      resources.name\n"
+                   ",      resources.filename\n"
+                   ",      resources.tooltip\n"
+                   ",      resources.thumbnail\n"
+                   ",      resources.status\n"
+                   ",      storages.location\n"
+                   ",      versioned_resources.version\n"
+                   ",      resource_types.name as resource_type\n"
+                   ",      resources.status as resource_active\n"
+                   ",      storages.active as storage_active\n"
+                   "FROM   resources\n"
+                   ",      resource_types\n"
+                   ",      storages\n"
+                   ",      versioned_resources\n"
+                   "WHERE  resources.id = :resource_id\n"
+                   "AND    versioned_resources.resource_id = resources.id\n"
+                   "AND    versioned_resources.version = (SELECT MAX(version) FROM versioned_resources WHERE versioned_resources.resource_id = resources.id)"))
     {
         qWarning() << "Could not prepare variantFromResourceQueryById query" << q.lastError();
         return v;
diff --git a/libs/resources/sql/create_resources.sql b/libs/resources/sql/create_resources.sql
index 48347aa1fd..8e6fa6d3fe 100644
--- a/libs/resources/sql/create_resources.sql
+++ b/libs/resources/sql/create_resources.sql
@@ -8,7 +8,6 @@ CREATE TABLE IF NOT EXISTS resources (
 ,   thumbnail BLOB           /* the image representing the resource visually*/
 ,   status INTEGER           /* active resources are visible in the UI, inactive resources are considered "deleted" */
 ,   temporary INTEGER        /* temporary resources are removed from the database on startup */
-,   version INTEGER          /* the current version number of the resource (cached for performance reasons */
 ,   FOREIGN KEY(resource_type_id) REFERENCES resource_types(id)
 ,   UNIQUE(storage_id, resource_type_id, name, filename)
 );
diff --git a/libs/resources/sql/create_versioned_resources.sql b/libs/resources/sql/create_versioned_resources.sql
index 1a5691dfce..fe107a9bdd 100644
--- a/libs/resources/sql/create_versioned_resources.sql
+++ b/libs/resources/sql/create_versioned_resources.sql
@@ -3,7 +3,7 @@ CREATE TABLE IF NOT EXISTS versioned_resources (
 ,   resource_id INTEGER
 ,   storage_id INTEGER
 ,   version INTEGER
-,   location TEXT NOT NULL
+,   filename TEXT NOT NULL
 ,   md5sum TEXT NOT NULL
 ,   timestamp INTEGER
 ,   FOREIGN KEY(resource_id) REFERENCES resources(id)



More information about the kimageshop mailing list