fix(ApplicationInfo):多线程使用数据库接口查询时会出现错误。

This commit is contained in:
JunjieBai 2024-01-04 17:16:45 +08:00 committed by iaom
parent c9d7b956bb
commit dae035d35b
5 changed files with 56 additions and 92 deletions

View File

@ -49,7 +49,7 @@ public:
//数据库错误信息 //数据库错误信息
QString lastError(void) const; QString lastError() const;
//通过pid查找desktop文件 //通过pid查找desktop文件
bool tranPidToDesktopFp(uint pid, QString &desktopfp); bool tranPidToDesktopFp(uint pid, QString &desktopfp);
@ -75,7 +75,7 @@ private:
QString m_ConnectionName; QString m_ConnectionName;
public Q_SLOTS: public Q_SLOTS:
void sendAppDBItemsUpdate(ApplicationInfoMap results); void sendAppDBItemsUpdate(UkuiSearch::ApplicationInfoMap results);
void sendAppDBItemsUpdateAll(QStringList desktopFilePaths); void sendAppDBItemsUpdateAll(QStringList desktopFilePaths);
void sendAppDBItemsAdd(QStringList desktopFilePaths); void sendAppDBItemsAdd(QStringList desktopFilePaths);
void sendAppDBItemsDelete(QStringList desktopFilePaths); void sendAppDBItemsDelete(QStringList desktopFilePaths);

View File

