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