36d461c81185b1bb07ea3660f985293857fc4842
[gnucomo.git] / src / lib / database.cpp
1
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.10 $
12 **
13 **  DESCRIPTION      :  Implementation of the gnucomo database classes
14 **
15 **  EXPORTED OBJECTS : 
16 **  LOCAL    OBJECTS : 
17 **  MODULES  USED    :
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
24 **      MODIFICATIONS   : 
25 **************************************************************************/
26
27 /*****************************
28    $Log: database.cpp,v $
29    Revision 1.10  2003-12-03 08:23:17  arjen
30    Write messages to the log stream instead of cout.
31
32    Revision 1.9  2003/08/17 11:39:56  arjen
33    Changed the gnucomo_database class to the new PostgreSQL
34    library, libpqxx
35
36    Revision 1.8  2003/07/31 15:44:02  arjen
37    Removed debug output.
38
39    Revision 1.7  2003/02/19 12:07:55  arjen
40    Use the SQL function currval() to obtain the identification number
41    of the most recently created notification.
42
43    Revision 1.6  2003/02/05 09:33:42  arjen
44    gnucomo_database::new_notification() retruns the id number of the
45    newly created notification record.
46
47    Revision 1.5  2003/01/20 07:31:42  arjen
48    Removed some debug output.
49
50    Revision 1.4  2003/01/18 08:52:32  arjen
51    New C++ function: gnucomo_database::new_notification()
52
53    Revision 1.3  2002/11/09 08:04:27  arjen
54    Added a reference to the GPL
55
56    Revision 1.2  2002/11/04 10:13:36  arjen
57    Use proper namespace for iostream classes
58
59    Revision 1.1  2002/10/05 10:25:49  arjen
60    Creation of gcm_input and a first approach to a web interface
61
62 *****************************/
63
64 static const char *RCSID = "$Id: database.cpp,v 1.10 2003-12-03 08:23:17 arjen Exp $";
65
66 #include <AXE/date.h>
67
68 #include "database.h"
69
70 extern std::ostream *log;
71
72 /*=========================================================================
73 **  NAME           : gnucomo_database
74 **  SYNOPSIS       : gnucomo_database(gnucomo_config &c);
75 **  PARAMETERS     : 
76 **  RETURN VALUE   : Database constructor. Establishes a connection with
77 **                   the database server.
78 **
79 **  DESCRIPTION    : 
80 **
81 **  VARS USED      :
82 **  VARS CHANGED   :
83 **  FUNCTIONS USED :
84 **  SEE ALSO       :
85 **  LAST MODIFIED  : Aug 17, 2003
86 **=========================================================================
87 */
88
89 static int gdb_refcount = 0;
90
91 gnucomo_database::gnucomo_database(gnucomo_config *c)
92 {
93    cfg = c;
94
95    dbconn = new pqxx::Connection(cfg->Database());
96
97    if (!dbconn->is_open())
98    {
99       std::cerr << "Connection to database failed.\n";
100    }
101    else
102    {
103       // Create the transaction object
104
105       dbxact = new pqxx::Transaction(*dbconn, "GnuCoMo");
106       gdb_refcount++;
107    }
108 }
109
110 gnucomo_database::gnucomo_database(const gnucomo_database &gdb)
111 {
112    dbconn = gdb.dbconn;
113    dbxact = gdb.dbxact;
114    gdb_refcount++;
115 }
116
117 void gnucomo_database::operator = (const gnucomo_database &gdb)
118 {
119    dbconn = gdb.dbconn;
120    dbxact = gdb.dbxact;
121    gdb_refcount++;
122 }
123
124    //      A destructor must Commit the transaction and
125    //      destroy the transaction before destroying the
126    //      connection.
127    //      The connection can only be destroyed by the last
128    //      object alive.
129
130 gnucomo_database::~gnucomo_database()
131 {
132    if (--gdb_refcount == 0 && dbconn != 0 && dbxact != 0)
133    {
134       dbxact->Commit();
135       delete dbxact;
136       dbxact = 0;
137       delete dbconn;
138       dbconn = 0;
139    }
140 }
141
142 /*=========================================================================
143 **  NAME           : find_host
144 **  SYNOPSIS       : String gnucomo_database::find_host(String hostname);
145 **  PARAMETERS     : 
146 **  RETURN VALUE   : Find a hostname in the 'object' table of the gnucomo database
147 **                   and return its object id.
148 **                   Return an empty string as objectid if the hostname is
149 **                   not found.
150 **
151 **  DESCRIPTION    : 
152 **
153 **  VARS USED      :
154 **  VARS CHANGED   :
155 **  FUNCTIONS USED :
156 **  SEE ALSO       :
157 **  LAST MODIFIED  : Aug 15, 2003
158 **=========================================================================
159 */
160
161 String gnucomo_database::find_host(const String hostname)
162 {
163    String objectid("");
164    String check_host("select objectid from object where ");
165
166    check_host += "objectname = '";
167    check_host += hostname;
168    check_host += "'";
169
170    if (Query(check_host) > 0)
171    {
172       objectid = Field(0, "objectid");
173    }
174
175    return objectid;
176 }
177
178 /*
179  *  Create a new notification with an action_user and return the notification id
180  */
181
182 String gnucomo_database::new_notification(String objectid, String issue, String remark)
183 {
184    String qry;
185    UTC    now = Now();
186
187    String insertion;
188    String notif_id("");
189
190    String issueid("");
191
192    *log << "Creating notification for " << issue << ": " << remark << "\n";
193
194    qry = "select type_of_issueid, suggested_priority from type_of_issue where name='";
195    qry += issue + "'";
196    if (Query(qry) == 1)
197    {
198       issueid = Field(0, "type_of_issueid");
199       insertion = "insert into notification (objectid, type_of_issueid, timestamp, ";
200       insertion += "   statuscode, priority) values ('";
201       insertion += objectid + "', '";
202       insertion += issueid + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
203       insertion += Field(0, "suggested_priority") + "')";
204
205       Query(insertion);
206
207       Query("select currval('notification_notificationid_seq')");
208       notif_id = Field(0, "currval");
209
210       if (notif_id != "")
211       {
212          insertion = "insert into action_user (actionid, username, notificationid,";
213          insertion += "    timestamp, statuscode, remarks) values ('1', 'gnucomo', '";
214          insertion += notif_id + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
215          insertion += remark + "')";
216
217          Query(insertion);
218       }
219       else
220       {
221          std::cerr << "Error inserting notification.\n";
222       }
223    }
224    else
225    {
226       std::cerr << "DATABASE ERROR: Type of issue " << issue << " not found.\n";
227    }
228
229    return notif_id;
230 }
231