Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
QH::PKG::DBObject Class Referenceabstract

The DBObject class- main class for work with data base. More...

#include <dbobject.h>

Inheritance diagram for QH::PKG::DBObject:
Inheritance graph
Collaboration diagram for QH::PKG::DBObject:
Collaboration graph

Public Member Functions

 DBObject ()
 
 ~DBObject () override
 
bool isValid () const override
 isValid This method check current object to valid.
 
bool isHaveAPrimaryKey () const
 isHaveAPrimaryKey This method return true if this object has a primary key. If you want to cache this database object then primary key must be not null.
 
virtual void clear ()
 clear This method clear all data of database object. Override This method for remove or reset your own members of class.
 
virtual DBObjectcreateDBObject () const =0
 createDBObject This method should be create a object with the some type as the object called this method. Example of override:
 
virtual PrepareResult prepareSelectQuery (QSqlQuery &q) const
 prepareSelectQuery This method should be prepare a query for selected data. Override this method for get item from database. The default implementation generate default select:
 
virtual bool fromSqlRecord (const QSqlRecord &q)=0
 fromSqlRecord This method should be initialize this object from the executed sqlRecord. The Default implementation get general dbObject information ( id and table name ). Override This method for initialize this object from sql query.
 
virtual PrepareResult prepareInsertQuery (QSqlQuery &q, bool replace) const
 prepareInsertQuery This method should be prepare a query for insert object into database. You need to create a own insert sql query for this object into database. Override this method for insert item into database. By Default This method prepare a insert query using the data that returned from the variantMap method.
 
virtual PrepareResult prepareUpdateQuery (QSqlQuery &q) const
 prepareUpdateQuery this method should be prepare a insert data query.
 
virtual PrepareResult prepareRemoveQuery (QSqlQuery &q) const
 prepareRemoveQuery This method should be prepare a query for remove this object from a database. Override this method for remove this item from database. The default implementation remove item from id or primaryKey for more information see DBObject::condition method.
 
virtual bool isCached () const
 isCached This method sholud be return status of object. If this method return true then this object can be saved into cache of database. Override this method and set this value to false for composite objects (objects for which a select query can return several objects or an object that does not have a direct representation in the database but contains common characteristics of several objects).
 
virtual bool isBundle () const
 isBundle This method definef determines whether the object will be abstract (composite objects) or single object If this function return true then SqlDBWriter create only one object after invoked selectquery. And if the selectquery function return a list of more 1 elements then a method fromSqlRecord must be invoked for each all elements of list.
 
DbAddress dbAddress () const
 dbAddress This method return address of the database object. IF the object is not valid then this method return an invalid database address.
 
QString toString () const override
 
virtual DBVariantMap variantMap () const
 variantMap This method should be create a DBVariantMap implementation of this database object.
 
virtual QString table () const =0
 table This method should be return name of the database table that should be contains objects with this type.
 
- Public Member Functions inherited from QH::PKG::AbstractData
 ~AbstractData () override
 
virtual unsigned short cmd () const =0
 cmd - This is command of this object, (for generate cmd use macross QH_PACKAGE)
 
virtual QString cmdString () const =0
 cmdString - This is command string of this object, (for generate cmd use macross QH_PACKAGE)
 
virtual bool toPackage (Package &package, const DistVersion &reqVersion, unsigned int triggerHash=0) const
 toPackage This method convert this class object to the package. For more info see Package class.
 
QString toString () const override
 toString - Return a string implementation for this object.
 
template<class C , class... Args>
C * create (Args &&... args) const
 create - This is factory method for create a new object.
 
void fromPakcage (const Package &pkg)
 fromPakcage - This method initialize the current object from a package class object.
 
- Public Member Functions inherited from QH::StreamBase
 StreamBase ()
 
virtual ~StreamBase ()
 
bool fromBytes (const QByteArray &data)
 fromBytes This method provide initialization of object from byte array.
 
QByteArray toBytes () const
 toBytes This method convert a current object to bytes array.
 
bool fromBase64 (const QString &data)
 fromBase64 This method provide initialization of object from the base64 string.
 
bool fromBase64 (const QByteArray &data)
 fromBase64 This method provide initialization of object from the base64 string.
 
QByteArray toBase64 () const
 toBase64 This method convert a current object to the base64 string.
 
template<class T >
T & copy (const StreamBase &right)
 copy This is base copy method for all StreamBase structures. Default implementation it is copy from byteArray.
 
template<class T >
bool compare (const T &right)
 compare This method compare array signatures of this and right objects.
 

