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