From 3676a26d3272ab4db47847f73869536617b78f34 Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Sat, 13 Mar 2021 12:54:28 +0100 Subject: [PATCH] Added database abstractions --- configure | 5 ++- configure.ac | 3 +- src/Makefile.am | 2 +- src/Makefile.in | 2 +- src/include/database.h | 32 ++++++++++---- src/include/object.h | 18 ++++---- src/include/objectlog.h | 63 ++++++++++++++++++++++++++++ src/lib/database.cpp | 109 +++++++++++++++++++++++++++--------------------- src/lib/object.cpp | 51 +++++++++++++++++++++- 9 files changed, 212 insertions(+), 73 deletions(-) create mode 100644 src/include/objectlog.h diff --git a/configure b/configure index 722b440..0219526 100755 --- a/configure +++ b/configure @@ -5502,7 +5502,7 @@ fi done -ac_config_files="$ac_config_files Makefile src/Makefile src/gcm_input/Makefile src/lib/Makefile test/Makefile" +ac_config_files="$ac_config_files Makefile src/Makefile src/lib/Makefile src/gcm_input/Makefile src/spam/Makefile test/Makefile" cat >confcache <<\_ACEOF # This file is a shell script that caches the results of configure @@ -6237,8 +6237,9 @@ do "depfiles") CONFIG_COMMANDS="$CONFIG_COMMANDS depfiles" ;; "Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;; "src/Makefile") CONFIG_FILES="$CONFIG_FILES src/Makefile" ;; - "src/gcm_input/Makefile") CONFIG_FILES="$CONFIG_FILES src/gcm_input/Makefile" ;; "src/lib/Makefile") CONFIG_FILES="$CONFIG_FILES src/lib/Makefile" ;; + "src/gcm_input/Makefile") CONFIG_FILES="$CONFIG_FILES src/gcm_input/Makefile" ;; + "src/spam/Makefile") CONFIG_FILES="$CONFIG_FILES src/spam/Makefile" ;; "test/Makefile") CONFIG_FILES="$CONFIG_FILES test/Makefile" ;; *) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;; diff --git a/configure.ac b/configure.ac index 1a55bbe..75ac187 100644 --- a/configure.ac +++ b/configure.ac @@ -52,7 +52,8 @@ AC_CHECK_FUNCS([gethostbyname gethostname memmove socket strchr strdup strerror] AC_CONFIG_FILES([Makefile src/Makefile - src/gcm_input/Makefile src/lib/Makefile + src/gcm_input/Makefile + src/spam/Makefile test/Makefile]) AC_OUTPUT diff --git a/src/Makefile.am b/src/Makefile.am index e49e19f..8e6e132 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1 +1 @@ -SUBDIRS = lib gcm_input +SUBDIRS = lib gcm_input spam diff --git a/src/Makefile.in b/src/Makefile.in index 6c2c7a2..3f7bfd0 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -276,7 +276,7 @@ target_alias = @target_alias@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ -SUBDIRS = lib gcm_input +SUBDIRS = lib gcm_input spam all: all-recursive .SUFFIXES: diff --git a/src/include/database.h b/src/include/database.h index 60f3796..4532bfb 100644 --- a/src/include/database.h +++ b/src/include/database.h @@ -76,6 +76,8 @@ #include #include "gnucomo_config.h" +#define DEBUG + /* /////////////////////////////////////////////////////////////////////////// // NAME : gnucomo_database @@ -197,22 +199,34 @@ public: class database_entity { - const gnucomo_database *db; + gnucomo_database *db; String table; + String connected_table; bool fresh; // Completely new, no tuple in the database bool changed; // A database update is needed bool deleted; // Tuple is to be deleted from the database +protected: + + std::map fields; + public: - database_entity(const gnucomo_database &gdb, const String tbl) - { - db = &gdb; - table = tbl; + database_entity(gnucomo_database &gdb, const String tbl) + { + db = &gdb; + table = tbl; + + fresh = true; + changed = false; + deleted = false; + } + + // contruct a new database entity from the result of another one + database_entity(database_entity *from, int row); + + int find_one(String key); + int find_many(String tab, String where); - fresh = true; - changed = false; - deleted = false; - } }; diff --git a/src/include/object.h b/src/include/object.h index c5539d7..7373fe2 100644 --- a/src/include/object.h +++ b/src/include/object.h @@ -24,16 +24,9 @@ ** MODIFICATIONS : **************************************************************************/ -/***************************** - $Log: object.h,v $ - Revision 1.1 2007-02-02 07:36:39 arjen - Started an object oriented abstraction of Gnucomo +#include -*****************************/ - -/* static const char *RCSID = "$Id: object.h,v 1.1 2007-02-02 07:36:39 arjen Exp $"; */ - -#include "database.h" +#include "objectlog.h" /* /////////////////////////////////////////////////////////////////////////// @@ -55,12 +48,15 @@ class Object : public database_entity { String hostname; + String id; public: - Object(const gnucomo_database &); - Object(const gnucomo_database &, String); + Object(gnucomo_database &); + Object(gnucomo_database &, String); String Hostname(); // Return the database access string. + + std::list select_logs(UTC, UTC, String); }; diff --git a/src/include/objectlog.h b/src/include/objectlog.h new file mode 100644 index 0000000..4c3d9bd --- /dev/null +++ b/src/include/objectlog.h @@ -0,0 +1,63 @@ + +/************************************************************************** +** (c) Copyright 2005, Andromeda Technology & Automation +** This is free software; you can redistribute it and/or modify it under the +** terms of the GNU General Public License, see the file COPYING. +*************************************************************************** +** MODULE INFORMATION * +*********************** +** FILE NAME : objectlog.h +** SYSTEM NAME : +** VERSION NUMBER : $Revision: 1.1 $ +** +** DESCRIPTION : +** +** EXPORTED OBJECTS : +** LOCAL OBJECTS : +** MODULES USED : +*************************************************************************** +** ADMINISTRATIVE INFORMATION * +******************************** +** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl +** CREATION DATE : Mar 05, 2021 +** LAST UPDATE : +** MODIFICATIONS : +**************************************************************************/ + +#include "database.h" + +/* +/////////////////////////////////////////////////////////////////////////// +// NAME : ObjectLog +// BASECLASS : database_entity +// MEMBERS : +// OPERATORS : +// METHODS : +// +// DESCRIPTION : +// +// RELATIONS : +// SEE ALSO : +// LAST MODIFIED : Mar 05, 2021 +/////////////////////////////////////////////////////////////////////////// +*/ + +class ObjectLog : public database_entity +{ + + long id; + +public: + + ObjectLog(gnucomo_database &); + ObjectLog(database_entity *from, int row) : database_entity(from, row) + { + } + + String raw() + { + return fields["rawdata"]; + } + +}; + diff --git a/src/lib/database.cpp b/src/lib/database.cpp index 63384a8..3a46852 100644 --- a/src/lib/database.cpp +++ b/src/lib/database.cpp @@ -24,52 +24,6 @@ ** MODIFICATIONS : **************************************************************************/ -/***************************** - $Log: database.cpp,v $ - Revision 1.13 2011-03-24 10:21:47 arjen - Adjusted to new version of libpqxx. - - Revision 1.12 2003/12/22 10:28:26 arjen - Catch an exception if we can not setup a database transaction. - - Revision 1.11 2003/12/04 10:40:28 arjen - Fixed name conflict with 'double log(double)' - - Revision 1.10 2003/12/03 08:23:17 arjen - Write messages to the log stream instead of cout. - - Revision 1.9 2003/08/17 11:39:56 arjen - Changed the gnucomo_database class to the new PostgreSQL - library, libpqxx - - Revision 1.8 2003/07/31 15:44:02 arjen - Removed debug output. - - Revision 1.7 2003/02/19 12:07:55 arjen - Use the SQL function currval() to obtain the identification number - of the most recently created notification. - - Revision 1.6 2003/02/05 09:33:42 arjen - gnucomo_database::new_notification() retruns the id number of the - newly created notification record. - - Revision 1.5 2003/01/20 07:31:42 arjen - Removed some debug output. - - Revision 1.4 2003/01/18 08:52:32 arjen - New C++ function: gnucomo_database::new_notification() - - Revision 1.3 2002/11/09 08:04:27 arjen - Added a reference to the GPL - - Revision 1.2 2002/11/04 10:13:36 arjen - Use proper namespace for iostream classes - - Revision 1.1 2002/10/05 10:25:49 arjen - Creation of gcm_input and a first approach to a web interface - -*****************************/ - #include //#define DEBUG @@ -115,7 +69,6 @@ gnucomo_database::gnucomo_database(gnucomo_config *c) { // Create the transaction object - //dbxact = new pqxx::transaction(*dbconn, "GnuCoMo"); dbxact = new pqxx::work(*dbconn, "GnuCoMo"); gdb_refcount++; } @@ -248,3 +201,65 @@ String gnucomo_database::new_notification(String objectid, String issue, String return notif_id; } +database_entity::database_entity(database_entity *from, int row) +{ + db = from->db; + table = from->connected_table; + + int col; + pqxx::result res; + + res = db->Result(); + + fresh = false; + + for (col = 0; col < res.columns(); col++) + { + fields[res.column_name(col)] = res[row][col].c_str(); + } +} + +int database_entity::find_one(String key) +{ + String query("select * from "); + + query += table; + query += " where "; + query += key; + + if ( db->Query(query) == 1) + { + int col; + pqxx::result res; + + res = db->Result(); + + fresh = false; + + for (col = 0; col < res.columns(); col++) + { + std::cerr << "Field[" << col << "] " << res.column_name(col); + std::cerr << " = " << res[0][col].c_str() << "\n"; + + fields[res.column_name(col)] = res[0][col].c_str(); + } + + return 1; + } + return 0; +} + +int database_entity::find_many(String tab, String where) +{ + connected_table = tab; + + String query("select * from "); + + query += tab; + query += " where "; + query += where; + + return db->Query(query); + +} + diff --git a/src/lib/object.cpp b/src/lib/object.cpp index 183c4dd..9c0f5a4 100644 --- a/src/lib/object.cpp +++ b/src/lib/object.cpp @@ -54,7 +54,56 @@ extern std::ostream *Log; **========================================================================= */ -Object::Object(const gnucomo_database &db) : database_entity(db, "object") +Object::Object(gnucomo_database &gdb) : database_entity(gdb, "object") { + id = "0"; + hostname = ""; } +Object::Object(gnucomo_database &gdb, String name) : database_entity(gdb, "object") +{ + id = "0"; + hostname = name; + + String objectid(""); + String check_host; + + check_host += "objectname = '"; + check_host += hostname; + check_host += "'"; + + if (find_one(check_host) > 0) + { + id = fields["objectid"]; + std::cerr << "Object id = " << id << "\n"; + } + +} + +std::list Object::select_logs(UTC start, UTC finish, String service) +{ + + int rows_found; + std::list logs; + + String where("objectid="); + + where += id; + where += " and timestamp >= '"; + where += start.format(); + where += "' and timestamp <= '"; + where += finish.format(); + where += "' and servicecode = '"; + where += service; + where += "'"; + + rows_found = find_many("log", where); + + for (int r = 0; r < rows_found; r++) + { + ObjectLog ol(this, r); + logs.push_back(ol); + } + + return logs; +} -- 2.11.0