Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
QH::DataBase Class Reference

The DataBase class is DataBase base implementation. This implementation contains methods for work with database. DataBaseNode is thread save class. More...

#include <database.h>

Inheritance diagram for QH::DataBase:
Inheritance graph
Collaboration diagram for QH::DataBase:
Collaboration graph

Signals

void sigObjectChanged (const QSharedPointer< QH::PKG::DBObject > &obj)
 sigItemChanged This signal emitted when database object is changed.
 
void sigObjectDeleted (const QH::DbAddress &obj)
 sigItemDeleted This signal emitted when database object is deleted.
 

Public Member Functions

 DataBase (QObject *ptr=nullptr)
 
 ~DataBase ()
 
virtual bool initSqlDb (QString DBparamsFile="", ISqlDB *cache=nullptr, SqlDBWriter *writer=nullptr)
 intSqlDb This method initalize database of this node or server.
 
bool isSqlInited () const
 isSqlInited This method return true if database initialized successful.
 
bool run ()
 run This method start and initialize the data base connection.
 
virtual bool run (const QString &localNodeName)
 run This method is some as AbstractNode::run but set for node custom work folder. This maybe use for multiple deployment nodes on one host.
 
void stop ()
 stop This method stop and diskonnect the database.
 
virtual QVariantMap defaultDbParams () const
 defaultDbParams This method return default database parameters for this node. Override this method for set new default parameters. Or use json database parameters file in DataBaseNode::run.
 
QString dbLocation () const
 dbLocation This method return location of nodes or clients database.
 
QVariant getDBAttribute (const QString &key, const QVariant &defaultVal)
 getDBAttribute This method gets value from the default of QH DB table "DataBaseAttributes".
 
bool setDBAttribute (const QString &key, const QVariant &newValue)
 setDBAttribute This method sets value for the default of QH DB table "DataBaseAttributes".
 

Protected Member Functions

QString backUp (int version) const
 backUp This method make a backup of database.
 
const QString & localNodeName () const
 localNodeName This method return local node name.
 
void setLocalNodeName (const QString &newLocalNodeName)
 setLocalNodeName This method sets new local name of database file.
 
virtual void initDefaultDbObjects (ISqlDB *cache, SqlDBWriter *writer)
 initDefaultDbObjects This method create a default cache and database writer objects if the input pointers is null Override this method for create a custom database objects for your node class.
 
ISqlDBdb () const
 db This node return pointer to database object.
 
virtual bool welcomeAddress (AbstractNodeInfo *node)
 welcomeAddress This method send to the node information about self. Override this method if you want send custom data to incoming connection.
 
virtual QStringList SQLSources () const
 SQLSources This method contains list of sqldatabase sources. This method will be invoked into initialize sql method and deploy sql database.
 
virtual QSet< QString > systemTables () const
 systemTables This method return the set of tables that forbidden for users. By default is NetworkMembers and MemberPermisions tables.
 
virtual void objectRemoved (const DbAddress &address)
 objectRemoved This method invoked when object with address removed from database. Oberride this method for handle this event. Default implementation do nothing.
 
virtual void objectChanged (const QSharedPointer< PKG::DBObject > &obj)
 objectChanged This method invoked when object with address changed in database. Override this method for handle this event. Default implementation do nothing.
 
virtual const DBPatchMap dbPatches () const
 dbPatches This method should be return map with functions that upgrade production data base. Eeach function shoul be can upgrade databae from preview version to own version. Example:
 
void addDBPatch (const DBPatch &patch)
 addDBPatch This method add database patch to the data base object.
 
virtual bool upgradeDataBase ()
 upgradeDataBase This method upgrade data base to actyaly database version.
 
virtual void onBeforeDBUpgrade (int currentVerion, int tergetVersion) const
 onBeforeDBUpgrade This method will be invoked before upgrade database.
 
template<class Object , class Id , class Setter >
QSharedPointer< Object > getById (const Id &id, Setter setter, bool ifNotExistsCreate=false)
 Get an object by its identifier.
 
template<class Object , class Id , class Setter >
bool deleteById (const Id &id, Setter setter)
 Delete an object by its identifier.
 
template<class Object >
bool saveObj (const Object &obj)
 Save an object in the database.
 
template<class Object >
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.
 
template<class Object >
QList< QSharedPointer< Object > > getAll (const QString &table, const QString &condition="") const
 Get a list of all objects from a specified table.
 

Friends

class DataBaseNode
 

Detailed Description

The DataBase class is DataBase base implementation. This implementation contains methods for work with database. DataBaseNode is thread save class.

Note
before use the database you need to run it. Use The DataBase::run method for this. If you need to stop database, run the DataBase::stop method.
See also
DBObject
DataBaseNode

