15#include <QSharedPointer>
17#include <qaglobalutils.h>
33 QString queryString =
"SELECT " + map.join(
",") +
" FROM %0 ";
34 queryString = queryString.arg(
table());
37 auto [conditionQueryString, conditionBindingMap] =
condition();
39 if (conditionQueryString.size()) {
41 queryString +=
" WHERE " + conditionQueryString;
42 if (!q.prepare(queryString)) {
46 for (
auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) {
47 q.bindValue(it.key(), it.value());
54 if (!q.prepare(queryString)) {
66 qCritical() <<
"The variantMap method return an empty map.";
71 QString queryString = (replace)?
72 "REPLACE INTO %0(%1) VALUES (%2) " :
73 "INSERT INTO %0(%1) VALUES (%2) ";
76 queryString = queryString.arg(
table());
77 QString tableInsertHeader =
"";
78 QString tableInsertValues =
"";
80 for (
auto it = map.begin(); it != map.end(); ++it) {
90 tableInsertHeader += it.key();
91 tableInsertValues +=
":" + it.key();
92 if (std::next(it, 1) != map.end()) {
93 tableInsertHeader +=
", ";
94 tableInsertValues +=
", ";
99 queryString = queryString.arg(tableInsertHeader);
100 queryString = queryString.arg(tableInsertValues);
102 if (q.prepare(queryString)) {
104 for (
auto it = map.begin(); it != map.end(); ++it) {
113 q.bindValue(
":" + it.key(), it.value().value);
127 qCritical() <<
"The variantMap method return an empty map.";
132 auto [conditionQueryString, conditionBindingMap] =
condition();
134 if (conditionQueryString.isEmpty()) {
135 qCritical() <<
"The object soue not have condition for update object.";
139 QString queryString =
"UPDATE %0 SET %1 WHERE " + conditionQueryString;
141 queryString = queryString.arg(
table());
142 QString tableUpdateValues =
"";
144 for (
auto it = map.begin(); it != map.end(); ++it) {
149 if (tableUpdateValues.size()) {
150 tableUpdateValues +=
", ";
153 tableUpdateValues += QString(
"%0= :%0").arg(it.key());
157 if (tableUpdateValues.isEmpty()) {
158 qCritical() <<
"Fail to generate condition for object: " +
toString() +
159 ". The object do not have valid update fields.";
163 queryString = queryString.arg(tableUpdateValues);
165 if (q.prepare(queryString)) {
167 for (
auto it = map.begin(); it != map.end(); ++it) {
172 q.bindValue(
":" + it.key(), it.value().value);
175 for (
auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) {
178 qWarning() <<
"Bad object configuration: "
179 "The " << it.key() <<
" field using in the condition and has MemberType::Update configuration."
180 " All condition fields should not use the MemberType::Update. \n" <<
toString();
183 q.bindValue(it.key(), it.value());
210 if (!primaryVal.isNull()) {
211 return {QString(
"%0 = :%0").arg(
primaryKey()),
212 {{QString(
":%0").arg(
primaryKey()), {primaryVal}}}};
215 qCritical() <<
"Fail to generate condition for object: " +
toString() +
216 ". Object do not have valid unique fields or valid database address.";
245 qWarning() <<
"This object not support stream operator."
246 " Please Override the fromStream method for this object. " +
toString();
253 qWarning() <<
"This object not support stream operator."
254 " Please Override the toStream method for this object. " +
toString();
260 QString queryString =
"DELETE FROM %0 ";
261 queryString = queryString.arg(
table());
263 auto [conditionQueryString, conditionBindingMap] =
condition();
265 if (conditionQueryString.size()) {
267 queryString +=
" WHERE " + conditionQueryString;
268 if (!q.prepare(queryString)) {
272 for (
auto it = conditionBindingMap.begin(); it != conditionBindingMap.end(); ++it) {
273 q.bindValue(it.key(), it.value());
279 if (!q.prepare(queryString)) {
302 return table().size();
The DbAddress class use to work with database addresses. Database Address it is structure with 2 valu...
virtual bool isValid() const
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
isValid This method check current object to valid.
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...
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 atribute automaticaly incemented into database, and will not to added into insert...
@ 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...