4
1
mirror of https://github.com/QuasarApp/Heart.git synced 2025-05-06 22:49:41 +00:00

added dbobjectrequest without stream support

This commit is contained in:
Andrei Yankovich 2022-01-31 21:23:38 +03:00
parent 546802c985
commit 3fcf9d3081
4 changed files with 107 additions and 31 deletions

@ -21,6 +21,12 @@ class QSqlQuery;
namespace QH {
namespace PKG {
/**
* The ONLY_DATABASE_PACKAGE macross is base macros for all database objects that do not use stream and network functions.
*/
#define ONLY_DATABASE_PACKAGE QH_PACKAGE(DBObject, "DBObject")
/**
* @brief The PrepareResult enum is result of work prepare sql query of dbobjects.
*/

@ -103,36 +103,6 @@ public:
protected:
QDataStream &fromStream(QDataStream &stream) override {
DBObjectSet::fromStream(stream);
clear();
int size = 0;
stream >> size;
for (int i = 0; i < size; ++i) {
auto ptr = QSharedPointer<T>::create();
stream >> *ptr;
_data.push_back(ptr);
}
return stream;
};
QDataStream &toStream(QDataStream &stream) const override {
DBObjectSet::toStream(stream);
stream << static_cast<int>(_data.size());
for (auto object: _data) {
stream << *object;
}
return stream;
};
QString condition() const override {
return _conditions;
}
@ -141,9 +111,11 @@ protected:
return create<DBObjectsRequest<T>>(tableName(), _conditions);
};
QList<QSharedPointer<T>> _data;
private:
QString _conditions;
QList<QSharedPointer<T>> _data;
};

@ -0,0 +1,15 @@
/*
* Copyright (C) 2018-2021 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "dbobjectsrequest.h"
namespace QH {
namespace PKG {
}
}

@ -0,0 +1,83 @@
/*
* Copyright (C) 2018-2021 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#ifndef DBOBJECTSREQUESTWITHSTREAM_H
#define DBOBJECTSREQUESTWITHSTREAM_H
#include "dbobjectsrequest.h"
#include <QDataStream>
namespace QH {
namespace PKG {
/**
* @brief The DBObjectsRequestWithStream class is template class some as DBObjectsRequest but with implementation for the StreamBase::fromStream and StreamBase::toStream methods.
*
* @see DBObjectsRequest
* @see DBObjectSet
*/
template <class T>
class DBObjectsRequestWithStream final: public DBObjectsRequest<T>
{
public:
/**
* @brief DBObjectsRequest This is default constructor for parsing packages.
* @param pkkg This is package.
*/
DBObjectsRequestWithStream(const Package& pkkg):DBObjectsRequest<T>(pkkg) {}
/**
* @brief 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 @a conditions argument or set it to empty string value.
* @param table This is name of database table.
* @param conditions This is string with conditions for create sql query. If you want to get all elemts just ignore this argument.
* @see DBObjectsRequest::setConditions
*/
DBObjectsRequestWithStream(const QString& table,
const QString& conditions = "" ):
DBObjectsRequest<T> (table, conditions) { };
protected:
QDataStream &fromStream(QDataStream &stream) override {
DBObjectSet::fromStream(stream);
clear();
int size = 0;
stream >> size;
for (int i = 0; i < size; ++i) {
auto ptr = QSharedPointer<T>::create();
stream >> *ptr;
_data.push_back(ptr);
}
return stream;
};
QDataStream &toStream(QDataStream &stream) const override {
DBObjectSet::toStream(stream);
stream << static_cast<int>(_data.size());
for (auto object: _data) {
stream << *object;
}
return stream;
};
};
}
}
#endif // DBOBJECTSREQUESTWITHSTREAM_H