|
| 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".
|
|
|
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.
|
|
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.
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
-
Object | The type of object to delete. |
Id | The type of the identifier. |
Setter | The type of the setter function. |
- Parameters
-
id | The identifier of the object. |
setter | The 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.
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
-
Object | The type of object to retrieve. |
Id | The type of the identifier. |
Setter | The type of the setter function. |
- Parameters
-
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. |
- 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.
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 130 of file database.cpp.