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