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