TreeFrogFramework  1.18
tmodelutil.h
Go to the documentation of this file.
1 #ifndef TMODELUTIL_H
2 #define TMODELUTIL_H
3 
4 #include <QList>
5 #include <TCriteria>
6 #include <TSqlORMapper>
7 #include <TSqlORMapperIterator>
8 #include <TMongoODMapper>
9 
10 
11 template <class T, class S>
12 inline QList<T> tfGetModelListByCriteria(const TCriteria &cri, const QList<QPair<int, Tf::SortOrder>> &sortColumns, int limit = 0, int offset = 0)
13 {
14  TSqlORMapper<S> mapper;
15  if (!sortColumns.isEmpty()) {
16  for (auto &p : sortColumns) {
17  if (p.first >= 0) {
18  mapper.setSortOrder(p.first, p.second);
19  }
20  }
21  }
22  if (limit > 0) {
23  mapper.setLimit(limit);
24  }
25  if (offset > 0) {
26  mapper.setOffset(offset);
27  }
28  QList<T> list;
29  if (mapper.find(cri) > 0) {
30  for (auto &o : mapper) {
31  list << T(o);
32  }
33  }
34  return list;
35 }
36 
37 
38 template <class T, class S>
39 inline QList<T> tfGetModelListByCriteria(const TCriteria &cri, int sortColumn, Tf::SortOrder order, int limit = 0, int offset = 0)
40 {
41  QList<QPair<int, Tf::SortOrder>> sortColumns = { qMakePair(sortColumn, order) };
42  return tfGetModelListByCriteria<T, S>(cri, sortColumns, limit, offset);
43 }
44 
45 
46 template <class T, class S>
47 inline QList<T> tfGetModelListByCriteria(const TCriteria &cri = TCriteria(), int limit = 0, int offset = 0)
48 {
49  QList<QPair<int, Tf::SortOrder>> sortColumns = { qMakePair(-1, Tf::AscendingOrder) };
50  return tfGetModelListByCriteria<T, S>(cri, sortColumns, limit, offset);
51 }
52 
53 
54 template <class T, class S>
55 inline QList<T> tfGetModelListByMongoCriteria(const TCriteria &cri, int limit = 0, int offset = 0)
56 {
57  TMongoODMapper<S> mapper;
58 
59  if (limit > 0) {
60  mapper.setLimit(limit);
61  }
62  if (offset > 0) {
63  mapper.setOffset(offset);
64  }
65  QList<T> list;
66  if (mapper.find(cri)) {
67  while (mapper.next()) {
68  list << T(mapper.value());
69  }
70  }
71  return list;
72 }
73 
74 #endif // TMODELUTIL_H
T value() const
Definition: tmongoodmapper.h:219
int find(const TCriteria &cri=TCriteria())
Retrieves with the criteria cri from the table and returns the number of the ORM objects.
Definition: tsqlormapper.h:186
void setOffset(int offset)
Sets the offset to offset, which is the number of rows to skip for execution of SELECT statement...
Definition: tsqlormapper.h:298
void setLimit(int limit)
Definition: tmongoodmapper.h:77
The TMongoODMapper class is a template class that provides concise functionality to object-document m...
Definition: tmongoodmapper.h:19
The TSqlORMapper class is a template class that provides concise functionality to object-relational m...
Definition: tsqlormapper.h:26
void setSortOrder(int column, Tf::SortOrder order=Tf::AscendingOrder)
Sets the sort order for column to order.
Definition: tsqlormapper.h:307
void setLimit(int limit)
Sets the limit to limit, which is the limited number of rows for execution of SELECT statement...
Definition: tsqlormapper.h:288
QList< T > tfGetModelListByMongoCriteria(const TCriteria &cri, int limit=0, int offset=0)
Definition: tmodelutil.h:55
QList< T > tfGetModelListByCriteria(const TCriteria &cri, const QList< QPair< int, Tf::SortOrder >> &sortColumns, int limit=0, int offset=0)
Definition: tmodelutil.h:12
Definition: tfnamespace.h:136
void setOffset(int offset)
Definition: tmongoodmapper.h:84
bool find(const TCriteria &cri=TCriteria())
Definition: tmongoodmapper.h:186
The TCriteria class represents a WHERE clause without SQL for the sake of database abstraction...
Definition: tcriteria.h:8
SortOrder
Definition: tfnamespace.h:135
bool next()
Definition: tmongoodmapper.h:212