Added database abstractions
[gnucomo.git] / src / lib / database.cpp
index 36d461c..3a46852 100644 (file)
@@ -8,7 +8,7 @@
 ***********************
 **      FILE NAME      : database.cpp
 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
-**      VERSION NUMBER : $Revision: 1.10 $
+**      VERSION NUMBER : $Revision: 1.13 $
 **
 **  DESCRIPTION      :  Implementation of the gnucomo database classes
 **
 **      MODIFICATIONS   : 
 **************************************************************************/
 
-/*****************************
-   $Log: database.cpp,v $
-   Revision 1.10  2003-12-03 08:23:17  arjen
-   Write messages to the log stream instead of cout.
+#include <date.h>
 
-   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
-
-*****************************/
-
-static const char *RCSID = "$Id: database.cpp,v 1.10 2003-12-03 08:23:17 arjen Exp $";
-
-#include <AXE/date.h>
+//#define DEBUG
 
 #include "database.h"
 
-extern std::ostream *log;
+extern std::ostream *Log;
 
 /*=========================================================================
 **  NAME           : gnucomo_database
@@ -82,7 +45,7 @@ extern std::ostream *log;
 **  VARS CHANGED   :
 **  FUNCTIONS USED :
 **  SEE ALSO       :
-**  LAST MODIFIED  : Aug 17, 2003
+**  LAST MODIFIED  : Sep 23, 2020
 **=========================================================================
 */
 
@@ -90,20 +53,29 @@ static int gdb_refcount = 0;
 
 gnucomo_database::gnucomo_database(gnucomo_config *c)
 {
+   dbconn = 0;
+   dbxact = 0;
    cfg = c;
 
-   dbconn = new pqxx::Connection(cfg->Database());
-
-   if (!dbconn->is_open())
+   try
    {
-      std::cerr << "Connection to database failed.\n";
+      dbconn = new pqxx::connection(cfg->Database());
+
+      if (!dbconn->is_open())
+      {
+         std::cerr << "Connection to database failed.\n";
+      }
+      else
+      {
+         // Create the transaction object
+
+         dbxact = new pqxx::work(*dbconn, "GnuCoMo");
+         gdb_refcount++;
+      }
    }
-   else
+   catch (std::exception &e)
    {
-      // Create the transaction object
-
-      dbxact = new pqxx::Transaction(*dbconn, "GnuCoMo");
-      gdb_refcount++;
+      *Log << "Cannot setup the database transaction: " << e.what() << "\n";
    }
 }
 
@@ -131,7 +103,7 @@ gnucomo_database::~gnucomo_database()
 {
    if (--gdb_refcount == 0 && dbconn != 0 && dbxact != 0)
    {
-      dbxact->Commit();
+      dbxact->commit();
       delete dbxact;
       dbxact = 0;
       delete dbconn;
@@ -189,7 +161,7 @@ String gnucomo_database::new_notification(String objectid, String issue, String
 
    String issueid("");
 
-   *log << "Creating notification for " << issue << ": " << remark << "\n";
+   *Log << "Creating notification for " << issue << ": " << remark << "\n";
 
    qry = "select type_of_issueid, suggested_priority from type_of_issue where name='";
    qry += issue + "'";
@@ -229,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);
+
+}
+