Protected Member Functions

QDataStream & fromStream (QDataStream &stream) override
 fromStream This method should be read all bytes from the stream object and full the current object.
 
QDataStream & toStream (QDataStream &stream) const override
 fromStream This method should be write all members of the current object to the stream object.
 
virtual std::pair< QString, QMap< QString, QVariant > > condition () const
 condition This method must to return a condition of the WHERE block of the sql query. This method using on default implementation of DBObject::prepareSelectQuery and DBObject::prepareRemoveQuery methods. The default implementation generate when block by map for more information see the variantMap nethod. Override this method for customize your select or delete query.
 
virtual QString primaryKey () const
 primaryKey This method must be return the name of primary key of this object table. If you want to create data object without primary key just return empty string.
 
virtual QVariant primaryValue () const
 primaryValue This method is wraper of DBAddress::id. If This object do not contains a id value then return invalid value.
 
bool isInsertPrimaryKey () const
 isInsertPrimaryKey This method check primaryKeys type. If the primary key have a type MemberType::Insert then return true.
 
- Protected Member Functions inherited from QH::PKG::AbstractData
 AbstractData ()
 AbstractData - Base constructor of this object.
 
- Protected Member Functions inherited from QH::StreamBase
virtual int parsingVersion () const
 parsingVersion this method return parsing version of Qt. By Default is 0 (last available parsing). see https://doc.qt.io/qt-6/qdatastream.html#Version-enum
 
virtual unsigned int typeId () const
 typeId This method return id of type.
 

Additional Inherited Members

- Static Public Member Functions inherited from QH::PKG::AbstractData
static unsigned int command ()
 command This static method that return glaball code of this object.
 
static QString commandText ()
 commandText This method return text of package command
 

Detailed Description

The DBObject class- main class for work with data base.

Note
If you set in the default constructor primaryKey to empty value. The your object disable cache support. For more information see the DBObject::isCached method.
Warning
Object with empty table name is invalid.
See also
DataBaseNode

Definition at line 93 of file dbobject.h.

Constructor & Destructor Documentation

◆ DBObject()

QH::PKG::DBObject::DBObject ( )

Definition at line 22 of file dbobject.cpp.

Here is the call graph for this function:

◆ ~DBObject()

QH::PKG::DBObject::~DBObject ( )
override

Definition at line 26 of file dbobject.cpp.

Member Function Documentation

◆ clear()

void QH::PKG::DBObject::clear ( )
virtual

clear This method clear all data of database object. Override This method for remove or reset your own members of class.

Note
The Default implementation do nothing

Reimplemented in QH::PKG::DBObjectsRequest< T >.

Definition at line 318 of file dbobject.cpp.

Here is the caller graph for this function:

◆ condition()

std::pair< QString, QMap< QString, QVariant > > QH::PKG::DBObject::condition ( ) const
protectedvirtual

condition This method must to return a condition of the WHERE block of the sql query. This method using on default implementation of DBObject::prepareSelectQuery and DBObject::prepareRemoveQuery methods. The default implementation generate when block by map for more information see the variantMap nethod. Override this method for customize your select or delete query.

Returns
condition string and qvariant map of the values for binding (bindingKey:bindValue).
Note
This operation can not be block the sql request. Use the QString or int type for values of condition. If you want to bytes array in condition then override the DBObject::condition method.

Example of overriding:

QString DBObject::condition() const {
return {{"id = :id"},
{{{":id"}, {getId()}}}};
}
virtual std::pair< QString, QMap< QString, QVariant > > condition() const
condition This method must to return a condition of the WHERE block of the sql query....
Definition dbobject.cpp:212

Reimplemented in QH::PKG::DBObjectsRequest< T >.

Definition at line 212 of file dbobject.cpp.

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

◆ createDBObject()

virtual DBObject * QH::PKG::DBObject::createDBObject ( ) const
pure virtual

createDBObject This method should be create a object with the some type as the object called this method. Example of override:

return create<MyClassName>();
Note
The object created on this method not destroyed automatically.
Returns
pointer of new object with the same type.

Implemented in QH::PKG::DBObjectsRequest< T >, QH::PKG::DeleteObject, QH::PKG::GetMaxIntegerId, QH::PKG::GetSingleValue, and QH::PKG::SetSingleValue.

Here is the caller graph for this function:

◆ dbAddress()

DbAddress QH::PKG::DBObject::dbAddress ( ) const

dbAddress This method return address of the database object. IF the object is not valid then this method return an invalid database address.

Returns
The database address of current object.

