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.6 $
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 : Jan 31, 2003
25 **************************************************************************/
27 /*****************************
28 $Log: database.cpp,v $
29 Revision 1.6 2003-02-05 09:33:42 arjen
30 gnucomo_database::new_notification() retruns the id number of the
31 newly created notification record.
33 Revision 1.5 2003/01/20 07:31:42 arjen
34 Removed some debug output.
36 Revision 1.4 2003/01/18 08:52:32 arjen
37 New C++ function: gnucomo_database::new_notification()
39 Revision 1.3 2002/11/09 08:04:27 arjen
40 Added a reference to the GPL
42 Revision 1.2 2002/11/04 10:13:36 arjen
43 Use proper namespace for iostream classes
45 Revision 1.1 2002/10/05 10:25:49 arjen
46 Creation of gcm_input and a first approach to a web interface
48 *****************************/
50 static const char *RCSID = "$Id: database.cpp,v 1.6 2003-02-05 09:33:42 arjen Exp $";
56 extern bool verbose; /* Defined in the main application */
58 /*=========================================================================
59 ** NAME : gnucomo_database
60 ** SYNOPSIS : gnucomo_database(gnucomo_config &c);
62 ** RETURN VALUE : Database constructor. Establishes a connection with
63 ** the database server.
71 ** LAST MODIFIED : Sep 26, 2002
72 **=========================================================================
75 gnucomo_database::gnucomo_database(gnucomo_config *c)
81 std::cout << "Database connection string = " << cfg->Database() << "\n";
84 db = new PgDatabase(cfg->Database());
86 if (db->ConnectionBad())
88 std::cerr << "Can not connect to database: " << db->ErrorMessage();
92 /*=========================================================================
94 ** SYNOPSIS : String gnucomo_database::find_host(String hostname);
96 ** RETURN VALUE : Find a hostname in the 'object' table of the gnucomo database
97 ** and return its object id.
98 ** Return an empty string as objectid if the hostname is
107 ** LAST MODIFIED : Sep 16, 2002
108 **=========================================================================
111 String gnucomo_database::find_host(const String hostname)
114 String check_host("select objectid from object where ");
116 check_host += "objectname = '";
117 check_host += hostname;
120 if (Query(check_host) > 0)
122 objectid = String(db->GetValue(0, "objectid"));
129 * Create a new notification with an action_user and return the notification id
132 String gnucomo_database::new_notification(String objectid, String issue, String remark)
142 std::cout << "Creating notification for " << issue << ": " << remark << "\n";
144 qry = "select type_of_issueid, suggested_priority from type_of_issue where name='";
148 issueid = Field(0, "type_of_issueid");
149 insertion = "insert into notification (objectid, type_of_issueid, timestamp, ";
150 insertion += " statuscode, priority) values ('";
151 insertion += objectid + "', '";
152 insertion += issueid + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
153 insertion += Field(0, "suggested_priority") + "')";
155 qry = "select notificationid from notification where objectid='";
156 qry += objectid + "' and type_of_issueid = '";
157 qry += issueid + "' order by notificationid";
160 int tuples = Query(qry);
164 notif_id = Field(tuples - 1, "notificationid");
166 insertion = "insert into action_user (actionid, username, notificationid,";
167 insertion += " timestamp, statuscode, remarks) values ('1', 'gnucomo', '";
168 insertion += notif_id + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
169 insertion += remark + "')";
175 std::cerr << "Error inserting notification.\n";