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文件
bool tranPidToDesktopFp(uint pid, QString &desktopfp);
@ -75,7 +75,7 @@ private:
QString m_ConnectionName;
public Q_SLOTS:
void sendAppDBItemsUpdate(ApplicationInfoMap results);
void sendAppDBItemsUpdate(UkuiSearch::ApplicationInfoMap results);
void sendAppDBItemsUpdateAll(QStringList desktopFilePaths);
void sendAppDBItemsAdd(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 res(true);
if (m_database->transaction() or keyWord.size() != 0) {
if (m_database->transaction() or !keyWord.isEmpty()) {
QSqlQuery sql(*m_database);
QString cmd;
if (keyWord.at(0).size() < 2) {
@ -362,19 +362,11 @@ void AppInfoTablePrivate::sendAppDBItemsDelete(QStringList 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))
{
}
bool AppInfoTable::query(ApplicationPropertyMap &propertyMap, const QString &desktopFile, ApplicationProperties properties)
bool AppInfoTable::query(ApplicationPropertyMap &propertyMap, const QString &desktopFile, const ApplicationProperties& properties)
{
QString field;
for(const ApplicationProperty::Property &pro : properties) {
@ -400,7 +392,7 @@ bool AppInfoTable::query(ApplicationPropertyMap &propertyMap, const QString &des
return true;
}
bool AppInfoTable::query(ApplicationInfoMap &infoMap, ApplicationProperties properties)
bool AppInfoTable::query(ApplicationInfoMap &infoMap, const ApplicationProperties& properties)
{
QString field;
for(const ApplicationProperty::Property &pro : properties) {
@ -431,7 +423,7 @@ bool AppInfoTable::query(ApplicationInfoMap &infoMap, ApplicationProperties prop
return true;
}
bool AppInfoTable::query(ApplicationInfoMap &infoMap, ApplicationProperties properties, ApplicationPropertyMap restrictions)
bool AppInfoTable::query(ApplicationInfoMap &infoMap, const ApplicationProperties& properties, const ApplicationPropertyMap& restrictions)
{
QString field;
for(const ApplicationProperty::Property &pro : properties) {

View File

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

View File

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

View File

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