Definition at line 42 of file database.h.

Constructor & Destructor Documentation

◆ DataBase()

QH::DataBase::DataBase ( QObject *  ptr = nullptr)

Definition at line 26 of file database.cpp.

◆ ~DataBase()

QH::DataBase::~DataBase ( )

Definition at line 109 of file database.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ addDBPatch()

void QH::DataBase::addDBPatch ( const DBPatch patch)
protected

addDBPatch This method add database patch to the data base object.

Parameters
patchThis is object of the database patch
Note
This method will be crashed if patch is invalid.
See also
DataBase::dbPatches
DataBase::onBeforeDBUpgrade

Definition at line 154 of file database.cpp.

Here is the call graph for this function:

◆ backUp()

QString QH::DataBase::backUp ( int  version) const
protected

backUp This method make a backup of database.

Parameters
versionThis is current version of db.
Returns
path to backupped db.

Definition at line 176 of file database.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ db()

ISqlDB * QH::DataBase::db ( ) const
protected

db This node return pointer to database object.

Returns
The pointer to data base.

Definition at line 202 of file database.cpp.

Here is the caller graph for this function:

◆ dbLocation()

QString QH::DataBase::dbLocation ( ) const

dbLocation This method return location of nodes or clients database.

Returns
path to the location of database.

Definition at line 168 of file database.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dbPatches()

const DBPatchMap QH::DataBase::dbPatches ( ) const
protectedvirtual

dbPatches This method should be return map with functions that upgrade production data base. Eeach function shoul be can upgrade databae from preview version to own version. Example:

We have 4 version of data base {0, 1, 2, 3} each version should be contains own function for upgrade data base. Where the 0 version is first version of database. (genesis)

0, // fromVersion
1, // toVersion
[](const QH::iObjectProvider* database) -> bool {
// Some code for update from 0 to 1
} // action of patch
});
1, // fromVersion
2, // toVersion
[](const QH::iObjectProvider* database) -> bool {
// Some code for update from 1 to 2
} // action of patch
});
2, // fromVersion
3, // toVersion
[](const QH::iObjectProvider* database) -> bool {
// Some code for update from 2 to 3
} // action of patch
});
void addDBPatch(const DBPatch &patch)
addDBPatch This method add database patch to the data base object.
Definition database.cpp:154
The iObjectProvider class is base interface for work with database objects. Using on database writers...
Returns
Map of database pactches.
See also
DBPatchMap
DBPatch
DataBase::addDBPatch
DataBase::onBeforeDBUpgrade

Definition at line 150 of file database.cpp.

Here is the caller graph for this function:

◆ defaultDbParams()

QVariantMap QH::DataBase::defaultDbParams ( ) const
virtual

defaultDbParams This method return default database parameters for this node. Override this method for set new default parameters. Or use json database parameters file in DataBaseNode::run.

Returns
Map with all database parameters. For more information of available parameters see the SqlDBWriter::defaultInitPararm method.

Definition at line 284 of file database.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ deleteById()

template<class Object , class Id , class Setter >
bool QH::DataBase::deleteById ( const Id &  id,
Setter  setter 
)
inlineprotected

Delete an object by its identifier.

This method deletes an object of type Object from the database using the given identifier and setter function.

Template Parameters
ObjectThe type of object to delete.
IdThe type of the identifier.
SetterThe type of the setter function.
Parameters
idThe identifier of the object.
setterThe setter function to set the identifier in the object.
Returns
true if the object is successfully deleted, false otherwise.

Example:

deleteById<Role>(userId, &Role::setUserId);

Definition at line 371 of file database.h.

◆ getAll()

template<class Object >
QList< QSharedPointer< Object > > QH::DataBase::getAll ( const QString &  table,
const QString &  condition = "" 
) const
inlineprotected

Get a list of all objects from a specified table.

This method retrieves a list of all objects of type Object from the specified table in the database.

Template Parameters
ObjectThe type of objects to retrieve.
Parameters
tableThe name of the table in the database.
Returns
A list of pointers to objects of type QSharedPointer<Object>. If no objects are found, an empty list is returned.

Example:

auto&& data = getAll<User>("Users");

Definition at line 446 of file database.h.

◆ getById()

template<class Object , class Id , class Setter >
QSharedPointer< Object > QH::DataBase::getById ( const Id &  id,
Setter  setter,
bool  ifNotExistsCreate = false 
)
inlineprotected

Get an object by its identifier.

This method retrieves an object of type Object from the database using the given identifier and setter function.

