TreeFrogFramework 2.10
Loading...
Searching...
No Matches
tmodelutil.h
Go to the documentation of this file.
1#pragma once
2#include <QList>
3#include <TCriteria>
4#include <TCriteriaConverter>
5#include <TMongoODMapper>
6#include <TSqlORMapper>
7#include <TSqlORMapperIterator>
8
9
10template <class T, class S>
11inline QList<T> tfGetModelListByCriteria(const TCriteria &cri, const QList<QPair<QString, Tf::SortOrder>> &sortColumns, int limit = 0, int offset = 0)
12{
13 TSqlORMapper<S> mapper;
14 if (!sortColumns.isEmpty()) {
15 for (auto &p : sortColumns) {
16 if (!p.first.isEmpty()) {
17 mapper.setSortOrder(p.first, p.second);
18 }
19 }
20 }
21 if (limit > 0) {
22 mapper.setLimit(limit);
23 }
24 if (offset > 0) {
25 mapper.setOffset(offset);
26 }
27 QList<T> list;
28 int count = mapper.find(cri);
29 if (count > 0) {
30 list.reserve(count);
31 for (auto &o : mapper) {
32 list << T(o);
33 }
34 }
35 return list;
36}
37
38template <class T, class S>
39inline QList<T> tfGetModelListByCriteria(const TCriteria &cri, const QList<QPair<int, Tf::SortOrder>> &sortColumns, int limit = 0, int offset = 0)
40{
41 QList<QPair<QString, Tf::SortOrder>> sorts;
42
43 for (auto &p : sortColumns) {
44 QString columnName = TCriteriaConverter<S>::getPropertyName(p.first, nullptr);
45 if (!columnName.isEmpty()) {
46 sorts << qMakePair(columnName, p.second);
47 }
48 }
49 return tfGetModelListByCriteria<T, S>(cri, sorts, limit, offset);
50}
51
52template <class T, class S>
53inline QList<T> tfGetModelListByCriteria(const TCriteria &cri, int sortColumn, Tf::SortOrder order, int limit = 0, int offset = 0)
54{
55 QList<QPair<int, Tf::SortOrder>> sortColumns = {qMakePair(sortColumn, order)};
56 return tfGetModelListByCriteria<T, S>(cri, sortColumns, limit, offset);
57}
58
59template <class T, class S>
60inline QList<T> tfGetModelListByCriteria(const TCriteria &cri, QString sortColumn, Tf::SortOrder order, int limit = 0, int offset = 0)
61{
62 QList<QPair<QString, Tf::SortOrder>> sortColumns = {qMakePair(sortColumn, order)};
63 return tfGetModelListByCriteria<T, S>(cri, sortColumns, limit, offset);
64}
65
66template <class T, class S>
67inline QList<T> tfGetModelListByCriteria(const TCriteria &cri = TCriteria(), int limit = 0, int offset = 0)
68{
69 QList<QPair<int, Tf::SortOrder>> sortColumns = {qMakePair(-1, Tf::AscendingOrder)};
70 return tfGetModelListByCriteria<T, S>(cri, sortColumns, limit, offset);
71}
72
73
74template <class T, class S>
75inline QList<T> tfGetModelListByMongoCriteria(const TCriteria &cri, int sortColumn, Tf::SortOrder order, int limit = 0, int offset = 0)
76{
77 TMongoODMapper<S> mapper;
78
79 if (sortColumn >= 0) {
80 mapper.setSortOrder(sortColumn, order);
81 }
82 if (limit > 0) {
83 mapper.setLimit(limit);
84 }
85 if (offset > 0) {
86 mapper.setOffset(offset);
87 }
88 QList<T> list;
89 if (mapper.find(cri)) {
90 while (mapper.next()) {
91 list << T(mapper.value());
92 }
93 }
94 return list;
95}
96
97
98template <class T, class S>
99inline QList<T> tfGetModelListByMongoCriteria(const TCriteria &cri, int limit = 0, int offset = 0)
100{
101 return tfGetModelListByMongoCriteria<T, S>(cri, -1, Tf::AscendingOrder, limit, offset);
102}
103
104
105template <class T>
106inline QJsonArray tfConvertToJsonArray(const QList<T> &list, const QStringList &properties = QStringList())
107{
108 QJsonArray array;
109 for (auto &it : list) {
110 array.append(it.toJsonObject(properties));
111 }
112 return array;
113}
114
115
116template <class T>
117inline QCborArray tfConvertToCborArray(const QList<T> &list)
118{
119 QCborArray array;
120 for (auto &it : list) {
121 array.append(QCborValue(it.toCborMap()));
122 }
123 return array;
124}
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
bool find(const TCriteria &cri=TCriteria())
Definition tmongoodmapper.h:185
void setLimit(int limit)
Definition tmongoodmapper.h:76
void setOffset(int offset)
Definition tmongoodmapper.h:83
bool next()
Definition tmongoodmapper.h:211
void setSortOrder(int column, Tf::SortOrder order=Tf::AscendingOrder)
Definition tmongoodmapper.h:90
T value() const
Definition tmongoodmapper.h:218
The TSqlORMapper class is a template class that provides concise functionality to object-relational m...
Definition tsqlormapper.h:53
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:241
void setSortOrder(int column, Tf::SortOrder order=Tf::AscendingOrder)
Sets the sort order for column to order.
Definition tsqlormapper.h:364
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:355
void setLimit(int limit)
Sets the limit to limit, which is the limited number of rows for execution of SELECT statement.
Definition tsqlormapper.h:345
SortOrder
Definition tfnamespace.h:133
@ AscendingOrder
Definition tfnamespace.h:134
QCborArray tfConvertToCborArray(const QList< T > &list)
Definition tmodelutil.h:117
QJsonArray tfConvertToJsonArray(const QList< T > &list, const QStringList &properties=QStringList())
Definition tmodelutil.h:106
QList< T > tfGetModelListByCriteria(const TCriteria &cri, const QList< QPair< QString, Tf::SortOrder > > &sortColumns, int limit=0, int offset=0)
Definition tmodelutil.h:11
QList< T > tfGetModelListByMongoCriteria(const TCriteria &cri, int sortColumn, Tf::SortOrder order, int limit=0, int offset=0)
Definition tmodelutil.h:75