2 /**************************************************************************
3 ** (c) Copyright 2002, Andromeda Technology & Automation
4 ** This is free software; you can redistribute it and/or modify it under the
5 ** terms of the GNU General Public License, see the file COPYING.
6 ***************************************************************************
7 ** MODULE INFORMATION *
8 ***********************
9 ** FILE NAME : database.h
11 ** VERSION NUMBER : $Revision: 1.11 $
13 ** DESCRIPTION : Classes to provide an abstract layer on the Gnucomo
19 ***************************************************************************
20 ** ADMINISTRATIVE INFORMATION *
21 ********************************
22 ** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
23 ** CREATION DATE : Sep 10, 2002
24 ** LAST UPDATE : Aug 19, 2005
26 **************************************************************************/
28 /*****************************
30 Revision 1.11 2007-01-11 13:50:10 arjen
31 Experimental start of database OO abstraction layer.
33 Revision 1.10 2003/12/04 10:39:36 arjen
36 Revision 1.9 2003/09/02 12:54:10 arjen
37 Overloaded gnucomo_database::Field() to include the Result
38 from a query as an argument.
40 Revision 1.8 2003/08/17 11:39:33 arjen
41 Changed the gnucomo_database class to the new PostgreSQL
44 Revision 1.7 2003/03/29 08:13:53 arjen
45 New member function gnucomo_database::is_conected().
47 Revision 1.6 2003/02/19 09:54:47 arjen
48 Print the query on cerr, along with the error message when
49 the query results in an error.
51 Revision 1.5 2003/02/05 09:33:17 arjen
52 gnucomo_database::new_notification() retruns the id number of the
53 newly created notification record.
55 Revision 1.4 2003/01/18 08:52:18 arjen
56 New C++ function: gnucomo_database::new_notification()
58 Revision 1.3 2002/11/09 08:04:27 arjen
59 Added a reference to the GPL
61 Revision 1.2 2002/11/04 10:13:36 arjen
62 Use proper namespace for iostream classes
64 Revision 1.1 2002/10/05 10:25:49 arjen
65 Creation of gcm_input and a first approach to a web interface
67 *****************************/
69 /* static const char *RCSID = "$Id: database.h,v 1.11 2007-01-11 13:50:10 arjen Exp $"; */
71 #include <pqxx/connection.h>
72 #include <pqxx/transaction.h>
73 #include <pqxx/result.h>
74 #include "gnucomo_config.h"
77 ///////////////////////////////////////////////////////////////////////////
78 // NAME : gnucomo_database
82 // METHODS : is_connected - Return true if the database is connected
88 // LAST MODIFIED : Aug 27, 2003
89 ///////////////////////////////////////////////////////////////////////////
92 class gnucomo_database
95 pqxx::Connection *dbconn;
96 pqxx::Transaction *dbxact;
98 pqxx::Result last_result;
109 // Use the configuration to connect to the database
111 gnucomo_database(gnucomo_config *c);
113 // A copy constructor and the assignement can copy
114 // the connection to the database must create a
115 // new Transaction object.
117 gnucomo_database(const gnucomo_database &gdb);
118 void operator = (const gnucomo_database &gdb);
122 // Error checking and handling.
126 return dbconn != 0 && dbconn->is_open();
129 // Low-level database access functions
131 int Query(String qry)
134 last_result = dbxact->Exec(qry);
136 std::cerr << "Query " << qry << " returned "
137 << last_result.size() << " tuples.\n";
139 return last_result.size();
142 pqxx::Result Result()
147 // The field value of a specific result.
149 String Field(pqxx::Result res, int tuple, const char *fieldname)
151 return String(res[tuple][fieldname].c_str());
154 // Use the result of the last query by default
156 String Field(int tuple, const char *fieldname)
158 return String(last_result[tuple][fieldname].c_str());
161 // Return the objectid of the host given its name.
163 String find_host(const String hostname);
165 // Create a new notification.
167 String new_notification(String objectid, String issue, String remark);
171 ///////////////////////////////////////////////////////////////////////////
172 // NAME : database_entity
182 // LAST MODIFIED : Aug 19, 2005
183 ///////////////////////////////////////////////////////////////////////////
186 class database_entity
188 const gnucomo_database *db;
191 bool fresh; // Completely new, no tuple in the database
192 bool changed; // A database update is needed
193 bool deleted; // Tuple is to be deleted from the database
197 database_entity(const gnucomo_database &gdb, const String tbl)