Definition at line 242 of file dbobject.cpp.

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

◆ fromSqlRecord()

virtual bool QH::PKG::DBObject::fromSqlRecord ( const QSqlRecord &  q)
pure virtual

fromSqlRecord This method should be initialize this object from the executed sqlRecord. The Default implementation get general dbObject information ( id and table name ). Override This method for initialize this object from sql query.

Note
This method will be invoke for one object. But if isBundle return 'true' then a function fromSqlRecord moust be invoked for each all elements of list.
Parameters
qThis is query object.
Returns
true if method finished successful.

Exampel of override fromSqlRecord method:

bool ExampleObject::fromSqlRecord(const QSqlRecord &q) {
id = q.value("id").toInt();
exampleMember = q.value("exampleMember").toInt();
return isValid();
}
bool isValid() const override
isValid This method check current object to valid.
Definition dbobject.cpp:303

Implemented in QH::PKG::DBObjectsRequest< T >, QH::PKG::DeleteObject, QH::PKG::GetMaxIntegerId, QH::PKG::GetSingleValue, and QH::PKG::SetSingleValue.

◆ fromStream()

QDataStream & QH::PKG::DBObject::fromStream ( QDataStream &  stream)
overrideprotectedvirtual

fromStream This method should be read all bytes from the stream object and full the current object.

Note
The implementation of this method should be invoke a method of base class.
Parameters
streamThis is Qt stream object.
Returns
Qt stream object.

Examle of base simple implementation:

QDataStream &ExampleClass::fromStream(QDataStream &stream) const {
stream >> exampleMember;
return stream;
}
virtual QDataStream & toStream(QDataStream &stream) const =0
fromStream This method should be write all members of the current object to the stream object.

Implements QH::StreamBase.

Reimplemented in QH::PKG::DBObjectsRequest< T >, QH::PKG::DBObjectsRequestWithStream< T >, and QH::PKG::DeleteObject.

Definition at line 251 of file dbobject.cpp.

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

◆ isBundle()

bool QH::PKG::DBObject::isBundle ( ) const
virtual

isBundle This method definef determines whether the object will be abstract (composite objects) or single object If this function return true then SqlDBWriter create only one object after invoked selectquery. And if the selectquery function return a list of more 1 elements then a method fromSqlRecord must be invoked for each all elements of list.

Returns
true if the object is a selection from a set of database object.

Reimplemented in QH::PKG::DBObjectSet.

Definition at line 208 of file dbobject.cpp.

Here is the caller graph for this function:

◆ isCached()

bool QH::PKG::DBObject::isCached ( ) const
virtual

isCached This method sholud be return status of object. If this method return true then this object can be saved into cache of database. Override this method and set this value to false for composite objects (objects for which a select query can return several objects or an object that does not have a direct representation in the database but contains common characteristics of several objects).

Returns
True if item in cache. The Default implementation check the primary key if this object and if primary key is valid then return true else return false.

Reimplemented in QH::PKG::DeleteObject, QH::PKG::GetMaxIntegerId, QH::PKG::GetSingleValue, QH::PKG::SetSingleValue, and QH::PKG::DBObjectSet.

Definition at line 200 of file dbobject.cpp.

Here is the call graph for this function:

◆ isHaveAPrimaryKey()

bool QH::PKG::DBObject::isHaveAPrimaryKey ( ) const

isHaveAPrimaryKey This method return true if this object has a primary key. If you want to cache this database object then primary key must be not null.

Returns
true if object has a primary key.

Definition at line 314 of file dbobject.cpp.

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

◆ isInsertPrimaryKey()

bool QH::PKG::DBObject::isInsertPrimaryKey ( ) const
protected

isInsertPrimaryKey This method check primaryKeys type. If the primary key have a type MemberType::Insert then return true.

Returns
true if the primary key have the MemberType::Insert type.

Definition at line 238 of file dbobject.cpp.

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

◆ isValid()

bool QH::PKG::DBObject::isValid ( ) const
overridevirtual

isValid This method check current object to valid.

Returns
True if class isValid.

Reimplemented from QH::PKG::AbstractData.

Reimplemented in QH::PKG::DBObjectsRequest< T >.

Definition at line 303 of file dbobject.cpp.

Here is the call graph for this function:

◆ prepareInsertQuery()

PrepareResult QH::PKG::DBObject::prepareInsertQuery ( QSqlQuery &  q,
bool  replace 
) const
virtual

prepareInsertQuery This method should be prepare a query for insert object into database. You need to create a own insert sql query for this object into database. Override this method for insert item into database. By Default This method prepare a insert query using the data that returned from the variantMap method.

