Write messages to the log stream instead of cout.
[gnucomo.git] / src / lib / database.cpp
index 9a20d6d..36d461c 100644 (file)
@@ -8,7 +8,7 @@
 ***********************
 **      FILE NAME      : database.cpp
 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
-**      VERSION NUMBER : $Revision: 1.4 $
+**      VERSION NUMBER : $Revision: 1.10 $
 **
 **  DESCRIPTION      :  Implementation of the gnucomo database classes
 **
 ********************************
 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
 **      CREATION DATE   : Sep 10, 2002
-**      LAST UPDATE     : Jan 17, 2003
+**      LAST UPDATE     : Aug 17, 2003
 **      MODIFICATIONS   : 
 **************************************************************************/
 
 /*****************************
    $Log: database.cpp,v $
-   Revision 1.4  2003-01-18 08:52:32  arjen
+   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
 
 *****************************/
 
-static const char *RCSID = "$Id: database.cpp,v 1.4 2003-01-18 08:52:32 arjen Exp $";
+static const char *RCSID = "$Id: database.cpp,v 1.10 2003-12-03 08:23:17 arjen Exp $";
 
 #include <AXE/date.h>
 
 #include "database.h"
 
-extern bool verbose;   /*  Defined in the main application */
+extern std::ostream *log;
 
 /*=========================================================================
 **  NAME           : gnucomo_database
@@ -61,24 +82,60 @@ extern bool verbose;   /*  Defined in the main application */
 **  VARS CHANGED   :
 **  FUNCTIONS USED :
 **  SEE ALSO       :
-**  LAST MODIFIED  : Sep 26, 2002
+**  LAST MODIFIED  : Aug 17, 2003
 **=========================================================================
 */
 
+static int gdb_refcount = 0;
+
 gnucomo_database::gnucomo_database(gnucomo_config *c)
 {
    cfg = c;
 
-   if (verbose)
+   dbconn = new pqxx::Connection(cfg->Database());
+
+   if (!dbconn->is_open())
    {
-      std::cout <<  "Database connection string = " << cfg->Database() << "\n";
+      std::cerr << "Connection to database failed.\n";
    }
+   else
+   {
+      // Create the transaction object
 
-   db = new PgDatabase(cfg->Database());
+      dbxact = new pqxx::Transaction(*dbconn, "GnuCoMo");
+      gdb_refcount++;
+   }
+}
+
+gnucomo_database::gnucomo_database(const gnucomo_database &gdb)
+{
+   dbconn = gdb.dbconn;
+   dbxact = gdb.dbxact;
+   gdb_refcount++;
+}
 
-   if (db->ConnectionBad())
+void gnucomo_database::operator = (const gnucomo_database &gdb)
+{
+   dbconn = gdb.dbconn;
+   dbxact = gdb.dbxact;
+   gdb_refcount++;
+}
+
+   //      A destructor must Commit the transaction and
+   //      destroy the transaction before destroying the
+   //      connection.
+   //      The connection can only be destroyed by the last
+   //      object alive.
+
+gnucomo_database::~gnucomo_database()
+{
+   if (--gdb_refcount == 0 && dbconn != 0 && dbxact != 0)
    {
-      std::cerr << "Can not connect to database: " << db->ErrorMessage();
+      dbxact->Commit();
+      delete dbxact;
+      dbxact = 0;
+      delete dbconn;
+      dbconn = 0;
    }
 }
 
@@ -97,7 +154,7 @@ gnucomo_database::gnucomo_database(gnucomo_config *c)
 **  VARS CHANGED   :
 **  FUNCTIONS USED :
 **  SEE ALSO       :
-**  LAST MODIFIED  : Sep 16, 2002
+**  LAST MODIFIED  : Aug 15, 2003
 **=========================================================================
 */
 
@@ -112,23 +169,27 @@ String gnucomo_database::find_host(const String hostname)
 
    if (Query(check_host) > 0)
    {
-      objectid = String(db->GetValue(0, "objectid"));
+      objectid = Field(0, "objectid");
    }
 
    return objectid;
 }
 
-void gnucomo_database::new_notification(String objectid, String issue, String remark)
+/*
+ *  Create a new notification with an action_user and return the notification id
+ */
+
+String gnucomo_database::new_notification(String objectid, String issue, String remark)
 {
    String qry;
    UTC    now = Now();
 
    String insertion;
-   String notif_id;
+   String notif_id("");
 
    String issueid("");
 
-   std::cout << "Creating notification for " << issue << "\n";
+   *log << "Creating notification for " << issue << ": " << remark << "\n";
 
    qry = "select type_of_issueid, suggested_priority from type_of_issue where name='";
    qry += issue + "'";
@@ -141,32 +202,30 @@ void gnucomo_database::new_notification(String objectid, String issue, String re
       insertion += issueid + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
       insertion += Field(0, "suggested_priority") + "')";
 
-      std::cout << insertion << "\n";
-
-      qry = "select notificationid from notification where objectid='";
-      qry += objectid + "' and type_of_issueid = '";
-      qry += issueid + "' order by notificationid";
-
       Query(insertion);
-      int tuples = Query(qry);
 
-      if (tuples > 0)
-      {
-         notif_id = Field(tuples - 1, "notificationid");
+      Query("select currval('notification_notificationid_seq')");
+      notif_id = Field(0, "currval");
 
+      if (notif_id != "")
+      {
          insertion = "insert into action_user (actionid, username, notificationid,";
          insertion += "    timestamp, statuscode, remarks) values ('1', 'gnucomo', '";
          insertion += notif_id + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
          insertion += remark + "')";
 
-         std::cout << insertion << "\n";
          Query(insertion);
       }
       else
       {
          std::cerr << "Error inserting notification.\n";
-         exit(1);
       }
    }
+   else
+   {
+      std::cerr << "DATABASE ERROR: Type of issue " << issue << " not found.\n";
+   }
+
+   return notif_id;
 }