搜索服务应用数据库新增跟随版本更新机制;

This commit is contained in:
jixiaoxu 2022-12-21 11:04:37 +08:00 committed by iaom
parent 09a423aa38
commit 1d737c8f2a
2 changed files with 80 additions and 91 deletions

View File

@ -13,7 +13,8 @@
#define LAST_LOCALE_NAME QDir::homePath() + "/.config/org.ukui/ukui-search/appdata/last-locale-name.conf"
#define LOCALE_NAME_VALUE "CurrentLocaleName"
#define APP_DATABASE_VERSION_CONFIG QDir::homePath() + "/.config/org.ukui/ukui-search/appdata/app-database-version.conf"
#define APP_DATABASE_VERSION_VALUE "AppDatabaseVersion"
using namespace UkuiSearch;
static AppDBManager *global_instance;
@ -32,22 +33,47 @@ AppDBManager::AppDBManager(QObject *parent) : QThread(parent), m_database(QSqlDa
//链接数据库
if (openDataBase()) {
//监听系统语言变化
m_qSettings = new QSettings(LAST_LOCALE_NAME, QSettings::IniFormat);
m_lastLocaleNameQsettings = new QSettings(LAST_LOCALE_NAME, QSettings::IniFormat);
m_localeChanged = false;
m_qSettings->beginGroup(LOCALE_NAME_VALUE);
QString lastLocale = m_qSettings->value(LOCALE_NAME_VALUE).toString();
m_lastLocaleNameQsettings->beginGroup(LOCALE_NAME_VALUE);
QString lastLocale = m_lastLocaleNameQsettings->value(LOCALE_NAME_VALUE).toString();
if (QLocale::system().name().compare(lastLocale)) {
qDebug() << "I'm going to update the locale name in conf file.";
if (!lastLocale.isEmpty()) {
m_localeChanged = true;
}
m_qSettings->setValue(LOCALE_NAME_VALUE, QLocale::system().name());
m_lastLocaleNameQsettings->setValue(LOCALE_NAME_VALUE, QLocale::system().name());
}
m_lastLocaleNameQsettings->endGroup();
//检查数据库版本
m_dbVersionQsettings = new QSettings(APP_DATABASE_VERSION_CONFIG, QSettings::IniFormat);
m_dbVersionQsettings->beginGroup(APP_DATABASE_VERSION_VALUE);
QString dbVersion = m_dbVersionQsettings->value(APP_DATABASE_VERSION_VALUE).toString();
if (dbVersion.isEmpty()) {
m_dbVersionNeedUpdate = true;
}else if (dbVersion != APP_DATABASE_VERSION) {
if (dbVersion.toDouble() < APP_DATABASE_VERSION.toDouble()) {
m_dbVersionNeedUpdate = true;
} else {
qDebug() << "app db version old version:" << dbVersion.toDouble() << "new version:" << APP_DATABASE_VERSION.toDouble();
}
}
m_dbVersionQsettings->endGroup();
if (m_dbVersionNeedUpdate) {
qDebug() << "app db version need update! old version:" << dbVersion.toDouble() << "new version:" << APP_DATABASE_VERSION.toDouble();
}
m_qSettings->endGroup();
//初始化数据库
refreshAllData2DB();
// refreshDataBase();
if (m_dbVersionNeedUpdate) {
m_dbVersionQsettings->beginGroup(APP_DATABASE_VERSION_VALUE);
m_dbVersionQsettings->setValue(APP_DATABASE_VERSION_VALUE, APP_DATABASE_VERSION);
m_dbVersionQsettings->endGroup();
m_dbVersionNeedUpdate = false;
}
//初始化FileSystemWatcher
m_watcher = new FileSystemWatcher;
@ -266,85 +292,7 @@ void AppDBManager::buildAppInfoDB()
void AppDBManager::updateAppInfoDB()
{
/*
QMutexLocker locker(&s_mutex);
m_installAppMap.clear();
this->getAllDesktopFilePath(GENERAL_APP_DESKTOP_PATH);
this->getAllDesktopFilePath(ANDROID_APP_DESKTOP_PATH);
this->getAllDesktopFilePath(SNAPD_APP_DESKTOP_PATH);
QStringList filePathList;
this->getFilePathList(filePathList);
QSqlQuery sql(m_database);
QString cmd = "SELECT COUNT(*) FROM APPINFO";
if (!sql.exec(cmd)) {
this->buildAppInfoDB();
for (auto &filePath : filePathList) {
this->addAppDesktopFile2DB(filePath);
}
} else {
if (sql.exec(cmd)) {
if (sql.next()) {
//删除多余项
if (sql.value(0).toInt() > filePathList.size()) {
int size = sql.value(0).toInt();
cmd = QString("SELECT DESKTOP_FILE_PATH FROM APPINFO");
if (!sql.exec(cmd)) {
qWarning() << m_database.lastError() << cmd;
return;
}
QString path;
for (int i = 0; i<size; ++i) {
if (!sql.next()) {
qWarning() << m_database.lastError() << cmd;
return;
}
path = sql.value(0).toString();
if (!filePathList.contains(path)) {
this->deleteAppDesktopFile2DB(path);
break;
}
}
}
//添加新增项根据md5判断desktop文件是否改变以更新对应项
for (QString &filePath : filePathList) {
cmd = QString("SELECT COUNT(*) FROM APPINFO WHERE DESKTOP_FILE_PATH = '%0'").arg(filePath);
if (sql.exec(cmd)) {
if (sql.next()) {
if (sql.value(0).toInt() == 0) {
this->addAppDesktopFile2DB(filePath);
} else {
cmd = QString("SELECT MD5 FROM APPINFO WHERE DESKTOP_FILE_PATH = '%0'").arg(filePath);
if (!sql.exec(cmd)) {
qWarning() << m_database.lastError() << cmd;
return;
}
if (!sql.next()) {
qWarning() << m_database.lastError() << cmd;
return;
}
if (sql.value(0).toString() != getAppDesktopMd5(filePath)) {
this->updateAppDesktopFile2DB(filePath);
}
}
} else {
qWarning() << m_database.lastError() << cmd;
return;
}
} else {
qWarning() << m_database.lastError() << cmd;
return;
}
}
} else {
qWarning() << m_database.lastError() << cmd;
return;
}
} else {
qWarning() << m_database.lastError() << cmd;
return;
}
}
*/
}
void AppDBManager::getFilePathList(QStringList &pathList)
@ -431,9 +379,43 @@ void AppDBManager::loadDesktopFilePaths(QString path, QFileInfoList &infolist)
infolist.append(dir.entryInfoList());
}
bool AppDBManager::addItem2BackIfNotExist(QString itemName, QString itemDataType, QVariant defult)
{
bool res(true);
QSqlQuery sql(m_database);
QString cmd = QString("SELECT * FROM sqlite_master WHERE name = 'appInfo' AND sql like '% %1 %' ").arg(itemName);
if (!sql.exec(cmd)) {
qWarning() << m_database.lastError() << cmd;
res = false;
return res;
}
if (sql.next()) {
qDebug() << cmd << "is exist!";
return res;
}
cmd = QString("ALTER TABLE appInfo ADD '%0' '%1' ").arg(itemName)
.arg(itemDataType);
if (defult != QVariant()) {
//TODO 根据数据类型将初始值转化为对应格式数据
if (itemDataType == "INT(4)") {
cmd += QString("DEFAULT %1").arg(defult.toInt());
} else if (itemDataType == "TEXT") {
cmd += QString("DEFAULT %1").arg(QString(defult.toByteArray()));
}
}
if (!sql.exec(cmd)) {
qWarning() << m_database.lastError() << cmd;
res = false;
}
if (!res) {
qDebug() << "Fail to addItem2Back :" << itemName;
}
return res;
}
void AppDBManager::refreshAllData2DB()
{
m_dbChanged = false;
bool firstExec = false;
QSqlQuery sql(m_database);
QString cmd = "SELECT DESKTOP_FILE_PATH,MD5 FROM APPINFO";
@ -482,7 +464,8 @@ void AppDBManager::refreshAllData2DB()
if (!firstExec) {
//数据库有记录
if (dataMap.contains(path)) {
if (!QString::compare(dataMap.value(path), getAppDesktopMd5(path))) {
if (!QString::compare(dataMap.value(path), getAppDesktopMd5(path))
and !m_dbVersionNeedUpdate) {
//判断系统语言是否改变
if (m_localeChanged) {
this->updateLocaleData(path);
@ -490,7 +473,7 @@ void AppDBManager::refreshAllData2DB()
dataMap.remove(path);
continue;
} else {
//数据库有记录但md5值改变则update
//数据库有记录但md5值改变或数据库版本需要更新则update
this->updateDBItem(path);
dataMap.remove(path);
continue;

View File

@ -20,6 +20,7 @@
//#include <QFileSystemWatcher>
#define CONNECTION_NAME QLatin1String("ukss-appdb-connection")
static const QString APP_DATABASE_VERSION = QStringLiteral("1.0");
namespace UkuiSearch {
/**
@ -110,6 +111,10 @@ private:
//加载指定路径path中的所有desktop文件路径到infolist中
void loadDesktopFilePaths(QString path, QFileInfoList &infolist);
//数据库查找指定字段不存在则添加到最后并设置初始值
bool addItem2BackIfNotExist(QString itemName, QString itemDataType, QVariant defult = QVariant());
//链接数据库
bool openDataBase();
//刷新数据库
@ -129,9 +134,10 @@ private:
private:
static QMutex s_mutex;
bool m_localeChanged;
bool m_dbChanged;
bool m_dbVersionNeedUpdate = false;
QSettings *m_qSettings = nullptr;
QSettings *m_lastLocaleNameQsettings = nullptr;
QSettings *m_dbVersionQsettings = nullptr;
// QTimer *m_timer = nullptr;
// QTimer *m_maxProcessTimer = nullptr;