Changed the gnucomo_database class to the new PostgreSQL
[gnucomo.git] / src / lib / database.cpp
index 79ceee2..2b073e7 100644 (file)
@@ -8,7 +8,7 @@
 ***********************
 **      FILE NAME      : database.cpp
 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
-**      VERSION NUMBER : $Revision: 1.8 $
+**      VERSION NUMBER : $Revision: 1.9 $
 **
 **  DESCRIPTION      :  Implementation of the gnucomo database classes
 **
 ********************************
 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
 **      CREATION DATE   : Sep 10, 2002
-**      LAST UPDATE     : Jul 24, 2003
+**      LAST UPDATE     : Aug 17, 2003
 **      MODIFICATIONS   : 
 **************************************************************************/
 
 /*****************************
    $Log: database.cpp,v $
-   Revision 1.8  2003-07-31 15:44:02  arjen
+   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
@@ -54,7 +58,7 @@
 
 *****************************/
 
-static const char *RCSID = "$Id: database.cpp,v 1.8 2003-07-31 15:44:02 arjen Exp $";
+static const char *RCSID = "$Id: database.cpp,v 1.9 2003-08-17 11:39:56 arjen Exp $";
 
 #include <AXE/date.h>
 
@@ -73,19 +77,60 @@ static const char *RCSID = "$Id: database.cpp,v 1.8 2003-07-31 15:44:02 arjen Ex
 **  VARS CHANGED   :
 **  FUNCTIONS USED :
 **  SEE ALSO       :
-**  LAST MODIFIED  : Jul 24, 2003
+**  LAST MODIFIED  : Aug 17, 2003
 **=========================================================================
 */
 
+static int gdb_refcount = 0;
+
 gnucomo_database::gnucomo_database(gnucomo_config *c)
 {
    cfg = c;
 
-   db = new PgDatabase(cfg->Database());
+   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::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;
    }
 }
 
@@ -104,7 +149,7 @@ gnucomo_database::gnucomo_database(gnucomo_config *c)
 **  VARS CHANGED   :
 **  FUNCTIONS USED :
 **  SEE ALSO       :
-**  LAST MODIFIED  : Sep 16, 2002
+**  LAST MODIFIED  : Aug 15, 2003
 **=========================================================================
 */
 
@@ -119,7 +164,7 @@ 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;