Heart/ClientProtocol/sqldbcache.h

91 lines
1.8 KiB
C
Raw Normal View History

2019-10-22 17:59:56 +03:00
#ifndef SQLDBCASHE_H
#define SQLDBCASHE_H
2019-10-27 12:08:16 +03:00
#include "iobjectprovider.h"
2019-10-22 17:59:56 +03:00
#include <QMap>
#include <QHash>
#include <QSet>
#include <QVariantMap>
#include <clientprotocol.h>
2019-10-23 20:32:13 +03:00
namespace ClientProtocol {
class SqlDBWriter;
class DBObject;
2019-10-22 17:59:56 +03:00
enum class SqlDBCasheWriteMode: int {
Default = 0x0,
On_New_Thread = 0x1,
Force = 0x2,
} ;
2019-10-24 18:04:52 +03:00
/**
* @brief The SqlDBCache class it is db cache and bridge for DbWriters
*/
2019-10-27 12:08:16 +03:00
class CLIENTPROTOCOLSHARED_EXPORT SqlDBCache: public QObject, public iObjectProvider
2019-10-22 17:59:56 +03:00
{
Q_OBJECT
public:
SqlDBCache(qint64 updateInterval = DEFAULT_UPDATE_INTERVAL);
~SqlDBCache() override;
2019-10-24 18:04:52 +03:00
/**
* @brief addDbObject
* @param obj
* @return
*/
bool addDbObject(const DBObject* obj);
/**
* @brief removeDbObject
* @param obj
* @return
*/
bool removeDbObject(const DBObject* obj);
/**
* @brief writer
* @return
*/
2019-10-23 20:32:13 +03:00
SqlDBWriter *writer() const;
2019-10-24 18:04:52 +03:00
/**
* @brief setWriter
* @param writer
*/
2019-10-23 20:32:13 +03:00
void setWriter(SqlDBWriter *writer);
2019-10-22 17:59:56 +03:00
2019-10-27 12:08:16 +03:00
bool getObject(DBObject *result, const QString &table, int id) const override;
bool saveObject(DBObject *saveObject) override;
bool deleteObject(const QString &table, int id) override;
2019-10-23 20:32:13 +03:00
protected:
2019-10-24 18:04:52 +03:00
/**
* @brief init
* @return
*/\
2019-10-26 20:53:47 +03:00
virtual bool init(const QString &initDbParams = "");
2019-10-22 17:59:56 +03:00
2019-10-23 20:32:13 +03:00
private:
qint64 lastUpdateTime = 0;
qint64 updateInterval = DEFAULT_UPDATE_INTERVAL;
2019-10-22 17:59:56 +03:00
2019-10-23 20:32:13 +03:00
SqlDBWriter *_writer = nullptr;
2019-10-22 17:59:56 +03:00
2019-10-24 18:04:52 +03:00
QHash<QString, QHash <int, QSharedPointer<DBObject>>> _cache;
2019-10-22 17:59:56 +03:00
2019-10-23 20:32:13 +03:00
void globalUpdateDataBasePrivate(qint64 currentTime);
void globalUpdateDataBase(SqlDBCasheWriteMode mode = SqlDBCasheWriteMode::Default);
2019-10-22 17:59:56 +03:00
signals:
2019-10-24 18:04:52 +03:00
void sigItemChanged(int id, QWeakPointer<DBObject> obj);
2019-10-22 17:59:56 +03:00
};
2019-10-23 20:32:13 +03:00
}
2019-10-22 17:59:56 +03:00
#endif // SQLDBCASHE_H