00001 /* $Id: pgdb_property.hh,v 1.12 2003/07/24 23:08:35 reed Exp $ */ 00002 00003 00004 /** @file pgdb_property.hh 00005 Defines DBProperty implementation using PostgreSQL DB (using libpq). 00006 00007 00008 Copyright (C) 2001, 2002 Reed Hedges <reed@zerohour.net> 00009 00010 This library is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU Lesser General Public 00012 License as published by the Free Software Foundation; either 00013 version 2 of the License, or (at your option) any later version. 00014 00015 This library is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 Lesser General Public License for more details. 00019 00020 You should have received a copy of the GNU Lesser General Public 00021 License along with this library; if not, write to the Free Software 00022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 00024 */ 00025 00026 #ifndef _PGDB_PROPERTY_HH_ 00027 #define _PGDB_PROPERTY_HH_ 00028 00029 #include "db_property.hh" 00030 00031 // Of course, RH 6.2, RH 6.3, and Debian all put libpq-fe.h in different 00032 // places... 00033 #ifdef HAVE_POSTGRESQL_LIBPQ_FE_H 00034 #include <postgresql/libpq-fe.h> 00035 #else 00036 #ifdef HAVE_PGSQL_LIBPQ_FE_H 00037 #include <pgsql/libpq-fe.h> 00038 #else 00039 #ifdef HAVE_LIBPQ_FE_H 00040 #include <libpq-fe.h> 00041 #else 00042 #error "Neither HAVE_POSTGRESQL_LIBPQ_FE_H nor HAVE_PGSQL_LIBPQ_FE_H nore HAVE_LIBPQ_FE_H are defined, so I don't know where libpq-fe.h is! (Something is probably wrong with the configure script). Please report this bug to <reed@zerohour.net>." 00043 #endif 00044 #endif 00045 #endif 00046 00047 /* Used to define array of functions to call on database update notifications */ 00048 //typedef void(POLL_FUNCTION)(void); 00049 00050 00051 00052 00053 /** @class PGDBProperty pgdb_property.hh vos/metaobjects/property/pgdb_property.hh 00054 * 00055 * DBProperty subclass for the PostgreSQL database. 00056 00057 @warning The select query must return a scalar value, currently. (If 00058 it returns multiple values, only the first field of the first row will 00059 be used. 00060 00061 From the VOS point of view, the PG Database is a backend to the 00062 property metaobject. 00063 From Postgres point of view, this is a frontend to the database; 00064 it is a very primitive one, essentially only doing SELECTs to read 00065 the data and UPDATEs (or INSERTs) to change it. You can talk to the 00066 database yourself, if you really want to, by using the queryDB() 00067 function, or by getting the connection handle with getConnectionHandle(). 00068 However, it must be said, Caveat Hackor. 00069 00070 @note this class will only be available on systems with the postgres 00071 development stuff (headers etc) installed, so you probably need to 00072 download and the source code from 00073 http://interreality.org/software/source.html and compile it yourself 00074 (after installing the PGSQL headers etc. of course) if you want to use 00075 this class. 00076 */ 00077 00078 class PGDBProperty : public virtual DBProperty 00079 { 00080 protected: 00081 00082 00083 PGconn* dbcon; // connection to the database 00084 00085 public: 00086 00087 /** basic constructor */ 00088 PGDBProperty(MetaObject *superobject); 00089 00090 /** Open database. 00091 @param dbinfo Informatino to pass to Posgres C library PGconn() function. if empty ("") or omitted, enironment variables and/or defaults are used. */ 00092 void open(const string& dbinfo = ""); 00093 00094 00095 /** Destructoratorizer. Closes database connection. */ 00096 ~PGDBProperty(); 00097 00098 00099 00100 /** called by factory to create a new instance */ 00101 static MetaObject* new_PGDBProperty(MetaObject* superobject, const string& type) { 00102 return new PGDBProperty(superobject); 00103 } 00104 00105 /** Register PGDBProperty Metaobject extender. You must also call Property::registerExtenders() for stuff to work! 00106 */ 00107 static void registerExtenders() { 00108 MetaFactory::addLocalObjectExtension(typeid(PGDBProperty).name(), &PGDBProperty::new_PGDBProperty); 00109 } 00110 00111 00112 /** Returns underlying database handle. it will really be type PGconn*, 00113 simply typecast to fit interface. */ 00114 virtual void *getConnectionHandle() { return (void *)dbcon; } 00115 00116 /** Perform a query on the database, in Postgres' SQL. */ 00117 virtual string queryDB(const string& query) throw(DBAccessError); 00118 00119 }; 00120 00121 #endif