mirror of
https://github.com/QuasarApp/Heart.git
synced 2025-05-12 17:39:42 +00:00
dev 10
This commit is contained in:
parent
6b1d33edac
commit
f4e655d2be
@ -52,6 +52,7 @@ SOURCES += \
|
||||
clientprotocol.cpp \
|
||||
dbtablebase.cpp \
|
||||
header.cpp \
|
||||
iobjectprovider.cpp \
|
||||
package.cpp \
|
||||
sqldbcache.cpp \
|
||||
sqldbwriter.cpp \
|
||||
@ -72,6 +73,7 @@ HEADERS += \
|
||||
config.h \
|
||||
dbtablebase.h \
|
||||
header.h \
|
||||
iobjectprovider.h \
|
||||
package.h \
|
||||
sqldbcache.h \
|
||||
sqldbwriter.h \
|
||||
|
@ -15,29 +15,56 @@ DBObject::DBObject(const QString &tableName) {
|
||||
// 0 - table name
|
||||
// 1 - headers of update values
|
||||
// 2 - update values
|
||||
bool DBObject::getSaveQueryString(QSqlQuery *query) const {
|
||||
bool DBObject::saveQuery(QSqlQuery *query) const {
|
||||
|
||||
QString queryString = "INSERT IGNORE INTO %0(%1) VALUES (%2)";
|
||||
return getBaseQueryString(queryString, query);
|
||||
if (!getBaseQueryString(queryString, query)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return exec(query);
|
||||
}
|
||||
|
||||
bool DBObject::getSelectQueryString(QSqlQuery *query) const {
|
||||
bool DBObject::selectQuery(QSqlQuery *query) {
|
||||
|
||||
if (getId() < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QString queryString = "SELECT (%1) from %0 where id=" + QString::number(getId());
|
||||
return getBaseQueryString(queryString, query);
|
||||
|
||||
if (!getBaseQueryString(queryString, query)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! exec(query)) {
|
||||
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::getDeleteQueryString(QSqlQuery *query) const {
|
||||
bool DBObject::deleteQuery(QSqlQuery *query) const {
|
||||
if (getId() < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QString queryString = "DELETE FROM %0 where id=" + QString::number(getId());
|
||||
return getBaseQueryString(queryString, query);
|
||||
|
||||
if (!getBaseQueryString(queryString, query)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return exec(query);
|
||||
}
|
||||
|
||||
bool DBObject::generateHeaderOfQuery(QString & retQuery) const {
|
||||
@ -130,13 +157,11 @@ bool DBObject::getBaseQueryString(QString queryString, QSqlQuery *query) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
DbTableBase DBObject::getTableStruct() const
|
||||
{
|
||||
DbTableBase DBObject::getTableStruct() const {
|
||||
return _tableStruct;
|
||||
}
|
||||
|
||||
void DBObject::setTableStruct(const DbTableBase &tableStruct)
|
||||
{
|
||||
void DBObject::setTableStruct(const DbTableBase &tableStruct) {
|
||||
_tableStruct = tableStruct;
|
||||
}
|
||||
|
||||
@ -166,7 +191,7 @@ QVariantMap &DBObject::toVariantmap(QVariantMap &map) const {
|
||||
return map;
|
||||
}
|
||||
|
||||
bool DBObject::fromQuery(QSqlQuery *query) {
|
||||
bool DBObject::exec(QSqlQuery *query) const {
|
||||
if (!query->exec()) {
|
||||
return false;
|
||||
}
|
||||
@ -175,16 +200,7 @@ bool DBObject::fromQuery(QSqlQuery *query) {
|
||||
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();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DBObject::isValid() const {
|
||||
|
@ -23,25 +23,25 @@ public:
|
||||
|
||||
|
||||
/**
|
||||
* @brief getSaveQueryString
|
||||
* @brief saveQuery
|
||||
* @param query update value for sql query
|
||||
* @return true if all good
|
||||
*/
|
||||
virtual bool getSaveQueryString(QSqlQuery *query) const;
|
||||
virtual bool saveQuery(QSqlQuery *query) const;
|
||||
|
||||
/**
|
||||
* @brief getSelectQueryString
|
||||
* @brief selectQuery
|
||||
* @param query update value
|
||||
* @return return true if all good
|
||||
*/
|
||||
virtual bool getSelectQueryString(QSqlQuery *query) const;
|
||||
virtual bool selectQuery(QSqlQuery *query);
|
||||
|
||||
/**
|
||||
* @brief getDeleteQueryString
|
||||
* @param query update value
|
||||
* @brief deleteQuery
|
||||
* @param query delete value
|
||||
* @return true if all good
|
||||
*/
|
||||
virtual bool getDeleteQueryString(QSqlQuery *query) const;
|
||||
virtual bool deleteQuery(QSqlQuery *query) const;
|
||||
|
||||
/**
|
||||
* @brief getId
|
||||
@ -53,14 +53,23 @@ public:
|
||||
* @brief getId
|
||||
* @return id of objcet
|
||||
*/
|
||||
void setId(int) const;
|
||||
void setId(int);
|
||||
|
||||
/**
|
||||
* @brief clear
|
||||
*/
|
||||
virtual void clear();
|
||||
|
||||
/**
|
||||
* @brief getTableStruct
|
||||
* @return
|
||||
*/
|
||||
DbTableBase getTableStruct() const;
|
||||
|
||||
/**
|
||||
* @brief setTableStruct
|
||||
* @param tableStruct
|
||||
*/
|
||||
void setTableStruct(const DbTableBase &tableStruct);
|
||||
|
||||
protected:
|
||||
@ -103,7 +112,7 @@ protected:
|
||||
* @param query
|
||||
* @return
|
||||
*/
|
||||
virtual bool fromQuery(QSqlQuery *query);
|
||||
virtual bool exec(QSqlQuery *query) const;
|
||||
|
||||
//// AbstractData interface
|
||||
bool isValid() const override;
|
||||
|
6
ClientProtocol/iobjectprovider.cpp
Normal file
6
ClientProtocol/iobjectprovider.cpp
Normal file
@ -0,0 +1,6 @@
|
||||
#include "iobjectprovider.h"
|
||||
|
||||
iObjectProvider::iObjectProvider()
|
||||
{
|
||||
|
||||
}
|
36
ClientProtocol/iobjectprovider.h
Normal file
36
ClientProtocol/iobjectprovider.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef IOBJECTPROVIDER_H
|
||||
#define IOBJECTPROVIDER_H
|
||||
#include "clientprotocol_global.h"
|
||||
|
||||
namespace ClientProtocol {
|
||||
|
||||
class DBObject;
|
||||
|
||||
class CLIENTPROTOCOLSHARED_EXPORT iObjectProvider
|
||||
{
|
||||
public:
|
||||
iObjectProvider();
|
||||
virtual ~iObjectProvider();
|
||||
|
||||
/**
|
||||
* @brief getObject
|
||||
* @return
|
||||
*/
|
||||
virtual bool getObject(DBObject *result, const QString &table, int id) const = 0;
|
||||
|
||||
/**
|
||||
* @brief saveObject
|
||||
* @return
|
||||
*/
|
||||
virtual bool saveObject(DBObject *saveObject) = 0;
|
||||
|
||||
/**
|
||||
* @brief deleteObject
|
||||
* @return
|
||||
*/
|
||||
virtual bool deleteObject(const QString &table, int id) = 0;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
#endif // IOBJECTPROVIDER_H
|
@ -77,6 +77,18 @@ void SqlDBCache::setWriter(SqlDBWriter *writer) {
|
||||
_writer = writer;
|
||||
}
|
||||
|
||||
bool SqlDBCache::getObject(DBObject *result, const QString &table, int id) const {
|
||||
|
||||
}
|
||||
|
||||
bool SqlDBCache::saveObject(DBObject *saveObject) {
|
||||
|
||||
}
|
||||
|
||||
bool SqlDBCache::deleteObject(const QString &table, int id) {
|
||||
|
||||
}
|
||||
|
||||
bool SqlDBCache::init(const QString &initDbParams) {
|
||||
|
||||
if (!_writer) {
|
||||
|
@ -1,6 +1,8 @@
|
||||
#ifndef SQLDBCASHE_H
|
||||
#define SQLDBCASHE_H
|
||||
|
||||
#include "iobjectprovider.h"
|
||||
|
||||
#include <QMap>
|
||||
#include <QHash>
|
||||
#include <QSet>
|
||||
@ -21,7 +23,7 @@ enum class SqlDBCasheWriteMode: int {
|
||||
/**
|
||||
* @brief The SqlDBCache class it is db cache and bridge for DbWriters
|
||||
*/
|
||||
class CLIENTPROTOCOLSHARED_EXPORT SqlDBCache: public QObject
|
||||
class CLIENTPROTOCOLSHARED_EXPORT SqlDBCache: public QObject, public iObjectProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@ -57,6 +59,10 @@ public:
|
||||
*/
|
||||
void setWriter(SqlDBWriter *writer);
|
||||
|
||||
bool getObject(DBObject *result, const QString &table, int id) const override;
|
||||
bool saveObject(DBObject *saveObject) override;
|
||||
bool deleteObject(const QString &table, int id) override;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief init
|
||||
|
@ -262,16 +262,29 @@ bool SqlDBWriter::getObject(DBObject *result, const QString& table, int id) cons
|
||||
result->setTableStruct(_dbStruct.value(table));
|
||||
result->setId(id);
|
||||
|
||||
return result->getSelectQueryString(&q);
|
||||
return result->selectQuery(&q);
|
||||
|
||||
}
|
||||
|
||||
bool SqlDBWriter::saveObject(DBObject *saveObject) {
|
||||
|
||||
if (!saveObject) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(db);
|
||||
return saveObject->saveQuery(&query);
|
||||
|
||||
}
|
||||
|
||||
bool SqlDBWriter::deleteObject(const QString& table, int id) {
|
||||
DBObject obj(table);
|
||||
|
||||
obj.setTableStruct(_dbStruct.value(table));
|
||||
obj.setId(id);
|
||||
|
||||
QSqlQuery query(db);
|
||||
return obj.deleteQuery(&query);
|
||||
}
|
||||
|
||||
SqlDBWriter::~SqlDBWriter() {
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include "clientprotocol_global.h"
|
||||
#include "config.h"
|
||||
#include "dbtablebase.h"
|
||||
#include "iobjectprovider.h"
|
||||
#include <QVariant>
|
||||
|
||||
class QSqlQuery;
|
||||
@ -22,7 +23,7 @@ class DBObject;
|
||||
/**
|
||||
* @brief The SqlDBWriter class
|
||||
*/
|
||||
class CLIENTPROTOCOLSHARED_EXPORT SqlDBWriter
|
||||
class CLIENTPROTOCOLSHARED_EXPORT SqlDBWriter : public iObjectProvider
|
||||
{
|
||||
private:
|
||||
|
||||
@ -111,21 +112,21 @@ public:
|
||||
* @brief getObject
|
||||
* @return
|
||||
*/
|
||||
virtual bool getObject(DBObject *result, const QString &table, int id) const;
|
||||
bool getObject(DBObject *result, const QString &table, int id) const override;
|
||||
|
||||
/**
|
||||
* @brief saveObject
|
||||
* @return
|
||||
*/
|
||||
virtual bool saveObject(DBObject *saveObject);
|
||||
bool saveObject(DBObject *saveObject) override;
|
||||
|
||||
/**
|
||||
* @brief deleteObject
|
||||
* @return
|
||||
*/
|
||||
virtual bool deleteObject(const QString &table, int id);
|
||||
bool deleteObject(const QString &table, int id) override;
|
||||
|
||||
virtual ~SqlDBWriter();
|
||||
virtual ~SqlDBWriter() override;
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user