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.cpp
10 ** SYSTEM NAME : Gnucomo - Gnu Computer Monitoring
11 ** VERSION NUMBER : $Revision: 1.13 $
13 ** DESCRIPTION : Implementation of the gnucomo database classes
18 ***************************************************************************
19 ** ADMINISTRATIVE INFORMATION *
20 ********************************
21 ** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
22 ** CREATION DATE : Sep 10, 2002
23 ** LAST UPDATE : Aug 17, 2003
25 **************************************************************************/
27 /*****************************
28 $Log: database.cpp,v $
29 Revision 1.13 2011-03-24 10:21:47 arjen
30 Adjusted to new version of libpqxx.
32 Revision 1.12 2003/12/22 10:28:26 arjen
33 Catch an exception if we can not setup a database transaction.
35 Revision 1.11 2003/12/04 10:40:28 arjen
36 Fixed name conflict with 'double log(double)'
38 Revision 1.10 2003/12/03 08:23:17 arjen
39 Write messages to the log stream instead of cout.
41 Revision 1.9 2003/08/17 11:39:56 arjen
42 Changed the gnucomo_database class to the new PostgreSQL
45 Revision 1.8 2003/07/31 15:44:02 arjen
48 Revision 1.7 2003/02/19 12:07:55 arjen
49 Use the SQL function currval() to obtain the identification number
50 of the most recently created notification.
52 Revision 1.6 2003/02/05 09:33:42 arjen
53 gnucomo_database::new_notification() retruns the id number of the
54 newly created notification record.
56 Revision 1.5 2003/01/20 07:31:42 arjen
57 Removed some debug output.
59 Revision 1.4 2003/01/18 08:52:32 arjen
60 New C++ function: gnucomo_database::new_notification()
62 Revision 1.3 2002/11/09 08:04:27 arjen
63 Added a reference to the GPL
65 Revision 1.2 2002/11/04 10:13:36 arjen
66 Use proper namespace for iostream classes
68 Revision 1.1 2002/10/05 10:25:49 arjen
69 Creation of gcm_input and a first approach to a web interface
71 *****************************/
79 extern std::ostream *Log;
81 /*=========================================================================
82 ** NAME : gnucomo_database
83 ** SYNOPSIS : gnucomo_database(gnucomo_config &c);
85 ** RETURN VALUE : Database constructor. Establishes a connection with
86 ** the database server.
94 ** LAST MODIFIED : Sep 23, 2020
95 **=========================================================================
98 static int gdb_refcount = 0;
100 gnucomo_database::gnucomo_database(gnucomo_config *c)
108 dbconn = new pqxx::connection(cfg->Database());
110 if (!dbconn->is_open())
112 std::cerr << "Connection to database failed.\n";
116 // Create the transaction object
118 //dbxact = new pqxx::transaction<pqxx::serializable>(*dbconn, "GnuCoMo");
119 dbxact = new pqxx::work(*dbconn, "GnuCoMo");
123 catch (std::exception &e)
125 *Log << "Cannot setup the database transaction: " << e.what() << "\n";
129 gnucomo_database::gnucomo_database(const gnucomo_database &gdb)
136 void gnucomo_database::operator = (const gnucomo_database &gdb)
143 // A destructor must Commit the transaction and
144 // destroy the transaction before destroying the
146 // The connection can only be destroyed by the last
149 gnucomo_database::~gnucomo_database()
151 if (--gdb_refcount == 0 && dbconn != 0 && dbxact != 0)
161 /*=========================================================================
163 ** SYNOPSIS : String gnucomo_database::find_host(String hostname);
165 ** RETURN VALUE : Find a hostname in the 'object' table of the gnucomo database
166 ** and return its object id.
167 ** Return an empty string as objectid if the hostname is
176 ** LAST MODIFIED : Aug 15, 2003
177 **=========================================================================
180 String gnucomo_database::find_host(const String hostname)
183 String check_host("select objectid from object where ");
185 check_host += "objectname = '";
186 check_host += hostname;
189 if (Query(check_host) > 0)
191 objectid = Field(0, "objectid");
198 * Create a new notification with an action_user and return the notification id
201 String gnucomo_database::new_notification(String objectid, String issue, String remark)
211 *Log << "Creating notification for " << issue << ": " << remark << "\n";
213 qry = "select type_of_issueid, suggested_priority from type_of_issue where name='";
217 issueid = Field(0, "type_of_issueid");
218 insertion = "insert into notification (objectid, type_of_issueid, timestamp, ";
219 insertion += " statuscode, priority) values ('";
220 insertion += objectid + "', '";
221 insertion += issueid + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
222 insertion += Field(0, "suggested_priority") + "')";
226 Query("select currval('notification_notificationid_seq')");
227 notif_id = Field(0, "currval");
231 insertion = "insert into action_user (actionid, username, notificationid,";
232 insertion += " timestamp, statuscode, remarks) values ('1', 'gnucomo', '";
233 insertion += notif_id + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
234 insertion += remark + "')";
240 std::cerr << "Error inserting notification.\n";
245 std::cerr << "DATABASE ERROR: Type of issue " << issue << " not found.\n";