1a4763d99a410aaaec6bf33241dd00583fd31b1e
[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.3 $
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     : Nov 04, 2002
25 **      MODIFICATIONS   : 
26 **************************************************************************/
27
28 /*****************************
29    $Log: database.h,v $
30    Revision 1.3  2002-11-09 08:04:27  arjen
31    Added a reference to the GPL
32
33    Revision 1.2  2002/11/04 10:13:36  arjen
34    Use proper namespace for iostream classes
35
36    Revision 1.1  2002/10/05 10:25:49  arjen
37    Creation of gcm_input and a first approach to a web interface
38
39 *****************************/
40
41 /* static const char *RCSID = "$Id: database.h,v 1.3 2002-11-09 08:04:27 arjen Exp $"; */
42
43 #include <libpq++/pgdatabase.h>
44 #include "gnucomo_config.h"
45
46 /*
47 ///////////////////////////////////////////////////////////////////////////
48 //  NAME           : gnucomo_database
49 //  BASECLASS      : configuration
50 //  MEMBERS        :
51 //  OPERATORS      :
52 //  METHODS        : Database - Obtain the database access string
53 //
54 //  DESCRIPTION    : 
55 //
56 //  RELATIONS      :
57 //  SEE ALSO       :
58 //  LAST MODIFIED  : Sep 16, 2002
59 ///////////////////////////////////////////////////////////////////////////
60 */
61
62 class gnucomo_database
63 {
64    gnucomo_config    *cfg;
65    PgDatabase        *db;
66
67 public:
68
69    gnucomo_database()
70    {
71       cfg = 0;
72       db  = 0;
73    }
74
75    gnucomo_database(gnucomo_config *c);  // Use the configuration to connect to the database
76
77    //  Low-level database access functions
78
79    int Query(String qry)
80    {
81       ExecStatusType  result;
82
83       result = db->Exec(qry);
84       if (result == PGRES_TUPLES_OK || result == PGRES_COMMAND_OK)
85       {
86          return db->Tuples();
87       }
88       else
89       {
90          std::cerr << "Database query error: " << db->ErrorMessage() << "\n";
91          return -1;
92       }
93    }
94
95    String Field(int tuple, const char *fieldname)
96    {
97       return String(db->GetValue(tuple, fieldname));
98    }
99
100    //  Return the objectid of the host given its name.
101
102    String find_host(const String hostname);
103 };
104