15#include <QSharedPointer>
16#include <qaglobalutils.h>
32 QString queryString =
"SELECT " + map.join(
",") +
" FROM %0 ";
33 queryString = queryString.arg(
table());
36 auto [conditionQueryString, conditionBindingMap] =
condition();
38 if (conditionQueryString.size()) {
40 queryString +=
" WHERE " + conditionQueryString;
41 if (!q.prepare(queryString)) {
45 for (
auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) {
46 q.bindValue(it.key(), it.value());
53 if (!q.prepare(queryString)) {
65 qCritical() <<
"The variantMap method return an empty map.";
70 QString queryString = (replace)?
71 "REPLACE INTO %0(%1) VALUES (%2) " :
72 "INSERT INTO %0(%1) VALUES (%2) ";
75 queryString = queryString.arg(
table());
76 QString tableInsertHeader =
"";
77 QString tableInsertValues =
"";
79 for (
auto it = map.begin(); it != map.end(); ++it) {
89 tableInsertHeader += it.key();
90 tableInsertValues +=
":" + it.key();
91 if (std::next(it, 1) != map.end()) {
92 tableInsertHeader +=
", ";
93 tableInsertValues +=
", ";
98 queryString = queryString.arg(tableInsertHeader);
99 queryString = queryString.arg(tableInsertValues);
101 if (q.prepare(queryString)) {
103 for (
auto it = map.begin(); it != map.end(); ++it) {
112 q.bindValue(
":" + it.key(), it.value().value);
126 qCritical() <<
"The variantMap method return an empty map.";
131 auto [conditionQueryString, conditionBindingMap] =
condition();
133 if (conditionQueryString.isEmpty()) {
134 qCritical() <<
"The object soue not have condition for update object.";
138 QString queryString =
"UPDATE %0 SET %1 WHERE " + conditionQueryString;
140 queryString = queryString.arg(
table());
141 QString tableUpdateValues =
"";
143 for (
auto it = map.begin(); it != map.end(); ++it) {
148 if (tableUpdateValues.size()) {
149 tableUpdateValues +=
", ";
152 tableUpdateValues += QString(
"%0= :%0").arg(it.key());
156 if (tableUpdateValues.isEmpty()) {
157 qCritical() <<
"Fail to generate condition for object: " +
toString() +
158 ". The object do not have valid update fields.";
162 queryString = queryString.arg(tableUpdateValues);
164 if (q.prepare(queryString)) {
166 for (
auto it = map.begin(); it != map.end(); ++it) {
171 q.bindValue(
":" + it.key(), it.value().value);
174 for (
auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) {
177 qWarning() <<
"Bad object configuration: "
178 "The " << it.key() <<
" field using in the condition and has MemberType::Update configuration."
179 " All condition fields should not use the MemberType::Update. \n" <<
toString();
182 q.bindValue(it.key(), it.value());
209 if (!primaryVal.isNull()) {
210 return {QString(
"%0 = :%0").arg(
primaryKey()),
211 {{QString(
":%0").arg(
primaryKey()), {primaryVal}}}};
214 qCritical() <<
"Fail to generate condition for object: " +
toString() +
215 ". Object do not have valid unique fields or valid database address.";
244 qWarning() <<
"This object not support stream operator."
245 " Please Override the fromStream method for this object. " +
toString();
252 qWarning() <<
"This object not support stream operator."
253 " Please Override the toStream method for this object. " +
toString();
259 QString queryString =
"DELETE FROM %0 ";
260 queryString = queryString.arg(
table());
262 auto [conditionQueryString, conditionBindingMap] =
condition();
264 if (conditionQueryString.size()) {
266 queryString +=
" WHERE " + conditionQueryString;
267 if (!q.prepare(queryString)) {
271 for (
auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) {
272 q.bindValue(it.key(), it.value());
278 if (!q.prepare(queryString)) {
301 return table().size();
The DbAddress class use to work with database addresses. Database Address it is structure with 2 valu...
bool isValid() const override
isValid This method check current object to valid.
QString toString() const override
toString - Return a string implementation for this object.
DbAddress dbAddress() const
dbAddress This method return address of the database object. IF the object is not valid then this met...
bool isHaveAPrimaryKey() const
isHaveAPrimaryKey This method return true if this object has a primary key. If you want to cache this...
virtual PrepareResult prepareRemoveQuery(QSqlQuery &q) const
prepareRemoveQuery This method should be prepare a query for remove this object from a database....
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....
QDataStream & toStream(QDataStream &stream) const override
fromStream This method should be write all members of the current object to the stream object.
bool isInsertPrimaryKey() const
isInsertPrimaryKey This method check primaryKeys type. If the primary key have a type MemberType::Ins...
virtual DBVariantMap variantMap() const
variantMap This method should be create a DBVariantMap implementation of this database object.
QDataStream & fromStream(QDataStream &stream) override
fromStream This method should be read all bytes from the stream object and full the current object.
virtual void clear()
clear This method clear all data of database object. Override This method for remove or reset your ow...
virtual QString primaryKey() const
primaryKey This method must be return the name of primary key of this object table....
QString toString() const override
virtual PrepareResult prepareSelectQuery(QSqlQuery &q) const
prepareSelectQuery This method should be prepare a query for selected data. Override this method for ...
virtual PrepareResult prepareUpdateQuery(QSqlQuery &q) const
prepareUpdateQuery this method should be prepare a insert data query.
virtual PrepareResult prepareInsertQuery(QSqlQuery &q, bool replace) const
prepareInsertQuery This method should be prepare a query for insert object into database....
virtual bool isCached() const
isCached This method sholud be return status of object. If this method return true then this object c...
virtual bool isBundle() const
isBundle This method definef determines whether the object will be abstract (composite objects) or si...
bool isValid() const override
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 wrapper of DBAddress::id. If This object do not contains a id value then ...
The PKG namespace - this namespace contains all default packages of the Heart library....
PrepareResult
The PrepareResult enum is result of work prepare sql query of dbobjects.
@ Success
prepare finished successful.
QMap< QString, DBVariant > DBVariantMap
DBVariantMap this is Map with key, and value with data type.
MemberType
The MemberType enum. This enum contains types of members DBObjects classes. for more information see ...
@ Update
The Field With This type can be updated but not inserted.
@ PrimaryKey
The primary key field without autoincrement.
@ Autoincement
The field with this attribute automatically incremented into database, and will not to added into ins...
@ Insert
The Field With This type can be inserted but not updated.
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...