Heart 1.4.918.d927d9c
Heart is base back end library for your c++ Qt projects.
Loading...
Searching...
No Matches
database.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 QH_DATABASE_H
9#define QH_DATABASE_H
10
11#include "dbobjectsrequest.h"
12#include "dbpatch.h"
13#include "isqldb.h"
14#include <dbobject.h>
15#include <hostaddress.h>
16
17
18namespace QH {
19
20namespace PKG {
21class WebSocket;
22class ISubscribableData;
23}
24
25class ISqlDB;
26class SqlDBWriter;
27class DbAddress;
28class NodeId;
29class iObjectProvider;
31
42class HEARTSHARED_EXPORT DataBase: public QObject
43{
44 Q_OBJECT
45public:
46
47 DataBase(QObject * ptr = nullptr);
48 ~DataBase();
49
60 virtual bool initSqlDb( QString DBparamsFile = "",
61 ISqlDB * cache = nullptr,
62 SqlDBWriter* writer = nullptr);
63
68 bool isSqlInited() const;
69
74 bool run();
75
81 virtual bool run(const QString &localNodeName);
82
86 void stop();
87
94 virtual QVariantMap defaultDbParams() const;
95
100 QString dbLocation() const;
101
108 QVariant getDBAttribute(const QString& key, const QVariant& defaultVal);
109
115 bool setDBAttribute(const QString& key, const QVariant& newValue);
116
117signals:
118
126 void sigObjectChanged(const QSharedPointer<QH::PKG::DBObject> &obj);
127
136
137protected:
138
144 QString backUp(int version) const;
145
150 const QString &localNodeName() const;
151
158 void setLocalNodeName(const QString &newLocalNodeName);
159
168 virtual void initDefaultDbObjects(ISqlDB *cache, SqlDBWriter *writer);
169
174 ISqlDB* db() const;
175
182 virtual bool welcomeAddress(AbstractNodeInfo *node);
183
221 virtual QStringList SQLSources() const;
222
228 virtual QSet<QString> systemTables() const;
229
235 virtual void objectRemoved(const DbAddress& address);
236
242 virtual void objectChanged(const QSharedPointer<PKG::DBObject>& obj);
243
285 virtual const DBPatchMap dbPatches() const;
286
294 void addDBPatch(const DBPatch& patch);
295
302 virtual bool upgradeDataBase();
303
311 virtual void onBeforeDBUpgrade(int currentVerion, int tergetVersion) const;
312
313
334 template <class Object, class Id, class Setter>
335 QSharedPointer<Object> getById(const Id& id, Setter setter, bool ifNotExistsCreate = false) {
336 if (auto&& database = db()) {
337 auto&& request = QSharedPointer<Object>::create();
338 (*request.*setter)(id);
339
340 if (auto&& result = database->getObject(*request)) {
341 return result;
342 }
343
344 if (ifNotExistsCreate)
345 return request;
346 }
347
348 return nullptr;
349 };
350
370 template <class Object, class Id, class Setter>
371 bool deleteById(const Id& id, Setter setter) {
372 if (auto&& database = db()) {
373 auto&& request = QSharedPointer<Object>::create();
374 (*request.*setter)(id);
375 return database->deleteObject(request);
376 }
377
378 return false;
379 };
380
396 template <class Object>
397 bool saveObj(const Object& obj) {
398 if (auto&& database = db()) {
399 return database->replaceObject(obj);
400 }
401
402 return false;
403 };
404
419 template <class Object>
420 bool insertObj(const Object& obj, const QWeakPointer<unsigned int>& resultId = {}) {
421 if (auto&& database = db()) {
422 return database->insertObject(obj, !resultId.isNull(), resultId);
423 }
424
425 return false;
426 };
427
445 template<class Object>
446 QList<QSharedPointer<Object>> getAll(const QString& table, const QString& condition = "") const {
447 QH::PKG::DBObjectsRequest<Object> request(table, condition);
448
449 auto&& response = db()->getObject(request);
450 if (!response) {
451 return {};
452 }
453
454 return response->data();
455 }
456
457private:
464 bool workWithSubscribe(const PKG::WebSocket &rec,
465 const QVariant &clientOrNodeid,
466 const AbstractNodeInfo *sender);
467
468
469 bool isForbidenTable(const QString& table);
470
471 ISqlDB *_db = nullptr;
472 unsigned short _targetDBVersion = 0;
473 DBPatchMap _dbPatches;
474 QString _localNodeName;
475 friend class DataBaseNode;
476
477};
478
479
480}
481
482Q_DECLARE_METATYPE(QSharedPointer<QH::PKG::DBObject>)
483#endif // QH_DATABASE_H
The AbstractNodeInfo class contains information about client or server connection and tcp socket of n...
void sigObjectChanged(const QSharedPointer< QH::PKG::DBObject > &obj)
sigItemChanged This signal emitted when database object is changed.
bool isSqlInited() const
isSqlInited This method return true if database initialized successful.
Definition database.cpp:72
bool deleteById(const Id &id, Setter setter)
Delete an object by its identifier.
Definition database.h:371
void stop()
stop This method stop and diskonnect the database.
Definition database.cpp:97
bool insertObj(const Object &obj, const QWeakPointer< unsigned int > &resultId={})
insert a new value into database, and save into resultId autoincremented id of inserted object.
Definition database.h:420
virtual bool upgradeDataBase()
upgradeDataBase This method upgrade data base to actyaly database version.
Definition database.cpp:208
void setLocalNodeName(const QString &newLocalNodeName)
setLocalNodeName This method sets new local name of database file.
Definition database.cpp:274
friend class DataBaseNode
Definition database.h:475
DataBase(QObject *ptr=nullptr)
Definition database.cpp:26
bool run()
run This method start and initialize the data base connection.
Definition database.cpp:76
QVariant getDBAttribute(const QString &key, const QVariant &defaultVal)
getDBAttribute This method gets value from the default of QH DB table "DataBaseAttributes".
Definition database.cpp:287
virtual void initDefaultDbObjects(ISqlDB *cache, SqlDBWriter *writer)
initDefaultDbObjects This method create a default cache and database writer objects if the input poin...
Definition database.cpp:111
bool setDBAttribute(const QString &key, const QVariant &newValue)
setDBAttribute This method sets value for the default of QH DB table "DataBaseAttributes".
Definition database.cpp:302
bool saveObj(const Object &obj)
Save an object in the database.
Definition database.h:397
virtual const DBPatchMap dbPatches() const
dbPatches This method should be return map with functions that upgrade production data base....
Definition database.cpp:148
virtual QVariantMap defaultDbParams() const
defaultDbParams This method return default database parameters for this node. Override this method fo...
Definition database.cpp:278
void sigObjectDeleted(const QH::DbAddress &obj)
sigItemDeleted This signal emitted when database object is deleted.
QSharedPointer< Object > getById(const Id &id, Setter setter, bool ifNotExistsCreate=false)
Get an object by its identifier.
Definition database.h:335
virtual QStringList SQLSources() const
SQLSources This method contains list of sqldatabase sources. This method will be invoked into initial...
Definition database.cpp:130
virtual bool welcomeAddress(AbstractNodeInfo *node)
welcomeAddress This method send to the node information about self. Override this method if you want ...
Definition database.cpp:126
const QString & localNodeName() const
localNodeName This method return local node name.
Definition database.cpp:270
virtual void objectRemoved(const DbAddress &address)
objectRemoved This method invoked when object with address removed from database. Oberride this metho...
Definition database.cpp:140
QString dbLocation() const
dbLocation This method return location of nodes or clients database.
Definition database.cpp:166
void addDBPatch(const DBPatch &patch)
addDBPatch This method add database patch to the data base object.
Definition database.cpp:152
virtual QSet< QString > systemTables() const
systemTables This method return the set of tables that forbidden for users. By default is NetworkMemb...
Definition database.cpp:136
ISqlDB * db() const
db This node return pointer to database object.
Definition database.cpp:200
virtual void objectChanged(const QSharedPointer< PKG::DBObject > &obj)
objectChanged This method invoked when object with address changed in database. Override this method ...
Definition database.cpp:144
virtual void onBeforeDBUpgrade(int currentVerion, int tergetVersion) const
onBeforeDBUpgrade This method will be invoked before upgrade database.
Definition database.cpp:266
QList< QSharedPointer< Object > > getAll(const QString &table, const QString &condition="") const
Get a list of all objects from a specified table.
Definition database.h:446
QString backUp(int version) const
backUp This method make a backup of database.
Definition database.cpp:174
virtual bool initSqlDb(QString DBparamsFile="", ISqlDB *cache=nullptr, SqlDBWriter *writer=nullptr)
intSqlDb This method initalize database of this node or server.
Definition database.cpp:30
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
The DBObjectsRequest class is template class for get array of TEMPLATE Objects from database.
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...
Q_DECLARE_METATYPE(QH::HostAddress)
The PKG namesapce - this namespace contains all default packages of the Heart library....
Definition heart.h:211
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13
QMap< unsigned short, QMap< unsigned short, DBPatch > > DBPatchMap
DBPatchMap This is 2 depch map of the DBPatch structure when the first key it is version (from) and s...
Definition dbpatch.h:49
DBPatch This is function that should be upgrade database.
Definition dbpatch.h:25