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.12 $
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.12 2011-03-24 10:21:43 arjen
31 Adjusted to new version of libpqxx.
33 Revision 1.11 2007/01/11 13:50:10 arjen
34 Experimental start of database OO abstraction layer.
36 Revision 1.10 2003/12/04 10:39:36 arjen
39 Revision 1.9 2003/09/02 12:54:10 arjen
40 Overloaded gnucomo_database::Field() to include the Result
41 from a query as an argument.
43 Revision 1.8 2003/08/17 11:39:33 arjen
44 Changed the gnucomo_database class to the new PostgreSQL
47 Revision 1.7 2003/03/29 08:13:53 arjen
48 New member function gnucomo_database::is_conected().
50 Revision 1.6 2003/02/19 09:54:47 arjen
51 Print the query on cerr, along with the error message when
52 the query results in an error.
54 Revision 1.5 2003/02/05 09:33:17 arjen
55 gnucomo_database::new_notification() retruns the id number of the
56 newly created notification record.
58 Revision 1.4 2003/01/18 08:52:18 arjen
59 New C++ function: gnucomo_database::new_notification()
61 Revision 1.3 2002/11/09 08:04:27 arjen
62 Added a reference to the GPL
64 Revision 1.2 2002/11/04 10:13:36 arjen
65 Use proper namespace for iostream classes
67 Revision 1.1 2002/10/05 10:25:49 arjen
68 Creation of gcm_input and a first approach to a web interface
70 *****************************/
72 /* static const char *RCSID = "$Id: database.h,v 1.12 2011-03-24 10:21:43 arjen Exp $"; */
74 #include <pqxx/connection>
75 #include <pqxx/transaction>
76 #include <pqxx/result>
77 #include "gnucomo_config.h"
80 ///////////////////////////////////////////////////////////////////////////
81 // NAME : gnucomo_database
85 // METHODS : is_connected - Return true if the database is connected
91 // LAST MODIFIED : Aug 27, 2003
92 ///////////////////////////////////////////////////////////////////////////
95 class gnucomo_database
98 pqxx::connection *dbconn;
99 //pqxx::transaction<pqxx::serializable> *dbxact;
102 pqxx::result last_result;
113 // Use the configuration to connect to the database
115 gnucomo_database(gnucomo_config *c);
117 // A copy constructor and the assignement can copy
118 // the connection to the database must create a
119 // new Transaction object.
121 gnucomo_database(const gnucomo_database &gdb);
122 void operator = (const gnucomo_database &gdb);
126 // Error checking and handling.
130 return dbconn != 0 && dbconn->is_open();
133 // Low-level database access functions
135 int Query(String qry)
139 last_result = dbxact->exec((char *)qry);
141 std::cerr << "Query " << qry << " returned "
142 << last_result.size() << " tuples.\n";
145 catch (const pqxx::pqxx_exception &e)
147 std::cerr << "Error in QUERY " << qry << ":\n";
148 std::cerr << e.base().what() << std::endl;
151 return last_result.size();
154 pqxx::result Result()
159 // The field value of a specific result.
161 String Field(pqxx::result res, int tuple, const char *fieldname)
163 return String(res[tuple][fieldname].c_str());
166 // Use the result of the last query by default
168 String Field(int tuple, const char *fieldname)
170 return String(last_result[tuple][fieldname].c_str());
173 // Return the objectid of the host given its name.
175 String find_host(const String hostname);
177 // Create a new notification.
179 String new_notification(String objectid, String issue, String remark);
183 ///////////////////////////////////////////////////////////////////////////
184 // NAME : database_entity
194 // LAST MODIFIED : Aug 19, 2005
195 ///////////////////////////////////////////////////////////////////////////
198 class database_entity
200 const gnucomo_database *db;
203 bool fresh; // Completely new, no tuple in the database
204 bool changed; // A database update is needed
205 bool deleted; // Tuple is to be deleted from the database
209 database_entity(const gnucomo_database &gdb, const String tbl)