From 7d4784db80284874cd5b7e8e9f1c8694f6985b90 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 12 Jan 2017 10:46:47 +0100 Subject: Modifications for multiple entities. This allows to apply a modification to all entities matching some query. --- common/store.cpp | 26 ++++++++++++++++++++++++++ common/store.h | 12 ++++++++++++ 2 files changed, 38 insertions(+) (limited to 'common') diff --git a/common/store.cpp b/common/store.cpp index c11e1e8..38445e5 100644 --- a/common/store.cpp +++ b/common/store.cpp @@ -188,6 +188,20 @@ KAsync::Job Store::modify(const DomainType &domainObject) return facade->modify(domainObject).addToContext(std::shared_ptr(facade)).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to modify"; }); } +template +KAsync::Job Store::modify(const Query &query, const DomainType &domainObject) +{ + SinkLog() << "Modify: " << query << domainObject; + return fetchAll(query) + .each([=] (const typename DomainType::Ptr &entity) { + auto copy = *entity; + for (const auto &p : domainObject.changedProperties()) { + copy.setProperty(p, domainObject.getProperty(p)); + } + return modify(*entity); + }); +} + template KAsync::Job Store::move(const DomainType &domainObject, const QByteArray &newResource) { @@ -215,6 +229,16 @@ KAsync::Job Store::remove(const DomainType &domainObject) return facade->remove(domainObject).addToContext(std::shared_ptr(facade)).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to remove"; }); } +template +KAsync::Job Store::remove(const Sink::Query &query) +{ + SinkLog() << "Remove: " << query; + return fetchAll(query) + .each([] (const typename DomainType::Ptr &entity) { + return remove(*entity); + }); +} + KAsync::Job Store::removeDataFromDisk(const QByteArray &identifier) { // All databases are going to become invalid, nuke the environments @@ -368,8 +392,10 @@ QList Store::read(const Sink::Query &query_) #define REGISTER_TYPE(T) \ template KAsync::Job Store::remove(const T &domainObject); \ + template KAsync::Job Store::remove(const Query &); \ template KAsync::Job Store::create(const T &domainObject); \ template KAsync::Job Store::modify(const T &domainObject); \ + template KAsync::Job Store::modify(const Query &, const T &); \ template KAsync::Job Store::move(const T &domainObject, const QByteArray &newResource); \ template KAsync::Job Store::copy(const T &domainObject, const QByteArray &newResource); \ template QSharedPointer Store::loadModel(const Query &query); \ diff --git a/common/store.h b/common/store.h index d2ab9b5..f787a3e 100644 --- a/common/store.h +++ b/common/store.h @@ -75,12 +75,24 @@ KAsync::Job SINK_EXPORT create(const DomainType &domainObject); template KAsync::Job SINK_EXPORT modify(const DomainType &domainObject); +/** + * Modify a set of entities identified by @param query. + */ +template +KAsync::Job SINK_EXPORT modify(const Query &query, const DomainType &domainObject); + /** * Remove an entity. */ template KAsync::Job SINK_EXPORT remove(const DomainType &domainObject); +/** + * Remove a set of entities identified by @param query. + */ +template +KAsync::Job SINK_EXPORT remove(const Query &query); + /** * Move an entity to a new resource. */ -- cgit v1.2.3