Experimental start of database OO abstraction layer.
[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.11 $
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 19, 2005
25 **      MODIFICATIONS   : 
26 **************************************************************************/
27
28 /*****************************
29    $Log: database.h,v $
30    Revision 1.11  2007-01-11 13:50:10  arjen
31    Experimental start of database OO abstraction layer.
32
33    Revision 1.10  2003/12/04 10:39:36  arjen
34    Fixed libpqxx headers
35
36    Revision 1.9  2003/09/02 12:54:10  arjen
37    Overloaded gnucomo_database::Field() to include the Result
38    from a query as an argument.
39
40    Revision 1.8  2003/08/17 11:39:33  arjen
41    Changed the gnucomo_database class to the new PostgreSQL
42    library, libpqxx
43
44    Revision 1.7  2003/03/29 08:13:53  arjen
45    New member function gnucomo_database::is_conected().
46
47    Revision 1.6  2003/02/19 09:54:47  arjen
48    Print the query on cerr, along with the error message when
49    the query results in an error.
50
51    Revision 1.5  2003/02/05 09:33:17  arjen
52    gnucomo_database::new_notification() retruns the id number of the
53    newly created notification record.
54
55    Revision 1.4  2003/01/18 08:52:18  arjen
56    New C++ function: gnucomo_database::new_notification()
57
58    Revision 1.3  2002/11/09 08:04:27  arjen
59    Added a reference to the GPL
60
61    Revision 1.2  2002/11/04 10:13:36  arjen
62    Use proper namespace for iostream classes
63
64    Revision 1.1  2002/10/05 10:25:49  arjen
65    Creation of gcm_input and a first approach to a web interface
66
67 *****************************/
68
69 /* static const char *RCSID = "$Id: database.h,v 1.11 2007-01-11 13:50:10 arjen Exp $"; */
70
71 #include <pqxx/connection.h>
72 #include <pqxx/transaction.h>
73 #include <pqxx/result.h>
74 #include "gnucomo_config.h"
75
76 /*
77 ///////////////////////////////////////////////////////////////////////////
78 //  NAME           : gnucomo_database
79 //  BASECLASS      : 
80 //  MEMBERS        :
81 //  OPERATORS      :
82 //  METHODS        : is_connected - Return true if the database is connected
83 //
84 //  DESCRIPTION    : 
85 //
86 //  RELATIONS      :
87 //  SEE ALSO       :
88 //  LAST MODIFIED  : Aug 27, 2003
89 ///////////////////////////////////////////////////////////////////////////
90 */
91
92 class gnucomo_database
93 {
94    gnucomo_config    *cfg;
95    pqxx::Connection  *dbconn;
96    pqxx::Transaction *dbxact;
97
98    pqxx::Result      last_result;
99
100 public:
101
102    gnucomo_database()
103    {
104       cfg = 0;
105       dbconn = 0;
106       dbxact = 0;
107    }
108
109    // Use the configuration to connect to the database
110
111    gnucomo_database(gnucomo_config *c);
112
113    //   A copy constructor and the assignement can copy
114    //   the connection to the database must create a
115    //   new Transaction object.
116
117    gnucomo_database(const gnucomo_database &gdb);
118    void operator = (const gnucomo_database &gdb);
119
120    ~gnucomo_database();
121    
122    //  Error checking and handling.
123
124    bool is_connected()
125    {
126       return dbconn != 0 && dbconn->is_open();
127    }
128
129    //  Low-level database access functions
130
131    int Query(String qry)
132    {
133
134       last_result = dbxact->Exec(qry);
135 #ifdef DEBUG
136       std::cerr << "Query " << qry << " returned "
137                 << last_result.size() << " tuples.\n";
138 #endif
139       return last_result.size();
140    }
141
142    pqxx::Result Result()
143    {
144       return last_result;
145    }
146
147    //  The field value of a specific result.
148
149    String Field(pqxx::Result res, int tuple, const char *fieldname)
150    {
151       return String(res[tuple][fieldname].c_str());
152    }
153
154    //  Use the result of the last query by default
155
156    String Field(int tuple, const char *fieldname)
157    {
158       return String(last_result[tuple][fieldname].c_str());
159    }
160
161    //  Return the objectid of the host given its name.
162
163    String find_host(const String hostname);
164
165    //  Create a new notification.
166
167    String new_notification(String objectid, String issue, String remark);
168 };
169
170 /*
171 ///////////////////////////////////////////////////////////////////////////
172 //  NAME           : database_entity
173 //  BASECLASS      : 
174 //  MEMBERS        :
175 //  OPERATORS      :
176 //  METHODS        : 
177 //
178 //  DESCRIPTION    : 
179 //
180 //  RELATIONS      :
181 //  SEE ALSO       :
182 //  LAST MODIFIED  : Aug 19, 2005
183 ///////////////////////////////////////////////////////////////////////////
184 */
185
186 class database_entity
187 {
188    const gnucomo_database  *db;
189    String    table;
190
191    bool fresh;       // Completely new, no tuple in the database
192    bool changed;     // A database update is needed
193    bool deleted;     // Tuple is to be deleted from the database
194
195 public:
196
197   database_entity(const gnucomo_database &gdb, const String tbl)
198   {
199      db = &gdb;
200      table = tbl;
201
202      fresh = true;
203      changed = false;
204      deleted = false;
205   }
206 };