b96c0b35dab740c044426d6228916483cf627208
[gnucomo.git] / src / include / database.h
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.h
10 **      SYSTEM NAME    : 
11 **      VERSION NUMBER : $Revision: 1.7 $
12 **
13 **  DESCRIPTION      :  Classes to provide an abstract layer on the Gnucomo
14 **                      database.
15 **
16 **  EXPORTED OBJECTS : 
17 **  LOCAL    OBJECTS : 
18 **  MODULES  USED    :
19 ***************************************************************************
20 **  ADMINISTRATIVE INFORMATION *
21 ********************************
22 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
23 **      CREATION DATE   : Sep 10, 2002
24 **      LAST UPDATE     : Mar 28, 2003
25 **      MODIFICATIONS   : 
26 **************************************************************************/
27
28 /*****************************
29    $Log: database.h,v $
30    Revision 1.7  2003-03-29 08:13:53  arjen
31    New member function gnucomo_database::is_conected().
32
33    Revision 1.6  2003/02/19 09:54:47  arjen
34    Print the query on cerr, along with the error message when
35    the query results in an error.
36
37    Revision 1.5  2003/02/05 09:33:17  arjen
38    gnucomo_database::new_notification() retruns the id number of the
39    newly created notification record.
40
41    Revision 1.4  2003/01/18 08:52:18  arjen
42    New C++ function: gnucomo_database::new_notification()
43
44    Revision 1.3  2002/11/09 08:04:27  arjen
45    Added a reference to the GPL
46
47    Revision 1.2  2002/11/04 10:13:36  arjen
48    Use proper namespace for iostream classes
49
50    Revision 1.1  2002/10/05 10:25:49  arjen
51    Creation of gcm_input and a first approach to a web interface
52
53 *****************************/
54
55 /* static const char *RCSID = "$Id: database.h,v 1.7 2003-03-29 08:13:53 arjen Exp $"; */
56
57 #include <libpq++/pgdatabase.h>
58 #include "gnucomo_config.h"
59
60 /*
61 ///////////////////////////////////////////////////////////////////////////
62 //  NAME           : gnucomo_database
63 //  BASECLASS      : configuration
64 //  MEMBERS        :
65 //  OPERATORS      :
66 //  METHODS        : Database - Obtain the database access string
67 //
68 //  DESCRIPTION    : 
69 //
70 //  RELATIONS      :
71 //  SEE ALSO       :
72 //  LAST MODIFIED  : Mar 28, 2003
73 ///////////////////////////////////////////////////////////////////////////
74 */
75
76 class gnucomo_database
77 {
78    gnucomo_config    *cfg;
79    PgDatabase        *db;
80
81 public:
82
83    gnucomo_database()
84    {
85       cfg = 0;
86       db  = 0;
87    }
88
89    gnucomo_database(gnucomo_config *c);  // Use the configuration to connect to the database
90
91    //  Error checking and handling.
92
93    bool is_connected()
94    {
95       return db != 0 && db->Status() == CONNECTION_OK;
96    }
97
98    //  Low-level database access functions
99
100    int Query(String qry)
101    {
102       ExecStatusType  result;
103
104       result = db->Exec(qry);
105       if (result == PGRES_TUPLES_OK || result == PGRES_COMMAND_OK)
106       {
107          return db->Tuples();
108       }
109       else
110       {
111          std::cerr << "Database query error: " << db->ErrorMessage() << "\n";
112          std::cerr << "Query: " << qry << "\n";
113          return -1;
114       }
115    }
116
117    String Field(int tuple, const char *fieldname)
118    {
119       return String(db->GetValue(tuple, fieldname));
120    }
121
122    //  Return the objectid of the host given its name.
123
124    String find_host(const String hostname);
125
126    //  Create a new notification.
127
128    String new_notification(String objectid, String issue, String remark);
129 };
130