Heart 1.4.984.b4712811
Heart is base back end library for your c++ Qt projects.
Loading...
Searching...
No Matches
dbobject.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2027 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#include "dbobject.h"
9#include <QDataStream>
10#include <QDateTime>
11#include <QSqlQuery>
12#include <QHash>
13#include <QSqlRecord>
14#include <QVariantMap>
15#include <QSharedPointer>
16#include <qaglobalutils.h>
17
18namespace QH {
19namespace PKG {
20
24
28
30
31 auto map = variantMap().keys();
32 QString queryString = "SELECT " + map.join(",") + " FROM %0 ";
33 queryString = queryString.arg(table());
34
35
36 auto [conditionQueryString, conditionBindingMap] = condition();
37
38 if (conditionQueryString.size()) {
39
40 queryString += " WHERE " + conditionQueryString;
41 if (!q.prepare(queryString)) {
43 }
44
45 for (auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) {
46 q.bindValue(it.key(), it.value());
47 }
48
50 }
51
52
53 if (!q.prepare(queryString)) {
55 }
56
58}
59
60PrepareResult DBObject::prepareInsertQuery(QSqlQuery &q, bool replace) const {
61
63
64 if (!map.size()) {
65 qCritical() << "The variantMap method return an empty map.";
66
68 }
69
70 QString queryString = (replace)?
71 "REPLACE INTO %0(%1) VALUES (%2) " :
72 "INSERT INTO %0(%1) VALUES (%2) ";
73
74
75 queryString = queryString.arg(table());
76 QString tableInsertHeader = "";
77 QString tableInsertValues = "";
78
79 for (auto it = map.begin(); it != map.end(); ++it) {
80
81 if (!static_cast<bool>(it.value().type & MemberType::Insert)) {
82 continue;
83 }
84
85 if (static_cast<bool>(it.value().type & MemberType::Autoincement) && !replace) {
86 continue;
87 }
88
89 tableInsertHeader += it.key();
90 tableInsertValues += ":" + it.key();
91 if (std::next(it, 1) != map.end()) {
92 tableInsertHeader += ", ";
93 tableInsertValues += ", ";
94 }
95
96 }
97
98 queryString = queryString.arg(tableInsertHeader);
99 queryString = queryString.arg(tableInsertValues);
100
101 if (q.prepare(queryString)) {
102
103 for (auto it = map.begin(); it != map.end(); ++it) {
104 if (!static_cast<bool>(it.value().type & MemberType::Insert)) {
105 continue;
106 }
107
108 if (static_cast<bool>(it.value().type & MemberType::Autoincement) && !replace) {
109 continue;
110 }
111
112 q.bindValue(":" + it.key(), it.value().value);
113 }
114
116 }
117
118 return PrepareResult::Fail;
119}
120
122
123 DBVariantMap map = variantMap();
124
125 if (!map.size()) {
126 qCritical() << "The variantMap method return an empty map.";
127
128 return PrepareResult::Fail;
129 }
130
131 auto [conditionQueryString, conditionBindingMap] = condition();
132
133 if (conditionQueryString.isEmpty()) {
134 qCritical() << "The object soue not have condition for update object.";
135 return PrepareResult::Fail;
136 }
137
138 QString queryString = "UPDATE %0 SET %1 WHERE " + conditionQueryString;
139
140 queryString = queryString.arg(table());
141 QString tableUpdateValues = "";
142
143 for (auto it = map.begin(); it != map.end(); ++it) {
144 if (!bool(it.value().type & MemberType::Update)) {
145 continue;
146 }
147
148 if (tableUpdateValues.size()) {
149 tableUpdateValues += ", ";
150 }
151
152 tableUpdateValues += QString("%0= :%0").arg(it.key());
153
154 }
155
156 if (tableUpdateValues.isEmpty()) {
157 qCritical() << "Fail to generate condition for object: " + toString() +
158 ". The object do not have valid update fields.";
159 return PrepareResult::Fail;
160 }
161
162 queryString = queryString.arg(tableUpdateValues);
163
164 if (q.prepare(queryString)) {
165
166 for (auto it = map.begin(); it != map.end(); ++it) {
167 if (!bool(it.value().type & MemberType::Update)) {
168 continue;
169 }
170
171 q.bindValue(":" + it.key(), it.value().value);
172 }
173
174 for (auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) {
175#ifdef QT_DEBUG
176 if (bool(map.value(it.key()).type & MemberType::Update)) {
177 qWarning() << "Bad object configuration: "
178 "The " << it.key() << " field using in the condition and has MemberType::Update configuration."
179 " All condition fields should not use the MemberType::Update. \n" << toString();
180 }
181#endif
182 q.bindValue(it.key(), it.value());
183 }
184
185
186
188 }
189
190 return PrepareResult::Fail;
191}
192
193bool DBObject::isCached() const {
194#ifdef HEART_DB_CACHE
195 return isHaveAPrimaryKey();
196#else
197 return false;
198#endif
199}
200
201bool DBObject::isBundle() const {
202 return false;
203}
204
205std::pair<QString, QMap<QString, QVariant>> DBObject::condition() const {
206
207 // if object have a primaryKey then return primary key
208 auto primaryVal = primaryValue();
209 if (!primaryVal.isNull()) {
210 return {QString("%0 = :%0").arg(primaryKey()),
211 {{QString(":%0").arg(primaryKey()), {primaryVal}}}};
212 }
213
214 qCritical() << "Fail to generate condition for object: " + toString() +
215 ". Object do not have valid unique fields or valid database address.";
216
217
218 return {};
219}
220
221QString DBObject::primaryKey() const {
222 return "";
223}
224
225QVariant DBObject::primaryValue() const {
226 return {};
227}
228
230 return bool(variantMap().value(primaryKey()).type & MemberType::Insert);
231}
232
234 return {table(), primaryValue()};
235}
236
237QString DBObject::toString() const {
238 return AbstractData::toString() +
239 QString(" %0").arg(dbAddress().toString());
240}
241
242QDataStream &DBObject::fromStream(QDataStream &stream) {
243
244 qWarning() << "This object not support stream operator."
245 " Please Override the fromStream method for this object. " + toString();
246
247 return stream;
248}
249
250QDataStream &DBObject::toStream(QDataStream &stream) const {
251
252 qWarning() << "This object not support stream operator."
253 " Please Override the toStream method for this object. " + toString();
254 return stream;
255}
256
258
259 QString queryString = "DELETE FROM %0 ";
260 queryString = queryString.arg(table());
261
262 auto [conditionQueryString, conditionBindingMap] = condition();
263
264 if (conditionQueryString.size()) {
265
266 queryString += " WHERE " + conditionQueryString;
267 if (!q.prepare(queryString)) {
268 return PrepareResult::Fail;
269 }
270
271 for (auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) {
272 q.bindValue(it.key(), it.value());
273 }
274
276 }
277
278 if (!q.prepare(queryString)) {
279 return PrepareResult::Fail;
280 }
281
283}
284
286 if (isHaveAPrimaryKey()) {
288 }
289
290 return {};
291}
292
293bool DBObject::isValid() const {
295 return false;
296
297 if (isInsertPrimaryKey()) {
298 return primaryValue().isValid();
299 }
300
301 return table().size();
302}
303
305 return primaryKey().size();
306}
307
309
313
315 this->value = value;
316 this->type = type;
317}
318
319}
320}
The DbAddress class use to work with database addresses. Database Address it is structure with 2 valu...
Definition dbaddress.h:24
bool isValid() const override
isValid This method check current object to valid.
QString toString() const override
toString - Return a string implementation for this object.
DbAddress dbAddress() const
dbAddress This method return address of the database object. IF the object is not valid then this met...
Definition dbobject.cpp:233
bool isHaveAPrimaryKey() const
isHaveAPrimaryKey This method return true if this object has a primary key. If you want to cache this...
Definition dbobject.cpp:304
virtual PrepareResult prepareRemoveQuery(QSqlQuery &q) const
prepareRemoveQuery This method should be prepare a query for remove this object from a database....
Definition dbobject.cpp:257
~DBObject() override
Definition dbobject.cpp:25
virtual std::pair< QString, QMap< QString, QVariant > > condition() const
condition This method must to return a condition of the WHERE block of the sql query....
Definition dbobject.cpp:205
QDataStream & toStream(QDataStream &stream) const override
fromStream This method should be write all members of the current object to the stream object.
Definition dbobject.cpp:250
bool isInsertPrimaryKey() const
isInsertPrimaryKey This method check primaryKeys type. If the primary key have a type MemberType::Ins...
Definition dbobject.cpp:229
virtual DBVariantMap variantMap() const
variantMap This method should be create a DBVariantMap implementation of this database object.
Definition dbobject.cpp:285
QDataStream & fromStream(QDataStream &stream) override
fromStream This method should be read all bytes from the stream object and full the current object.
Definition dbobject.cpp:242
virtual void clear()
clear This method clear all data of database object. Override This method for remove or reset your ow...
Definition dbobject.cpp:308
virtual QString primaryKey() const
primaryKey This method must be return the name of primary key of this object table....
Definition dbobject.cpp:221
QString toString() const override
Definition dbobject.cpp:237
virtual PrepareResult prepareSelectQuery(QSqlQuery &q) const
prepareSelectQuery This method should be prepare a query for selected data. Override this method for ...
Definition dbobject.cpp:29
virtual PrepareResult prepareUpdateQuery(QSqlQuery &q) const
prepareUpdateQuery this method should be prepare a insert data query.
Definition dbobject.cpp:121
virtual PrepareResult prepareInsertQuery(QSqlQuery &q, bool replace) const
prepareInsertQuery This method should be prepare a query for insert object into database....
Definition dbobject.cpp:60
virtual bool isCached() const
isCached This method sholud be return status of object. If this method return true then this object c...
Definition dbobject.cpp:193
virtual bool isBundle() const
isBundle This method definef determines whether the object will be abstract (composite objects) or si...
Definition dbobject.cpp:201
bool isValid() const override
Definition dbobject.cpp:293
virtual QString table() const =0
table This method should be return name of the database table that should be contains objects with th...
virtual QVariant primaryValue() const
primaryValue This method is wrapper of DBAddress::id. If This object do not contains a id value then ...
Definition dbobject.cpp:225
The PKG namespace - this namespace contains all default packages of the Heart library....
Definition heart.h:211
PrepareResult
The PrepareResult enum is result of work prepare sql query of dbobjects.
Definition dbobject.h:24
@ Success
prepare finished successful.
Definition dbobject.h:28
@ Fail
prepare is failed.
Definition dbobject.h:26
QMap< QString, DBVariant > DBVariantMap
DBVariantMap this is Map with key, and value with data type.
Definition dbobject.h:84
MemberType
The MemberType enum. This enum contains types of members DBObjects classes. for more information see ...
Definition dbobject.h:37
@ Update
The Field With This type can be updated but not inserted.
Definition dbobject.h:45
@ PrimaryKey
The primary key field without autoincrement.
Definition dbobject.h:57
@ Autoincement
The field with this attribute automatically incremented into database, and will not to added into ins...
Definition dbobject.h:51
@ Insert
The Field With This type can be inserted but not updated.
Definition dbobject.h:42
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13
MemberType type
Definition dbobject.h:75