TreeFrogFramework  1.18
tsqlqueryormapper.h
Go to the documentation of this file.
1 #ifndef TSQLQUERYORMAPPER_H
2 #define TSQLQUERYORMAPPER_H
3 
4 #include <QtSql>
5 #include <QList>
6 #include <TSqlQuery>
7 #include <TCriteriaConverter>
8 #include <TSystemGlobal>
9 
19 template <class T>
21 {
22 public:
23  TSqlQueryORMapper(int databaseId = 0);
24 
25  TSqlQueryORMapper<T> &prepare(const QString &query);
26  bool load(const QString &filename);
27  TSqlQueryORMapper<T> &bind(const QString &placeholder, const QVariant &val);
28  TSqlQueryORMapper<T> &bind(int pos, const QVariant &val);
29  TSqlQueryORMapper<T> &addBind(const QVariant &val);
30  bool exec(const QString &query);
31  bool exec();
32  T execFirst(const QString &query);
33  T execFirst();
34  int numRowsAffected() const;
35  int size() const;
36  bool next();
37  T value() const;
38  QString fieldName(int index) const;
39 
40  class ConstIterator;
41  inline ConstIterator begin() { return ConstIterator(this, 0); }
42  inline ConstIterator end() { return ConstIterator(this, size()); }
43 
47  class ConstIterator {
48  public:
49  TSqlQueryORMapper<T> *m {nullptr};
50  int it {0};
51 
52  inline ConstIterator() {}
53  inline ConstIterator(const ConstIterator &o) : m(o.m), it(o.it) {}
54  inline ConstIterator &operator=(const ConstIterator &o) { m = o.m; it = o.it; return *this; }
55  inline const T operator*() const { if (it == 0) m->first(); return m->value(); }
56  inline bool operator==(const ConstIterator &o) const { return m == o.m && it == o.it; }
57  inline bool operator!=(const ConstIterator &o) const { return m != o.m || it != o.it; }
58  inline ConstIterator &operator++() { it = qMin(it + 1, m->size()); m->next(); return *this; }
59 
60  private:
61  inline ConstIterator(TSqlQueryORMapper<T> *mapper, int i) : m(mapper), it(i) {}
62  friend class TSqlQueryORMapper;
63  };
64 };
65 
66 
67 template <class T>
69  : TSqlQuery(databaseId)
70 { }
71 
72 
73 template <class T>
75 {
76  TSqlQuery::prepare(query);
77  return *this;
78 }
79 
80 
81 template <class T>
82 inline bool TSqlQueryORMapper<T>::load(const QString &filename)
83 {
84  return TSqlQuery::load(filename);
85 }
86 
87 
88 template <class T>
89 inline TSqlQueryORMapper<T> &TSqlQueryORMapper<T>::bind(const QString &placeholder, const QVariant &val)
90 {
91  TSqlQuery::bind(placeholder, val);
92  return *this;
93 }
94 
95 
96 template <class T>
97 inline TSqlQueryORMapper<T> &TSqlQueryORMapper<T>::bind(int pos, const QVariant &val)
98 {
99  TSqlQuery::bind(pos, val);
100  return *this;
101 }
102 
103 
104 template <class T>
106 {
107  TSqlQuery::addBind(val);
108  return *this;
109 }
110 
111 
112 template <class T>
113 inline bool TSqlQueryORMapper<T>::exec(const QString &query)
114 {
115  return TSqlQuery::exec(query);
116 }
117 
118 
119 template <class T>
121 {
122  return TSqlQuery::exec();
123 }
124 
125 
126 template <class T>
127 inline T TSqlQueryORMapper<T>::execFirst(const QString &query)
128 {
129  return (exec(query) && next()) ? value() : T();
130 }
131 
132 
133 template <class T>
135 {
136  return (exec() && next()) ? value() : T();
137 }
138 
139 
140 template <class T>
142 {
144 }
145 
146 
147 template <class T>
148 inline int TSqlQueryORMapper<T>::size() const
149 {
150  return TSqlQuery::size();
151 }
152 
153 
154 template <class T>
156 {
157  return TSqlQuery::next();
158 }
159 
160 
161 template <class T>
163 {
164  T rec;
165  QSqlRecord r = record();
166  rec.setRecord(r, lastError());
167  return rec;
168 }
169 
170 
171 template <class T>
172 inline QString TSqlQueryORMapper<T>::fieldName(int index) const
173 {
174  return TCriteriaConverter<T>::getPropertyName(index, driver());
175 }
176 
177 #endif // TSQLQUERYORMAPPER_H
int it
Definition: tsqlqueryormapper.h:50
bool exec()
Executes a previously prepared SQL query.
Definition: tsqlquery.cpp:162
TSqlQueryORMapper< T > * m
Definition: tsqlqueryormapper.h:49
ConstIterator & operator=(const ConstIterator &o)
Definition: tsqlqueryormapper.h:54
int size() const
Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or i...
Definition: tsqlquery.cpp:188
The TSqlQuery class provides a means of executing and manipulating SQL statements.
Definition: tsqlquery.h:8
bool next()
Definition: tsqlqueryormapper.h:155
TSqlQueryORMapper< T > & prepare(const QString &query)
Prepares the SQL query query to retrieve the ORM objects specified by the class T.
Definition: tsqlqueryormapper.h:74
ConstIterator & operator++()
Definition: tsqlqueryormapper.h:58
ConstIterator begin()
Definition: tsqlqueryormapper.h:41
bool load(const QString &filename)
Definition: tsqlqueryormapper.h:82
QString fieldName(int index) const
Returns the name of the field at position index in the class T.
Definition: tsqlqueryormapper.h:172
ConstIterator end()
Definition: tsqlqueryormapper.h:42
TSqlQuery & addBind(const QVariant &val)
Adds the value val to the list of values when using positional value binding and returns the query ob...
Definition: tsqlquery.h:72
TSqlQuery & prepare(const QString &query)
Prepares the SQL query query for execution.
Definition: tsqlquery.h:40
bool operator!=(const ConstIterator &o) const
Definition: tsqlqueryormapper.h:57
TSqlQueryORMapper< T > & addBind(const QVariant &val)
Definition: tsqlqueryormapper.h:105
The TSqlQueryORMapper class is a template class that creates ORM objects by executing and manipulatin...
Definition: tsqlqueryormapper.h:20
bool load(const QString &filename)
Loads a query from the given file filename.
Definition: tsqlquery.cpp:42
bool next()
Retrieves the next record in the result, if available, and positions the query on the retrieved recor...
Definition: tsqlquery.cpp:199
const T operator*() const
Definition: tsqlqueryormapper.h:55
T execFirst()
Definition: tsqlqueryormapper.h:134
T value() const
Returns the current ORM object in the results retrieved by the query.
Definition: tsqlqueryormapper.h:162
ConstIterator(const ConstIterator &o)
Definition: tsqlqueryormapper.h:53
bool operator==(const ConstIterator &o) const
Definition: tsqlqueryormapper.h:56
TSqlQueryORMapper< T > & bind(const QString &placeholder, const QVariant &val)
Definition: tsqlqueryormapper.h:89
Const iterator.
Definition: tsqlqueryormapper.h:47
int size() const
Definition: tsqlqueryormapper.h:148
ConstIterator()
Definition: tsqlqueryormapper.h:52
bool exec()
Definition: tsqlqueryormapper.h:120
TSqlQueryORMapper(int databaseId=0)
Constructs a TSqlQueryORMapper object using the SQL query and the database databaseId.
Definition: tsqlqueryormapper.h:68
int numRowsAffected() const
Returns the number of rows affected by the result&#39;s SQL statement, or -1 if it cannot be determined...
Definition: tsqlquery.cpp:174
int numRowsAffected() const
Definition: tsqlqueryormapper.h:141
TSqlQuery & bind(const QString &placeholder, const QVariant &val)
Set the placeholder placeholder to be bound to value val in the prepared statement.
Definition: tsqlquery.h:50