Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
isqldb.h
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#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,
33 On_New_Thread = 0x1,
35 Force = 0x2,
36} ;
37
44enum class CacheAction: int {
46 None,
48 Insert,
50 Update,
52 Delete
53};
54
72class HEARTSHARED_EXPORT ISqlDB: public QObject, public iObjectProvider, public SoftDelete
73{
74 Q_OBJECT
75
76public:
82 ISqlDB(qint64 updateInterval = DEFAULT_UPDATE_INTERVAL,
83 SqlDBCasheWriteMode mode = SqlDBCasheWriteMode::Default);
84 ~ISqlDB() override;
85
90 SqlDBWriter* writer() const;
91
96 void setWriter(SqlDBWriter* writer);
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
211 SqlDBCasheWriteMode getMode() const;
212
217 void setMode(const SqlDBCasheWriteMode &mode);
218
224 virtual void globalUpdateDataBasePrivate(qint64 currentTime);
225
230 virtual void globalUpdateDataBase(SqlDBCasheWriteMode mode = SqlDBCasheWriteMode::Default);
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
The ISqlDB class it is db cache and bridge for DbWriters. Work Scheme of the database cache:
Definition isqldb.h:73
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).
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.
virtual bool updateCache(const QSharedPointer< QH::PKG::DBObject > &obj)=0
updateCache This method update already exits object on the cache, but not database.
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...
The DBObject class- main class for work with data base.
Definition dbobject.h:94
The SoftDelete class povide the soft delete functionality. All child classes of this class must be de...
Definition softdelete.h:20
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
#define HEARTSHARED_EXPORT
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
bool init()
init This method initialize default resources of the Heart Library.
Definition heart.cpp:15
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.
@ 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.
@ None
Do nothing.
@ Insert
Invoke the SqlDBWriter::insertObject method of a private database writer implementation.
@ Delete
Invoke the SqlDBWriter::deleteObject method of a private database writer implementation.