Parameters
qThuis is query object.
replaceThis option disable or enable replacing of exists objects.

Default insert query have a next template:

INSERT INTO %0(%1) VALUES (%3)

For more information see the DBObject::variantMap method.

Note
before creating a own object you need to create a own database structure.
Parameters
qThis is query object.
Returns
PrepareResult object with information about prepare results.

Example of overriding:

PrepareResult ExampleObject::prepareInsertQuery(QSqlQuery &q, replace) const {
QString queryString = "INSERT INTO %0(%1) VALUES (%2) ";
queryString = queryString.arg(table());
QString tableInsertHeader = "";
QString tableInsertValues = "";
for (auto it = map.begin(); it != map.end(); ++it) {
tableInsertHeader += it.key();
tableInsertValues += ":" + it.key();
if (it + 1 != map.end()) {
tableInsertHeader += ", ";
tableInsertValues += ", ";
}
}
queryString = queryString.arg(tableInsertHeader);
queryString = queryString.arg(tableInsertValues);
if (q.prepare(queryString)) {
for (auto it = map.begin(); it != map.end(); ++it) {
q.bindValue(":" + it.key(), it.value().value);
}
}
}
virtual DBVariantMap variantMap() const
variantMap This method should be create a DBVariantMap implementation of this database object.
Definition dbobject.cpp:295
virtual QString table() const =0
table This method should be return name of the database table that should be contains objects with th...
PrepareResult
The PrepareResult enum is result of work prepare sql query of dbobjects.
Definition dbobject.h:24
@ Success
prepare finished successful.
@ Fail
prepare is failed.
QMap< QString, DBVariant > DBVariantMap
DBVariantMap this is Map with key, and value with data type.
Definition dbobject.h:84
Note
If you want disable this method, just override it and return the PrepareResult::Disabled value.

Reimplemented in QH::PKG::SetSingleValue, and QH::PKG::DBObjectSet.

Definition at line 61 of file dbobject.cpp.

Here is the call graph for this function:

◆ prepareRemoveQuery()

PrepareResult QH::PKG::DBObject::prepareRemoveQuery ( QSqlQuery &  q) const
virtual

prepareRemoveQuery This method should be prepare a query for remove this object from a database. Override this method for remove this item from database. The default implementation remove item from id or primaryKey for more information see DBObject::condition method.

Parameters
qThis is query object.
Returns
PrepareResult object with information about prepare results.
Note
If you want disable this method just override it and return the PrepareResult::Disabled value.

Reimplemented in QH::PKG::DBObjectSet.

Definition at line 267 of file dbobject.cpp.

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

◆ prepareSelectQuery()

PrepareResult QH::PKG::DBObject::prepareSelectQuery ( QSqlQuery &  q) const
virtual

prepareSelectQuery This method should be prepare a query for selected data. Override this method for get item from database. The default implementation generate default select:

"select * from [table] where id=[id]".

This method create where block using the condition method. See DBObject::condition fr more information.

Parameters
qThis is query object.
Returns
PrepareResult object with information about prepare results.
Note
If you want disable this mehod just override it and return the PrepareResult::Disabled value.

Reimplemented in QH::PKG::GetMaxIntegerId, QH::PKG::GetSingleValue, and QH::PKG::DBObjectSet.

Definition at line 30 of file dbobject.cpp.

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

◆ prepareUpdateQuery()

PrepareResult QH::PKG::DBObject::prepareUpdateQuery ( QSqlQuery &  q) const
virtual

prepareUpdateQuery this method should be prepare a insert data query.

Bt Default This method generate query like this:

UPDATE value SET value = 2 WHERE id = 1

If you want to create a own insert query override this method.

Example of overriding this method:

PrepareResult prepareUpdateQuery(QSqlQuery& q) const {
QString queryString = "UPDATE %0 SET %1 WHERE %2";
queryString = queryString.arg(table());
QString tableUpdateValues = "";
QString tableUpdateRules = QString("%0 = :%0").
arg(primaryKey());
for (auto it = map.begin(); it != map.end(); ++it) {
if (!(it.value().type & MemberType::Update)) {
continue;
}
if (tableUpdateValues.size()) {
tableUpdateValues += ", ";
}
tableUpdateValues += QString("%0= :%0").arg(it.key());
}
queryString = queryString.arg(tableUpdateValues);
queryString = queryString.arg(tableUpdateRules);
if (q.prepare(queryString)) {
for (auto it = map.begin(); it != map.end(); ++it) {
if (it.value().type != MemberType::InsertUpdate) {
continue;
}
q.bindValue(":" + it.key(), it.value().value);
}
}
}
virtual QString primaryKey() const
primaryKey This method must be return the name of primary key of this object table....
Definition dbobject.cpp:230
virtual PrepareResult prepareUpdateQuery(QSqlQuery &q) const
prepareUpdateQuery this method should be prepare a insert data query.
Definition dbobject.cpp:123
@ Update
The Field With This type can be updated but not inserted.
@ InsertUpdate
The Field With This type can be inserted and updated.
Parameters
qThis is query object.
Returns
PrepareResult object with information about prepare results.

