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