051adb86ebb950d18e6dd44b951e94ed0d9f587c
[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.8 $
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     : Aug 17, 2003
25 **      MODIFICATIONS   : 
26 **************************************************************************/
27
28 /*****************************
29    $Log: database.h,v $
30    Revision 1.8  2003-08-17 11:39:33  arjen
31    Changed the gnucomo_database class to the new PostgreSQL
32    library, libpqxx
33
34    Revision 1.7  2003/03/29 08:13:53  arjen
35    New member function gnucomo_database::is_conected().
36
37    Revision 1.6  2003/02/19 09:54:47  arjen
38    Print the query on cerr, along with the error message when
39    the query results in an error.
40
41    Revision 1.5  2003/02/05 09:33:17  arjen
42    gnucomo_database::new_notification() retruns the id number of the
43    newly created notification record.
44
45    Revision 1.4  2003/01/18 08:52:18  arjen
46    New C++ function: gnucomo_database::new_notification()
47
48    Revision 1.3  2002/11/09 08:04:27  arjen
49    Added a reference to the GPL
50
51    Revision 1.2  2002/11/04 10:13:36  arjen
52    Use proper namespace for iostream classes
53
54    Revision 1.1  2002/10/05 10:25:49  arjen
55    Creation of gcm_input and a first approach to a web interface
56
57 *****************************/
58
59 /* static const char *RCSID = "$Id: database.h,v 1.8 2003-08-17 11:39:33 arjen Exp $"; */
60
61 #include <pqxx/transaction.h>
62 #include "gnucomo_config.h"
63
64 /*
65 ///////////////////////////////////////////////////////////////////////////
66 //  NAME           : gnucomo_database
67 //  BASECLASS      : 
68 //  MEMBERS        :
69 //  OPERATORS      :
70 //  METHODS        : is_connected - Return true if the database is connected
71 //
72 //  DESCRIPTION    : 
73 //
74 //  RELATIONS      :
75 //  SEE ALSO       :
76 //  LAST MODIFIED  : Aug 17, 2003
77 ///////////////////////////////////////////////////////////////////////////
78 */
79
80 class gnucomo_database
81 {
82    gnucomo_config    *cfg;
83    pqxx::Connection  *dbconn;
84    pqxx::Transaction *dbxact;
85
86    pqxx::Result      last_result;
87
88 public:
89
90    gnucomo_database()
91    {
92       cfg = 0;
93       dbconn = 0;
94       dbxact = 0;
95    }
96
97    // Use the configuration to connect to the database
98
99    gnucomo_database(gnucomo_config *c);
100
101    //   A copy constructor and the assignement can copy
102    //   the connection to the database must create a
103    //   new Transaction object.
104
105    gnucomo_database(const gnucomo_database &gdb);
106    void operator = (const gnucomo_database &gdb);
107
108    ~gnucomo_database();
109    
110    //  Error checking and handling.
111
112    bool is_connected()
113    {
114       return dbconn != 0 && dbconn->is_open();
115    }
116
117    //  Low-level database access functions
118
119    int Query(String qry)
120    {
121
122       last_result = dbxact->Exec(qry);
123 #ifdef DEBUG
124       std::cerr << "Query " << qry << " returned "
125                 << last_result.size() << " tuples.\n";
126 #endif
127       return last_result.size();
128    }
129
130    pqxx::Result Result()
131    {
132       return last_result;
133    }
134
135    String Field(int tuple, const char *fieldname)
136    {
137       return String(last_result[tuple][fieldname].c_str());
138    }
139
140    //  Return the objectid of the host given its name.
141
142    String find_host(const String hostname);
143
144    //  Create a new notification.
145
146    String new_notification(String objectid, String issue, String remark);
147 };
148