diff --git a/src/zm_config.h b/src/zm_config.h index a6d0420b4..3d38cf683 100644 --- a/src/zm_config.h +++ b/src/zm_config.h @@ -38,10 +38,7 @@ #define ZM_SCALE_BASE 100 // The factor by which we bump up 'scale' to simulate FP #define ZM_RATE_BASE 100 // The factor by which we bump up 'rate' to simulate FP -#define ZM_SQL_BATCH_SIZE 50 // Limit the size of multi-row SQL statements -#define ZM_SQL_SML_BUFSIZ 256 // Size of SQL buffer #define ZM_SQL_MED_BUFSIZ 1024 // Size of SQL buffer -#define ZM_SQL_LGE_BUFSIZ 8192 // Size of SQL buffer #define ZM_NETWORK_BUFSIZ 32768 // Size of network buffer diff --git a/src/zm_group.cpp b/src/zm_group.cpp index b2fc43e5f..c3cf55069 100644 --- a/src/zm_group.cpp +++ b/src/zm_group.cpp @@ -20,6 +20,7 @@ #include "zm_group.h" #include "zm_logger.h" +#include "zm_utils.h" #include Group::Group() { @@ -39,27 +40,27 @@ Group::Group(const MYSQL_ROW &dbrow) { /* 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; + id = 0; - if ( p_id ) { - char sql[ZM_SQL_SML_BUFSIZ]; - snprintf(sql, sizeof(sql), "SELECT `Id`, `ParentId`, `Name` FROM `Group` WHERE `Id`=%u", p_id); - Debug(2,"Loading Group for %u using %s", p_id, sql); - zmDbRow dbrow; - if ( !dbrow.fetch(sql) ) { - Error("Unable to load group for id %u: %s", p_id, mysql_error(&dbconn)); - } else { - unsigned int index = 0; - 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()); - } - } - if ( ! id ) { - Debug(1,"No id passed to Group constructor."); - strcpy(name, "Default"); - } + if (p_id) { + std::string sql = stringtf("SELECT `Id`, `ParentId`, `Name` FROM `Group` WHERE `Id`=%u", p_id); + Debug(2, "Loading Group for %u using %s", p_id, sql.c_str()); + zmDbRow dbrow; + if (!dbrow.fetch(sql.c_str())) { + Error("Unable to load group for id %u: %s", p_id, mysql_error(&dbconn)); + } else { + unsigned int index = 0; + 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()); + } + } + if (!id) { + Debug(1, "No id passed to Group constructor."); + strcpy(name, "Default"); + } } Group::~Group() { diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 4dc7a3128..633dc3cea 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -1287,17 +1287,15 @@ void Monitor::actionReload() { void Monitor::actionEnable() { shared_data->action |= RELOAD; - char sql[ZM_SQL_SML_BUFSIZ]; - snprintf(sql, sizeof(sql), "UPDATE `Monitors` SET `Enabled` = 1 WHERE `Id` = %u", id); - zmDbDo(sql); + std::string sql = stringtf("UPDATE `Monitors` SET `Enabled` = 1 WHERE `Id` = %u", id); + zmDbDo(sql.c_str()); } void Monitor::actionDisable() { shared_data->action |= RELOAD; - char sql[ZM_SQL_SML_BUFSIZ]; - snprintf(sql, sizeof(sql), "UPDATE `Monitors` SET `Enabled` = 0 WHERE `Id` = %u", id); - zmDbDo(sql); + std::string sql = stringtf("UPDATE `Monitors` SET `Enabled` = 0 WHERE `Id` = %u", id); + zmDbDo(sql.c_str()); } void Monitor::actionSuspend() { diff --git a/src/zm_rtsp.cpp b/src/zm_rtsp.cpp index d31df3012..4cd641fac 100644 --- a/src/zm_rtsp.cpp +++ b/src/zm_rtsp.cpp @@ -86,11 +86,10 @@ bool RtspThread::recvResponse(std::string &response) { int RtspThread::requestPorts() { if ( !smMinDataPort ) { - char sql[ZM_SQL_SML_BUFSIZ]; //FIXME Why not load specifically by Id? This will get ineffeicient with a lot of monitors - strncpy(sql, "SELECT `Id` FROM `Monitors` WHERE `Function` != 'None' AND `Type` = 'Remote' AND `Protocol` = 'rtsp' AND `Method` = 'rtpUni' ORDER BY `Id` ASC", sizeof(sql)); + std::string sql = "SELECT `Id` FROM `Monitors` WHERE `Function` != 'None' AND `Type` = 'Remote' AND `Protocol` = 'rtsp' AND `Method` = 'rtpUni' ORDER BY `Id` ASC"; - MYSQL_RES *result = zmDbFetch(sql); + MYSQL_RES *result = zmDbFetch(sql.c_str()); int nMonitors = mysql_num_rows(result); int position = 0; diff --git a/src/zm_storage.cpp b/src/zm_storage.cpp index 5edc36e00..79f3e8c0b 100644 --- a/src/zm_storage.cpp +++ b/src/zm_storage.cpp @@ -21,32 +21,33 @@ #include "zm_db.h" #include "zm_logger.h" +#include "zm_utils.h" #include Storage::Storage() : id(0) { - Warning("Instantiating default Storage Object. Should not happen."); - strcpy(name, "Default"); - if ( staticConfig.DIR_EVENTS[0] != '/' ) { + Warning("Instantiating default Storage Object. Should not happen."); + strcpy(name, "Default"); + if (staticConfig.DIR_EVENTS[0] != '/') { // not using an absolute path. Make it one by appending ZM_PATH_WEB snprintf(path, sizeof(path), "%s/%s", - staticConfig.PATH_WEB.c_str(), staticConfig.DIR_EVENTS.c_str()); + staticConfig.PATH_WEB.c_str(), staticConfig.DIR_EVENTS.c_str()); } else { - strncpy(path, staticConfig.DIR_EVENTS.c_str(), sizeof(path)-1); + strncpy(path, staticConfig.DIR_EVENTS.c_str(), sizeof(path) - 1); } scheme = MEDIUM; scheme_str = "Medium"; } Storage::Storage(MYSQL_ROW &dbrow) { - unsigned int index = 0; - id = atoi(dbrow[index++]); - strncpy(name, dbrow[index++], sizeof(name)-1); - strncpy(path, dbrow[index++], sizeof(path)-1); + unsigned int index = 0; + id = atoi(dbrow[index++]); + strncpy(name, dbrow[index++], sizeof(name) - 1); + strncpy(path, dbrow[index++], sizeof(path) - 1); type_str = std::string(dbrow[index++]); scheme_str = std::string(dbrow[index++]); - if ( scheme_str == "Deep" ) { + if (scheme_str == "Deep") { scheme = DEEP; - } else if ( scheme_str == "Medium" ) { + } else if (scheme_str == "Medium") { scheme = MEDIUM; } else { scheme = SHALLOW; @@ -55,44 +56,42 @@ Storage::Storage(MYSQL_ROW &dbrow) { /* If a zero or invalid p_id is passed, then the old default path will be assumed. */ Storage::Storage(unsigned int p_id) : id(p_id) { - - if ( id ) { - char sql[ZM_SQL_SML_BUFSIZ]; - snprintf(sql, sizeof(sql), "SELECT `Id`, `Name`, `Path`, `Type`, `Scheme` FROM `Storage` WHERE `Id`=%u", id); - Debug(2, "Loading Storage for %u using %s", id, sql); - zmDbRow dbrow; - if ( !dbrow.fetch(sql) ) { - Error("Unable to load storage area for id %d: %s", id, mysql_error(&dbconn)); - } else { - unsigned int index = 0; - id = atoi(dbrow[index++]); - strncpy(name, dbrow[index++], sizeof(name)-1); - strncpy(path, dbrow[index++], sizeof(path)-1); + if (id) { + std::string sql = stringtf("SELECT `Id`, `Name`, `Path`, `Type`, `Scheme` FROM `Storage` WHERE `Id`=%u", id); + Debug(2, "Loading Storage for %u using %s", id, sql.c_str()); + zmDbRow dbrow; + if (!dbrow.fetch(sql.c_str())) { + Error("Unable to load storage area for id %d: %s", id, mysql_error(&dbconn)); + } else { + unsigned int index = 0; + id = atoi(dbrow[index++]); + strncpy(name, dbrow[index++], sizeof(name) - 1); + strncpy(path, dbrow[index++], sizeof(path) - 1); type_str = std::string(dbrow[index++]); scheme_str = std::string(dbrow[index++]); - if ( scheme_str == "Deep" ) { + if (scheme_str == "Deep") { scheme = DEEP; - } else if ( scheme_str == "Medium" ) { + } else if (scheme_str == "Medium") { scheme = MEDIUM; } else { scheme = SHALLOW; } - Debug(1, "Loaded Storage area %d '%s'", id, name); - } - } - if ( !id ) { - if ( staticConfig.DIR_EVENTS[0] != '/' ) { + Debug(1, "Loaded Storage area %d '%s'", id, name); + } + } + if (!id) { + if (staticConfig.DIR_EVENTS[0] != '/') { // not using an absolute path. Make it one by appending ZM_PATH_WEB snprintf(path, sizeof(path), "%s/%s", - staticConfig.PATH_WEB.c_str(), staticConfig.DIR_EVENTS.c_str()); + staticConfig.PATH_WEB.c_str(), staticConfig.DIR_EVENTS.c_str()); } else { - strncpy(path, staticConfig.DIR_EVENTS.c_str(), sizeof(path)-1); + strncpy(path, staticConfig.DIR_EVENTS.c_str(), sizeof(path) - 1); } - Debug(1, "No id passed to Storage constructor. Using default path %s instead", path); - strcpy(name, "Default"); + Debug(1, "No id passed to Storage constructor. Using default path %s instead", path); + strcpy(name, "Default"); scheme = MEDIUM; scheme_str = "Medium"; - } + } } Storage::~Storage() { diff --git a/src/zm_zone.cpp b/src/zm_zone.cpp index 41174857f..dfda5d868 100644 --- a/src/zm_zone.cpp +++ b/src/zm_zone.cpp @@ -116,8 +116,7 @@ Zone::~Zone() { } void Zone::RecordStats(const Event *event) { - static char sql[ZM_SQL_MED_BUFSIZ]; - snprintf(sql, sizeof(sql), + std::string sql = stringtf( "INSERT INTO Stats SET MonitorId=%d, ZoneId=%d, EventId=%" PRIu64 ", FrameId=%d, " "PixelDiff=%d, AlarmPixels=%d, FilterPixels=%d, BlobPixels=%d, " "Blobs=%d, MinBlobSize=%d, MaxBlobSize=%d, " @@ -136,7 +135,7 @@ void Zone::RecordStats(const Event *event) { stats.alarm_box_.Hi().y_, stats.score_ ); - zmDbDo(sql); + zmDbDo(sql.c_str()); } // end void Zone::RecordStats( const Event *event ) bool Zone::CheckOverloadCount() { diff --git a/src/zmc.cpp b/src/zmc.cpp index 9cb23dfba..3f9db1bd5 100644 --- a/src/zmc.cpp +++ b/src/zmc.cpp @@ -232,7 +232,6 @@ int main(int argc, char *argv[]) { while (!zm_terminate) { result = 0; - static char sql[ZM_SQL_SML_BUFSIZ]; for (const std::shared_ptr &monitor : monitors) { monitor->LoadCamera(); @@ -244,11 +243,11 @@ int main(int argc, char *argv[]) { monitor->SetStartupTime(now); monitor->SetHeartbeatTime(now); - snprintf(sql, sizeof(sql), + std::string sql = stringtf( "INSERT INTO Monitor_Status (MonitorId,Status,CaptureFPS,AnalysisFPS)" " VALUES (%u, 'Running',0,0) ON DUPLICATE KEY UPDATE Status='Running',CaptureFPS=0,AnalysisFPS=0", monitor->Id()); - zmDbDo(sql); + zmDbDo(sql.c_str()); Seconds sleep_time = Seconds(0); while (monitor->PrimeCapture() <= 0) { @@ -273,10 +272,10 @@ int main(int argc, char *argv[]) { break; } - snprintf(sql, sizeof(sql), + sql = stringtf( "INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%u, 'Connected') ON DUPLICATE KEY UPDATE Status='Connected'", - monitor->Id()); - zmDbDo(sql); + monitor->Id()); + zmDbDo(sql.c_str()); } // end foreach monitor if (zm_terminate){ @@ -360,11 +359,10 @@ int main(int argc, char *argv[]) { } // end while ! zm_terminate outer connection loop for (std::shared_ptr &monitor : monitors) { - static char sql[ZM_SQL_SML_BUFSIZ]; - snprintf(sql, sizeof(sql), + std::string sql = stringtf( "INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%u, 'NotRunning') ON DUPLICATE KEY UPDATE Status='NotRunning'", monitor->Id()); - zmDbDo(sql); + zmDbDo(sql.c_str()); } Image::Deinitialise();