15#include <QtConcurrent/QtConcurrent>
16#include <qaglobalutils.h>
23 qint64 currentTime = QDateTime::currentMSecsSinceEpoch();
25 if (currentTime - lastUpdateTime > updateInterval ||
30#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
31 QtConcurrent::run([currentTime,
this]() {
36 auto future = QtConcurrent::run([currentTime,
this]() {
40 if (!future.isValid()) {
41 qDebug() <<
"Failde to start update database thread";
50bool ISqlDB::updateObjectP(
const QSharedPointer<DBObject> &saveObject,
56 return _writer && _writer->
isValid() &&
66 return _writer && _writer->
isValid() &&
70bool ISqlDB::deleteObjectP(
const QSharedPointer<DBObject> &delObj,
76 if (_writer && _writer->
isValid()) {
83bool ISqlDB::insertObjectP(
const QSharedPointer<DBObject> &saveObject,
85 const QWeakPointer<unsigned int>& autoincrementIdResult) {
91 return _writer && _writer->
isValid() &&
92 _writer->
insertObject(saveObject, wait, autoincrementIdResult);
101 return _writer && _writer->
isValid() &&
102 _writer->
insertObject(saveObject, wait, autoincrementIdResult);
105bool ISqlDB::replaceObjectP(
const QSharedPointer<PKG::DBObject> &saveObject,
bool wait) {
110 return _writer && _writer->
isValid() &&
120 return _writer && _writer->
isValid() &&
125 return lastUpdateTime;
129 lastUpdateTime = value;
134 _saveLaterMutex.lock();
135 _changes.insert(type, obj);
136 _saveLaterMutex.unlock();
140 lastUpdateTime = QDateTime::currentMSecsSinceEpoch();
141 this->updateInterval = updateInterval;
159 QList<QSharedPointer<QH::PKG::DBObject>> &result) {
166 if (_writer && _writer->
isValid()) {
171 for (
const auto &
object: std::as_const(result)) {
173 qWarning() <<
"Selected object from database can not be saved into database cache. " +
object->toString();
189 auto id = delObj->dbAddress();
191 if (!deleteObjectP(delObj, wait)) {
205 if (!saveObject || !saveObject->isValid()) {
209 if (!updateObjectP(saveObject, wait)) {
219 const QWeakPointer<unsigned int>& autoincrementIdResult) {
220 if (!saveObject || !saveObject->isValid()) {
224 if (!insertObjectP(saveObject, wait, autoincrementIdResult)) {
234 if (!saveObject || !saveObject->isValid()) {
238 if (!replaceObjectP(saveObject, wait)) {
248 bool wait, QSqlQuery *result)
const {
254 return _writer->
doQuery(query, toBind, wait, result);
262 return _writer->
doSql(sqlFile, wait);
271 return _writer->
initDb(initDbParams);
280 return _writer->
initDb(params);
286 db->setSQLSources(list);
294 const std::function<
bool (
const QSharedPointer<QH::PKG::DBObject>&)> &changeAction) {
296 QList<QSharedPointer<DBObject>> list;
304 for (
const auto& obj :std::as_const(list)) {
306 if (!changeAction(obj)) {
327 QMutexLocker lock(&_saveLaterMutex);
329 for (
auto it = _changes.begin(); it != _changes.end(); ++it) {
333 auto obj = it.value();
335 if (!obj || !obj->isValid()) {
338 qCritical() <<
"writeUpdateItemIntoDB failed when db object is not valid! obj=" << obj->toString();
342 bool saveResult =
false;
358 qWarning() <<
"The Object of the cache have wrong type " << obj->toString();
365 qCritical() <<
"writeUpdateItemIntoDB failed when work globalUpdateDataRelease!!! obj=" << obj->toString();
369 qCritical() <<
"writeUpdateItemIntoDB failed when db writer is not inited!";
379 return updateInterval;
383 updateInterval = value;
SqlDBCasheWriteMode getMode() const
getMode This method return mode of work database cache. For more information see the QH::SqlDBCasheWr...
void setWriter(SqlDBWriter *writer)
setWriter This method set new writer for this cache.
void prepareForDelete() override
prepareForDelete This method must be prepare object for delete. Override this for working main functi...
SqlDBWriter * writer() const
writer This method return is database writer object. For more inforamation about writer see the SqlDB...
bool insertObject(const QSharedPointer< QH::PKG::DBObject > &saveObject, bool wait=false, const QWeakPointer< unsigned int > &autoincrementIdResult={}) override
virtual QList< QSharedPointer< QH::PKG::DBObject > > getFromCache(const PKG::DBObject *obj)=0
getFromCache This method return strong pointer to the database object from cache (pool).
bool doQuery(const QString &query, const QVariantMap &bindValues, bool wait=false, QSqlQuery *result=nullptr) const override
doQuery This method execute a query in this database.
virtual bool init(const QString &initDbParams="")
init This method init the cache object and invoke the SqlDBWriter::initDb method.
bool getAllObjects(const PKG::DBObject &templateObject, QList< QSharedPointer< QH::PKG::DBObject > > &result) override
virtual void globalUpdateDataBasePrivate(qint64 currentTime)
globalUpdateDataBasePrivate This method update(write) all data from cache into database....
virtual void pushToQueue(const QSharedPointer< QH::PKG::DBObject > &obj, CacheAction type)
pushToQueue this method should be add the object to the update queue in the physical data dash.
bool doSql(const QString &sqlFile, bool wait) const override
doSql This method execute a query in this database.
void setLastUpdateTime(const qint64 &value)
setLastUpdateTime This method set new value of the update time.
bool deleteObject(const QSharedPointer< QH::PKG::DBObject > &delObj, bool wait=false) override
virtual void globalUpdateDataBase(SqlDBCasheWriteMode mode=SqlDBCasheWriteMode::Default)
globalUpdateDataBase This is base method for syncing data from the cache with database.
void setUpdateInterval(const qint64 &value)
getUpdateInterval This method set new value of an update interval for save changes into database....
void sigItemChanged(const QSharedPointer< QH::PKG::DBObject > &obj)
sigItemChanged This signal emitted when database object is changed.
virtual void deleteFromCache(const QSharedPointer< QH::PKG::DBObject > &delObj)=0
deleteFromCache This method delete object from db cache, bat not from database.
void sigItemDeleted(const QH::DbAddress &obj)
sigItemDeleted This signal emitted when database object is deleted.
bool changeObjects(const PKG::DBObject &templateObject, const std::function< bool(const QSharedPointer< PKG::DBObject > &)> &changeAction)
changeObjects This method change object of the database.
virtual bool updateCache(const QSharedPointer< QH::PKG::DBObject > &obj)=0
updateCache This method update already exits object on the cache, but not database.
ISqlDB(qint64 updateInterval=DEFAULT_UPDATE_INTERVAL, SqlDBCasheWriteMode mode=SqlDBCasheWriteMode::Default)
ISqlDB This is default constructor of dbcache.
void setSQLSources(const QStringList &list) override
setSQLSources This method set sql sources for deployed database.
virtual bool insertToCache(const QSharedPointer< QH::PKG::DBObject > &obj)=0
insertToCache This method insert object into cache, but not database. If Object exists in the cache t...
void setMode(const SqlDBCasheWriteMode &mode)
setMode This method set a new value of the mode database cache.
bool updateObject(const QSharedPointer< QH::PKG::DBObject > &saveObject, bool wait=false) override
bool replaceObject(const QSharedPointer< QH::PKG::DBObject > &saveObject, bool wait=false) override
qint64 getUpdateInterval() const
getUpdateInterval This method return update interval for save changes into database....
qint64 getLastUpdateTime() const
getLastUpdateTime This method return time of last update.
The DBObject class- main class for work with data base.
The SqlDBWriter class. This class write and read objects from database (hard level)....
bool deleteObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false) override
deleteObject This method execute a delete method of obj and remove current object from database.
bool updateObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false) override
updateObject This method execute a update method of the saveObject and save all changes into database...
bool doSql(const QString &sqlFile, bool wait) const override
doSql This method execute a query in this database.
bool doQuery(const QString &query, const QVariantMap &bindValues={}, bool wait=false, QSqlQuery *result=nullptr) const override
doQuery This method execute a query in this database.
bool replaceObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false) override
replaceObject This method execute a replace method of the saveObject and insert or save if not exists...
virtual bool isValid() const
isValid This method return true if database is successful inited and working correctly.
virtual bool initDb(const QString &initDbParams=DEFAULT_DB_PATH)
initDb This method is wraper of the initDb(const QVariantMap ¶ms) method. This implementation rea...
bool getAllObjects(const PKG::DBObject &templateObject, QList< QSharedPointer< PKG::DBObject > > &result) override
getAllObjects This method execute a select method of the templateObject and return list of all select...
bool insertObject(const QSharedPointer< PKG::DBObject > &ptr, bool wait=false, const QWeakPointer< unsigned int > &autoincrementIdResult={}) override
insertObject This method execute a insert method of the saveObject and insert it into database.
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
SqlDBCasheWriteMode
The SqlDBCasheWriteMode enum contains list of available modes of write data into database.
@ Force
This mode writes all changes to the database as soon as they come to the cache.
@ On_New_Thread
This mode write a changes into another thread.
CacheAction
The CacheAction enum contains types of database cache actions. The any database caches save all chang...
@ Update
Invoke the SqlDBWriter::updateObject method of a private database writer implementation.
@ Insert
Invoke the SqlDBWriter::insertObject method of a private database writer implementation.
@ Delete
Invoke the SqlDBWriter::deleteObject method of a private database writer implementation.