Adjusted to new version of libpqxx.
[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 /*
80 ///////////////////////////////////////////////////////////////////////////
81 //  NAME           : gnucomo_database
82 //  BASECLASS      : 
83 //  MEMBERS        :
84 //  OPERATORS      :
85 //  METHODS        : is_connected - Return true if the database is connected
86 //
87 //  DESCRIPTION    : 
88 //
89 //  RELATIONS      :
90 //  SEE ALSO       :
91 //  LAST MODIFIED  : Aug 27, 2003
92 ///////////////////////////////////////////////////////////////////////////
93 */
94
95 class gnucomo_database
96 {
97    gnucomo_config    *cfg;
98    pqxx::connection  *dbconn;
99    //pqxx::transaction<pqxx::serializable> *dbxact;
100    pqxx::work *dbxact;
101
102    pqxx::result      last_result;
103
104 public:
105
106    gnucomo_database()
107    {
108       cfg = 0;
109       dbconn = 0;
110       dbxact = 0;
111    }
112
113    // Use the configuration to connect to the database
114
115    gnucomo_database(gnucomo_config *c);
116
117    //   A copy constructor and the assignement can copy
118    //   the connection to the database must create a
119    //   new Transaction object.
120
121    gnucomo_database(const gnucomo_database &gdb);
122    void operator = (const gnucomo_database &gdb);
123
124    ~gnucomo_database();
125    
126    //  Error checking and handling.
127
128    bool is_connected()
129    {
130       return dbconn != 0 && dbconn->is_open();
131    }
132
133    //  Low-level database access functions
134
135    int Query(String qry)
136    {
137       try
138       {
139          last_result = dbxact->exec((char *)qry);
140 #ifdef DEBUG
141       std::cerr << "Query " << qry << " returned "
142                 << last_result.size() << " tuples.\n";
143 #endif
144       }
145       catch (const pqxx::pqxx_exception &e)
146       {
147          std::cerr << "Error in QUERY " << qry << ":\n";
148          std::cerr << e.base().what() << std::endl;
149       }
150
151       return last_result.size();
152    }
153
154    pqxx::result Result()
155    {
156       return last_result;
157    }
158
159    //  The field value of a specific result.
160
161    String Field(pqxx::result res, int tuple, const char *fieldname)
162    {
163       return String(res[tuple][fieldname].c_str());
164    }
165
166    //  Use the result of the last query by default
167
168    String Field(int tuple, const char *fieldname)
169    {
170       return String(last_result[tuple][fieldname].c_str());
171    }
172
173    //  Return the objectid of the host given its name.
174
175    String find_host(const String hostname);
176
177    //  Create a new notification.
178
179    String new_notification(String objectid, String issue, String remark);
180 };
181
182 /*
183 ///////////////////////////////////////////////////////////////////////////
184 //  NAME           : database_entity
185 //  BASECLASS      : 
186 //  MEMBERS        :
187 //  OPERATORS      :
188 //  METHODS        : 
189 //
190 //  DESCRIPTION    : 
191 //
192 //  RELATIONS      :
193 //  SEE ALSO       :
194 //  LAST MODIFIED  : Aug 19, 2005
195 ///////////////////////////////////////////////////////////////////////////
196 */
197
198 class database_entity
199 {
200    const gnucomo_database  *db;
201    String    table;
202
203    bool fresh;       // Completely new, no tuple in the database
204    bool changed;     // A database update is needed
205    bool deleted;     // Tuple is to be deleted from the database
206
207 public:
208
209   database_entity(const gnucomo_database &gdb, const String tbl)
210   {
211      db = &gdb;
212      table = tbl;
213
214      fresh = true;
215      changed = false;
216      deleted = false;
217   }
218 };