summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-11-04 12:00:35 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-11-04 12:00:35 +0100
commit98b682acb8c57cadaea5ff0ac6709d21a591b97b (patch)
tree840d2c2ac681a7a45c7d011609327a7732fa2293
parentf4a22e49f23c930b244a1c7f33c7efb9e285750c (diff)
downloadsink-98b682acb8c57cadaea5ff0ac6709d21a591b97b.tar.gz
sink-98b682acb8c57cadaea5ff0ac6709d21a591b97b.zip
Separated the base filter from the rest of the query.
-rw-r--r--common/datastorequery.cpp4
-rw-r--r--common/query.h192
2 files changed, 115 insertions, 81 deletions
diff --git a/common/datastorequery.cpp b/common/datastorequery.cpp
index aa7b772..0987d68 100644
--- a/common/datastorequery.cpp
+++ b/common/datastorequery.cpp
@@ -400,8 +400,8 @@ QVector<QByteArray> DataStoreQuery::indexLookup(const QByteArray &property, cons
400 400
401QByteArrayList DataStoreQuery::executeSubquery(const Query &subquery) 401QByteArrayList DataStoreQuery::executeSubquery(const Query &subquery)
402{ 402{
403 Q_ASSERT(!subquery.type.isEmpty()); 403 Q_ASSERT(!subquery.type().isEmpty());
404 auto sub = DataStoreQuery(subquery, subquery.type, mStore); 404 auto sub = DataStoreQuery(subquery, subquery.type(), mStore);
405 auto result = sub.execute(); 405 auto result = sub.execute();
406 QByteArrayList ids; 406 QByteArrayList ids;
407 while (result.next([&ids](const ResultSet::Result &result) { 407 while (result.next([&ids](const ResultSet::Result &result) {
diff --git a/common/query.h b/common/query.h
index 3ab5acf..c0ea0f3 100644
--- a/common/query.h
+++ b/common/query.h
@@ -27,21 +27,9 @@
27 27
28namespace Sink { 28namespace Sink {
29 29
30/** 30class SINK_EXPORT QueryBase
31 * A query that matches a set of entities.
32 */
33class SINK_EXPORT Query
34{ 31{
35public: 32public:
36 enum Flag
37 {
38 /** Leave the query running and continuously update the result set. */
39 LiveQuery,
40 /** Run the query synchronously. */
41 SynchronousQuery
42 };
43 Q_DECLARE_FLAGS(Flags, Flag)
44
45 struct Comparator { 33 struct Comparator {
46 enum Comparators { 34 enum Comparators {
47 Invalid, 35 Invalid,
@@ -59,137 +47,184 @@ public:
59 Comparators comparator; 47 Comparators comparator;
60 }; 48 };
61 49
62 template <typename T> 50 class Filter {
63 Query &request() 51 public:
52 QByteArrayList ids;
53 QHash<QByteArray, Comparator> propertyFilter;
54 };
55
56 Comparator getFilter(const QByteArray &property) const
64 { 57 {
65 requestedProperties << T::name; 58 return mBaseFilterStage.propertyFilter.value(property);
66 return *this;
67 } 59 }
68 60
69 template <typename T> 61 bool hasFilter(const QByteArray &property) const
70 Query &requestTree()
71 { 62 {
72 parentProperty = T::name; 63 return mBaseFilterStage.propertyFilter.contains(property);
73 return *this;
74 } 64 }
75 65
76 template <typename T> 66 void setBaseFilters(const QHash<QByteArray, Comparator> &filter)
77 Query &sort()
78 { 67 {
79 sortProperty = T::name; 68 mBaseFilterStage.propertyFilter = filter;
80 return *this;
81 } 69 }
82 70
83 Query(const ApplicationDomain::Entity &value) : limit(0), liveQuery(false), synchronousQuery(false) 71 QHash<QByteArray, Comparator> getBaseFilters() const
84 { 72 {
85 filter(value.identifier()); 73 return mBaseFilterStage.propertyFilter;
86 resourceFilter(value.resourceInstanceIdentifier());
87 } 74 }
88 75
89 Query(Flags flags = Flags()) : limit(0), liveQuery(false), synchronousQuery(false) 76 QByteArrayList ids() const
90 { 77 {
78 return mBaseFilterStage.ids;
91 } 79 }
92 80
93 QByteArrayList requestedProperties; 81 void filter(const QByteArray &id)
94 QByteArray parentProperty; 82 {
95 QByteArray sortProperty; 83 mBaseFilterStage.ids << id;
96 QByteArray type; 84 }
97 int limit;
98 bool liveQuery;
99 bool synchronousQuery;
100
101 class FilterStage {
102 public:
103 virtual ~FilterStage(){};
104 };
105 85
106 QList<QSharedPointer<FilterStage>> getFilterStages() 86 void filter(const QByteArrayList &ids)
107 { 87 {
108 return mFilterStages; 88 mBaseFilterStage.ids << ids;
109 } 89 }
110 90
111 /* 91 void filter(const QByteArray &property, const QueryBase::Comparator &comparator)
112 * Filters 92 {
113 */ 93 mBaseFilterStage.propertyFilter.insert(property, comparator);
114 class Filter : public FilterStage { 94 }
115 public:
116 QByteArrayList ids;
117 QHash<QByteArray, Comparator> propertyFilter;
118 };
119 95
120 template <typename T> 96 void setType(const QByteArray &type)
121 Query &filter(const QVariant &value)
122 { 97 {
123 return filter(T::name, value); 98 mType = type;
124 } 99 }
125 100
126 template <typename T> 101 QByteArray type() const
127 Query &containsFilter(const QVariant &value)
128 { 102 {
129 return filter(T::name, Comparator(value, Comparator::Contains)); 103 return mType;
130 } 104 }
131 105
106private:
107 Filter mBaseFilterStage;
108 QByteArray mType;
109};
110
111/**
112 * A query that matches a set of entities.
113 */
114class SINK_EXPORT Query : public QueryBase
115{
116public:
117 enum Flag
118 {
119 /** Leave the query running and continuously update the result set. */
120 LiveQuery,
121 /** Run the query synchronously. */
122 SynchronousQuery
123 };
124 Q_DECLARE_FLAGS(Flags, Flag)
125
132 template <typename T> 126 template <typename T>
133 Query &filter(const Comparator &comparator) 127 Query &request()
134 { 128 {
135 return filter(T::name, comparator); 129 requestedProperties << T::name;
130 return *this;
136 } 131 }
137 132
138 Query &filter(const QByteArray &id) 133 template <typename T>
134 Query &requestTree()
139 { 135 {
140 mBaseFilterStage.ids << id; 136 parentProperty = T::name;
141 return *this; 137 return *this;
142 } 138 }
143 139
144 Query &filter(const QByteArrayList &ids) 140 template <typename T>
141 Query &sort()
145 { 142 {
146 mBaseFilterStage.ids << ids; 143 sortProperty = T::name;
147 return *this; 144 return *this;
148 } 145 }
149 146
150 Query &filter(const QByteArray &property, const Comparator &comparator) 147 template <typename T>
148 Query &filter(const QVariant &value)
151 { 149 {
152 mBaseFilterStage.propertyFilter.insert(property, comparator); 150 filter(T::name, value);
153 return *this; 151 return *this;
154 } 152 }
155 153
156 Comparator getFilter(const QByteArray &property) const 154 template <typename T>
155 Query &containsFilter(const QVariant &value)
157 { 156 {
158 return mBaseFilterStage.propertyFilter.value(property); 157 QueryBase::filter(T::name, QueryBase::Comparator(value, QueryBase::Comparator::Contains));
158 return *this;
159 } 159 }
160 160
161 bool hasFilter(const QByteArray &property) const 161 template <typename T>
162 Query &filter(const QueryBase::Comparator &comparator)
162 { 163 {
163 return mBaseFilterStage.propertyFilter.contains(property); 164 QueryBase::filter(T::name, comparator);
165 return *this;
164 } 166 }
165 167
166 void setBaseFilters(const QHash<QByteArray, Comparator> &filter) 168 Query &filter(const QByteArray &id)
167 { 169 {
168 mBaseFilterStage.propertyFilter = filter; 170 QueryBase::filter(id);
171 return *this;
169 } 172 }
170 173
171 QHash<QByteArray, Comparator> getBaseFilters() const 174 Query &filter(const QByteArrayList &ids)
172 { 175 {
173 return mBaseFilterStage.propertyFilter; 176 QueryBase::filter(ids);
177 return *this;
174 } 178 }
175 179
176 QByteArrayList ids() const 180 Query &filter(const QByteArray &property, const QueryBase::Comparator &comparator)
177 { 181 {
178 return mBaseFilterStage.ids; 182 QueryBase::filter(property, comparator);
183 return *this;
179 } 184 }
180 185
181 template <typename T> 186 template <typename T>
182 Query &filter(const ApplicationDomain::Entity &value) 187 Query &filter(const ApplicationDomain::Entity &value)
183 { 188 {
184 return filter(T::name, QVariant::fromValue(value.identifier())); 189 filter(T::name, QVariant::fromValue(value.identifier()));
190 return *this;
185 } 191 }
186 192
187 template <typename T> 193 template <typename T>
188 Query &filter(const Query &query) 194 Query &filter(const Query &query)
189 { 195 {
190 auto q = query; 196 auto q = query;
191 q.type = ApplicationDomain::getTypeName<typename T::ReferenceType>(); 197 q.setType(ApplicationDomain::getTypeName<typename T::ReferenceType>());
192 return filter(T::name, QVariant::fromValue(q)); 198 filter(T::name, QVariant::fromValue(q));
199 return *this;
200 }
201
202
203 Query(const ApplicationDomain::Entity &value) : limit(0), liveQuery(false), synchronousQuery(false)
204 {
205 filter(value.identifier());
206 resourceFilter(value.resourceInstanceIdentifier());
207 }
208
209 Query(Flags flags = Flags()) : limit(0), liveQuery(false), synchronousQuery(false)
210 {
211 }
212
213 QByteArrayList requestedProperties;
214 QByteArray parentProperty;
215 QByteArray sortProperty;
216 int limit;
217 bool liveQuery;
218 bool synchronousQuery;
219
220 class FilterStage {
221 public:
222 virtual ~FilterStage(){};
223 };
224
225 QList<QSharedPointer<FilterStage>> getFilterStages()
226 {
227 return mFilterStages;
193 } 228 }
194 229
195 Filter getResourceFilter() const 230 Filter getResourceFilter() const
@@ -342,7 +377,6 @@ public:
342 377
343private: 378private:
344 Filter mResourceFilter; 379 Filter mResourceFilter;
345 Filter mBaseFilterStage;
346 QList<QSharedPointer<FilterStage>> mFilterStages; 380 QList<QSharedPointer<FilterStage>> mFilterStages;
347}; 381};
348 382