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"
82 ///////////////////////////////////////////////////////////////////////////
83 // NAME : gnucomo_database
87 // METHODS : is_connected - Return true if the database is connected
93 // LAST MODIFIED : Aug 27, 2003
94 ///////////////////////////////////////////////////////////////////////////
97 class gnucomo_database
100 pqxx::connection *dbconn;
101 //pqxx::transaction<pqxx::serializable> *dbxact;
104 pqxx::result last_result;
115 // Use the configuration to connect to the database
117 gnucomo_database(gnucomo_config *c);
119 // A copy constructor and the assignement can copy
120 // the connection to the database must create a
121 // new Transaction object.
123 gnucomo_database(const gnucomo_database &gdb);
124 void operator = (const gnucomo_database &gdb);
128 // Error checking and handling.
132 return dbconn != 0 && dbconn->is_open();
135 // Low-level database access functions
137 int Query(String qry)
141 last_result = dbxact->exec((char *)qry);
143 std::cerr << "Query " << qry << " returned "
144 << last_result.size() << " tuples.\n";
147 catch (const pqxx::pqxx_exception &e)
149 std::cerr << "Error in QUERY " << qry << ":\n";
150 std::cerr << e.base().what() << std::endl;
153 return last_result.size();
156 pqxx::result Result()
161 // The field value of a specific result.
163 String Field(pqxx::result res, int tuple, const char *fieldname)
165 return String(res[tuple][fieldname].c_str());
168 // Use the result of the last query by default
170 String Field(int tuple, const char *fieldname)
172 return String(last_result[tuple][fieldname].c_str());
175 // Return the objectid of the host given its name.
177 String find_host(const String hostname);
179 // Create a new notification.
181 String new_notification(String objectid, String issue, String remark);
185 ///////////////////////////////////////////////////////////////////////////
186 // NAME : database_entity
196 // LAST MODIFIED : Aug 19, 2005
197 ///////////////////////////////////////////////////////////////////////////
200 class database_entity
202 gnucomo_database *db;
204 String connected_table;
206 bool fresh; // Completely new, no tuple in the database
207 bool changed; // A database update is needed
208 bool deleted; // Tuple is to be deleted from the database
212 std::map<String,String> fields;
216 database_entity(gnucomo_database &gdb, const String tbl)
226 // contruct a new database entity from the result of another one
227 database_entity(database_entity *from, int row);
229 int find_one(String key);
230 int find_many(String tab, String where);