Reimplemented in QH::PKG::SetSingleValue, and QH::PKG::DBObjectSet.

Definition at line 123 of file dbobject.cpp.

Here is the call graph for this function:

◆ primaryKey()

QString QH::PKG::DBObject::primaryKey ( ) const
protectedvirtual

primaryKey This method must be return the name of primary key of this object table. If you want to create data object without primary key just return empty string.

Note
If you returned empty value then this method can not be prepare insert update and delete querys.
Returns
The primary key name.

Reimplemented in QH::PKG::DBObjectSet, QH::PKG::DeleteObject, QH::PKG::GetSingleValue, and QH::PKG::SetSingleValue.

Definition at line 230 of file dbobject.cpp.

Here is the caller graph for this function:

◆ primaryValue()

QVariant QH::PKG::DBObject::primaryValue ( ) const
protectedvirtual

primaryValue This method is wraper of DBAddress::id. If This object do not contains a id value then return invalid value.

Returns
Value of primaryKey ( database id ).
Note
If you alredy override the condition method then You can return empty string because this method using in generate default condition only.
See also
DBObject::condition.

Reimplemented in QH::PKG::DeleteObject, QH::PKG::GetSingleValue, and QH::PKG::SetSingleValue.

Definition at line 234 of file dbobject.cpp.

Here is the caller graph for this function:

◆ table()

virtual QString QH::PKG::DBObject::table ( ) const
pure virtual

table This method should be return name of the database table that should be contains objects with this type.

Returns
table name that contains object with this type.

Implemented in QH::PKG::DBObjectSet, QH::PKG::DeleteObject, QH::PKG::GetMaxIntegerId, QH::PKG::GetSingleValue, and QH::PKG::SetSingleValue.

Here is the caller graph for this function:

◆ toStream()

QDataStream & QH::PKG::DBObject::toStream ( QDataStream &  stream) const
overrideprotectedvirtual

fromStream This method should be write all members of the current object to the stream object.

Note
The implementation of this method should be invoke a method of base class.
Parameters
streamThis is Qt stream object.
Returns
Qt stream object.

Examle of base simple implementation:

QDataStream &ExampleClass::toStream(QDataStream &stream) const {
stream << exampleMember;
return stream;
}

Implements QH::StreamBase.

Reimplemented in QH::PKG::DBObjectsRequest< T >, QH::PKG::DBObjectsRequestWithStream< T >, and QH::PKG::DeleteObject.

Definition at line 259 of file dbobject.cpp.

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

◆ toString()

QString QH::PKG::DBObject::toString ( ) const
override

Definition at line 246 of file dbobject.cpp.

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

◆ variantMap()

DBVariantMap QH::PKG::DBObject::variantMap ( ) const
virtual

variantMap This method should be create a DBVariantMap implementation of this database object.

IF you have the database field with autoincrement you need to remove MemberType::Insert attribute from the your key. For more information see the MemberType::PrimaryKeyAutoIncrement attribute.

Example of return value:

return {{"name", {"Andrei", MemberType::Insert}},
{"age", {26, MemberType::InsertUpdate}},
{"extraData", {QByteArray{}, MemberType::InsertUpdate}}};
@ Insert
The Field With This type can be inserted but not updated.

Example of override:

auto map = ParrentClass::variantMap();
map.insert("myNewMembers", {_newMember.toBytes(), MemberType::InsertUpdate});
return map;
Note
This method using for create a default sql save request see the DBObject::prepareSaveQuery method for more information.
Returns
the QVariantMap implementation of this database object.
Note
If you want disable this functionality then override this method and return an empty map. But do not forget override the DBObject::prepareSelectQuery method, because its default implementation return error message.

Reimplemented in QH::PKG::DBObjectsRequest< T >, and QH::PKG::DeleteObject.

Definition at line 295 of file dbobject.cpp.

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

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