mirror of
https://github.com/QuasarApp/Heart.git
synced 2025-05-13 01:49:41 +00:00
dev 10
This commit is contained in:
parent
0432d87179
commit
6b1d33edac
ClientProtocol
@ -58,6 +58,14 @@ QDataStream &AbstractData::toStream(QDataStream &stream) const {
|
||||
return stream;
|
||||
}
|
||||
|
||||
QVariantMap &AbstractData::fromVariantMap(QVariantMap &) {
|
||||
|
||||
}
|
||||
|
||||
QVariantMap &AbstractData::toVariantmap(QVariantMap &) const {
|
||||
|
||||
}
|
||||
|
||||
bool AbstractData::isValid() const {
|
||||
return _cmd;
|
||||
}
|
||||
|
@ -79,6 +79,20 @@ public:
|
||||
*/
|
||||
QDataStream& toStream(QDataStream& stream) const override;
|
||||
|
||||
/**
|
||||
* @brief fromVariantMap
|
||||
* @param map
|
||||
* @return
|
||||
*/
|
||||
QVariantMap &fromVariantMap(QVariantMap &) override;
|
||||
|
||||
/**
|
||||
* @brief toVariantmap
|
||||
* @param map
|
||||
* @return
|
||||
*/
|
||||
QVariantMap &toVariantmap(QVariantMap &) const override;
|
||||
|
||||
/**
|
||||
* @brief isValid
|
||||
* @return true if class isValid
|
||||
@ -87,7 +101,6 @@ public:
|
||||
|
||||
virtual ~AbstractData() override;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <QDateTime>
|
||||
#include <QSqlQuery>
|
||||
#include <QHash>
|
||||
#include <QSqlRecord>
|
||||
|
||||
namespace ClientProtocol {
|
||||
|
||||
@ -22,20 +23,20 @@ bool DBObject::getSaveQueryString(QSqlQuery *query) const {
|
||||
|
||||
bool DBObject::getSelectQueryString(QSqlQuery *query) const {
|
||||
|
||||
if (_id < 0) {
|
||||
if (getId() < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QString queryString = "SELECT (%1) from %0 where id=" + QString::number(_id);
|
||||
QString queryString = "SELECT (%1) from %0 where id=" + QString::number(getId());
|
||||
return getBaseQueryString(queryString, query);
|
||||
}
|
||||
|
||||
bool DBObject::getDeleteQueryString(QSqlQuery *query) const {
|
||||
if (_id < 0) {
|
||||
if (getId() < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QString queryString = "DELETE FROM %0 where id=" + QString::number(_id);
|
||||
QString queryString = "DELETE FROM %0 where id=" + QString::number(getId());
|
||||
return getBaseQueryString(queryString, query);
|
||||
}
|
||||
|
||||
@ -129,13 +130,26 @@ bool DBObject::getBaseQueryString(QString queryString, QSqlQuery *query) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
DbTableBase DBObject::getTableStruct() const
|
||||
{
|
||||
return _tableStruct;
|
||||
}
|
||||
|
||||
void DBObject::setTableStruct(const DbTableBase &tableStruct)
|
||||
{
|
||||
_tableStruct = tableStruct;
|
||||
}
|
||||
|
||||
QDataStream &DBObject::fromStream(QDataStream &stream) {
|
||||
AbstractData::fromStream(stream);
|
||||
|
||||
stream >> _dataTable;
|
||||
stream >> _tableStruct.name;
|
||||
return stream;
|
||||
}
|
||||
|
||||
QDataStream &DBObject::toStream(QDataStream &stream) const {
|
||||
AbstractData::toStream(stream);
|
||||
|
||||
stream << _dataTable;
|
||||
stream << _tableStruct.name;
|
||||
@ -152,12 +166,42 @@ QVariantMap &DBObject::toVariantmap(QVariantMap &map) const {
|
||||
return map;
|
||||
}
|
||||
|
||||
bool DBObject::fromQuery(QSqlQuery *query) {
|
||||
if (!query->exec()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!query->next()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlRecord record = query->record();
|
||||
QVariantMap initMap;
|
||||
|
||||
for (int i = 0; i < query->size(); ++i ) {
|
||||
initMap[record.fieldName(i)] = record.value(i);
|
||||
}
|
||||
|
||||
fromVariantMap(initMap);
|
||||
|
||||
return isValid();
|
||||
}
|
||||
|
||||
bool DBObject::isValid() const {
|
||||
return AbstractData::isValid() && _tableStruct.isValid();
|
||||
return AbstractData::isValid() && _tableStruct.isValid() &&
|
||||
_dataTable.contains("id");
|
||||
}
|
||||
|
||||
int DBObject::getId() const {
|
||||
return _id;
|
||||
return _dataTable.value("id").toInt();
|
||||
}
|
||||
|
||||
void DBObject::setId(int id) {
|
||||
_dataTable["id"] = id;
|
||||
}
|
||||
|
||||
void DBObject::clear() {
|
||||
_dataTable.clear();
|
||||
}
|
||||
|
||||
|
||||
|
@ -49,6 +49,20 @@ public:
|
||||
*/
|
||||
int getId() const;
|
||||
|
||||
/**
|
||||
* @brief getId
|
||||
* @return id of objcet
|
||||
*/
|
||||
void setId(int) const;
|
||||
|
||||
/**
|
||||
* @brief clear
|
||||
*/
|
||||
virtual void clear();
|
||||
|
||||
DbTableBase getTableStruct() const;
|
||||
void setTableStruct(const DbTableBase &tableStruct);
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
@ -76,7 +90,6 @@ protected:
|
||||
|
||||
QVariantMap _dataTable;
|
||||
DbTableBase _tableStruct;
|
||||
int _id = -1;
|
||||
|
||||
|
||||
//// StreamBase interface
|
||||
@ -85,6 +98,13 @@ protected:
|
||||
QVariantMap &fromVariantMap(QVariantMap &map) override;
|
||||
QVariantMap &toVariantmap(QVariantMap &map) const override;
|
||||
|
||||
/**
|
||||
* @brief fromQuery
|
||||
* @param query
|
||||
* @return
|
||||
*/
|
||||
virtual bool fromQuery(QSqlQuery *query);
|
||||
|
||||
//// AbstractData interface
|
||||
bool isValid() const override;
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include <QJsonObject>
|
||||
#include <QHash>
|
||||
#include <dbobject.h>
|
||||
#include <QSqlRecord>
|
||||
|
||||
namespace ClientProtocol {
|
||||
|
||||
@ -250,19 +251,19 @@ bool SqlDBWriter::isValid() const {
|
||||
}
|
||||
|
||||
bool SqlDBWriter::getObject(DBObject *result, const QString& table, int id) const {
|
||||
|
||||
if (!_dbStruct.contains(table)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery q(db);
|
||||
result->getSelectQueryString(&q);
|
||||
if (!q.exec()) {
|
||||
return false;
|
||||
}
|
||||
result->clear();
|
||||
|
||||
if (!q.next()) {
|
||||
return false;
|
||||
}
|
||||
result->setTableStruct(_dbStruct.value(table));
|
||||
result->setId(id);
|
||||
|
||||
q.
|
||||
return result->getSelectQueryString(&q);
|
||||
|
||||
q.value(0).
|
||||
}
|
||||
|
||||
bool SqlDBWriter::saveObject(DBObject *saveObject) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user