Heart 1.4.918.d927d9c
Heart is base back end library for your c++ Qt projects.
Loading...
Searching...
No Matches
dbobject.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2025 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#ifndef DBOBJECT_H
9#define DBOBJECT_H
10#include <QSqlRecord>
11#include <QVariantMap>
12#include "abstractdata.h"
13#include "heart_global.h"
14#include "dbaddress.h"
15
16class QSqlQuery;
17
18namespace QH {
19namespace PKG {
20
32
62
63constexpr inline uint qHash(MemberType type) {
64 return static_cast<uint>(type);
65}
66
70struct HEARTSHARED_EXPORT DBVariant {
71 DBVariant();
72
73 DBVariant(const QVariant& value, MemberType type);
74 QVariant value;
76};
77
84typedef QMap<QString, DBVariant> DBVariantMap;
85
93class HEARTSHARED_EXPORT DBObject : public AbstractData
94{
95 QH_PACKAGE("DBObject")
96
97public:
98
99 DBObject();
100
101 ~DBObject() override;
102
103 bool isValid() const override;
104
110 bool isHaveAPrimaryKey() const;
111
117 virtual void clear();
118
128 virtual DBObject* createDBObject() const = 0;
129
143 virtual PrepareResult prepareSelectQuery(QSqlQuery& q) const;
144
163 virtual bool fromSqlRecord(const QSqlRecord& q) = 0;
164
228 virtual PrepareResult prepareInsertQuery(QSqlQuery& q, bool replace) const;
229
288 virtual PrepareResult prepareUpdateQuery(QSqlQuery& q) const;
289
299 virtual PrepareResult prepareRemoveQuery(QSqlQuery& q) const;
300
312 virtual bool isCached() const;
313
320 virtual bool isBundle() const;
321
327 DbAddress dbAddress() const;
328
329 QString toString() const override;
330
361 virtual DBVariantMap variantMap() const;
362
367 virtual QString table() const = 0;
368
369protected:
370
371 QDataStream &fromStream(QDataStream &stream) override;
372 QDataStream &toStream(QDataStream &stream) const override;
373
390
391 virtual std::pair<QString, QMap<QString, QVariant>> condition() const;
392
399 virtual QString primaryKey() const;
400
407 virtual QVariant primaryValue() const;
408
414 bool isInsertPrimaryKey() const;
415
416};
417}
418}
419
422Q_DECLARE_METATYPE(QList<QH::PKG::DBObject *>*);
423Q_DECLARE_METATYPE(QList<const QH::PKG::DBObject *>*);
424
425#endif // DBOBJECT_H
#define QH_PACKAGE(S)
QH_PACKAGE This macross prepare data to send and create a global id for package. For get global id us...
The DbAddress class use to work with database addresses. Database Address it is structure with 2 valu...
Definition dbaddress.h:24
AbstractData()
AbstractData - Base constructor of this object.
The DBObject class- main class for work with data base.
Definition dbobject.h:94
DbAddress dbAddress() const
dbAddress This method return address of the database object. IF the object is not valid then this met...
Definition dbobject.cpp:234
bool isHaveAPrimaryKey() const
isHaveAPrimaryKey This method return true if this object has a primary key. If you want to cache this...
Definition dbobject.cpp:305
virtual bool fromSqlRecord(const QSqlRecord &q)=0
fromSqlRecord This method should be initialize this object from the executed sqlRecord....
virtual PrepareResult prepareRemoveQuery(QSqlQuery &q) const
prepareRemoveQuery This method should be prepare a query for remove this object from a database....
Definition dbobject.cpp:258
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:206
QDataStream & toStream(QDataStream &stream) const override
fromStream This method should be write all members of the current object to the stream object.
Definition dbobject.cpp:251
bool isInsertPrimaryKey() const
isInsertPrimaryKey This method check primaryKeys type. If the primary key have a type MemberType::Ins...
Definition dbobject.cpp:230
virtual DBVariantMap variantMap() const
variantMap This method should be create a DBVariantMap implementation of this database object.
Definition dbobject.cpp:286
QDataStream & fromStream(QDataStream &stream) override
fromStream This method should be read all bytes from the stream object and full the current object.
Definition dbobject.cpp:243
virtual void clear()
clear This method clear all data of database object. Override This method for remove or reset your ow...
Definition dbobject.cpp:309
virtual QString primaryKey() const
primaryKey This method must be return the name of primary key of this object table....
Definition dbobject.cpp:222
QString toString() const override
Definition dbobject.cpp:238
virtual PrepareResult prepareSelectQuery(QSqlQuery &q) const
prepareSelectQuery This method should be prepare a query for selected data. Override this method for ...
Definition dbobject.cpp:30
virtual PrepareResult prepareUpdateQuery(QSqlQuery &q) const
prepareUpdateQuery this method should be prepare a insert data query.
Definition dbobject.cpp:122
virtual PrepareResult prepareInsertQuery(QSqlQuery &q, bool replace) const
prepareInsertQuery This method should be prepare a query for insert object into database....
Definition dbobject.cpp:61
virtual bool isCached() const
isCached This method sholud be return status of object. If this method return true then this object c...
Definition dbobject.cpp:194
virtual bool isBundle() const
isBundle This method definef determines whether the object will be abstract (composite objects) or si...
Definition dbobject.cpp:202
virtual DBObject * createDBObject() const =0
createDBObject This method should be create a object with the some type as the object called this met...
bool isValid() const override
Definition dbobject.cpp:294
virtual QString table() const =0
table This method should be return name of the database table that should be contains objects with th...
virtual QVariant primaryValue() const
primaryValue This method is wraper of DBAddress::id. If This object do not contains a id value then r...
Definition dbobject.cpp:226
Q_DECLARE_METATYPE(QH::HostAddress)
The PKG namesapce - this namespace contains all default packages of the Heart library....
Definition heart.h:211
PrepareResult
The PrepareResult enum is result of work prepare sql query of dbobjects.
Definition dbobject.h:24
@ Success
prepare finished successful.
Definition dbobject.h:28
@ Disabled
prepare disabled for method. Use this case for disable prepare method for object without errors.
Definition dbobject.h:30
@ Fail
prepare is failed.
Definition dbobject.h:26
QMap< QString, DBVariant > DBVariantMap
DBVariantMap this is Map with key, and value with data type.
Definition dbobject.h:84
constexpr uint qHash(MemberType type)
Definition dbobject.h:63
MemberType
The MemberType enum. This enum contains types of members DBObjects classes. for more information see ...
Definition dbobject.h:37
@ PrimaryKey
The primary key field without autoincrement.
Definition dbobject.h:57
@ None
The Field with this type can not be update and Inserted.
Definition dbobject.h:39
@ Autoincement
The field with this atribute automaticaly incemented into database, and will not to added into insert...
Definition dbobject.h:51
@ InsertUpdate
The Field With This type can be inserted and updated.
Definition dbobject.h:54
@ Unique
The Field with this type can not be duplicate on a table. If a Database object do not have a primary ...
Definition dbobject.h:48
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13
@ Update
Invoke the SqlDBWriter::updateObject method of a private database writer implementation.
Definition isqldb.h:50
@ None
Do nothing.
Definition isqldb.h:46
@ Insert
Invoke the SqlDBWriter::insertObject method of a private database writer implementation.
Definition isqldb.h:48
MemberType type
Definition dbobject.h:75