From b3d83146273fbf4c89607e9553229131840266f2 Mon Sep 17 00:00:00 2001 From: Tom Early Date: Tue, 14 Apr 2020 20:47:30 -0700 Subject: [PATCH] count for GATEWAYS table --- .gitignore | 1 + QnetDB.cpp | 32 ++++++++++++++++++++++++++++++-- QnetDB.h | 1 + QnetLink.cpp | 4 ++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ba0e0c6..b1e9ca5 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ qndtmf gwys.txt qnitap qndvap +qndv qndvrptr qnlink qngateway diff --git a/QnetDB.cpp b/QnetDB.cpp index 05d5760..e459765 100644 --- a/QnetDB.cpp +++ b/QnetDB.cpp @@ -233,7 +233,7 @@ bool CQnetDB::FindGW(const char *name, std::string &address, unsigned short &por sqlite3_stmt *stmt; int rval = sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, 0); if (SQLITE_OK != rval) { - fprintf(stderr, "CQnetDB::FindLS error: %d\n", rval); + fprintf(stderr, "CQnetDB::FindGW error: %d\n", rval); return true; } @@ -250,12 +250,40 @@ bool CQnetDB::FindGW(const char *name, std::string &address, unsigned short &por void CQnetDB::ClearGW() { + if (NULL == db) + return; + std::string sql("DELETE FROM GATEWAYS;"); char *eMsg; if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), NULL, 0, &eMsg)) { - fprintf(stderr, "CQnetDB::Open drop table LINKSTATUS error: %s\n", eMsg); + fprintf(stderr, "CQnetDB::ClearGW error: %s\n", eMsg); sqlite3_free(eMsg); } +} + +static int countcallback(void *count, int argc, char **argv, char **azColName) { + auto c = (int *)count; + *c = atoi(argv[0]); + return 0; +} + +int CQnetDB::Count(const char *table) +{ + if (NULL == db) + return; + + std::string sql("SELECT COUNT(*) FROM "); + sql.append(table); + sql.append(";"); + + int count = 0; + + char *eMsg; + if (SQLITE_OK != sqlite3_exec(db, sql.c_str(), countcallback, &count, &eMsg)) { + fprintf(stderr, "CQnetDB::Count error: %s\n", eMsg); + sqlite3_free(eMsg); + } + return count; } diff --git a/QnetDB.h b/QnetDB.h index dcf0f32..6769c70 100644 --- a/QnetDB.h +++ b/QnetDB.h @@ -60,6 +60,7 @@ public: bool FindLS(const char mod, std::list &linklist); bool FindGW(const char *name, std::string &address, unsigned short &port); void ClearGW(); + int Count(const char *table); private: sqlite3 *db; diff --git a/QnetLink.cpp b/QnetLink.cpp index 30a34a1..f65729a 100644 --- a/QnetLink.cpp +++ b/QnetLink.cpp @@ -326,6 +326,10 @@ bool CQnetLink::load_gwys(const std::string &filename) fprintf(stderr, "DPlus Authorization completed!\n"); } + auto count = qnDB.Count("GATEWAYS"); + if (count) + printf("Loaded %d gateways from %s%s\n", count, filename.c_str(), dplus_authorize ? " and auth.dstargateway.org" : ""); + return true; }