99#include < QDir>
1010#include < QElapsedTimer>
1111#include < QLoggingCategory>
12+ #include < QMutexLocker>
1213#include < QSqlDatabase>
1314#include < QSqlError>
1415#include < QSqlQuery>
@@ -173,6 +174,7 @@ qint64 DBAccessor::addEntity(const NotifyEntity &entity)
173174{
174175 BENCHMARK ();
175176
177+ QMutexLocker locker (&m_mutex);
176178 QSqlQuery query (m_connection);
177179
178180 QString columns = QStringList{
@@ -224,6 +226,7 @@ void DBAccessor::updateEntityProcessedType(qint64 id, int processedType)
224226{
225227 BENCHMARK ();
226228
229+ QMutexLocker locker (&m_mutex);
227230 QSqlQuery query (m_connection);
228231
229232 QString cmd = QString (" UPDATE %1 SET ProcessedType = :processed WHERE ID = :id" ).arg (TableName_v2);
@@ -240,6 +243,7 @@ NotifyEntity DBAccessor::fetchEntity(qint64 id)
240243{
241244 BENCHMARK ();
242245
246+ QMutexLocker locker (&m_mutex);
243247 QSqlQuery query (m_connection);
244248 QString cmd = QString (" SELECT %1 FROM notifications2 WHERE ID = :id" ).arg (EntityFields.join (" ," ));
245249 query.prepare (cmd);
@@ -260,6 +264,7 @@ int DBAccessor::fetchEntityCount(const QString &appName, int processedType) cons
260264{
261265 BENCHMARK ();
262266
267+ QMutexLocker locker (&m_mutex);
263268 QSqlQuery query (m_connection);
264269 if (appName == DataAccessor::AllApp ()) {
265270 QString cmd = QString (" SELECT COUNT(*) FROM notifications2 WHERE (ProcessedType = :processedType OR ProcessedType IS NULL)" );
@@ -287,6 +292,7 @@ NotifyEntity DBAccessor::fetchLastEntity(const QString &appName, int processedTy
287292{
288293 BENCHMARK ();
289294
295+ QMutexLocker locker (&m_mutex);
290296 QSqlQuery query (m_connection);
291297 QString cmd =
292298 QString (
@@ -314,6 +320,7 @@ QList<NotifyEntity> DBAccessor::fetchEntities(const QString &appName, int proces
314320{
315321 BENCHMARK ();
316322
323+ QMutexLocker locker (&m_mutex);
317324 QSqlQuery query (m_connection);
318325 if (appName == DataAccessor::AllApp ()) {
319326 if (maxCount >= 0 ) {
@@ -360,6 +367,7 @@ NotifyEntity DBAccessor::fetchLastEntity(uint notifyId)
360367{
361368 BENCHMARK ();
362369
370+ QMutexLocker locker (&m_mutex);
363371 QSqlQuery query (m_connection);
364372 QString cmd = QString (" SELECT %1 FROM notifications2 WHERE notifyId = :notifyId ORDER BY CTime DESC LIMIT 1" ).arg (EntityFields.join (" ," ));
365373 query.prepare (cmd);
@@ -383,6 +391,7 @@ QList<QString> DBAccessor::fetchApps(int maxCount) const
383391{
384392 BENCHMARK ();
385393
394+ QMutexLocker locker (&m_mutex);
386395 QSqlQuery query (m_connection);
387396 if (maxCount >= 0 ) {
388397 QString cmd (" SELECT DISTINCT AppName FROM notifications2 ORDER BY CTime DESC LIMIT :limit" );
@@ -413,6 +422,7 @@ void DBAccessor::removeEntity(qint64 id)
413422{
414423 BENCHMARK ();
415424
425+ QMutexLocker locker (&m_mutex);
416426 QSqlQuery query (m_connection);
417427
418428 QString cmd (" DELETE FROM notifications2 WHERE ID = :id" );
@@ -431,6 +441,7 @@ void DBAccessor::removeEntityByApp(const QString &appName)
431441{
432442 BENCHMARK ();
433443
444+ QMutexLocker locker (&m_mutex);
434445 QSqlQuery query (m_connection);
435446
436447 QString cmd (" DELETE FROM notifications2 WHERE AppName = :appName" );
@@ -449,6 +460,7 @@ void DBAccessor::clear()
449460{
450461 BENCHMARK ();
451462
463+ QMutexLocker locker (&m_mutex);
452464 QSqlQuery query (m_connection);
453465
454466 QString cmd (" DELETE FROM notifications2" );
0 commit comments