Heart 1.3.848.aa44c26
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-2025 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 qDebug() << "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 qWarning() << "Selected object from database can not be saved into database cache. " + object->toString();
174 }
175 }
176
177 return true;
178 }
179
180 return false;
181}
182
183bool ISqlDB::deleteObject(const QSharedPointer<DBObject> &delObj,
184 bool wait) {
185
186 if (!delObj)
187 return false;
188
189 auto id = delObj->dbAddress();
190
191 if (!deleteObjectP(delObj, wait)) {
192 return false;
193 }
194
195 if (id.isValid())
196 emit sigItemDeleted(id);
197
198 return true;
199
200}
201
202bool ISqlDB::updateObject(const QSharedPointer<DBObject> &saveObject,
203 bool wait) {
204
205 if (!saveObject || !saveObject->isValid()) {
206 return false;
207 }
208
209 if (!updateObjectP(saveObject, wait)) {
210 return false;
211 }
212
213 emit sigItemChanged(saveObject);
214
215 return true;
216}
217
218bool ISqlDB::insertObject(const QSharedPointer<DBObject> &saveObject, bool wait,
219 const QWeakPointer<unsigned int>& autoincrementIdResult) {
220 if (!saveObject || !saveObject->isValid()) {
221 return false;
222 }
223
224 if (!insertObjectP(saveObject, wait, autoincrementIdResult)) {
225 return false;
226 }
227
228 emit sigItemChanged(saveObject);
229
230 return true;
231}
232
233bool ISqlDB::replaceObject(const QSharedPointer<PKG::DBObject> &saveObject, bool wait) {
234 if (!saveObject || !saveObject->isValid()) {
235 return false;
236 }
237
238 if (!replaceObjectP(saveObject, wait)) {
239 return false;
240 }
241
242 emit sigItemChanged(saveObject);
243
244 return true;
245}
246
247bool ISqlDB::doQuery(const QString &query, const QVariantMap& toBind,
248 bool wait, QSqlQuery *result) const {
249
250 if (!_writer) {
251 return false;
252 }
253
254 return _writer->doQuery(query, toBind, wait, result);
255}
256
257bool ISqlDB::doSql(const QString &sqlFile, bool wait) const {
258 if (!_writer) {
259 return false;
260 }
261
262 return _writer->doSql(sqlFile, wait);
263}
264
265bool ISqlDB::init(const QString &initDbParams) {
266
267 if (!_writer) {
268 return false;
269 }
270
271 return _writer->initDb(initDbParams);
272}
273
274bool ISqlDB::init(const QVariantMap &params) {
275
276 if (!_writer) {
277 return false;
278 }
279
280 return _writer->initDb(params);
281}
282
283void ISqlDB::setSQLSources(const QStringList &list) {
284 auto db = writer();
285 if (db)
286 db->setSQLSources(list);
287}
288
292
293bool ISqlDB::changeObjects(const DBObject &templateObject,
294 const std::function<bool (const QSharedPointer<QH::PKG::DBObject>&)> &changeAction) {
295
296 QList<QSharedPointer<DBObject>> list;
297 if (!getAllObjects(templateObject, list)) {
298 return false;
299 }
300
301 if (!list.size())
302 return false;
303
304 for (const auto& obj :std::as_const(list)) {
305
306 if (!changeAction(obj)) {
307 return false;
308 };
309
310 if (!updateObject(obj)) {
311 return false;
312 };
313 }
314
315 return true;
316}
317
319 return _mode;
320}
321
323 _mode = mode;
324}
325
326void ISqlDB::globalUpdateDataBasePrivate(qint64 currentTime) {
327 QMutexLocker lock(&_saveLaterMutex);
328
329 for (auto it = _changes.begin(); it != _changes.end(); ++it) {
330
331 if (writer() && writer()->isValid()) {
332
333 auto obj = it.value();
334
335 if (!obj || !obj->isValid()) {
336 deleteFromCache(obj);
337
338 qCritical() << "writeUpdateItemIntoDB failed when db object is not valid! obj=" << obj->toString();
339 continue;
340 }
341
342 bool saveResult = false;
343
344 switch (it.key()) {
345 case CacheAction::Insert: {
346 saveResult = writer()->insertObject(obj, true);
347 break;
348 }
349 case CacheAction::Update: {
350 saveResult = writer()->updateObject(obj, true);
351 break;
352 }
353 case CacheAction::Delete: {
354 saveResult = writer()->deleteObject(obj, true);
355 break;
356 }
357 default: {
358 qWarning() << "The Object of the cache have wrong type " << obj->toString();
359
360 continue;
361 }
362 }
363
364 if (!saveResult ) {
365 qCritical() << "writeUpdateItemIntoDB failed when work globalUpdateDataRelease!!! obj=" << obj->toString();
366 }
367 } else {
368
369 qCritical() << "writeUpdateItemIntoDB failed when db writer is not inited!";
370 return;
371 }
372 }
373
374 _changes.clear();
375 setLastUpdateTime(currentTime);
376}
377
379 return updateInterval;
380}
381
382void ISqlDB::setUpdateInterval(const qint64 &value) {
383 updateInterval = value;
384}
385
386}
SqlDBCasheWriteMode getMode() const
getMode This method return mode of work database cache. For more information see the QH::SqlDBCasheWr...
Definition isqldb.cpp:318
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:289
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:218
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:247
virtual bool init(const QString &initDbParams="")
init This method init the cache object and invoke the SqlDBWriter::initDb method.
Definition isqldb.cpp:265
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:326
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:257
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:183
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:382
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:293
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:283
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:322
bool updateObject(const QSharedPointer< QH::PKG::DBObject > &saveObject, bool wait=false) override
Definition isqldb.cpp:202
bool replaceObject(const QSharedPointer< QH::PKG::DBObject > &saveObject, bool wait=false) override
Definition isqldb.cpp:233
qint64 getUpdateInterval() const
getUpdateInterval This method return update interval for save changes into database....
Definition isqldb.cpp:378
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.