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