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