***********************
** 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.
********************************
** 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
*****************************/
-/* 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 <libpq++/pgdatabase.h>
+#include <pqxx/transaction.h>
#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.