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.10 $
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 27, 2003
26 **************************************************************************/
28 /*****************************
30 Revision 1.10 2003-12-04 10:39:36 arjen
33 Revision 1.9 2003/09/02 12:54:10 arjen
34 Overloaded gnucomo_database::Field() to include the Result
35 from a query as an argument.
37 Revision 1.8 2003/08/17 11:39:33 arjen
38 Changed the gnucomo_database class to the new PostgreSQL
41 Revision 1.7 2003/03/29 08:13:53 arjen
42 New member function gnucomo_database::is_conected().
44 Revision 1.6 2003/02/19 09:54:47 arjen
45 Print the query on cerr, along with the error message when
46 the query results in an error.
48 Revision 1.5 2003/02/05 09:33:17 arjen
49 gnucomo_database::new_notification() retruns the id number of the
50 newly created notification record.
52 Revision 1.4 2003/01/18 08:52:18 arjen
53 New C++ function: gnucomo_database::new_notification()
55 Revision 1.3 2002/11/09 08:04:27 arjen
56 Added a reference to the GPL
58 Revision 1.2 2002/11/04 10:13:36 arjen
59 Use proper namespace for iostream classes
61 Revision 1.1 2002/10/05 10:25:49 arjen
62 Creation of gcm_input and a first approach to a web interface
64 *****************************/
66 /* static const char *RCSID = "$Id: database.h,v 1.10 2003-12-04 10:39:36 arjen Exp $"; */
68 #include <pqxx/connection.h>
69 #include <pqxx/transaction.h>
70 #include <pqxx/result.h>
71 #include "gnucomo_config.h"
74 ///////////////////////////////////////////////////////////////////////////
75 // NAME : gnucomo_database
79 // METHODS : is_connected - Return true if the database is connected
85 // LAST MODIFIED : Aug 27, 2003
86 ///////////////////////////////////////////////////////////////////////////
89 class gnucomo_database
92 pqxx::Connection *dbconn;
93 pqxx::Transaction *dbxact;
95 pqxx::Result last_result;
106 // Use the configuration to connect to the database
108 gnucomo_database(gnucomo_config *c);
110 // A copy constructor and the assignement can copy
111 // the connection to the database must create a
112 // new Transaction object.
114 gnucomo_database(const gnucomo_database &gdb);
115 void operator = (const gnucomo_database &gdb);
119 // Error checking and handling.
123 return dbconn != 0 && dbconn->is_open();
126 // Low-level database access functions
128 int Query(String qry)
131 last_result = dbxact->Exec(qry);
133 std::cerr << "Query " << qry << " returned "
134 << last_result.size() << " tuples.\n";
136 return last_result.size();
139 pqxx::Result Result()
144 // The field value of a specific result.
146 String Field(pqxx::Result res, int tuple, const char *fieldname)
148 return String(res[tuple][fieldname].c_str());
151 // Use the result of the last query by default
153 String Field(int tuple, const char *fieldname)
155 return String(last_result[tuple][fieldname].c_str());
158 // Return the objectid of the host given its name.
160 String find_host(const String hostname);
162 // Create a new notification.
164 String new_notification(String objectid, String issue, String remark);