Template Parameters
ObjectThe type of object to retrieve.
IdThe type of the identifier.
SetterThe type of the setter function.
Parameters
idThe identifier of the object.
setterThe setter function to set the identifier in the object.
ifNotExistsCreate- this option will create a new object if object with id is not existst into database. But object wil not save into database.
Returns
A pointer to an object of type QSharedPointer<Object>, or nullptr if the object is not found.

Example:

return getById<MenuItem>(id, &MenuItem::setId);

Definition at line 335 of file database.h.

◆ getDBAttribute()

QVariant QH::DataBase::getDBAttribute ( const QString &  key,
const QVariant &  defaultVal 
)

getDBAttribute This method gets value from the default of QH DB table "DataBaseAttributes".

Parameters
keyThis is key of attribute
defaultValthis is default value, if needed attribute is not exists.
Returns
attribute value.

Definition at line 293 of file database.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initDefaultDbObjects()

void QH::DataBase::initDefaultDbObjects ( ISqlDB cache,
SqlDBWriter writer 
)
protectedvirtual

initDefaultDbObjects This method create a default cache and database writer objects if the input pointers is null Override this method for create a custom database objects for your node class.

Note
If you override this object then you no longer need to overload the run method to set your own controls. This method invoked automatically when you call the DataBaseNode::run method.
Parameters
cacheThis is Cache database object.
writerThis is Database writerObject.

Definition at line 113 of file database.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initSqlDb()

bool QH::DataBase::initSqlDb ( QString  DBparamsFile = "",
ISqlDB cache = nullptr,
SqlDBWriter writer = nullptr 
)
virtual

intSqlDb This method initalize database of this node or server.

Parameters
DBparamsFileThis is path to json file with all params of database. For more information of available parameters see the SqlDBWriter::defaultInitPararm method.
cacheThis is pointer to the custom child of SqlDBCache class. IF you set nullptr value of this parameter then well be created a default SqlDBCache object.
writerThis is pointer tot the custom child of SqlDBWriter class. If you set nullptr value of this parameter then well be created a default AsyncSqlDbWriter object.
Returns
True if the database initialized successful.

Definition at line 30 of file database.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ insertObj()

template<class Object >
bool QH::DataBase::insertObj ( const Object &  obj,
const QWeakPointer< unsigned int > &  resultId = {} 
)
inlineprotected

insert a new value into database, and save into resultId autoincremented id of inserted object.

Template Parameters
ObjectThe type of object to save.
Parameters
objThe object to save.
Returns
true if the object is successfully saved, false otherwise.

Example:

auto&& id = QSharedPointe<unsigned int>:: create();
insertObj(role.dynamicCast<Role>(), id.toWeekRef());
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

Definition at line 420 of file database.h.

◆ isSqlInited()

bool QH::DataBase::isSqlInited ( ) const

isSqlInited This method return true if database initialized successful.

Returns
True if database initialized successful.

Definition at line 74 of file database.cpp.

Here is the caller graph for this function:

◆ localNodeName()

const QString & QH::DataBase::localNodeName ( ) const
protected

localNodeName This method return local node name.

Returns
local node name

Definition at line 276 of file database.cpp.

Here is the caller graph for this function:

◆ objectChanged()

void QH::DataBase::objectChanged ( const QSharedPointer< PKG::DBObject > &  obj)
protectedvirtual

objectChanged This method invoked when object with address changed in database. Override this method for handle this event. Default implementation do nothing.

Parameters
objThis is address of the changed object.

Definition at line 146 of file database.cpp.

◆ objectRemoved()

void QH::DataBase::objectRemoved ( const DbAddress address)
protectedvirtual

objectRemoved This method invoked when object with address removed from database. Oberride this method for handle this event. Default implementation do nothing.

Parameters
addressThis is address of the deteted object.

Definition at line 142 of file database.cpp.

◆ onBeforeDBUpgrade()

void QH::DataBase::onBeforeDBUpgrade ( int  currentVerion,
int  tergetVersion 
) const
protectedvirtual

onBeforeDBUpgrade This method will be invoked before upgrade database.

Parameters
currentVerionThis is current database version
tergetVersionThis is target database version.
See also
DataBase::dbPatches
DataBase::addDBPatch

Definition at line 272 of file database.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run() [1/2]

bool QH::DataBase::run ( )

run This method start and initialize the data base connection.

Returns
true if finished successful else false.

Definition at line 78 of file database.cpp.

Here is the call graph for this function:

◆ run() [2/2]

bool QH::DataBase::run ( const QString &  localNodeName)
virtual

run This method is some as AbstractNode::run but set for node custom work folder. This maybe use for multiple deployment nodes on one host.

Parameters
localNodeNameThis is name of node and work folder of node.

Definition at line 85 of file database.cpp.

Here is the call graph for this function:

