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.13 $
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 : Aug 17, 2003
25 **************************************************************************/
33 extern std::ostream *Log;
35 /*=========================================================================
36 ** NAME : gnucomo_database
37 ** SYNOPSIS : gnucomo_database(gnucomo_config &c);
39 ** RETURN VALUE : Database constructor. Establishes a connection with
40 ** the database server.
48 ** LAST MODIFIED : Sep 23, 2020
49 **=========================================================================
52 static int gdb_refcount = 0;
54 gnucomo_database::gnucomo_database(gnucomo_config *c)
62 dbconn = new pqxx::connection(cfg->Database());
64 if (!dbconn->is_open())
66 std::cerr << "Connection to database failed.\n";
70 // Create the transaction object
72 dbxact = new pqxx::work(*dbconn, "GnuCoMo");
76 catch (std::exception &e)
78 *Log << "Cannot setup the database transaction: " << e.what() << "\n";
82 gnucomo_database::gnucomo_database(const gnucomo_database &gdb)
89 void gnucomo_database::operator = (const gnucomo_database &gdb)
96 // A destructor must Commit the transaction and
97 // destroy the transaction before destroying the
99 // The connection can only be destroyed by the last
102 gnucomo_database::~gnucomo_database()
104 if (--gdb_refcount == 0 && dbconn != 0 && dbxact != 0)
114 /*=========================================================================
116 ** SYNOPSIS : String gnucomo_database::find_host(String hostname);
118 ** RETURN VALUE : Find a hostname in the 'object' table of the gnucomo database
119 ** and return its object id.
120 ** Return an empty string as objectid if the hostname is
129 ** LAST MODIFIED : Aug 15, 2003
130 **=========================================================================
133 String gnucomo_database::find_host(const String hostname)
136 String check_host("select objectid from object where ");
138 check_host += "objectname = '";
139 check_host += hostname;
142 if (Query(check_host) > 0)
144 objectid = Field(0, "objectid");
151 * Create a new notification with an action_user and return the notification id
154 String gnucomo_database::new_notification(String objectid, String issue, String remark)
164 *Log << "Creating notification for " << issue << ": " << remark << "\n";
166 qry = "select type_of_issueid, suggested_priority from type_of_issue where name='";
170 issueid = Field(0, "type_of_issueid");
171 insertion = "insert into notification (objectid, type_of_issueid, timestamp, ";
172 insertion += " statuscode, priority) values ('";
173 insertion += objectid + "', '";
174 insertion += issueid + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
175 insertion += Field(0, "suggested_priority") + "')";
179 Query("select currval('notification_notificationid_seq')");
180 notif_id = Field(0, "currval");
184 insertion = "insert into action_user (actionid, username, notificationid,";
185 insertion += " timestamp, statuscode, remarks) values ('1', 'gnucomo', '";
186 insertion += notif_id + "', '" + now.format("%Y-%m-%d %T") + "', 'new', '";
187 insertion += remark + "')";
193 std::cerr << "Error inserting notification.\n";
198 std::cerr << "DATABASE ERROR: Type of issue " << issue << " not found.\n";
204 database_entity::database_entity(database_entity *from, int row)
207 table = from->connected_table;
216 for (col = 0; col < res.columns(); col++)
218 fields[res.column_name(col)] = res[row][col].c_str();
222 int database_entity::find_one(String key)
224 String query("select * from ");
230 if ( db->Query(query) == 1)
239 for (col = 0; col < res.columns(); col++)
241 std::cerr << "Field[" << col << "] " << res.column_name(col);
242 std::cerr << " = " << res[0][col].c_str() << "\n";
244 fields[res.column_name(col)] = res[0][col].c_str();
252 int database_entity::find_many(String tab, String where)
254 connected_table = tab;
256 String query("select * from ");
262 return db->Query(query);