Use proper namespace for iostream classes
[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.2 $
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     : Nov 04, 2002
22 **      MODIFICATIONS   : 
23 **************************************************************************/
24
25 /*****************************
26    $Log: database.cpp,v $
27    Revision 1.2  2002-11-04 10:13:36  arjen
28    Use proper namespace for iostream classes
29
30    Revision 1.1  2002/10/05 10:25:49  arjen
31    Creation of gcm_input and a first approach to a web interface
32
33 *****************************/
34
35 static const char *RCSID = "$Id: database.cpp,v 1.2 2002-11-04 10:13:36 arjen Exp $";
36
37 #include "database.h"
38
39 extern bool verbose;   /*  Defined in the main application */
40
41 /*=========================================================================
42 **  NAME           : gnucomo_database
43 **  SYNOPSIS       : gnucomo_database(gnucomo_config &c);
44 **  PARAMETERS     : 
45 **  RETURN VALUE   : Database constructor. Establishes a connection with
46 **                   the database server.
47 **
48 **  DESCRIPTION    : 
49 **
50 **  VARS USED      :
51 **  VARS CHANGED   :
52 **  FUNCTIONS USED :
53 **  SEE ALSO       :
54 **  LAST MODIFIED  : Sep 26, 2002
55 **=========================================================================
56 */
57
58 gnucomo_database::gnucomo_database(gnucomo_config *c)
59 {
60    cfg = c;
61
62    if (verbose)
63    {
64       std::cout <<  "Database connection string = " << cfg->Database() << "\n";
65    }
66
67    db = new PgDatabase(cfg->Database());
68
69    if (db->ConnectionBad())
70    {
71       std::cerr << "Can not connect to database: " << db->ErrorMessage();
72    }
73 }
74
75 /*=========================================================================
76 **  NAME           : find_host
77 **  SYNOPSIS       : String gnucomo_database::find_host(String hostname);
78 **  PARAMETERS     : 
79 **  RETURN VALUE   : Find a hostname in the 'object' table of the gnucomo database
80 **                   and return its object id.
81 **                   Return an empty string as objectid if the hostname is
82 **                   not found.
83 **
84 **  DESCRIPTION    : 
85 **
86 **  VARS USED      :
87 **  VARS CHANGED   :
88 **  FUNCTIONS USED :
89 **  SEE ALSO       :
90 **  LAST MODIFIED  : Sep 16, 2002
91 **=========================================================================
92 */
93
94 String gnucomo_database::find_host(const String hostname)
95 {
96    String objectid("");
97    String check_host("select objectid from object where ");
98
99    check_host += "objectname = '";
100    check_host += hostname;
101    check_host += "'";
102
103    if (Query(check_host) > 0)
104    {
105       objectid = String(db->GetValue(0, "objectid"));
106    }
107
108    return objectid;
109 }