aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Vrátil <dvratil@kde.org>2016-09-08 10:42:51 (GMT)
committerDaniel Vrátil <dvratil@kde.org>2016-09-08 10:42:51 (GMT)
commit39aff54e8bb6a26116ffffeab37700646a944fc9 (patch)
treeac956f511027e3feb3ed033d95c408f826f79a3b
parent9e0e38adbbbad6bee1196192a55824bc620b230a (diff)
DataStore: append collection mimetypes atomically
Use the atomic MimeType::retriveByNameOrCreate() method to get the mime type. This should prevent race conditions when the DB is empty (e.g. in unit tests).
-rw-r--r--src/server/storage/datastore.cpp24
1 files changed, 4 insertions, 20 deletions
diff --git a/src/server/storage/datastore.cpp b/src/server/storage/datastore.cpp
index 104bee4..ef3a962 100644
--- a/src/server/storage/datastore.cpp
+++ b/src/server/storage/datastore.cpp
@@ -908,29 +908,13 @@ bool DataStore::appendMimeTypeForCollection(qint64 collectionId, const QStringLi
if (mimeTypes.isEmpty()) {
return true;
}
- SelectQueryBuilder<MimeType> qb;
- qb.addValueCondition(MimeType::nameColumn(), Query::In, mimeTypes);
- if (!qb.exec()) {
- return false;
- }
- QStringList missingMimeTypes = mimeTypes;
- Q_FOREACH (const MimeType &mt, qb.result()) {
- // unique index on n:m relation prevents duplicates, ie. this will fail
- // if this mimetype is already set
- if (!Collection::addMimeType(collectionId, mt.id())) {
+ Q_FOREACH (const QString &mimeType, mimeTypes) {
+ const auto &mt = MimeType::retrieveByNameOrCreate(mimeType);
+ if (!mt.isValid()) {
return false;
}
- missingMimeTypes.removeAll(mt.name());
- }
-
- // the MIME type doesn't exist, so we have to add it to the db
- Q_FOREACH (const QString &mtName, missingMimeTypes) {
- qint64 mimeTypeId;
- if (!appendMimeType(mtName, &mimeTypeId)) {
- return false;
- }
- if (!Collection::addMimeType(collectionId, mimeTypeId)) {
+ if (!Collection::addMimeType(collectionId, mt.id())) {
return false;
}
}