Added database abstractions
[gnucomo.git] / src / lib / database.cpp
index 63384a8..3a46852 100644 (file)
 **      MODIFICATIONS   : 
 **************************************************************************/
 
-/*****************************
-   $Log: database.cpp,v $
-   Revision 1.13  2011-03-24 10:21:47  arjen
-   Adjusted to new version of libpqxx.
-
-   Revision 1.12  2003/12/22 10:28:26  arjen
-   Catch an exception if we can not setup a database transaction.
-
-   Revision 1.11  2003/12/04 10:40:28  arjen
-   Fixed name conflict with 'double log(double)'
-
-   Revision 1.10  2003/12/03 08:23:17  arjen
-   Write messages to the log stream instead of cout.
-
-   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
-   Use the SQL function currval() to obtain the identification number
-   of the most recently created notification.
-
-   Revision 1.6  2003/02/05 09:33:42  arjen
-   gnucomo_database::new_notification() retruns the id number of the
-   newly created notification record.
-
-   Revision 1.5  2003/01/20 07:31:42  arjen
-   Removed some debug output.
-
-   Revision 1.4  2003/01/18 08:52:32  arjen
-   New C++ function: gnucomo_database::new_notification()
-
-   Revision 1.3  2002/11/09 08:04:27  arjen
-   Added a reference to the GPL
-
-   Revision 1.2  2002/11/04 10:13:36  arjen
-   Use proper namespace for iostream classes
-
-   Revision 1.1  2002/10/05 10:25:49  arjen
-   Creation of gcm_input and a first approach to a web interface
-
-*****************************/
-
 #include <date.h>
 
 //#define DEBUG
@@ -115,7 +69,6 @@ gnucomo_database::gnucomo_database(gnucomo_config *c)
       {
          // Create the transaction object
 
-         //dbxact = new pqxx::transaction<pqxx::serializable>(*dbconn, "GnuCoMo");
          dbxact = new pqxx::work(*dbconn, "GnuCoMo");
          gdb_refcount++;
       }
@@ -248,3 +201,65 @@ String gnucomo_database::new_notification(String objectid, String issue, String
    return notif_id;
 }
 
+database_entity::database_entity(database_entity *from, int row)
+{
+   db = from->db;
+   table = from->connected_table;
+
+   int          col;
+   pqxx::result res;
+
+   res = db->Result();
+
+   fresh = false;
+
+   for  (col = 0; col < res.columns(); col++)
+   {
+      fields[res.column_name(col)] = res[row][col].c_str();
+   }
+}
+
+int database_entity::find_one(String key)
+{
+   String query("select * from ");
+
+   query += table;
+   query += " where ";
+   query += key;
+
+   if ( db->Query(query) == 1)
+   {
+      int          col;
+      pqxx::result res;
+
+      res = db->Result();
+
+      fresh = false;
+
+      for  (col = 0; col < res.columns(); col++)
+      {
+         std::cerr << "Field[" << col << "] " << res.column_name(col);
+         std::cerr << "  = " << res[0][col].c_str() << "\n";
+
+         fields[res.column_name(col)] = res[0][col].c_str();
+      }
+
+      return 1;
+   }
+   return 0;
+}
+
+int database_entity::find_many(String tab, String where)
+{
+   connected_table = tab;
+
+   String query("select * from ");
+
+   query += tab;
+   query += " where ";
+   query += where;
+
+   return db->Query(query);
+
+}
+