Heart 1.4.918.d927d9c
Heart is base back end library for your c++ Qt projects.
Loading...
Searching...
No Matches
isqldb.h
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#ifndef ISQLDB_H
9#define ISQLDB_H
10
11#include "iobjectprovider.h"
12
13#include <QMap>
14#include <QHash>
15#include <QSet>
16#include <QVariantMap>
17#include <QMutex>
18#include "config.h"
19#include "softdelete.h"
20
21namespace QH {
22
23class SqlDBWriter;
24class DbAddress;
25
29enum class SqlDBCasheWriteMode: int {
31 Default = 0x0,
35 Force = 0x2,
36} ;
37
54
72class HEARTSHARED_EXPORT ISqlDB: public QObject, public iObjectProvider, public SoftDelete
73{
74 Q_OBJECT
75
76public:
82 ISqlDB(qint64 updateInterval = DEFAULT_UPDATE_INTERVAL,
84 ~ISqlDB() override;
85
90 SqlDBWriter* writer() const;
91
97
98 bool getAllObjects(const PKG::DBObject &templateObject,
99 QList<QSharedPointer<QH::PKG::DBObject>> &result) override;
100
101 bool updateObject(const QSharedPointer<QH::PKG::DBObject>& saveObject,
102 bool wait = false) override;
103 bool deleteObject(const QSharedPointer<QH::PKG::DBObject>& delObj,
104 bool wait = false) override;
105 bool insertObject(const QSharedPointer<QH::PKG::DBObject>& saveObject,
106 bool wait = false,
107 const QWeakPointer<unsigned int>& autoincrementIdResult = {}) override;
108
109 bool replaceObject(const QSharedPointer<QH::PKG::DBObject>& saveObject,
110 bool wait = false) override;
111
112 bool doQuery(const QString &query, const QVariantMap& bindValues,
113 bool wait = false, QSqlQuery* result = nullptr) const override;
114
115 bool doSql(const QString &sqlFile, bool wait) const override;
116
124 bool changeObjects(const PKG::DBObject &templateObject,
125 const std::function<bool (const QSharedPointer<PKG::DBObject> &)> &changeAction);
130 qint64 getUpdateInterval() const;
131
136 void setUpdateInterval(const qint64 &value);
137
143 virtual bool init(const QString &initDbParams = "");
144
150 virtual bool init(const QVariantMap &params);
151
152 void setSQLSources(const QStringList &list) override;
153
154protected:
155 void prepareForDelete() override;
156
161 qint64 getLastUpdateTime() const;
162
167 void setLastUpdateTime(const qint64 &value);
168
174 virtual void deleteFromCache(const QSharedPointer<QH::PKG::DBObject> &delObj) = 0;
175
182 virtual bool insertToCache(const QSharedPointer<QH::PKG::DBObject> &obj) = 0;
183
189 virtual bool updateCache(const QSharedPointer<QH::PKG::DBObject> &obj) = 0;
190
196 virtual QList<QSharedPointer<QH::PKG::DBObject>>
198
204 virtual void pushToQueue(const QSharedPointer<QH::PKG::DBObject> &obj,
205 CacheAction type);
206
212
217 void setMode(const SqlDBCasheWriteMode &mode);
218
224 virtual void globalUpdateDataBasePrivate(qint64 currentTime);
225
231
232private:
233
234 bool updateObjectP(const QSharedPointer<QH::PKG::DBObject>& saveObject,
235 bool wait = false);
236 bool deleteObjectP(const QSharedPointer<QH::PKG::DBObject>& delObj,
237 bool wait = false);
238 bool insertObjectP(const QSharedPointer<QH::PKG::DBObject>& saveObject,
239 bool wait,
240 const QWeakPointer<unsigned int>& autoincrementIdResult);
241 bool replaceObjectP(const QSharedPointer<QH::PKG::DBObject>& saveObject,
242 bool wait = false);
243
244 qint64 lastUpdateTime = 0;
245 qint64 updateInterval = DEFAULT_UPDATE_INTERVAL;
246
248
249 SqlDBWriter* _writer = nullptr;
250
251 QHash<CacheAction, QSharedPointer<QH::PKG::DBObject>> _changes;
252 QMutex _saveLaterMutex;
253
254signals:
261 void sigItemChanged(const QSharedPointer<QH::PKG::DBObject> &obj);
262
270
271};
272
278constexpr inline uint qHash(CacheAction action) {
279 return static_cast<uint>(action);
280}
281
282}
283
284#endif // ISQLDB_H
The DbAddress class use to work with database addresses. Database Address it is structure with 2 valu...
Definition dbaddress.h:24
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...
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
The iObjectProvider class is base interface for work with database objects. Using on database writers...
#define DEFAULT_UPDATE_INTERVAL
Definition config.h:29
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13
uint qHash(NodeCoonectionStatus status)
qHash - Simple hash function of NodeCoonectionStatus
SqlDBCasheWriteMode
The SqlDBCasheWriteMode enum contains list of available modes of write data into database.
Definition isqldb.h:29
@ Default
This is default mode. The database is written to the database with each function call no more often t...
Definition isqldb.h:31
@ Force
This mode writes all changes to the database as soon as they come to the cache.
Definition isqldb.h:35
@ On_New_Thread
This mode write a changes into another thread.
Definition isqldb.h:33
@ Default
General trust of the node or client.
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.
Definition isqldb.h:50
@ None
Do nothing.
Definition isqldb.h:46
@ Insert
Invoke the SqlDBWriter::insertObject method of a private database writer implementation.
Definition isqldb.h:48
@ Delete
Invoke the SqlDBWriter::deleteObject method of a private database writer implementation.
Definition isqldb.h:52