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