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