Overloaded gnucomo_database::Field() to include the Result
[gnucomo.git] / src / include / database.h
index d77f7a2..2060cb5 100644 (file)
@@ -8,7 +8,7 @@
 ***********************
 **      FILE NAME      : database.h
 **      SYSTEM NAME    : 
-**      VERSION NUMBER : $Revision: 1.6 $
+**      VERSION NUMBER : $Revision: 1.9 $
 **
 **  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     : Jan 31, 2003
+**      LAST UPDATE     : Aug 27, 2003
 **      MODIFICATIONS   : 
 **************************************************************************/
 
 /*****************************
    $Log: database.h,v $
-   Revision 1.6  2003-02-19 09:54:47  arjen
+   Revision 1.9  2003-09-02 12:54:10  arjen
+   Overloaded gnucomo_database::Field() to include the Result
+   from a query as an argument.
+
+   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
    Print the query on cerr, along with the error message when
    the query results in an error.
 
 
 *****************************/
 
-/* static const char *RCSID = "$Id: database.h,v 1.6 2003-02-19 09:54:47 arjen Exp $"; */
+/* static const char *RCSID = "$Id: database.h,v 1.9 2003-09-02 12:54:10 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  : Jan 17, 2003
+//  LAST MODIFIED  : Aug 27, 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 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;
    }
 
+   //  The field value of a specific result.
+
+   String Field(pqxx::Result res, int tuple, const char *fieldname)
+   {
+      return String(res[tuple][fieldname].c_str());
+   }
+
+   //  Use the result of the last query by default
+
    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.