mirror of
https://github.com/tnodir/fort
synced 2024-11-15 05:18:07 +00:00
UI: ConfRuleManager: Simplify free Rule Id handling
This commit is contained in:
parent
4748781681
commit
936940b9e6
2
src/ui/3rdparty/sqlite/dbquery.cpp
vendored
2
src/ui/3rdparty/sqlite/dbquery.cpp
vendored
@ -88,7 +88,7 @@ bool DbQuery::executeOk()
|
||||
return ok;
|
||||
}
|
||||
|
||||
int DbQuery::getFreeId(int minId, int maxId)
|
||||
int DbQuery::getFreeId(int maxId, int minId)
|
||||
{
|
||||
int resId = minId;
|
||||
|
||||
|
2
src/ui/3rdparty/sqlite/dbquery.h
vendored
2
src/ui/3rdparty/sqlite/dbquery.h
vendored
@ -27,7 +27,7 @@ public:
|
||||
QVariant execute(int resultCount = 1);
|
||||
bool executeOk();
|
||||
|
||||
int getFreeId(int minId, int maxId);
|
||||
int getFreeId(int maxId, int minId = 1);
|
||||
|
||||
protected:
|
||||
void setResult(bool v);
|
||||
|
@ -34,7 +34,7 @@ namespace {
|
||||
|
||||
const QLoggingCategory LC("conf");
|
||||
|
||||
constexpr int DATABASE_USER_VERSION = 40;
|
||||
constexpr int DATABASE_USER_VERSION = 41;
|
||||
|
||||
const char *const sqlSelectAddressGroups = "SELECT addr_group_id, include_all, exclude_all,"
|
||||
" include_zones, exclude_zones,"
|
||||
|
@ -30,16 +30,8 @@ const char *const sqlUpdateRule = "UPDATE rule"
|
||||
" accept_zones = ?9, reject_zones = ?10, mod_time = ?11"
|
||||
" WHERE rule_id = ?1;";
|
||||
|
||||
const char *const sqlSelectMaxRuleId = "SELECT MAX(rule_id) FROM rule;";
|
||||
|
||||
const char *const sqlInsertFreeRuleId = "INSERT INTO rule_free(rule_id) VALUES(?1);";
|
||||
|
||||
const char *const sqlDeleteFreeRuleId = "DELETE FROM rule_free"
|
||||
" WHERE rule_id = ("
|
||||
" SELECT MAX(rule_id) FROM rule_free"
|
||||
" ) RETURNING rule_id;";
|
||||
|
||||
const char *const sqlDeleteFreeRuleIds = "DELETE FROM rule_free WHERE rule_id >= ?1;";
|
||||
const char *const sqlSelectRuleIds = "SELECT rule_id FROM rule"
|
||||
" WHERE rule_id < ?1 ORDER BY rule_id;";
|
||||
|
||||
const char *const sqlDeleteRule = "DELETE FROM rule WHERE rule_id = ?1;";
|
||||
|
||||
@ -188,8 +180,10 @@ bool ConfRuleManager::addOrUpdateRule(Rule &rule)
|
||||
|
||||
const bool isNew = (rule.ruleId == 0);
|
||||
if (isNew) {
|
||||
// Get Rule Id from the free list
|
||||
rule.ruleId = getFreeRuleId(ok);
|
||||
rule.ruleId = DbQuery(sqliteDb(), &ok)
|
||||
.sql(sqlSelectRuleIds)
|
||||
.vars({ ConfUtil::ruleMaxCount() })
|
||||
.getFreeId(/*maxId=*/ConfUtil::ruleMaxCount() - 1);
|
||||
} else {
|
||||
updateDriverRuleFlag(rule.ruleId, rule.enabled);
|
||||
}
|
||||
@ -244,9 +238,6 @@ bool ConfRuleManager::deleteRule(int ruleId)
|
||||
// Delete the Preset Rule from Rules
|
||||
DbQuery(sqliteDb()).sql(sqlDeleteRuleSet).vars(vars).executeOk();
|
||||
DbQuery(sqliteDb()).sql(sqlDeleteRuleSetSub).vars(vars).executeOk();
|
||||
|
||||
// Put the Rule Id back to the free list
|
||||
putFreeRuleId(ruleId);
|
||||
}
|
||||
|
||||
commitTransaction(ok);
|
||||
@ -324,35 +315,6 @@ bool ConfRuleManager::updateDriverRuleFlag(int ruleId, bool enabled)
|
||||
return true;
|
||||
}
|
||||
|
||||
int ConfRuleManager::getFreeRuleId(bool &ok)
|
||||
{
|
||||
const int ruleId = DbQuery(sqliteDb()).sql(sqlDeleteFreeRuleId).execute().toInt();
|
||||
if (ruleId > 0) {
|
||||
return ruleId;
|
||||
}
|
||||
|
||||
const int maxRuleId = DbQuery(sqliteDb()).sql(sqlSelectMaxRuleId).execute().toInt();
|
||||
|
||||
ok = (maxRuleId < ConfUtil::ruleMaxCount());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ConfRuleManager::putFreeRuleId(int ruleId)
|
||||
{
|
||||
const QVariantList vars = { ruleId };
|
||||
|
||||
const int maxRuleId = DbQuery(sqliteDb()).sql(sqlSelectMaxRuleId).execute().toInt();
|
||||
|
||||
if (ruleId < maxRuleId) {
|
||||
// Add the Rule Id to free list
|
||||
DbQuery(sqliteDb()).sql(sqlInsertFreeRuleId).vars(vars).executeOk();
|
||||
} else {
|
||||
// Delete outdated free Rule Ids
|
||||
DbQuery(sqliteDb()).sql(sqlDeleteFreeRuleIds).vars(vars).executeOk();
|
||||
}
|
||||
}
|
||||
|
||||
bool ConfRuleManager::beginTransaction()
|
||||
{
|
||||
return sqliteDb()->beginWriteTransaction();
|
||||
|
@ -45,9 +45,6 @@ signals:
|
||||
private:
|
||||
bool updateDriverRuleFlag(int ruleId, bool enabled);
|
||||
|
||||
int getFreeRuleId(bool &ok);
|
||||
void putFreeRuleId(int ruleId);
|
||||
|
||||
bool beginTransaction();
|
||||
void commitTransaction(bool &ok);
|
||||
|
||||
|
@ -106,7 +106,7 @@ bool ConfZoneManager::addOrUpdateZone(Zone &zone)
|
||||
zone.zoneId = DbQuery(sqliteDb(), &ok)
|
||||
.sql(sqlSelectZoneIds)
|
||||
.vars({ ConfUtil::zoneMaxCount() })
|
||||
.getFreeId(/*minId=*/1, /*maxId=*/ConfUtil::zoneMaxCount() - 1);
|
||||
.getFreeId(/*maxId=*/ConfUtil::zoneMaxCount() - 1);
|
||||
} else {
|
||||
updateDriverZoneFlag(zone.zoneId, zone.enabled);
|
||||
}
|
||||
|
@ -102,10 +102,6 @@ CREATE TABLE rule(
|
||||
|
||||
CREATE INDEX rule_rule_type_name_idx ON rule(rule_type, lower(name));
|
||||
|
||||
CREATE TABLE rule_free(
|
||||
rule_id INTEGER PRIMARY KEY
|
||||
);
|
||||
|
||||
CREATE TABLE rule_set(
|
||||
rule_set_id INTEGER PRIMARY KEY,
|
||||
rule_id INTEGER NOT NULL,
|
||||
|
Loading…
Reference in New Issue
Block a user