From 8b0d77c41fd3e710eff4e9e2f84d5038864f255e Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 23 Jan 2024 16:11:15 -0500 Subject: [PATCH] Fix complains by cpplint. Switch to std::string for name. Add MonitorIds function which will recursively load monitors Ids from children groups. --- src/zm_group.cpp | 65 ++++++++++++++++++++++++++++++++++++++---------- src/zm_group.h | 30 +++++++++++----------- 2 files changed, 68 insertions(+), 27 deletions(-) diff --git a/src/zm_group.cpp b/src/zm_group.cpp index 9ed7a1093..0702aeb21 100644 --- a/src/zm_group.cpp +++ b/src/zm_group.cpp @@ -23,11 +23,11 @@ #include "zm_utils.h" #include -Group::Group() { +Group::Group() : + id(0), + parent_id(0), + name("Default") { Warning("Instantiating default Group Object. Should not happen."); - id = 0; - parent_id = 0; - strcpy(name, "Default"); } // The order of columns is: Id, ParentId, Name @@ -35,15 +35,12 @@ Group::Group(const MYSQL_ROW &dbrow) { unsigned int index = 0; id = atoi(dbrow[index++]); parent_id = dbrow[index] ? atoi(dbrow[index]): 0; index++; - strncpy(name, dbrow[index++], sizeof(name)-1); + name = dbrow[index++]; } -/* If a zero or invalid p_id is passed, then the old default path will be assumed. */ -Group::Group(unsigned int p_id) { - id = 0; - +Group::Group(unsigned int p_id) : id(0) { if (p_id) { - std::string sql = stringtf("SELECT `Id`, `ParentId`, `Name` FROM `Group` WHERE `Id`=%u", p_id); + std::string sql = stringtf("SELECT `Id`, `ParentId`, `Name` FROM `Groups` WHERE `Id`=%u", p_id); Debug(2, "Loading Group for %u using %s", p_id, sql.c_str()); zmDbRow dbrow; if (!dbrow.fetch(sql)) { @@ -53,15 +50,57 @@ Group::Group(unsigned int p_id) { id = atoi(dbrow[index++]); parent_id = dbrow[index] ? atoi(dbrow[index]) : 0; index++; - strncpy(name, dbrow[index++], sizeof(name) - 1); - Debug(1, "Loaded Group area %d '%s'", id, this->Name()); + name = dbrow[index++]; + Debug(1, "Loaded Group area %d '%s'", id, name.c_str()); } } if (!id) { Debug(1, "No id passed to Group constructor."); - strcpy(name, "Default"); + name = "Default"; } } Group::~Group() { } + +std::vector Group::MonitorIds() { + std::vector monitor_ids; + if (!id) { + Warning("Calling MoniotorIds on a group with no id"); + return monitor_ids; + } + + std::string sql = stringtf("SELECT `MonitorId` FROM Groups_Monitors WHERE `GroupId`=%d", id); + MYSQL_RES *result = zmDbFetch(sql.c_str()); + if (!result) { + Error("Error loading MonitorIds from %s", sql.c_str()); + return monitor_ids; + } + + monitor_ids.reserve(mysql_num_rows(result)); + while (MYSQL_ROW dbrow = mysql_fetch_row(result)) { + monitor_ids.push_back(atoi(dbrow[0])); + } + mysql_free_result(result); + + sql = stringtf("SELECT `Id` FROM `Groups` WHERE `ParentId`=%d", id); + result = zmDbFetch(sql.c_str()); + if (result) { + while (MYSQL_ROW dbrow = mysql_fetch_row(result)) { + Group child(atoi(dbrow[0])); + + std::vector child_monitor_ids = child.MonitorIds(); + if (!child_monitor_ids.empty()) { + monitor_ids.insert( + monitor_ids.end(), + std::make_move_iterator(child_monitor_ids.begin()), + std::make_move_iterator(child_monitor_ids.end()) + ); + } + } // end foreach child + mysql_free_result(result); + } else { + Error("Error loading Ids from %s", sql.c_str()); + } + return monitor_ids; +} diff --git a/src/zm_group.h b/src/zm_group.h index f3ce1f3cf..87f874b6b 100644 --- a/src/zm_group.h +++ b/src/zm_group.h @@ -21,23 +21,25 @@ #define ZM_GROUP_H #include "zm_db.h" +#include +#include class Group { + protected: + unsigned int id; + unsigned int parent_id; + std::string name; -protected: - unsigned int id; - unsigned int parent_id; - char name[64+1]; + public: + Group(); + explicit Group(const MYSQL_ROW &dbrow); + explicit Group(unsigned int p_id); + ~Group(); -public: - Group(); - explicit Group(const MYSQL_ROW &dbrow ); - explicit Group( unsigned int p_id ); - ~Group(); - - unsigned int Id() const { return id; } - unsigned int ParentId() const { return id; } - const char *Name() const { return name; } + unsigned int Id() const { return id; } + unsigned int ParentId() const { return id; } + const std::string Name() const { return name; } + std::vector MonitorIds(); }; -#endif // ZM_GROUP_H +#endif // ZM_GROUP_H