@ -130,7 +130,7 @@ bool AppInfoTablePrivate::searchInstallApp(QString &keyWord, QStringList &instal
bool AppInfoTablePrivate::searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes) bool AppInfoTablePrivate::searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes)
{ {
bool res(true); bool res(true);
if (m_database->transaction() or keyWord.size() != 0) { if (m_database->transaction() or !keyWord.isEmpty()) {
QSqlQuery sql(*m_database); QSqlQuery sql(*m_database);
QString cmd; QString cmd;
if (keyWord.at(0).size() < 2) { if (keyWord.at(0).size() < 2) {
@ -362,19 +362,11 @@ void AppInfoTablePrivate::sendAppDBItemsDelete(QStringList desktopFilePaths)
Q_EMIT q->appDBItems2BDelete(desktopFilePaths); Q_EMIT q->appDBItems2BDelete(desktopFilePaths);
} }
AppInfoTable *AppInfoTable::self()
{
std::call_once(flag, [ & ] {
global_intance = new AppInfoTable();
});
return global_intance;
}
AppInfoTable::AppInfoTable(QObject *parent) : QObject(parent), d(new AppInfoTablePrivate(this)) AppInfoTable::AppInfoTable(QObject *parent) : QObject(parent), d(new AppInfoTablePrivate(this))
{ {
} }
bool AppInfoTable::query(ApplicationPropertyMap &propertyMap, const QString &desktopFile, ApplicationProperties properties) bool AppInfoTable::query(ApplicationPropertyMap &propertyMap, const QString &desktopFile, const ApplicationProperties& properties)
{ {
QString field; QString field;
for(const ApplicationProperty::Property &pro : properties) { for(const ApplicationProperty::Property &pro : properties) {
@ -400,7 +392,7 @@ bool AppInfoTable::query(ApplicationPropertyMap &propertyMap, const QString &des
return true; return true;
} }
bool AppInfoTable::query(ApplicationInfoMap &infoMap, ApplicationProperties properties) bool AppInfoTable::query(ApplicationInfoMap &infoMap, const ApplicationProperties& properties)
{ {
QString field; QString field;
for(const ApplicationProperty::Property &pro : properties) { for(const ApplicationProperty::Property &pro : properties) {
@ -431,7 +423,7 @@ bool AppInfoTable::query(ApplicationInfoMap &infoMap, ApplicationProperties prop
return true; return true;
} }
bool AppInfoTable::query(ApplicationInfoMap &infoMap, ApplicationProperties properties, ApplicationPropertyMap restrictions) bool AppInfoTable::query(ApplicationInfoMap &infoMap, const ApplicationProperties& properties, const ApplicationPropertyMap& restrictions)
{ {
QString field; QString field;
for(const ApplicationProperty::Property &pro : properties) { for(const ApplicationProperty::Property &pro : properties) {

View File

@ -35,14 +35,11 @@ class AppInfoTable : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
static AppInfoTable* self(); explicit AppInfoTable(QObject *parent = nullptr);
AppInfoTable(AppInfoTable &) = delete; bool query(ApplicationPropertyMap &propertyMap, const QString &desktopFile, const ApplicationProperties& properties);
AppInfoTable &operator =(const AppInfoTable &) = delete; bool query(ApplicationInfoMap &infoMap, const ApplicationProperties& properties);
bool query(ApplicationInfoMap &infoMap, const ApplicationProperties& properties, const ApplicationPropertyMap& restrictions);
bool query(ApplicationPropertyMap &propertyMap, const QString &desktopFile, ApplicationProperties properties);
bool query(ApplicationInfoMap &infoMap, ApplicationProperties properties);
bool query(ApplicationInfoMap &infoMap, ApplicationProperties properties, ApplicationPropertyMap restrictions);
bool query(ApplicationInfoMap &infoMap, const ApplicationProperties& properties, const QStringList &keywords, const ApplicationPropertyMap& restrictions); bool query(ApplicationInfoMap &infoMap, const ApplicationProperties& properties, const QStringList &keywords, const ApplicationPropertyMap& restrictions);
/** /**
@ -77,16 +74,13 @@ public:
void setAppLaunchedState(const QString &desktopFilePath, bool launched); void setAppLaunchedState(const QString &desktopFilePath, bool launched);
bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes);
bool searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes);
/** /**
* @brief AppInfoTable::lastError * @brief AppInfoTable::lastError
* the last error of the database * the last error of the database
* @return QString: the text of the last error * @return QString: the text of the last error
*/ */
QString lastError(void) const; QString lastError() const;
/** /**
* @brief AppInfoTable::tranPid2DesktopFp * @brief AppInfoTable::tranPid2DesktopFp
@ -106,9 +100,10 @@ private:
bool setAppLaunchTimes(const QString &desktopfp, size_t num); bool setAppLaunchTimes(const QString &desktopfp, size_t num);
bool setAppLockState(const QString &desktopfp, size_t num); bool setAppLockState(const QString &desktopfp, size_t num);
bool updateAppLaunchTimes(const QString &desktopfp); bool updateAppLaunchTimes(const QString &desktopfp);
bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes);
bool searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes);
private: private:
explicit AppInfoTable(QObject *parent = nullptr);
AppInfoTablePrivate *d; AppInfoTablePrivate *d;
Q_SIGNALS: Q_SIGNALS:

View File

@ -1,113 +1,91 @@
#include "application-info.h" #include "application-info.h"
#include "app-info-table.h" #include "app-info-table.h"
#include "application-property-helper.h"
using namespace UkuiSearch; using namespace UkuiSearch;
namespace UkuiSearch { namespace UkuiSearch {
class ApplicationInfoPrivate class ApplicationInfoPrivate : public QObject
{ {
public:
explicit ApplicationInfoPrivate(ApplicationInfo *parent = nullptr) : QObject(parent), m_infoTable(new AppInfoTable(this)) {}
AppInfoTable *m_infoTable = nullptr;
}; };
} }
ApplicationInfo::ApplicationInfo(QObject *parent)
: QObject(parent), d(new ApplicationInfoPrivate) ApplicationInfo::ApplicationInfo(QObject *parent) : QObject(parent), d(new ApplicationInfoPrivate(this)) {
{ connect(d->m_infoTable, &AppInfoTable::appDBItems2BUpdate, this, &ApplicationInfo::appDBItems2BUpdate);
connect(AppInfoTable::self(), &AppInfoTable::appDBItems2BUpdate, this, &ApplicationInfo::appDBItems2BUpdate); connect(d->m_infoTable, &AppInfoTable::appDBItems2BUpdateAll, this, &ApplicationInfo::appDBItems2BUpdateAll);
connect(AppInfoTable::self(), &AppInfoTable::appDBItems2BUpdateAll, this, &ApplicationInfo::appDBItems2BUpdateAll); connect(d->m_infoTable, &AppInfoTable::appDBItems2BAdd, this, &ApplicationInfo::appDBItems2BAdd);
connect(AppInfoTable::self(), &AppInfoTable::appDBItems2BAdd, this, &ApplicationInfo::appDBItems2BAdd); connect(d->m_infoTable, &AppInfoTable::appDBItems2BDelete, this, &ApplicationInfo::appDBItems2BDelete);
connect(AppInfoTable::self(), &AppInfoTable::appDBItems2BDelete, this, &ApplicationInfo::appDBItems2BDelete);
} }
ApplicationInfo::~ApplicationInfo() QVariant ApplicationInfo::getInfo(const QString &desktopFile, ApplicationProperty::Property property) {
{
if(d) {
delete d;
d = nullptr;
}
}
QVariant UkuiSearch::ApplicationInfo::getInfo(const QString &desktopFile, ApplicationProperty::Property property)
{
ApplicationPropertyMap map; ApplicationPropertyMap map;
AppInfoTable::self()->query(map, desktopFile, ApplicationProperties{property}); d->m_infoTable->query(map, desktopFile, ApplicationProperties{property});
return map.value(property); return map.value(property);
} }
ApplicationPropertyMap ApplicationInfo::getInfo(const QString &desktopFile, ApplicationProperties properties) ApplicationPropertyMap ApplicationInfo::getInfo(const QString& desktopFile, ApplicationProperties properties) {
{
ApplicationPropertyMap propertyMap; ApplicationPropertyMap propertyMap;
AppInfoTable::self()->query(propertyMap, desktopFile, properties); d->m_infoTable->query(propertyMap, desktopFile, properties);
return propertyMap; return propertyMap;
} }
ApplicationInfoMap ApplicationInfo::getInfo(ApplicationProperties properties) ApplicationInfoMap ApplicationInfo::getInfo(ApplicationProperties properties) {
{
ApplicationInfoMap infoMap; ApplicationInfoMap infoMap;
AppInfoTable::self()->query(infoMap, properties); d->m_infoTable->query(infoMap, properties);
return infoMap; return infoMap;
} }
ApplicationInfoMap ApplicationInfo::getInfo(ApplicationProperties properties, ApplicationPropertyMap restrictions) ApplicationInfoMap ApplicationInfo::getInfo(ApplicationProperties properties, ApplicationPropertyMap restrictions) {
{
ApplicationInfoMap infoMap; ApplicationInfoMap infoMap;
AppInfoTable::self()->query(infoMap, properties, restrictions); d->m_infoTable->query(infoMap, properties, restrictions);
return infoMap; return infoMap;
} }
ApplicationInfoMap ApplicationInfo::searchApp(ApplicationProperties properties, const QString &keyword, ApplicationPropertyMap restrictions) {
ApplicationInfoMap ApplicationInfo::searchApp(ApplicationProperties properties, const QString &keyword, ApplicationPropertyMap restrictions)
{
ApplicationInfoMap infoMap; ApplicationInfoMap infoMap;
AppInfoTable::self()->query(infoMap, properties, QStringList{keyword}, restrictions); d->m_infoTable->query(infoMap, properties, QStringList{keyword}, restrictions);
return infoMap; return infoMap;
} }
ApplicationInfoMap ApplicationInfo::searchApp(ApplicationProperties properties, const QStringList &keywords, ApplicationPropertyMap restrictions) ApplicationInfoMap ApplicationInfo::searchApp(ApplicationProperties properties, const QStringList &keywords, ApplicationPropertyMap restrictions) {
{
ApplicationInfoMap infoMap; ApplicationInfoMap infoMap;
AppInfoTable::self()->query(infoMap, properties, keywords, restrictions); d->m_infoTable->query(infoMap, properties, keywords, restrictions);
return infoMap; return infoMap;
} }
void ApplicationInfo::setAppToFavorites(const QString &desktopFilePath) void ApplicationInfo::setAppToFavorites(const QString &desktopFilePath) {
{ return d->m_infoTable->setAppFavoritesState(desktopFilePath);
AppInfoTable::self()->setAppFavoritesState(desktopFilePath);
} }
void ApplicationInfo::setFavoritesOfApp(const QString &desktopFilePath, size_t num) void ApplicationInfo::setFavoritesOfApp(const QString &desktopFilePath, size_t num) {
{ return d->m_infoTable->setAppFavoritesState(desktopFilePath, num);
AppInfoTable::self()->setAppFavoritesState(desktopFilePath, num);
} }
void ApplicationInfo::setAppToTop(const QString &desktopFilePath) void ApplicationInfo::setAppToTop(const QString &desktopFilePath) {
{ return d->m_infoTable->setAppTopState(desktopFilePath);
AppInfoTable::self()->setAppTopState(desktopFilePath);
} }
void ApplicationInfo::setTopOfApp(const QString &desktopFilePath, size_t num) void ApplicationInfo::setTopOfApp(const QString &desktopFilePath, size_t num) {
{ return d->m_infoTable->setAppTopState(desktopFilePath, num);
AppInfoTable::self()->setAppTopState(desktopFilePath, num);
} }
void ApplicationInfo::setAppLaunchedState(const QString &desktopFilePath, bool launched) void ApplicationInfo::setAppLaunchedState(const QString &desktopFilePath, bool launched) {
{ return d->m_infoTable->setAppLaunchedState(desktopFilePath, launched);
AppInfoTable::self()->setAppLaunchedState(desktopFilePath, launched);
} }
bool ApplicationInfo::tranPidToDesktopFp(int pid, QString &desktopFilePath) bool ApplicationInfo::tranPidToDesktopFp(int pid, QString &desktopFilePath) {
{ return d->m_infoTable->tranPidToDesktopFp(pid, desktopFilePath);
return AppInfoTable::self()->tranPidToDesktopFp(pid, desktopFilePath);
} }
bool ApplicationInfo::tranPidToDesktopFp(uint pid, QString &desktopFilePath) bool ApplicationInfo::tranPidToDesktopFp(uint pid, QString &desktopFilePath) {
{ return d->m_infoTable->tranPidToDesktopFp(pid, desktopFilePath);
return AppInfoTable::self()->tranPidToDesktopFp(pid, desktopFilePath);
} }
bool ApplicationInfo::desktopFilePathFromName(const QString &desktopFileName, QString &desktopFilePath) bool ApplicationInfo::desktopFilePathFromName(const QString &desktopFileName, QString &desktopFilePath) {
{ return d->m_infoTable->desktopFilePathFromName(desktopFileName, desktopFilePath);
return AppInfoTable::self()->desktopFilePathFromName(desktopFileName, desktopFilePath);
} }
bool ApplicationInfo::tranWinIdToDesktopFilePath(const QVariant &winId, QString &desktopFilePath) bool ApplicationInfo::tranWinIdToDesktopFilePath(const QVariant &winId, QString &desktopFilePath) {
{ return d->m_infoTable->tranWinIdToDesktopFilePath(winId, desktopFilePath);
return AppInfoTable::self()->tranWinIdToDesktopFilePath(winId, desktopFilePath);
} }

View File

@ -30,7 +30,7 @@ class ApplicationInfo : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit ApplicationInfo(QObject *parent = nullptr); explicit ApplicationInfo(QObject *parent = nullptr);
~ApplicationInfo(); ~ApplicationInfo() = default;
/** /**
* @brief getInfo * @brief getInfo
* @param desktopFile * @param desktopFile
@ -140,7 +140,6 @@ Q_SIGNALS:
private: private:
ApplicationInfoPrivate *d = nullptr; ApplicationInfoPrivate *d = nullptr;
}; };
} }