Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
database.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 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;
30class AbstractNodeInfo;
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...
The DataBase class is DataBase base implementation. This implementation contains methods for work wit...
Definition database.h:43
void sigObjectChanged(const QSharedPointer< QH::PKG::DBObject > &obj)
sigItemChanged This signal emitted when database object is changed.
bool deleteById(const Id &id, Setter setter)
Delete an object by its identifier.
Definition database.h:371
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
bool saveObj(const Object &obj)
Save an object in the database.
Definition database.h:397
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
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
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
#define HEARTSHARED_EXPORT
Q_DECLARE_METATYPE(QH::HostAddress)
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