134a53602b4bc3fc18d81466754ae98cbc6b4391
[gnucomo.git] / src / lib / database.cpp
1
2 /**************************************************************************
3 **  (c) Copyright 2002, Andromeda Technology & Automation
4 ***************************************************************************
5 ** MODULE INFORMATION *
6 ***********************
7 **      FILE NAME      : database.cpp
8 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
9 **      VERSION NUMBER : $Revision: 1.1 $
10 **
11 **  DESCRIPTION      :  Implementation of the gnucomo database classes
12 **
13 **  EXPORTED OBJECTS : 
14 **  LOCAL    OBJECTS : 
15 **  MODULES  USED    :
16 ***************************************************************************
17 **  ADMINISTRATIVE INFORMATION *
18 ********************************
19 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
20 **      CREATION DATE   : Sep 10, 2002
21 **      LAST UPDATE     : Sep 26, 2002
22 **      MODIFICATIONS   : 
23 **************************************************************************/
24
25 /*****************************
26    $Log: database.cpp,v $
27    Revision 1.1  2002-10-05 10:25:49  arjen
28    Creation of gcm_input and a first approach to a web interface
29
30 *****************************/
31
32 static const char *RCSID = "$Id: database.cpp,v 1.1 2002-10-05 10:25:49 arjen Exp $";
33
34 #include "database.h"
35
36 extern bool verbose;   /*  Defined in the main application */
37
38 /*=========================================================================
39 **  NAME           : gnucomo_database
40 **  SYNOPSIS       : gnucomo_database(gnucomo_config &c);
41 **  PARAMETERS     : 
42 **  RETURN VALUE   : Database constructor. Establishes a connection with
43 **                   the database server.
44 **
45 **  DESCRIPTION    : 
46 **
47 **  VARS USED      :
48 **  VARS CHANGED   :
49 **  FUNCTIONS USED :
50 **  SEE ALSO       :
51 **  LAST MODIFIED  : Sep 26, 2002
52 **=========================================================================
53 */
54
55 gnucomo_database::gnucomo_database(gnucomo_config *c)
56 {
57    cfg = c;
58
59    if (verbose)
60    {
61       cout <<  "Database connection string = " << cfg->Database() << "\n";
62    }
63
64    db = new PgDatabase(cfg->Database());
65
66    if (db->ConnectionBad())
67    {
68       cerr << "Can not connect to database: " << db->ErrorMessage();
69    }
70 }
71
72 /*=========================================================================
73 **  NAME           : find_host
74 **  SYNOPSIS       : String gnucomo_database::find_host(String hostname);
75 **  PARAMETERS     : 
76 **  RETURN VALUE   : Find a hostname in the 'object' table of the gnucomo database
77 **                   and return its object id.
78 **                   Return an empty string as objectid if the hostname is
79 **                   not found.
80 **
81 **  DESCRIPTION    : 
82 **
83 **  VARS USED      :
84 **  VARS CHANGED   :
85 **  FUNCTIONS USED :
86 **  SEE ALSO       :
87 **  LAST MODIFIED  : Sep 16, 2002
88 **=========================================================================
89 */
90
91 String gnucomo_database::find_host(const String hostname)
92 {
93    String objectid("");
94    String check_host("select objectid from object where ");
95
96    check_host += "objectname = '";
97    check_host += hostname;
98    check_host += "'";
99
100    if (Query(check_host) > 0)
101    {
102       objectid = String(db->GetValue(0, "objectid"));
103    }
104
105    return objectid;
106 }