Added database abstractions
[gnucomo.git] / src / include / database.h
index 051adb8..4532bfb 100644 (file)
@@ -8,7 +8,7 @@
 ***********************
 **      FILE NAME      : database.h
 **      SYSTEM NAME    : 
-**      VERSION NUMBER : $Revision: 1.8 $
+**      VERSION NUMBER : $Revision: 1.12 $
 **
 **  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     : Aug 17, 2003
+**      LAST UPDATE     : Aug 19, 2005
 **      MODIFICATIONS   : 
 **************************************************************************/
 
 /*****************************
    $Log: database.h,v $
-   Revision 1.8  2003-08-17 11:39:33  arjen
+   Revision 1.12  2011-03-24 10:21:43  arjen
+   Adjusted to new version of libpqxx.
+
+   Revision 1.11  2007/01/11 13:50:10  arjen
+   Experimental start of database OO abstraction layer.
+
+   Revision 1.10  2003/12/04 10:39:36  arjen
+   Fixed libpqxx headers
+
+   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
 
 
 *****************************/
 
-/* static const char *RCSID = "$Id: database.h,v 1.8 2003-08-17 11:39:33 arjen Exp $"; */
+/* static const char *RCSID = "$Id: database.h,v 1.12 2011-03-24 10:21:43 arjen Exp $"; */
 
-#include <pqxx/transaction.h>
+#include <pqxx/connection>
+#include <pqxx/transaction>
+#include <pqxx/result>
 #include "gnucomo_config.h"
 
+#define DEBUG
+
 /*
 ///////////////////////////////////////////////////////////////////////////
 //  NAME           : gnucomo_database
 //
 //  RELATIONS      :
 //  SEE ALSO       :
-//  LAST MODIFIED  : Aug 17, 2003
+//  LAST MODIFIED  : Aug 27, 2003
 ///////////////////////////////////////////////////////////////////////////
 */
 
 class gnucomo_database
 {
    gnucomo_config    *cfg;
-   pqxx::Connection  *dbconn;
-   pqxx::Transaction *dbxact;
+   pqxx::connection  *dbconn;
+   //pqxx::transaction<pqxx::serializable> *dbxact;
+   pqxx::work *dbxact;
 
-   pqxx::Result      last_result;
+   pqxx::result      last_result;
 
 public:
 
@@ -118,20 +136,37 @@ public:
 
    int Query(String qry)
    {
-
-      last_result = dbxact->Exec(qry);
+      try
+      {
+         last_result = dbxact->exec((char *)qry);
 #ifdef DEBUG
       std::cerr << "Query " << qry << " returned "
                 << last_result.size() << " tuples.\n";
 #endif
+      }
+      catch (const pqxx::pqxx_exception &e)
+      {
+         std::cerr << "Error in QUERY " << qry << ":\n";
+         std::cerr << e.base().what() << std::endl;
+      }
+
       return last_result.size();
    }
 
-   pqxx::Result Result()
+   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(last_result[tuple][fieldname].c_str());
@@ -146,3 +181,52 @@ public:
    String new_notification(String objectid, String issue, String remark);
 };
 
+/*
+///////////////////////////////////////////////////////////////////////////
+//  NAME           : database_entity
+//  BASECLASS      : 
+//  MEMBERS        :
+//  OPERATORS      :
+//  METHODS        : 
+//
+//  DESCRIPTION    : 
+//
+//  RELATIONS      :
+//  SEE ALSO       :
+//  LAST MODIFIED  : Aug 19, 2005
+///////////////////////////////////////////////////////////////////////////
+*/
+
+class database_entity
+{
+   gnucomo_database  *db;
+   String    table;
+   String    connected_table;
+
+   bool fresh;       // Completely new, no tuple in the database
+   bool changed;     // A database update is needed
+   bool deleted;     // Tuple is to be deleted from the database
+
+protected:
+
+   std::map<String,String> fields;
+
+public:
+
+   database_entity(gnucomo_database &gdb, const String tbl)
+   {
+      db = &gdb;
+      table = tbl;
+
+      fresh = true;
+      changed = false;
+      deleted = false;
+   }
+
+   // contruct a new database entity from the result of another one
+   database_entity(database_entity *from, int row);
+
+   int find_one(String key);
+   int find_many(String tab, String where);
+
+};