VTK
dox/IO/PostgreSQL/vtkPostgreSQLDatabase.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; -*- */
00002 /*=========================================================================
00003 
00004   Program:   Visualization Toolkit
00005   Module:    vtkPostgreSQLDatabase.h
00006 
00007   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00008   All rights reserved.
00009   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00010 
00011      This software is distributed WITHOUT ANY WARRANTY; without even
00012      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00013      PURPOSE.  See the above copyright notice for more information.
00014 
00015 =========================================================================*/
00016 /*-------------------------------------------------------------------------
00017   Copyright 2008 Sandia Corporation.
00018   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00019   the U.S. Government retains certain rights in this software.
00020 -------------------------------------------------------------------------*/
00048 #ifndef __vtkPostgreSQLDatabase_h
00049 #define __vtkPostgreSQLDatabase_h
00050 
00051 #include "vtkIOPostgreSQLModule.h" // For export macro
00052 #include "vtkSQLDatabase.h"
00053 
00054 class vtkPostgreSQLQuery;
00055 class vtkStringArray;
00056 class vtkPostgreSQLDatabasePrivate;
00057 struct PQconn;
00058 
00059 class VTKIOPOSTGRESQL_EXPORT vtkPostgreSQLDatabase : public vtkSQLDatabase
00060 {
00061   //BTX
00062   friend class vtkPostgreSQLQuery;
00063   friend class vtkPostgreSQLQueryPrivate;
00064   //ETX
00065 
00066 public:
00067   vtkTypeMacro(vtkPostgreSQLDatabase, vtkSQLDatabase);
00068   void PrintSelf(ostream& os, vtkIndent indent);
00069   static vtkPostgreSQLDatabase *New();
00070 
00074   bool Open( const char* password = 0 );
00075 
00077   void Close();
00078 
00080   bool IsOpen();
00081 
00083   vtkSQLQuery* GetQueryInstance();
00084 
00086   virtual bool HasError();
00087 
00089   const char* GetLastErrorText();
00090 
00092 
00093   vtkGetStringMacro(DatabaseType);
00095 
00097 
00098   virtual void SetHostName( const char* );
00099   vtkGetStringMacro(HostName);
00101 
00103 
00104   virtual void SetUser( const char* );
00105   vtkGetStringMacro(User);
00107 
00109   virtual void SetPassword( const char* );
00110 
00112 
00113   virtual void SetDatabaseName( const char* );
00114   vtkGetStringMacro(DatabaseName);
00116 
00118 
00119   virtual void SetConnectOptions( const char* );
00120   vtkGetStringMacro(ConnectOptions);
00122 
00124 
00125   virtual void SetServerPort( int );
00126   virtual int GetServerPortMinValue()
00127     {
00128     return 0;
00129     }
00130   virtual int GetServerPortMaxValue()
00131     {
00132     return VTK_INT_MAX;
00133     }
00134   vtkGetMacro(ServerPort, int);
00136 
00142   virtual vtkStdString GetURL();
00143 
00145   vtkStringArray* GetTables();
00146 
00148   vtkStringArray* GetRecord( const char* table );
00149 
00151   bool IsSupported( int feature );
00152 
00154   vtkStringArray* GetDatabases();
00155 
00159   bool CreateDatabase( const char* dbName, bool dropExisting = false );
00160 
00163   bool DropDatabase( const char* dbName );
00164 
00166 
00170   virtual vtkStdString GetColumnSpecification(
00171     vtkSQLDatabaseSchema* schema, int tblHandle, int colHandle );
00173 
00177   virtual bool ParseURL(const char* url);
00178 
00179 protected:
00180   vtkPostgreSQLDatabase();
00181   ~vtkPostgreSQLDatabase();
00182 
00193   void UpdateDataTypeMap();
00194 
00195   vtkSetStringMacro(DatabaseType);
00196   vtkSetStringMacro(LastErrorText);
00197   void NullTrailingWhitespace( char* msg );
00198   bool OpenInternal( const char* connectionOptions );
00199 
00200   vtkTimeStamp URLMTime;
00201   vtkPostgreSQLDatabasePrivate *Connection;
00202   vtkTimeStamp ConnectionMTime;
00203   vtkStringArray *Tables;
00204   char* DatabaseType;
00205   char* HostName;
00206   char* User;
00207   char* Password;
00208   char* DatabaseName;
00209   int ServerPort;
00210   char* ConnectOptions;
00211   char* LastErrorText;
00212 
00213 private:
00214   vtkPostgreSQLDatabase( const vtkPostgreSQLDatabase& ); // Not implemented.
00215   void operator = ( const vtkPostgreSQLDatabase& ); // Not implemented.
00216 };
00217 
00218 // This is basically the body of the SetStringMacro but with a
00219 // call to update an additional vtkTimeStamp. We inline the implementation
00220 // so that wrapping will work.
00221 #define vtkSetStringPlusMTimeMacro(className,name,timeStamp) \
00222   inline void className::Set##name (const char* _arg) \
00223   { \
00224     vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting " << #name " to " << (_arg?_arg:"(null)") ); \
00225     if ( this->name == NULL && _arg == NULL) { return;} \
00226     if ( this->name && _arg && (!strcmp(this->name,_arg))) { return;} \
00227     if (this->name) { delete [] this->name; } \
00228     if (_arg) \
00229       { \
00230           size_t n = strlen(_arg) + 1; \
00231           char *cp1 =  new char[n]; \
00232           const char *cp2 = (_arg); \
00233           this->name = cp1; \
00234           do { *cp1++ = *cp2++; } while ( --n ); \
00235           } \
00236      else \
00237       { \
00238           this->name = NULL; \
00239           } \
00240     this->Modified(); \
00241     this->timeStamp.Modified(); \
00242     this->Close(); /* Force a re-open on next query */ \
00243     }
00244 
00245 vtkSetStringPlusMTimeMacro(vtkPostgreSQLDatabase,HostName,URLMTime);
00246 vtkSetStringPlusMTimeMacro(vtkPostgreSQLDatabase,User,URLMTime);
00247 vtkSetStringPlusMTimeMacro(vtkPostgreSQLDatabase,Password,URLMTime);
00248 vtkSetStringPlusMTimeMacro(vtkPostgreSQLDatabase,DatabaseName,URLMTime);
00249 vtkSetStringPlusMTimeMacro(vtkPostgreSQLDatabase,ConnectOptions,URLMTime);
00250 
00251 inline void vtkPostgreSQLDatabase::SetServerPort( int _arg )
00252 {
00253   vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting ServerPort to " << _arg );
00254   if ( this->ServerPort != ( _arg < 0 ? 0 : ( _arg > VTK_INT_MAX ? VTK_INT_MAX : _arg ) ) )
00255     {
00256     this->ServerPort = ( _arg < 0 ? 0 : ( _arg > VTK_INT_MAX ? VTK_INT_MAX : _arg ) );
00257     this->Modified();
00258     this->URLMTime.Modified();
00259     this->Close(); // Force a re-open on next query
00260     }
00261 }
00262 
00263 #endif // __vtkPostgreSQLDatabase_h