d77f7a27930ffe9c3d0efc817c788f7fdff7db35
[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.6 $
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     : Jan 31, 2003
25 **      MODIFICATIONS   : 
26 **************************************************************************/
27
28 /*****************************
29    $Log: database.h,v $
30    Revision 1.6  2003-02-19 09:54:47  arjen
31    Print the query on cerr, along with the error message when
32    the query results in an error.
33
34    Revision 1.5  2003/02/05 09:33:17  arjen
35    gnucomo_database::new_notification() retruns the id number of the
36    newly created notification record.
37
38    Revision 1.4  2003/01/18 08:52:18  arjen
39    New C++ function: gnucomo_database::new_notification()
40
41    Revision 1.3  2002/11/09 08:04:27  arjen
42    Added a reference to the GPL
43
44    Revision 1.2  2002/11/04 10:13:36  arjen
45    Use proper namespace for iostream classes
46
47    Revision 1.1  2002/10/05 10:25:49  arjen
48    Creation of gcm_input and a first approach to a web interface
49
50 *****************************/
51
52 /* static const char *RCSID = "$Id: database.h,v 1.6 2003-02-19 09:54:47 arjen Exp $"; */
53
54 #include <libpq++/pgdatabase.h>
55 #include "gnucomo_config.h"
56
57 /*
58 ///////////////////////////////////////////////////////////////////////////
59 //  NAME           : gnucomo_database
60 //  BASECLASS      : configuration
61 //  MEMBERS        :
62 //  OPERATORS      :
63 //  METHODS        : Database - Obtain the database access string
64 //
65 //  DESCRIPTION    : 
66 //
67 //  RELATIONS      :
68 //  SEE ALSO       :
69 //  LAST MODIFIED  : Jan 17, 2003
70 ///////////////////////////////////////////////////////////////////////////
71 */
72
73 class gnucomo_database
74 {
75    gnucomo_config    *cfg;
76    PgDatabase        *db;
77
78 public:
79
80    gnucomo_database()
81    {
82       cfg = 0;
83       db  = 0;
84    }
85
86    gnucomo_database(gnucomo_config *c);  // Use the configuration to connect to the database
87
88    //  Low-level database access functions
89
90    int Query(String qry)
91    {
92       ExecStatusType  result;
93
94       result = db->Exec(qry);
95       if (result == PGRES_TUPLES_OK || result == PGRES_COMMAND_OK)
96       {
97          return db->Tuples();
98       }
99       else
100       {
101          std::cerr << "Database query error: " << db->ErrorMessage() << "\n";
102          std::cerr << "Query: " << qry << "\n";
103          return -1;
104       }
105    }
106
107    String Field(int tuple, const char *fieldname)
108    {
109       return String(db->GetValue(tuple, fieldname));
110    }
111
112    //  Return the objectid of the host given its name.
113
114    String find_host(const String hostname);
115
116    //  Create a new notification.
117
118    String new_notification(String objectid, String issue, String remark);
119 };
120