Skip to content

Commit b7fe080

Browse files
committed
Plugin: add CDatabaseFilter::OnProcess
1 parent 6c83d3b commit b7fe080

5 files changed

Lines changed: 71 additions & 24 deletions

File tree

Src/Database/Database.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ bool CDatabase::OpenDatabase(const CParameterDatabase *pPara,
9090
qDebug(log) << "Multiple result sets:" << driver->hasFeature(QSqlDriver::MultipleResultSets);
9191
qDebug(log) << "Cancel query:" << driver->hasFeature(QSqlDriver::CancelQuery);
9292
}
93-
93+
9494
return bRet;
9595
}
9696

@@ -132,7 +132,7 @@ bool CDatabase::OpenSQLiteDatabase(const CParameterDatabase *pPara,
132132
qInfo(log) << "Open sqlite database connect:"
133133
<< m_database.connectionName()
134134
<< "database name:" << m_database.databaseName();
135-
135+
136136
return OnInitializeDatabase();
137137
}
138138

@@ -179,7 +179,7 @@ bool CDatabase::OpenMySqlDatabase(const CParameterDatabase *pPara,
179179
return false;
180180
}
181181

182-
success = query.exec("use remote_control");
182+
success = query.exec("use " + szDbName);
183183
if (!success) {
184184
qCritical(log) << "Failed to use" << szDbName << "database:"
185185
<< query.lastError().text()
@@ -231,7 +231,7 @@ bool CDatabase::OpenODBCDatabase(const CParameterDatabase *pPara,
231231
return false;
232232
}
233233

234-
success = query.exec("use remote_control");
234+
success = query.exec("use " + szDbName);
235235
if (!success) {
236236
qCritical(log) << "Failed to use" << szDbName << "database:"
237237
<< query.lastError().text()

Src/Database/DatabaseFilter.cpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ bool CDatabaseFilter::isEmpty()
105105
return !query.next();
106106
}
107107

108+
int CDatabaseFilter::OnProcess(std::function<int (const QString &)> cb, bool bErrExit)
109+
{
110+
QSqlQuery query(GetDatabase());
111+
query.prepare("SELECT `key` FROM " + m_szTableName);
112+
bool bRet = query.exec();
113+
if(!bRet) {
114+
qCritical(log) << "Failed to isEmpty:"
115+
<< query.lastError().text()
116+
<< "Sql:" << query.executedQuery();
117+
return -1;
118+
}
119+
while(query.next()) {
120+
QString szKey = query.value(0).toString();
121+
int nRet = cb(szKey);
122+
if(bErrExit && nRet)
123+
return nRet;
124+
}
125+
return 0;
126+
}
127+
108128
bool CDatabaseFilter::ExportToJson(QJsonObject &obj)
109129
{
110130
return true;
@@ -117,32 +137,42 @@ bool CDatabaseFilter::ImportFromJson(const QJsonObject &obj)
117137

118138
bool CDatabaseFilter::OnInitializeSqliteDatabase()
119139
{
120-
return true;
121-
}
140+
QSqlQuery query(GetDatabase());
122141

123-
bool CDatabaseFilter::OnInitializeMySqlDatabase()
124-
{
125-
return true;
142+
query.prepare(
143+
"CREATE TABLE IF NOT EXISTS "
144+
+ m_szTableName +
145+
" ("
146+
" `key` TEXT PRIMARY KEY NOT NULL"
147+
" )"
148+
);
149+
bool success = query.exec();
150+
151+
if (!success) {
152+
qCritical(log) << "Failed to create sqlite table:"
153+
<< m_szTableName << query.lastError().text()
154+
<< "Sql:" << query.executedQuery();
155+
return false;
156+
}
126157
}
127158

128-
bool CDatabaseFilter::OnInitializeDatabase()
159+
bool CDatabaseFilter::OnInitializeMySqlDatabase()
129160
{
130161
QSqlQuery query(GetDatabase());
131162

132-
bool success = query.exec(
163+
query.prepare(
133164
"CREATE TABLE IF NOT EXISTS "
134165
+ m_szTableName +
135166
" ("
136-
" `key` TEXT PRIMARY KEY NOT NULL"
167+
" `key` TEXT NOT NULL,"
168+
" UNIQUE KEY `uk_key` (`key`(255))"
137169
" )"
138170
);
139-
171+
bool success = query.exec();
140172
if (!success) {
141-
qCritical(log) << "Failed to create folders sqlite table:"
173+
qCritical(log) << "Failed to create mysql table:"
142174
<< m_szTableName << query.lastError().text()
143175
<< "Sql:" << query.executedQuery();
144176
return false;
145177
}
146-
147-
return true;
148178
}

Src/Database/DatabaseFilter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class PLUGIN_EXPORT CDatabaseFilter : public CDatabase
1414
int RemoveKey(const QString& szKey);
1515
int Clear();
1616
[[nodiscard]] virtual bool isEmpty();
17+
int OnProcess(std::function<int(const QString& key)> cb, bool bErrExit = false);
1718

18-
[[nodiscard]] virtual bool OnInitializeDatabase() override;
1919
[[nodiscard]] virtual bool ExportToJson(QJsonObject &obj) override;
2020
[[nodiscard]] virtual bool ImportFromJson(const QJsonObject &obj) override;
2121

Src/FrmManagePlugins.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,19 @@ int CFrmManagePlugins::FindPlugins(QDir dir, QStringList filters, bool bAdd)
142142
int CFrmManagePlugins::AddItem(CPlugin* plugin, const QString& szPath)
143143
{
144144
QList<QStandardItem*> lstItems;
145-
auto pWhitelist = new QStandardItem();
145+
auto pWhitelist = new QStandardItem(QString(tr("Whitelist").toStdString().size(), ' '));
146146
pWhitelist->setCheckable(true);
147-
if(m_pPara->m_WhiteList.contains(szPath))
147+
if(m_pPara->m_WhiteList.contains(szPath)) {
148148
pWhitelist->setCheckState(Qt::Checked);
149+
//pWhitelist->setText(QString(tr("Whitelist").toStdString().size(), ' '));
150+
}
149151
lstItems << pWhitelist;
150-
auto pBlacklist = new QStandardItem();
152+
auto pBlacklist = new QStandardItem(QString(tr("Blacklist").toStdString().size(), ' '));
151153
pBlacklist->setCheckable(true);
152-
if(m_pPara->m_BlackList.contains(szPath))
154+
if(m_pPara->m_BlackList.contains(szPath)) {
153155
pBlacklist->setCheckState(Qt::Checked);
156+
//pBlacklist->setText(QString(tr("Blacklist").toStdString().size(), ' '));
157+
}
154158
lstItems << pBlacklist;
155159
auto pName = new QStandardItem(plugin->Icon(), plugin->DisplayName());
156160
lstItems << pName;

Src/ParameterCompone/ParameterFilter.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#include <QLoggingCategory>
12
#include "ParameterFilter.h"
23
#include "DatabaseFilter.h"
34

5+
static Q_LOGGING_CATEGORY(log, "Client.Parameter.Filter")
46
CParameterFilter::CParameterFilter(QObject *parent, const QString &szPrefix)
57
: CParameter{parent, szPrefix}
68
, m_pDatabase(nullptr)
@@ -18,11 +20,15 @@ CParameterFilter::~CParameterFilter()
1820

1921
bool CParameterFilter::InitDatabase(CParameterDatabase *pDB)
2022
{
21-
if(!m_pDatabase) return false;
23+
if(!pDB) return false;
2224
m_pDatabase = new CDatabaseFilter(m_szSuffix);
2325
if(m_pDatabase) {
24-
bool ok = m_pDatabase->OpenDatabase(pDB, "connect_filter_" + m_szSuffix);
25-
if(!ok) return false;
26+
QString szConnectName = "connect_filter_" + m_szSuffix;
27+
bool ok = m_pDatabase->OpenDatabase(pDB, szConnectName);
28+
if(!ok) {
29+
qCritical(log) << "Failed to open database:" << szConnectName;
30+
return false;
31+
}
2632
}
2733
return true;
2834
}
@@ -78,6 +84,9 @@ bool CParameterFilter::isEmpty()
7884
int CParameterFilter::OnProcess(std::function<int (const QString &)> cb, bool bErrExit)
7985
{
8086
if(!cb) return -1;
87+
if(m_pDatabase) {
88+
return m_pDatabase->OnProcess(cb, bErrExit);
89+
}
8190
foreach(auto k, m_Key) {
8291
int nRet = cb(k);
8392
if(bErrExit && nRet)
@@ -88,6 +97,8 @@ int CParameterFilter::OnProcess(std::function<int (const QString &)> cb, bool bE
8897

8998
int CParameterFilter::OnLoad(QSettings &set)
9099
{
100+
if(m_pDatabase) return 0;
101+
91102
QStringList s = set.value("Key").toStringList();
92103
foreach(auto k, s) {
93104
AddKey(k);
@@ -97,6 +108,8 @@ int CParameterFilter::OnLoad(QSettings &set)
97108

98109
int CParameterFilter::OnSave(QSettings &set)
99110
{
111+
if(m_pDatabase) return 0;
112+
100113
QStringList s;
101114
foreach (auto k, m_Key) {
102115
s << k;

0 commit comments

Comments
 (0)