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.4 $
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 17, 2003
25 **************************************************************************/
27 /*****************************
28 $Log: database.cpp,v $
29 Revision 1.4 2003-01-18 08:52:32 arjen
30 New C++ function: gnucomo_database::new_notification()
32 Revision 1.3 2002/11/09 08:04:27 arjen
33 Added a reference to the GPL
35 Revision 1.2 2002/11/04 10:13:36 arjen
36 Use proper namespace for iostream classes
38 Revision 1.1 2002/10/05 10:25:49 arjen
39 Creation of gcm_input and a first approach to a web interface
41 *****************************/
43 static const char *RCSID = "$Id: database.cpp,v 1.4 2003-01-18 08:52:32 arjen Exp $";
49 extern bool verbose; /* Defined in the main application */
51 /*=========================================================================
52 ** NAME : gnucomo_database
53 ** SYNOPSIS : gnucomo_database(gnucomo_config &c);
55 ** RETURN VALUE : Database constructor. Establishes a connection with
56 ** the database server.
64 ** LAST MODIFIED : Sep 26, 2002
65 **=========================================================================
68 gnucomo_database::gnucomo_database(gnucomo_config *c)
74 std::cout << "Database connection string = " << cfg->Database() << "\n";
77 db = new PgDatabase(cfg->Database());
79 if (db->ConnectionBad())
81 std::cerr << "Can not connect to database: " << db->ErrorMessage();
85 /*=========================================================================
87 ** SYNOPSIS : String gnucomo_database::find_host(String hostname);
89 ** RETURN VALUE : Find a hostname in the 'object' table of the gnucomo database
90 ** and return its object id.
91 ** Return an empty string as objectid if the hostname is
100 ** LAST MODIFIED : Sep 16, 2002
101 **=========================================================================
104 String gnucomo_database::find_host(const String hostname)
107 String check_host("select objectid from object where ");
109 check_host += "objectname = '";
110 check_host += hostname;
113 if (Query(check_host) > 0)
115 objectid = String(db->GetValue(0, "objectid"));
121 void gnucomo_database::new_notification(String objectid, String issue, String remark)
131 std::cout << "Creating notification for " << issue << "\n";
133 qry = "select type_of_issueid, suggested_priority from type_of_issue where name='";
137 issueid = Field(0, "type_of_issueid");
138 insertion = "insert into notification (objectid, type_of_issueid, timestamp, ";
139 insertion += " statuscode, priority) values ('";
140 insertion += objectid + "', '";
141 insertion += issueid + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
142 insertion += Field(0, "suggested_priority") + "')";
144 std::cout << insertion << "\n";
146 qry = "select notificationid from notification where objectid='";
147 qry += objectid + "' and type_of_issueid = '";
148 qry += issueid + "' order by notificationid";
151 int tuples = Query(qry);
155 notif_id = Field(tuples - 1, "notificationid");
157 insertion = "insert into action_user (actionid, username, notificationid,";
158 insertion += " timestamp, statuscode, remarks) values ('1', 'gnucomo', '";
159 insertion += notif_id + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
160 insertion += remark + "')";
162 std::cout << insertion << "\n";
167 std::cerr << "Error inserting notification.\n";