◆ saveObj()

template<class Object >
bool QH::DataBase::saveObj ( const Object &  obj)
inlineprotected

Save an object in the database.

This method saves an object of type Object in the database.

Template Parameters
ObjectThe type of object to save.
Parameters
objThe object to save.
Returns
true if the object is successfully saved, false otherwise.

Example:

saveObj(role.dynamicCast<Role>());
bool saveObj(const Object &obj)
Save an object in the database.
Definition database.h:397

Definition at line 397 of file database.h.

◆ setDBAttribute()

bool QH::DataBase::setDBAttribute ( const QString &  key,
const QVariant &  newValue 
)

setDBAttribute This method sets value for the default of QH DB table "DataBaseAttributes".

Parameters
keyThis is key of attribute
newValuethis is new value

Definition at line 308 of file database.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setLocalNodeName()

void QH::DataBase::setLocalNodeName ( const QString &  newLocalNodeName)
protected

setLocalNodeName This method sets new local name of database file.

Note
This method must be invoked before the DataBaseNode::initsqlDb and the DataBaseNode::run methods.
Parameters
newLocalNodeNameThis is new name of node object and node databae file.

Definition at line 280 of file database.cpp.

Here is the caller graph for this function:

◆ sigObjectChanged

void QH::DataBase::sigObjectChanged ( const QSharedPointer< QH::PKG::DBObject > &  obj)
signal

sigItemChanged This signal emitted when database object is changed.

Note
emit implemented in updateObject and insertObject methods. So If you override then methods do not forget add emit of the sigItemChanged signal.
Parameters
objThis is changed object.
Note
This is wrapper of the ISqlDBCache::sigItemChanged
Here is the caller graph for this function:

◆ sigObjectDeleted

void QH::DataBase::sigObjectDeleted ( const QH::DbAddress obj)
signal

sigItemDeleted This signal emitted when database object is deleted.

Note
emit implemented in the deleteObject method. So if you override the deleteObject method do not forget add emit of the sigItemChanged signal.
Parameters
objThis is address of the removed object.
Note
This is wrapper of the ISqlDBCache::sigItemDeleted
Here is the caller graph for this function:

◆ SQLSources()

QStringList QH::DataBase::SQLSources ( ) const
protectedvirtual

SQLSources This method contains list of sqldatabase sources. This method will be invoked into initialize sql method and deploy sql database.

All sql files will be deployed in QList order.

By Default This method deploy next sql code:

CREATE TABLE IF NOT EXISTS NetworkMembers (
id VARCHAR(64) PRIMARY KEY NOT NULL,
authenticationData BLOB default NULL,
trust INTEGER default 0
);
CREATE TABLE IF NOT EXISTS MemberPermisions (
memberId VARCHAR(64) NOT NULL,
objectTable VARCHAR(100) NOT NULL,
objectId VARCHAR(64) NOT NULL,
lvl INTEGER NOT NULL,
FOREIGN KEY(memberId) REFERENCES NetworkMembers(id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE UNIQUE INDEX IF NOT EXISTS MemberPermisionsIndex ON MemberPermisions(memberId, objectTable, objectId);

For add own sql code just override this method, but do not forget invoke a base method of a parent class.

Example:

return DataBaseNode::SQLSources() << "path/to/mySqlFile.sql";
Returns
the list to deploy sql files.

Definition at line 132 of file database.cpp.

Here is the caller graph for this function:

◆ stop()

void QH::DataBase::stop ( )

stop This method stop and diskonnect the database.

Definition at line 99 of file database.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ systemTables()

QSet< QString > QH::DataBase::systemTables ( ) const
protectedvirtual

systemTables This method return the set of tables that forbidden for users. By default is NetworkMembers and MemberPermisions tables.

Returns
set of tables names.

Definition at line 138 of file database.cpp.

◆ upgradeDataBase()

bool QH::DataBase::upgradeDataBase ( )
protectedvirtual

upgradeDataBase This method upgrade data base to actyaly database version.

Note
The last version of dbPatches is actyaly version.
Returns
true if operation finished successful
Note
if you want to disable this feature then override this method and return true.

Definition at line 210 of file database.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ welcomeAddress()

bool QH::DataBase::welcomeAddress ( AbstractNodeInfo node)
protectedvirtual

welcomeAddress This method send to the node information about self. Override this method if you want send custom data to incoming connection.

Parameters
nodeThis is info object of the peer node.
Returns
true if all information sender successful.

Definition at line 128 of file database.cpp.

Friends And Related Symbol Documentation

◆ DataBaseNode

friend class DataBaseNode
friend

Definition at line 475 of file database.h.


The documentation for this class was generated from the following files: