Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
isqldb.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2024 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 "isqldb.h"
9#include "sqldbwriter.h"
10
11#include <dbobject.h>
12#include <asyncsqldbwriter.h>
13
14#include <QDateTime>
15#include <QtConcurrent/QtConcurrent>
16#include <qaglobalutils.h>
17
18namespace QH {
19
20using namespace PKG;
21
23 qint64 currentTime = QDateTime::currentMSecsSinceEpoch();
24
25 if (currentTime - lastUpdateTime > updateInterval ||
26 static_cast<bool>(mode & SqlDBCasheWriteMode::Force)) {
27
28 if (static_cast<bool>(mode & SqlDBCasheWriteMode::On_New_Thread)) {
29
30#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
31 QtConcurrent::run([currentTime, this]() {
32 globalUpdateDataBasePrivate(currentTime);
33 });
34
35#else
36 auto future = QtConcurrent::run([currentTime, this]() {
37 globalUpdateDataBasePrivate(currentTime);
38 });
39
40 if (!future.isValid()) {
41 QuasarAppUtils::Params::log("Failde to start update database thread");
42 }
43#endif
44 } else {
45 globalUpdateDataBasePrivate(currentTime);
46 }
47 }
48}
49
50bool ISqlDB::updateObjectP(const QSharedPointer<DBObject> &saveObject,
51 bool wait) {
52
53 if (updateCache(saveObject)) {
54
56 return _writer && _writer->isValid() &&
57 _writer->updateObject(saveObject, wait);
58 }
59
62
63 return true;
64 }
65
66 return _writer && _writer->isValid() &&
67 _writer->updateObject(saveObject, wait);
68}
69
70bool ISqlDB::deleteObjectP(const QSharedPointer<DBObject> &delObj,
71 bool wait) {
72
73 deleteFromCache(delObj);
75
76 if (_writer && _writer->isValid()) {
77 return _writer->deleteObject(delObj, wait);
78 }
79
80 return false;
81}
82
83bool ISqlDB::insertObjectP(const QSharedPointer<DBObject> &saveObject,
84 bool wait,
85 const QWeakPointer<unsigned int>& autoincrementIdResult) {
86
87 if (insertToCache(saveObject)) {
88
90
91 return _writer && _writer->isValid() &&
92 _writer->insertObject(saveObject, wait, autoincrementIdResult);
93 }
94
97
98 return true;
99 }
100
101 return _writer && _writer->isValid() &&
102 _writer->insertObject(saveObject, wait, autoincrementIdResult);
103}
104
105bool ISqlDB::replaceObjectP(const QSharedPointer<PKG::DBObject> &saveObject, bool wait) {
106 if (updateCache(saveObject)) {
107
109
110 return _writer && _writer->isValid() &&
111 _writer->replaceObject(saveObject, wait);
112 }
113
114 pushToQueue(saveObject, CacheAction::Update);
116
117 return true;
118 }
119
120 return _writer && _writer->isValid() &&
121 _writer->replaceObject(saveObject, wait);
122}
123
125 return lastUpdateTime;
126}
127
128void ISqlDB::setLastUpdateTime(const qint64 &value) {
129 lastUpdateTime = value;
130}
131
132void ISqlDB::pushToQueue(const QSharedPointer<DBObject> &obj,
133 CacheAction type) {
134 _saveLaterMutex.lock();
135 _changes.insert(type, obj);
136 _saveLaterMutex.unlock();
137}
138
139ISqlDB::ISqlDB(qint64 updateInterval, SqlDBCasheWriteMode mode) {
140 lastUpdateTime = QDateTime::currentMSecsSinceEpoch();
141 this->updateInterval = updateInterval;
142 setMode(mode);
143
144}
145
147
148}
149
151 return _writer;
152}
153
155 _writer = writer;
156}
157
158bool ISqlDB::getAllObjects(const DBObject &templateObject,
159 QList<QSharedPointer<QH::PKG::DBObject>> &result) {
160
161 result = getFromCache(&templateObject);
162 if(result.size()) {
163 return true;
164 }
165
166 if (_writer && _writer->isValid()) {
167 if (!_writer->getAllObjects(templateObject, result)) {
168 return false;
169 }
170
171 for (const auto &object: std::as_const(result)) {
172 if (object->isCached() && !insertToCache(object)) {
173 QuasarAppUtils::Params::log("Selected object from database can not be saved into database cache. " +
174 object->toString(),
175 QuasarAppUtils::Warning);
176 }
177 }
178
179 return true;
180 }
181
182 return false;
183}
184
185bool ISqlDB::deleteObject(const QSharedPointer<DBObject> &delObj,
186 bool wait) {
187
188 if (!delObj)
189 return false;
190
191 auto id = delObj->dbAddress();
192
193 if (!deleteObjectP(delObj, wait)) {
194 return false;
195 }
196
197 if (id.isValid())
198 emit sigItemDeleted(id);
199
200 return true;
201
202}
203
204bool ISqlDB::updateObject(const QSharedPointer<DBObject> &saveObject,
205 bool wait) {
206
207 if (!saveObject || !saveObject->isValid()) {
208 return false;
209 }
210
211 if (!updateObjectP(saveObject, wait)) {
212 return false;
213 }
214
215 emit sigItemChanged(saveObject);
216
217 return true;
218}
219
220bool ISqlDB::insertObject(const QSharedPointer<DBObject> &saveObject, bool wait,
221 const QWeakPointer<unsigned int>& autoincrementIdResult) {
222 if (!saveObject || !saveObject->isValid()) {
223 return false;
224 }
225
226 if (!insertObjectP(saveObject, wait, autoincrementIdResult)) {
227 return false;
228 }
229
230 emit sigItemChanged(saveObject);
231
232 return true;
233}
234
235bool ISqlDB::replaceObject(const QSharedPointer<PKG::DBObject> &saveObject, bool wait) {
236 if (!saveObject || !saveObject->isValid()) {
237 return false;
238 }
239
240 if (!replaceObjectP(saveObject, wait)) {
241 return false;
242 }
243
244 emit sigItemChanged(saveObject);
245
246 return true;
247}
248
249bool ISqlDB::doQuery(const QString &query, const QVariantMap& toBind,
250 bool wait, QSqlQuery *result) const {
251
252 if (!_writer) {
253 return false;
254 }
255
256 return _writer->doQuery(query, toBind, wait, result);
257}
258
259bool ISqlDB::doSql(const QString &sqlFile, bool wait) const {
260 if (!_writer) {
261 return false;
262 }
263
264 return _writer->doSql(sqlFile, wait);
265}
266
267bool ISqlDB::init(const QString &initDbParams) {
268
269 if (!_writer) {
270 return false;
271 }
272
273 return _writer->initDb(initDbParams);
274}
275
276bool ISqlDB::init(const QVariantMap &params) {
277
278 if (!_writer) {
279 return false;
280 }
281
282 return _writer->initDb(params);
283}
284
285void ISqlDB::setSQLSources(const QStringList &list) {
286 auto db = writer();
287 if (db)
288 db->setSQLSources(list);
289}
290
294
295bool ISqlDB::changeObjects(const DBObject &templateObject,
296 const std::function<bool (const QSharedPointer<QH::PKG::DBObject>&)> &changeAction) {
297
298 QList<QSharedPointer<DBObject>> list;
299 if (!getAllObjects(templateObject, list)) {
300 return false;
301 }
302
303 if (!list.size())
304 return false;
305
306 for (const auto& obj :std::as_const(list)) {
307
308 if (!changeAction(obj)) {
309 return false;
310 };
311
312 if (!updateObject(obj)) {
313 return false;
314 };
315 }
316
317 return true;
318}
319
321 return _mode;
322}
323
325 _mode = mode;
326}
327
328void ISqlDB::globalUpdateDataBasePrivate(qint64 currentTime) {
329 QMutexLocker lock(&_saveLaterMutex);
330
331 for (auto it = _changes.begin(); it != _changes.end(); ++it) {
332
333 if (writer() && writer()->isValid()) {
334
335 auto obj = it.value();
336
337 if (!obj || !obj->isValid()) {
338 deleteFromCache(obj);
339
340 QuasarAppUtils::Params::log("writeUpdateItemIntoDB failed when"
341 " db object is not valid! obj=" +
342 obj->toString(),
343 QuasarAppUtils::VerboseLvl::Error);
344 continue;
345 }
346
347 bool saveResult = false;
348
349 switch (it.key()) {
350 case CacheAction::Insert: {
351 saveResult = writer()->insertObject(obj, true);
352 break;
353 }
354 case CacheAction::Update: {
355 saveResult = writer()->updateObject(obj, true);
356 break;
357 }
358 case CacheAction::Delete: {
359 saveResult = writer()->deleteObject(obj, true);
360 break;
361 }
362 default: {
363 QuasarAppUtils::Params::log("The Object of the cache have wrong type " +
364 obj->toString(),
365 QuasarAppUtils::VerboseLvl::Warning);
366
367 continue;
368 }
369 }
370
371 if (!saveResult ) {
372 QuasarAppUtils::Params::log("writeUpdateItemIntoDB failed when"
373 " work globalUpdateDataRelease!!! obj=" +
374 obj->toString(),
375 QuasarAppUtils::VerboseLvl::Error);
376 }
377 } else {
378
379 QuasarAppUtils::Params::log("writeUpdateItemIntoDB failed when"
380 " db writer is npt inited! ",
381 QuasarAppUtils::VerboseLvl::Error);
382 return;
383 }
384 }
385
386 _changes.clear();
387 setLastUpdateTime(currentTime);
388}
389
391 return updateInterval;
392}
393
394void ISqlDB::setUpdateInterval(const qint64 &value) {
395 updateInterval = value;
396}
397
398}
SqlDBCasheWriteMode getMode() const
getMode This method return mode of work database cache. For more information see the QH::SqlDBCasheWr...
Definition isqldb.cpp:320
void setWriter(SqlDBWriter *writer)
setWriter This method set new writer for this cache.
Definition isqldb.cpp:154
void prepareForDelete() override
prepareForDelete This method must be prepare object for delete. Override this for working main functi...
Definition isqldb.cpp:291
SqlDBWriter * writer() const
writer This method return is database writer object. For more inforamation about writer see the SqlDB...
Definition isqldb.cpp:150
bool insertObject(const QSharedPointer< QH::PKG::DBObject > &saveObject, bool wait=false, const QWeakPointer< unsigned int > &autoincrementIdResult={}) override
Definition isqldb.cpp:220
virtual QList< QSharedPointer< QH::PKG::DBObject > > getFromCache(const PKG::DBObject *obj)=0
getFromCache This method return strong pointer to the database object from cache (pool).
bool doQuery(const QString &query, const QVariantMap &bindValues, bool wait=false, QSqlQuery *result=nullptr) const override
doQuery This method execute a query in this database.
Definition isqldb.cpp:249
virtual bool init(const QString &initDbParams="")
init This method init the cache object and invoke the SqlDBWriter::initDb method.
Definition isqldb.cpp:267
bool getAllObjects(const PKG::DBObject &templateObject, QList< QSharedPointer< QH::PKG::DBObject > > &result) override
Definition isqldb.cpp:158
virtual void globalUpdateDataBasePrivate(qint64 currentTime)
globalUpdateDataBasePrivate This method update(write) all data from cache into database....
Definition isqldb.cpp:328
virtual void pushToQueue(const QSharedPointer< QH::PKG::DBObject > &obj, CacheAction type)
pushToQueue this method should be add the object to the update queue in the physical data dash.
Definition isqldb.cpp:132
bool doSql(const QString &sqlFile, bool wait) const override
doSql This method execute a query in this database.
Definition isqldb.cpp:259
void setLastUpdateTime(const qint64 &value)
setLastUpdateTime This method set new value of the update time.
Definition isqldb.cpp:128
bool deleteObject(const QSharedPointer< QH::PKG::DBObject > &delObj, bool wait=false) override
Definition isqldb.cpp:185
virtual void globalUpdateDataBase(SqlDBCasheWriteMode mode=SqlDBCasheWriteMode::Default)
globalUpdateDataBase This is base method for syncing data from the cache with database.
Definition isqldb.cpp:22
void setUpdateInterval(const qint64 &value)
getUpdateInterval This method set new value of an update interval for save changes into database....
Definition isqldb.cpp:394
void sigItemChanged(const QSharedPointer< QH::PKG::DBObject > &obj)
sigItemChanged This signal emitted when database object is changed.
virtual void deleteFromCache(const QSharedPointer< QH::PKG::DBObject > &delObj)=0
deleteFromCache This method delete object from db cache, bat not from database.
void sigItemDeleted(const QH::DbAddress &obj)
sigItemDeleted This signal emitted when database object is deleted.
bool changeObjects(const PKG::DBObject &templateObject, const std::function< bool(const QSharedPointer< PKG::DBObject > &)> &changeAction)
changeObjects This method change object of the database.
Definition isqldb.cpp:295
virtual bool updateCache(const QSharedPointer< QH::PKG::DBObject > &obj)=0
updateCache This method update already exits object on the cache, but not database.
ISqlDB(qint64 updateInterval=DEFAULT_UPDATE_INTERVAL, SqlDBCasheWriteMode mode=SqlDBCasheWriteMode::Default)
ISqlDB This is default constructor of dbcache.
Definition isqldb.cpp:139
void setSQLSources(const QStringList &list) override
setSQLSources This method set sql sources for deployed database.
Definition isqldb.cpp:285
virtual bool insertToCache(const QSharedPointer< QH::PKG::DBObject > &obj)=0
insertToCache This method insert object into cache, but not database. If Object exists in the cache t...
~ISqlDB() override
Definition isqldb.cpp:146
void setMode(const SqlDBCasheWriteMode &mode)
setMode This method set a new value of the mode database cache.
Definition isqldb.cpp:324
bool updateObject(const QSharedPointer< QH::PKG::DBObject > &saveObject, bool wait=false) override
Definition isqldb.cpp:204
bool replaceObject(const QSharedPointer< QH::PKG::DBObject > &saveObject, bool wait=false) override
Definition isqldb.cpp:235
qint64 getUpdateInterval() const
getUpdateInterval This method return update interval for save changes into database....
Definition isqldb.cpp:390
qint64 getLastUpdateTime() const
getLastUpdateTime This method return time of last update.
Definition isqldb.cpp:124
The DBObject class- main class for work with data base.
Definition dbobject.h:94
The SqlDBWriter class. This class write and read objects from database (hard level)....
Definition sqldbwriter.h:36
bool deleteObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false) override
deleteObject This method execute a delete method of obj and remove current object from database.
bool updateObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false) override
updateObject This method execute a update method of the saveObject and save all changes into database...
bool doSql(const QString &sqlFile, bool wait) const override
doSql This method execute a query in this database.
bool doQuery(const QString &query, const QVariantMap &bindValues={}, bool wait=false, QSqlQuery *result=nullptr) const override
doQuery This method execute a query in this database.
bool replaceObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false) override
replaceObject This method execute a replace method of the saveObject and insert or save if not exists...
virtual bool isValid() const
isValid This method return true if database is successful inited and working correctly.
virtual bool initDb(const QString &initDbParams=DEFAULT_DB_PATH)
initDb This method is wraper of the initDb(const QVariantMap &params) method. This implementation rea...
bool getAllObjects(const PKG::DBObject &templateObject, QList< QSharedPointer< PKG::DBObject > > &result) override
getAllObjects This method execute a select method of the templateObject and return list of all select...
bool insertObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false, const QWeakPointer< unsigned int > &autoincrementIdResult={}) override
insertObject This method execute a insert method of the saveObject and insert it into database.
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13
SqlDBCasheWriteMode
The SqlDBCasheWriteMode enum contains list of available modes of write data into database.
Definition isqldb.h:29
@ Force
This mode writes all changes to the database as soon as they come to the cache.
@ On_New_Thread
This mode write a changes into another thread.
CacheAction
The CacheAction enum contains types of database cache actions. The any database caches save all chang...
Definition isqldb.h:44
@ Update
Invoke the SqlDBWriter::updateObject method of a private database writer implementation.
@ Insert
Invoke the SqlDBWriter::insertObject method of a private database writer implementation.
@ Delete
Invoke the SqlDBWriter::deleteObject method of a private database writer implementation.