Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
QH::PKG::DBObjectsRequest< T > Class Template Referencefinal

The DBObjectsRequest class is template class for get array of TEMPLATE Objects from database. More...

#include <dbobjectsrequest.h>

Inheritance diagram for QH::PKG::DBObjectsRequest< T >:
Inheritance graph
Collaboration diagram for QH::PKG::DBObjectsRequest< T >:
Collaboration graph

Public Member Functions

 DBObjectsRequest (const Package &pkkg)
 DBObjectsRequest This is default constructor for parsing packages.
 
 DBObjectsRequest (const QString &table, const QString &conditions="", const QVariantMap &valuesToBind={})
 DBObjectsRequest This contsrucor create a object with request the array of T objects.
 
void clear () override
 clear This method clear all data of database object. Override This method for remove or reset your own members of class.
 
bool fromSqlRecord (const QSqlRecord &q) override
 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.
 
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.
 
bool isValid () const override
 isValid This method check current object to valid.
 
const QList< QSharedPointer< T > > & data () const
 data This method return a list of getted objects.
 
DBVariantMap variantMap () const override
 variantMap This method should be create a DBVariantMap implementation of this database object.
 
void setConditions (const QString &newConditions)
 setConditions This method sets condition value for request.
 
- Public Member Functions inherited from QH::PKG::DBObjectSet
 DBObjectSet (const QString table)
 
PrepareResult prepareInsertQuery (QSqlQuery &q, bool replace) const override final
 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.
 
PrepareResult prepareRemoveQuery (QSqlQuery &q) const override final
 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.
 
PrepareResult prepareSelectQuery (QSqlQuery &q) const override final
 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:
 
PrepareResult prepareUpdateQuery (QSqlQuery &q) const override final
 prepareUpdateQuery this method should be prepare a insert data query.
 
bool isCached () const override final
 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).
 
bool isBundle () const override final
 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.
 
QString primaryKey () const override
 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.
 
QString table () const override
 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::DBObject
 DBObject ()
 
 ~DBObject () override
 
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.
 
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
 
- 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

std::pair< QString, QMap< QString, QVariant > > condition () const override
 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.
 
DBObjectcreateDBObject () const override
 createDBObject This method should be create a object with the some type as the object called this method. Example of override:
 
- Protected Member Functions inherited from QH::PKG::DBObject
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.
 

Protected Attributes

QList< QSharedPointer< T > > _data
 

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

template<class T>
class QH::PKG::DBObjectsRequest< T >

The DBObjectsRequest class is template class for get array of TEMPLATE Objects from database.

Example for using this class.

DBObjectsRequest<User> query("users", "id='Andrei'");
SingleServer::getObject(query);
auto result = query.data();
The DBObjectsRequest class is template class for get array of TEMPLATE Objects from database.
Note
Any objects in the query well not be saved in to cache. For caching your objects use the CachedDbObjectsRequest class.

Definition at line 33 of file dbobjectsrequest.h.

Constructor & Destructor Documentation

◆ DBObjectsRequest() [1/2]

template<class T >
QH::PKG::DBObjectsRequest< T >::DBObjectsRequest ( const Package pkkg)
inline

DBObjectsRequest This is default constructor for parsing packages.

Parameters
pkkgThis is package.

Definition at line 41 of file dbobjectsrequest.h.

Here is the call graph for this function:

◆ DBObjectsRequest() [2/2]

template<class T >
QH::PKG::DBObjectsRequest< T >::DBObjectsRequest ( const QString &  table,
const QString &  conditions = "",
const QVariantMap &  valuesToBind = {} 
)
inline

DBObjectsRequest This contsrucor create a object with request the array of T objects.

Note
If you want to get all elements from table then skip conditions argument or set it to empty string value.
Parameters
tableThis is name of database table.
conditionsThis is string with conditions for create sql query. If you want to get all elemts just ignore this argument.
valuesToBindThis is map of the value for the reques.
See also
DBObjectsRequest::setConditions

Definition at line 53 of file dbobjectsrequest.h.

Member Function Documentation

◆ clear()

template<class T >
void QH::PKG::DBObjectsRequest< T >::clear ( )
inlineoverridevirtual

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 from QH::PKG::DBObject.

Definition at line 62 of file dbobjectsrequest.h.

Here is the caller graph for this function:

◆ condition()

template<class T >
std::pair< QString, QMap< QString, QVariant > > QH::PKG::DBObjectsRequest< T >::condition ( ) const
inlineoverrideprotectedvirtual

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 from QH::PKG::DBObject.

Definition at line 113 of file dbobjectsrequest.h.

◆ createDBObject()

template<class T >
DBObject * QH::PKG::DBObjectsRequest< T >::createDBObject ( ) const
inlineoverrideprotectedvirtual

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.

Implements QH::PKG::DBObject.

Definition at line 117 of file dbobjectsrequest.h.

Here is the call graph for this function:

◆ data()

template<class T >
const QList< QSharedPointer< T > > & QH::PKG::DBObjectsRequest< T >::data ( ) const
inline

data This method return a list of getted objects.

Returns
list of const T * objects.

Definition at line 95 of file dbobjectsrequest.h.

◆ fromSqlRecord()

template<class T >
bool QH::PKG::DBObjectsRequest< T >::fromSqlRecord ( const QSqlRecord &  q)
inlineoverridevirtual

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.

Implements QH::PKG::DBObject.

Definition at line 66 of file dbobjectsrequest.h.

Here is the call graph for this function:

◆ fromStream()

template<class T >
QDataStream & QH::PKG::DBObjectsRequest< T >::fromStream ( QDataStream &  stream)
inlineoverridevirtual

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.

Reimplemented from QH::PKG::DBObject.

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

Definition at line 79 of file dbobjectsrequest.h.

◆ isValid()

template<class T >
bool QH::PKG::DBObjectsRequest< T >::isValid ( ) const
inlineoverridevirtual

isValid This method check current object to valid.

Returns
True if class isValid.

Reimplemented from QH::PKG::DBObject.

Definition at line 87 of file dbobjectsrequest.h.

◆ setConditions()

template<class T >
void QH::PKG::DBObjectsRequest< T >::setConditions ( const QString &  newConditions)
inline

setConditions This method sets condition value for request.

Parameters
newConditionsThis is new condition value.

Definition at line 107 of file dbobjectsrequest.h.

◆ toStream()

template<class T >
QDataStream & QH::PKG::DBObjectsRequest< T >::toStream ( QDataStream &  stream) const
inlineoverridevirtual

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;
}

Reimplemented from QH::PKG::DBObject.

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

Definition at line 83 of file dbobjectsrequest.h.

◆ variantMap()

template<class T >
DBVariantMap QH::PKG::DBObjectsRequest< T >::variantMap ( ) const
inlineoverridevirtual

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}}};
@ InsertUpdate
The Field With This type can be inserted and updated.
@ 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 from QH::PKG::DBObject.

Definition at line 99 of file dbobjectsrequest.h.

Member Data Documentation

◆ _data

template<class T >
QList<QSharedPointer<T> > QH::PKG::DBObjectsRequest< T >::_data
protected

Definition at line 121 of file dbobjectsrequest.h.


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