Use the SQL function currval() to obtain the identification number
[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.7 $
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     : Jan 31, 2003
24 **      MODIFICATIONS   : 
25 **************************************************************************/
26
27 /*****************************
28    $Log: database.cpp,v $
29    Revision 1.7  2003-02-19 12:07:55  arjen
30    Use the SQL function currval() to obtain the identification number
31    of the most recently created notification.
32
33    Revision 1.6  2003/02/05 09:33:42  arjen
34    gnucomo_database::new_notification() retruns the id number of the
35    newly created notification record.
36
37    Revision 1.5  2003/01/20 07:31:42  arjen
38    Removed some debug output.
39
40    Revision 1.4  2003/01/18 08:52:32  arjen
41    New C++ function: gnucomo_database::new_notification()
42
43    Revision 1.3  2002/11/09 08:04:27  arjen
44    Added a reference to the GPL
45
46    Revision 1.2  2002/11/04 10:13:36  arjen
47    Use proper namespace for iostream classes
48
49    Revision 1.1  2002/10/05 10:25:49  arjen
50    Creation of gcm_input and a first approach to a web interface
51
52 *****************************/
53
54 static const char *RCSID = "$Id: database.cpp,v 1.7 2003-02-19 12:07:55 arjen Exp $";
55
56 #include <AXE/date.h>
57
58 #include "database.h"
59
60 extern bool verbose;   /*  Defined in the main application */
61
62 /*=========================================================================
63 **  NAME           : gnucomo_database
64 **  SYNOPSIS       : gnucomo_database(gnucomo_config &c);
65 **  PARAMETERS     : 
66 **  RETURN VALUE   : Database constructor. Establishes a connection with
67 **                   the database server.
68 **
69 **  DESCRIPTION    : 
70 **
71 **  VARS USED      :
72 **  VARS CHANGED   :
73 **  FUNCTIONS USED :
74 **  SEE ALSO       :
75 **  LAST MODIFIED  : Sep 26, 2002
76 **=========================================================================
77 */
78
79 gnucomo_database::gnucomo_database(gnucomo_config *c)
80 {
81    cfg = c;
82
83    if (verbose)
84    {
85       std::cout <<  "Database connection string = " << cfg->Database() << "\n";
86    }
87
88    db = new PgDatabase(cfg->Database());
89
90    if (db->ConnectionBad())
91    {
92       std::cerr << "Can not connect to database: " << db->ErrorMessage();
93    }
94 }
95
96 /*=========================================================================
97 **  NAME           : find_host
98 **  SYNOPSIS       : String gnucomo_database::find_host(String hostname);
99 **  PARAMETERS     : 
100 **  RETURN VALUE   : Find a hostname in the 'object' table of the gnucomo database
101 **                   and return its object id.
102 **                   Return an empty string as objectid if the hostname is
103 **                   not found.
104 **
105 **  DESCRIPTION    : 
106 **
107 **  VARS USED      :
108 **  VARS CHANGED   :
109 **  FUNCTIONS USED :
110 **  SEE ALSO       :
111 **  LAST MODIFIED  : Sep 16, 2002
112 **=========================================================================
113 */
114
115 String gnucomo_database::find_host(const String hostname)
116 {
117    String objectid("");
118    String check_host("select objectid from object where ");
119
120    check_host += "objectname = '";
121    check_host += hostname;
122    check_host += "'";
123
124    if (Query(check_host) > 0)
125    {
126       objectid = String(db->GetValue(0, "objectid"));
127    }
128
129    return objectid;
130 }
131
132 /*
133  *  Create a new notification with an action_user and return the notification id
134  */
135
136 String gnucomo_database::new_notification(String objectid, String issue, String remark)
137 {
138    String qry;
139    UTC    now = Now();
140
141    String insertion;
142    String notif_id("");
143
144    String issueid("");
145
146    std::cout << "Creating notification for " << issue << ": " << remark << "\n";
147
148    qry = "select type_of_issueid, suggested_priority from type_of_issue where name='";
149    qry += issue + "'";
150    if (Query(qry) == 1)
151    {
152       issueid = Field(0, "type_of_issueid");
153       insertion = "insert into notification (objectid, type_of_issueid, timestamp, ";
154       insertion += "   statuscode, priority) values ('";
155       insertion += objectid + "', '";
156       insertion += issueid + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
157       insertion += Field(0, "suggested_priority") + "')";
158
159       Query(insertion);
160
161       Query("select currval('notification_notificationid_seq')");
162       notif_id = Field(0, "currval");
163
164       if (notif_id != "")
165       {
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 + "')";
170
171          Query(insertion);
172       }
173       else
174       {
175          std::cerr << "Error inserting notification.\n";
176       }
177    }
178    else
179    {
180       std::cerr << "DATABASE ERROR: Type of issue " << issue << " not found.\n";
181    }
182
183    return notif_id;
184 }
185