TreeFrogFramework 2.10
Loading...
Searching...
No Matches
tmongoodmapper.h
Go to the documentation of this file.
1#pragma once
2#include <QVariant>
3#include <TCriteria>
4#include <TCriteriaMongoConverter>
5#include <TMongoObject>
6#include <TMongoQuery>
7
16template <class T>
17class TMongoODMapper : protected TMongoQuery {
18public:
20 virtual ~TMongoODMapper();
21
22 // Method chaining
26 TMongoODMapper<T> &orderBy(const QString &column, Tf::SortOrder order = Tf::AscendingOrder);
27
28 void setLimit(int limit);
29 void setOffset(int offset);
30 void setSortOrder(int column, Tf::SortOrder order = Tf::AscendingOrder);
31 void setSortOrder(const QString &column, Tf::SortOrder order = Tf::AscendingOrder);
32
33 T findOne(const TCriteria &cri = TCriteria());
34 T findFirst(const TCriteria &cri = TCriteria()) { return findOne(cri); }
35 T findFirstBy(int column, const QVariant &value);
36 T findByObjectId(const QString &id);
37 bool find(const TCriteria &cri = TCriteria());
38 bool findBy(int column, const QVariant &value);
39 bool findIn(int column, const QVariantList &values);
40 bool next();
41 T value() const;
42
43 int findCount(const TCriteria &cri = TCriteria());
44 int findCountBy(int column, const QVariant &value);
45 int updateAll(const TCriteria &cri, int column, const QVariant &value);
46 int updateAll(const TCriteria &cri, const QMap<int, QVariant> &values);
47 int removeAll(const TCriteria &cri = TCriteria());
48
49private:
50 QString sortColumn;
51 Tf::SortOrder sortOrder;
52
55};
56
57
61template <class T>
63 TMongoQuery(T().collectionName()), sortColumn(), sortOrder(Tf::AscendingOrder)
64{
65}
66
70template <class T>
74
75template <class T>
76inline void TMongoODMapper<T>::setLimit(int limit)
77{
79}
80
81
82template <class T>
83inline void TMongoODMapper<T>::setOffset(int offset)
84{
86}
87
88
89template <class T>
90inline void TMongoODMapper<T>::setSortOrder(int column, Tf::SortOrder order)
91{
92 if (column >= 0) {
93 sortColumn = TCriteriaMongoConverter<T>::propertyName(column);
94 sortOrder = order;
95 }
96}
97
98
99template <class T>
100inline void TMongoODMapper<T>::setSortOrder(const QString &column, Tf::SortOrder order)
101{
102 if (!column.isEmpty()) {
103 T obj;
104 if (obj.propertyNames().contains(column, Qt::CaseSensitive)) {
105 sortColumn = column;
106 sortOrder = order;
107 } else {
108 Tf::warn("Unable to set sort order : '{}' field not found in '{}' collection",
109 qUtf8Printable(column), qUtf8Printable(obj.collectionName()));
110 }
111 }
112}
113
114
115template <class T>
117{
118 setLimit(l);
119 return *this;
120}
121
122
123template <class T>
125{
126 setOffset(o);
127 return *this;
128}
129
130
131template <class T>
133{
134 setSortOrder(column, order);
135 return *this;
136}
137
138
139template <class T>
140inline TMongoODMapper<T> &TMongoODMapper<T>::orderBy(const QString &column, Tf::SortOrder order)
141{
142 setSortOrder(column, order);
143 return *this;
144}
145
146
147template <class T>
148inline T TMongoODMapper<T>::findOne(const TCriteria &criteria)
149{
150 T t;
151 QVariantMap doc = TMongoQuery::findOne(TCriteriaMongoConverter<T>(criteria).toVariantMap());
152 if (!doc.isEmpty()) {
153 t.setBsonData(doc);
154 }
155 return t;
156}
157
158
159template <class T>
160inline T TMongoODMapper<T>::findFirstBy(int column, const QVariant &value)
161{
162 T t;
163 TCriteria cri(column, value);
164 QVariantMap doc = TMongoQuery::findOne(TCriteriaMongoConverter<T>(cri).toVariantMap());
165 if (!doc.isEmpty()) {
166 t.setBsonData(doc);
167 }
168 return t;
169}
170
171
172template <class T>
173inline T TMongoODMapper<T>::findByObjectId(const QString &id)
174{
175 T t;
176 QVariantMap doc = TMongoQuery::findById(id);
177 if (!doc.isEmpty()) {
178 t.setBsonData(doc);
179 }
180 return t;
181}
182
183
184template <class T>
185inline bool TMongoODMapper<T>::find(const TCriteria &criteria)
186{
187 QVariantMap order;
188 if (!sortColumn.isEmpty()) {
189 order.insert(sortColumn, ((sortOrder == Tf::AscendingOrder) ? 1 : -1));
190 }
191
192 return TMongoQuery::find(TCriteriaMongoConverter<T>(criteria).toVariantMap(), order);
193}
194
195
196template <class T>
197inline bool TMongoODMapper<T>::findBy(int column, const QVariant &value)
198{
199 return find(TCriteria(column, value));
200}
201
202
203template <class T>
204inline bool TMongoODMapper<T>::findIn(int column, const QVariantList &values)
205{
206 return find(TCriteria(column, TMongo::In, values));
207}
208
209
210template <class T>
212{
213 return TMongoQuery::next();
214}
215
216
217template <class T>
219{
220 T t;
221 QVariantMap doc = TMongoQuery::value();
222 if (!doc.isEmpty()) {
223 t.setBsonData(doc);
224 }
225 return t;
226}
227
228
229template <class T>
230inline int TMongoODMapper<T>::findCount(const TCriteria &criteria)
231{
232 return TMongoQuery::count(TCriteriaMongoConverter<T>(criteria).toVariantMap());
233}
234
235
236template <class T>
237inline int TMongoODMapper<T>::findCountBy(int column, const QVariant &value)
238{
239 return findCount(TCriteria(column, value));
240}
241
242
243// template <class T>
244// inline QList<T> TMongoODMapper<T>::findAll(const TCriteria &cri)
245// {
246// QList<T> lst;
247// int cnt = find(cri);
248// tSystemDebug("Mongo documents count: {}", cnt);
249
250// if (cnt > 0) {
251// while (next()) {
252// lst << value();
253// }
254// }
255// return lst;
256// }
257
258
259// template <class T>
260// inline QList<T> TMongoODMapper<T>::findAllBy(int column, QVariant value)
261// {
262// return findAll(TCriteria(column, value));
263// }
264
265
266// template <class T>
267// inline QList<T> TMongoODMapper<T>::findAllIn(int column, const QVariantList &values)
268// {
269// return findAll(TCriteria(column, TMongo::In, values));
270// }
271
272
273template <class T>
274inline int TMongoODMapper<T>::updateAll(const TCriteria &cri, int column, const QVariant &value)
275{
276 QString s = TCriteriaMongoConverter<T>::propertyName(column);
277 if (s.isEmpty())
278 return -1;
279
280 QVariantMap doc;
281 doc.insert(s, value);
282 return TMongoQuery::updateMulti(TCriteriaMongoConverter<T>(cri).toVariantMap(), doc);
283}
284
285
286template <class T>
287inline int TMongoODMapper<T>::updateAll(const TCriteria &cri, const QMap<int, QVariant> &values)
288{
289 QVariantMap doc;
290
291 for (auto it = values.begin(); it != values.end(); ++it) {
292 QString s = TCriteriaMongoConverter<T>::propertyName(it.key());
293 if (!s.isEmpty()) {
294 doc.insert(s, it.value());
295 }
296 }
297
298 return TMongoQuery::updateMulti(TCriteriaMongoConverter<T>(cri).toVariantMap(), doc);
299}
300
301
302template <class T>
303inline int TMongoODMapper<T>::removeAll(const TCriteria &criteria)
304{
305 return TMongoQuery::remove(TCriteriaMongoConverter<T>(criteria).toVariantMap());
306}
307
The TCriteria class represents a WHERE clause without SQL for the sake of database abstraction.
Definition tcriteria.h:6
The TMongoODMapper class is a template class that provides concise functionality to object-document m...
Definition tmongoodmapper.h:17
T findFirstBy(int column, const QVariant &value)
Definition tmongoodmapper.h:160
bool find(const TCriteria &cri=TCriteria())
Definition tmongoodmapper.h:185
bool findIn(int column, const QVariantList &values)
Definition tmongoodmapper.h:204
void setLimit(int limit)
Definition tmongoodmapper.h:76
virtual ~TMongoODMapper()
Destructor.
Definition tmongoodmapper.h:71
int removeAll(const TCriteria &cri=TCriteria())
Definition tmongoodmapper.h:303
TMongoODMapper()
Constructor.
Definition tmongoodmapper.h:62
T findOne(const TCriteria &cri=TCriteria())
Definition tmongoodmapper.h:148
int findCountBy(int column, const QVariant &value)
Definition tmongoodmapper.h:237
void setOffset(int offset)
Definition tmongoodmapper.h:83
int findCount(const TCriteria &cri=TCriteria())
Definition tmongoodmapper.h:230
bool next()
Definition tmongoodmapper.h:211
TMongoODMapper< T > & orderBy(int column, Tf::SortOrder order=Tf::AscendingOrder)
Definition tmongoodmapper.h:132
T findFirst(const TCriteria &cri=TCriteria())
Definition tmongoodmapper.h:34
T findByObjectId(const QString &id)
Definition tmongoodmapper.h:173
void setSortOrder(int column, Tf::SortOrder order=Tf::AscendingOrder)
Definition tmongoodmapper.h:90
bool findBy(int column, const QVariant &value)
Definition tmongoodmapper.h:197
int updateAll(const TCriteria &cri, int column, const QVariant &value)
Definition tmongoodmapper.h:274
T value() const
Definition tmongoodmapper.h:218
The TMongoQuery class provides a means of operating a MongoDB system.
Definition tmongoquery.h:10
int limit() const
Definition tmongoquery.h:54
int updateMulti(const QVariantMap &criteria, const QVariantMap &document)
Definition tmongoquery.h:30
bool next()
Retrieves the next document in the result set, if available, and positions on the retrieved document.
Definition tmongoquery.cpp:82
int count(const QVariantMap &criteria=QVariantMap())
Definition tmongoquery.cpp:273
void setLimit(int limit)
Sets the limit to limit, which is the limited number of documents for finding documents.
Definition tmongoquery.h:60
bool find(const QVariantMap &criteria=QVariantMap(), const QVariantMap &orderBy=QVariantMap(), const QStringList &fields=QStringList())
Finds documents by the criteria criteria in the collection and returns the number of the documents.
Definition tmongoquery.cpp:68
int offset() const
Definition tmongoquery.h:66
QVariantMap value() const
Returns the current document as a QVariantMap object.
Definition tmongoquery.cpp:93
void setOffset(int offset)
Sets the offset to offset, which is the number of documents to skip for finding documents.
Definition tmongoquery.h:72
QVariantMap findOne(const QVariantMap &criteria=QVariantMap(), const QStringList &fields=QStringList())
Finds documents by the criteria criteria in the collection and returns a retrieved document as a QVar...
Definition tmongoquery.cpp:106
int remove(const QVariantMap &criteria)
Removes documents that matches the criteria from the collection.
Definition tmongoquery.cpp:157
QVariantMap findById(const QString &id, const QStringList &fields=QStringList())
Definition tmongoquery.cpp:116
@ In
Definition tfnamespace.h:323
The Tf namespace contains miscellaneous identifiers used throughout the library of TreeFrog Framework...
Definition tdebug.h:10
SortOrder
Definition tfnamespace.h:133
@ AscendingOrder
Definition tfnamespace.h:134
void warn(const std::format_string< Args... > &fmt, Args &&... args)
Outputs the warning message to the log file, formatting args according to the format string fmt.
Definition tglobal.h:259
#define T_DISABLE_COPY(Class)
Definition tdeclexport.h:37
#define T_DISABLE_MOVE(Class)
Definition tdeclexport.h:41