From 80525b93854831e2030689833bf8fcfc2ea5a902 Mon Sep 17 00:00:00 2001 From: arjen Date: Sun, 17 Aug 2003 11:39:33 +0000 Subject: [PATCH] Changed the gnucomo_database class to the new PostgreSQL library, libpqxx --- src/include/database.h | 70 +++++++++++++++++++++++++++++++------------------- src/lib/database.cpp | 65 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 99 insertions(+), 36 deletions(-) diff --git a/src/include/database.h b/src/include/database.h index b96c0b3..051adb8 100644 --- a/src/include/database.h +++ b/src/include/database.h @@ -8,7 +8,7 @@ *********************** ** FILE NAME : database.h ** SYSTEM NAME : -** VERSION NUMBER : $Revision: 1.7 $ +** VERSION NUMBER : $Revision: 1.8 $ ** ** DESCRIPTION : Classes to provide an abstract layer on the Gnucomo ** database. @@ -21,13 +21,17 @@ ******************************** ** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl ** CREATION DATE : Sep 10, 2002 -** LAST UPDATE : Mar 28, 2003 +** LAST UPDATE : Aug 17, 2003 ** MODIFICATIONS : **************************************************************************/ /***************************** $Log: database.h,v $ - Revision 1.7 2003-03-29 08:13:53 arjen + Revision 1.8 2003-08-17 11:39:33 arjen + Changed the gnucomo_database class to the new PostgreSQL + library, libpqxx + + Revision 1.7 2003/03/29 08:13:53 arjen New member function gnucomo_database::is_conected(). Revision 1.6 2003/02/19 09:54:47 arjen @@ -52,71 +56,85 @@ *****************************/ -/* static const char *RCSID = "$Id: database.h,v 1.7 2003-03-29 08:13:53 arjen Exp $"; */ +/* static const char *RCSID = "$Id: database.h,v 1.8 2003-08-17 11:39:33 arjen Exp $"; */ -#include +#include #include "gnucomo_config.h" /* /////////////////////////////////////////////////////////////////////////// // NAME : gnucomo_database -// BASECLASS : configuration +// BASECLASS : // MEMBERS : // OPERATORS : -// METHODS : Database - Obtain the database access string +// METHODS : is_connected - Return true if the database is connected // // DESCRIPTION : // // RELATIONS : // SEE ALSO : -// LAST MODIFIED : Mar 28, 2003 +// LAST MODIFIED : Aug 17, 2003 /////////////////////////////////////////////////////////////////////////// */ class gnucomo_database { gnucomo_config *cfg; - PgDatabase *db; + pqxx::Connection *dbconn; + pqxx::Transaction *dbxact; + + pqxx::Result last_result; public: gnucomo_database() { cfg = 0; - db = 0; + dbconn = 0; + dbxact = 0; } - gnucomo_database(gnucomo_config *c); // Use the configuration to connect to the database + // Use the configuration to connect to the database + + gnucomo_database(gnucomo_config *c); + + // A copy constructor and the assignement can copy + // the connection to the database must create a + // new Transaction object. + + gnucomo_database(const gnucomo_database &gdb); + void operator = (const gnucomo_database &gdb); + ~gnucomo_database(); + // Error checking and handling. bool is_connected() { - return db != 0 && db->Status() == CONNECTION_OK; + return dbconn != 0 && dbconn->is_open(); } // Low-level database access functions int Query(String qry) { - ExecStatusType result; - - result = db->Exec(qry); - if (result == PGRES_TUPLES_OK || result == PGRES_COMMAND_OK) - { - return db->Tuples(); - } - else - { - std::cerr << "Database query error: " << db->ErrorMessage() << "\n"; - std::cerr << "Query: " << qry << "\n"; - return -1; - } + + last_result = dbxact->Exec(qry); +#ifdef DEBUG + std::cerr << "Query " << qry << " returned " + << last_result.size() << " tuples.\n"; +#endif + return last_result.size(); + } + + pqxx::Result Result() + { + return last_result; } String Field(int tuple, const char *fieldname) { - return String(db->GetValue(tuple, fieldname)); + return String(last_result[tuple][fieldname].c_str()); } // Return the objectid of the host given its name. diff --git a/src/lib/database.cpp b/src/lib/database.cpp index 79ceee2..2b073e7 100644 --- a/src/lib/database.cpp +++ b/src/lib/database.cpp @@ -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 ** @@ -20,13 +20,17 @@ ******************************** ** 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 @@ -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; -- 2.11.0