X-Git-Url: http://www.andromeda.nl/gitweb/?p=gnucomo.git;a=blobdiff_plain;f=src%2Finclude%2Fdatabase.h;h=051adb86ebb950d18e6dd44b951e94ed0d9f587c;hp=b96c0b35dab740c044426d6228916483cf627208;hb=80525b93854831e2030689833bf8fcfc2ea5a902;hpb=475fc6925dbbc4992caf1e1a4127b49267d69a1a 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.