From 936940b9e62071a8b15752365c614b7c27a79add Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Tue, 9 Apr 2024 13:55:56 +0300 Subject: [PATCH] UI: ConfRuleManager: Simplify free Rule Id handling --- src/ui/3rdparty/sqlite/dbquery.cpp | 2 +- src/ui/3rdparty/sqlite/dbquery.h | 2 +- src/ui/conf/confmanager.cpp | 2 +- src/ui/conf/confrulemanager.cpp | 50 ++++-------------------------- src/ui/conf/confrulemanager.h | 3 -- src/ui/conf/confzonemanager.cpp | 2 +- src/ui/conf/migrations/1.sql | 4 --- 7 files changed, 10 insertions(+), 55 deletions(-) diff --git a/src/ui/3rdparty/sqlite/dbquery.cpp b/src/ui/3rdparty/sqlite/dbquery.cpp index cc110339..2f2df9be 100644 --- a/src/ui/3rdparty/sqlite/dbquery.cpp +++ b/src/ui/3rdparty/sqlite/dbquery.cpp @@ -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; diff --git a/src/ui/3rdparty/sqlite/dbquery.h b/src/ui/3rdparty/sqlite/dbquery.h index 212e2e5c..68a19556 100644 --- a/src/ui/3rdparty/sqlite/dbquery.h +++ b/src/ui/3rdparty/sqlite/dbquery.h @@ -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); diff --git a/src/ui/conf/confmanager.cpp b/src/ui/conf/confmanager.cpp index f4beae2a..5e2a5947 100644 --- a/src/ui/conf/confmanager.cpp +++ b/src/ui/conf/confmanager.cpp @@ -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," diff --git a/src/ui/conf/confrulemanager.cpp b/src/ui/conf/confrulemanager.cpp index 4c38ed16..f29b6bba 100644 --- a/src/ui/conf/confrulemanager.cpp +++ b/src/ui/conf/confrulemanager.cpp @@ -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(); diff --git a/src/ui/conf/confrulemanager.h b/src/ui/conf/confrulemanager.h index 8bc74a91..cbd850f7 100644 --- a/src/ui/conf/confrulemanager.h +++ b/src/ui/conf/confrulemanager.h @@ -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); diff --git a/src/ui/conf/confzonemanager.cpp b/src/ui/conf/confzonemanager.cpp index 71153331..dd341bff 100644 --- a/src/ui/conf/confzonemanager.cpp +++ b/src/ui/conf/confzonemanager.cpp @@ -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); } diff --git a/src/ui/conf/migrations/1.sql b/src/ui/conf/migrations/1.sql index 6bfb2d2b..f4605955 100644 --- a/src/ui/conf/migrations/1.sql +++ b/src/ui/conf/migrations/1.sql @@ -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,