![]() |
Heart 1.3.866.61e5c3c
Heart is base back end library for your c++ Qt projects.
|
The DataBase class is DataBase base implementation. This implementation contains methods for work with database. DataBaseNode is thread save class. More...
#include <database.h>
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. | |
ISqlDB * | db () 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 |
The DataBase class is DataBase base implementation. This implementation contains methods for work with database. DataBaseNode is thread save class.
Definition at line 42 of file database.h.
QH::DataBase::DataBase | ( | QObject * | ptr = nullptr | ) |
Definition at line 26 of file database.cpp.
QH::DataBase::~DataBase | ( | ) |
|
protected |
addDBPatch This method add database patch to the data base object.
patch | This is object of the database patch |
Definition at line 152 of file database.cpp.
|
protected |
backUp This method make a backup of database.
version | This is current version of db. |
Definition at line 174 of file database.cpp.
|
protected |
db This node return pointer to database object.
Definition at line 200 of file database.cpp.
QString QH::DataBase::dbLocation | ( | ) | const |
dbLocation This method return location of nodes or clients database.
Definition at line 166 of file database.cpp.
|
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)
Definition at line 148 of file database.cpp.
|
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.
Definition at line 278 of file database.cpp.
|
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.
Object | The type of object to delete. |
Id | The type of the identifier. |
Setter | The type of the setter function. |
id | The identifier of the object. |
setter | The setter function to set the identifier in the object. |
Example:
Definition at line 371 of file database.h.
|
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.
Object | The type of objects to retrieve. |
table | The name of the table in the database. |
Example:
Definition at line 446 of file database.h.
|
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.
Object | The type of object to retrieve. |
Id | The type of the identifier. |
Setter | The type of the setter function. |
id | The identifier of the object. |
setter | The 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. |
Example:
Definition at line 335 of file database.h.
QVariant QH::DataBase::getDBAttribute | ( | const QString & | key, |
const QVariant & | defaultVal | ||
) |
getDBAttribute This method gets value from the default of QH DB table "DataBaseAttributes".
key | This is key of attribute |
defaultVal | this is default value, if needed attribute is not exists. |
Definition at line 287 of file database.cpp.
|
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.
cache | This is Cache database object. |
writer | This is Database writerObject. |
Definition at line 111 of file database.cpp.
|
virtual |
intSqlDb This method initalize database of this node or server.
DBparamsFile | This is path to json file with all params of database. For more information of available parameters see the SqlDBWriter::defaultInitPararm method. |
cache | This 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. |
writer | This 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. |
Definition at line 30 of file database.cpp.
|
inlineprotected |
insert a new value into database, and save into resultId autoincremented id of inserted object.
Object | The type of object to save. |
obj | The object to save. |
Example:
Definition at line 420 of file database.h.
bool QH::DataBase::isSqlInited | ( | ) | const |
isSqlInited This method return true if database initialized successful.
Definition at line 72 of file database.cpp.
|
protected |
localNodeName This method return local node name.
Definition at line 270 of file database.cpp.
|
protectedvirtual |
objectChanged This method invoked when object with address changed in database. Override this method for handle this event. Default implementation do nothing.
obj | This is address of the changed object. |
Definition at line 144 of file database.cpp.
|
protectedvirtual |
objectRemoved This method invoked when object with address removed from database. Oberride this method for handle this event. Default implementation do nothing.
address | This is address of the deteted object. |
Definition at line 140 of file database.cpp.
|
protectedvirtual |
onBeforeDBUpgrade This method will be invoked before upgrade database.
currentVerion | This is current database version |
tergetVersion | This is target database version. |
Definition at line 266 of file database.cpp.
bool QH::DataBase::run | ( | ) |
run This method start and initialize the data base connection.
Definition at line 76 of file database.cpp.
|
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.
localNodeName | This is name of node and work folder of node. |
Definition at line 83 of file database.cpp.
|
inlineprotected |
Save an object in the database.
This method saves an object of type Object
in the database.
Object | The type of object to save. |
obj | The object to save. |
Example:
Definition at line 397 of file database.h.
bool QH::DataBase::setDBAttribute | ( | const QString & | key, |
const QVariant & | newValue | ||
) |
setDBAttribute This method sets value for the default of QH DB table "DataBaseAttributes".
key | This is key of attribute |
newValue | this is new value |
Definition at line 302 of file database.cpp.
|
protected |
setLocalNodeName This method sets new local name of database file.
newLocalNodeName | This is new name of node object and node databae file. |
Definition at line 274 of file database.cpp.
|
signal |
sigItemChanged This signal emitted when database object is changed.
obj | This is changed object. |
|
signal |
sigItemDeleted This signal emitted when database object is deleted.
obj | This is address of the removed object. |
|
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:
For add own sql code just override this method, but do not forget invoke a base method of a parent class.
Example:
Definition at line 130 of file database.cpp.
void QH::DataBase::stop | ( | ) |
stop This method stop and diskonnect the database.
Definition at line 97 of file database.cpp.
|
protectedvirtual |
systemTables This method return the set of tables that forbidden for users. By default is NetworkMembers and MemberPermisions tables.
Definition at line 136 of file database.cpp.
|
protectedvirtual |
upgradeDataBase This method upgrade data base to actyaly database version.
Definition at line 208 of file database.cpp.
|
protectedvirtual |
welcomeAddress This method send to the node information about self. Override this method if you want send custom data to incoming connection.
node | This is info object of the peer node. |
Definition at line 126 of file database.cpp.
|
friend |
Definition at line 475 of file database.h.