mirror of
https://github.com/tnodir/fort
synced 2024-11-15 09:45:44 +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;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DbQuery::getFreeId(int minId, int maxId)
|
int DbQuery::getFreeId(int maxId, int minId)
|
||||||
{
|
{
|
||||||
int resId = 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);
|
QVariant execute(int resultCount = 1);
|
||||||
bool executeOk();
|
bool executeOk();
|
||||||
|
|
||||||
int getFreeId(int minId, int maxId);
|
int getFreeId(int maxId, int minId = 1);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void setResult(bool v);
|
void setResult(bool v);
|
||||||
|
@ -34,7 +34,7 @@ namespace {
|
|||||||
|
|
||||||
const QLoggingCategory LC("conf");
|
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,"
|
const char *const sqlSelectAddressGroups = "SELECT addr_group_id, include_all, exclude_all,"
|
||||||
" include_zones, exclude_zones,"
|
" include_zones, exclude_zones,"
|
||||||
|
@ -30,16 +30,8 @@ const char *const sqlUpdateRule = "UPDATE rule"
|
|||||||
" accept_zones = ?9, reject_zones = ?10, mod_time = ?11"
|
" accept_zones = ?9, reject_zones = ?10, mod_time = ?11"
|
||||||
" WHERE rule_id = ?1;";
|
" WHERE rule_id = ?1;";
|
||||||
|
|
||||||
const char *const sqlSelectMaxRuleId = "SELECT MAX(rule_id) FROM rule;";
|
const char *const sqlSelectRuleIds = "SELECT rule_id FROM rule"
|
||||||
|
" WHERE rule_id < ?1 ORDER BY rule_id;";
|
||||||
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 sqlDeleteRule = "DELETE FROM rule WHERE rule_id = ?1;";
|
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);
|
const bool isNew = (rule.ruleId == 0);
|
||||||
if (isNew) {
|
if (isNew) {
|
||||||
// Get Rule Id from the free list
|
rule.ruleId = DbQuery(sqliteDb(), &ok)
|
||||||
rule.ruleId = getFreeRuleId(ok);
|
.sql(sqlSelectRuleIds)
|
||||||
|
.vars({ ConfUtil::ruleMaxCount() })
|
||||||
|
.getFreeId(/*maxId=*/ConfUtil::ruleMaxCount() - 1);
|
||||||
} else {
|
} else {
|
||||||
updateDriverRuleFlag(rule.ruleId, rule.enabled);
|
updateDriverRuleFlag(rule.ruleId, rule.enabled);
|
||||||
}
|
}
|
||||||
@ -244,9 +238,6 @@ bool ConfRuleManager::deleteRule(int ruleId)
|
|||||||
// Delete the Preset Rule from Rules
|
// Delete the Preset Rule from Rules
|
||||||
DbQuery(sqliteDb()).sql(sqlDeleteRuleSet).vars(vars).executeOk();
|
DbQuery(sqliteDb()).sql(sqlDeleteRuleSet).vars(vars).executeOk();
|
||||||
DbQuery(sqliteDb()).sql(sqlDeleteRuleSetSub).vars(vars).executeOk();
|
DbQuery(sqliteDb()).sql(sqlDeleteRuleSetSub).vars(vars).executeOk();
|
||||||
|
|
||||||
// Put the Rule Id back to the free list
|
|
||||||
putFreeRuleId(ruleId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
commitTransaction(ok);
|
commitTransaction(ok);
|
||||||
@ -324,35 +315,6 @@ bool ConfRuleManager::updateDriverRuleFlag(int ruleId, bool enabled)
|
|||||||
return true;
|
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()
|
bool ConfRuleManager::beginTransaction()
|
||||||
{
|
{
|
||||||
return sqliteDb()->beginWriteTransaction();
|
return sqliteDb()->beginWriteTransaction();
|
||||||
|
@ -45,9 +45,6 @@ signals:
|
|||||||
private:
|
private:
|
||||||
bool updateDriverRuleFlag(int ruleId, bool enabled);
|
bool updateDriverRuleFlag(int ruleId, bool enabled);
|
||||||
|
|
||||||
int getFreeRuleId(bool &ok);
|
|
||||||
void putFreeRuleId(int ruleId);
|
|
||||||
|
|
||||||
bool beginTransaction();
|
bool beginTransaction();
|
||||||
void commitTransaction(bool &ok);
|
void commitTransaction(bool &ok);
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ bool ConfZoneManager::addOrUpdateZone(Zone &zone)
|
|||||||
zone.zoneId = DbQuery(sqliteDb(), &ok)
|
zone.zoneId = DbQuery(sqliteDb(), &ok)
|
||||||
.sql(sqlSelectZoneIds)
|
.sql(sqlSelectZoneIds)
|
||||||
.vars({ ConfUtil::zoneMaxCount() })
|
.vars({ ConfUtil::zoneMaxCount() })
|
||||||
.getFreeId(/*minId=*/1, /*maxId=*/ConfUtil::zoneMaxCount() - 1);
|
.getFreeId(/*maxId=*/ConfUtil::zoneMaxCount() - 1);
|
||||||
} else {
|
} else {
|
||||||
updateDriverZoneFlag(zone.zoneId, zone.enabled);
|
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 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(
|
CREATE TABLE rule_set(
|
||||||
rule_set_id INTEGER PRIMARY KEY,
|
rule_set_id INTEGER PRIMARY KEY,
|
||||||
rule_id INTEGER NOT NULL,
|
rule_id INTEGER NOT NULL,
|
||||||
|
Loading…
Reference in New Issue
Block a user