VTK  9.0.20201019
vtkPostgreSQLDatabase.h
Go to the documentation of this file.
1 /* -*- Mode: C++; -*- */
2 /*=========================================================================
3 
4  Program: Visualization Toolkit
5  Module: vtkPostgreSQLDatabase.h
6 
7  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
8  All rights reserved.
9  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
10 
11  This software is distributed WITHOUT ANY WARRANTY; without even
12  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the above copyright notice for more information.
14 
15 =========================================================================*/
16 /*-------------------------------------------------------------------------
17  Copyright 2008 Sandia Corporation.
18  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
19  the U.S. Government retains certain rights in this software.
20 -------------------------------------------------------------------------*/
46 #ifndef vtkPostgreSQLDatabase_h
47 #define vtkPostgreSQLDatabase_h
48 
49 #include "vtkIOPostgreSQLModule.h" // For export macro
50 #include "vtkSQLDatabase.h"
51 
52 class vtkPostgreSQLQuery;
53 class vtkStringArray;
55 struct PQconn;
56 
57 class VTKIOPOSTGRESQL_EXPORT vtkPostgreSQLDatabase : public vtkSQLDatabase
58 {
59 
60  friend class vtkPostgreSQLQuery;
61  friend class vtkPostgreSQLQueryPrivate;
62 
63 public:
65  void PrintSelf(ostream& os, vtkIndent indent) override;
66  static vtkPostgreSQLDatabase* New();
67 
73  bool Open(const char* password = 0) override;
74 
78  void Close() override;
79 
83  bool IsOpen() override;
84 
88  vtkSQLQuery* GetQueryInstance() override;
89 
93  bool HasError() override;
94 
98  const char* GetLastErrorText() override;
99 
101 
104  const char* GetDatabaseType() override { return this->DatabaseType; }
106 
108 
111  virtual void SetHostName(const char*);
112  vtkGetStringMacro(HostName);
114 
116 
119  virtual void SetUser(const char*);
120  vtkGetStringMacro(User);
122 
126  virtual void SetPassword(const char*);
127 
129 
132  virtual void SetDatabaseName(const char*);
133  vtkGetStringMacro(DatabaseName);
135 
137 
140  virtual void SetConnectOptions(const char*);
141  vtkGetStringMacro(ConnectOptions);
143 
145 
148  virtual void SetServerPort(int);
149  virtual int GetServerPortMinValue() { return 0; }
150  virtual int GetServerPortMaxValue() { return VTK_INT_MAX; }
151  vtkGetMacro(ServerPort, int);
153 
160  vtkStdString GetURL() override;
161 
165  vtkStringArray* GetTables() override;
166 
170  vtkStringArray* GetRecord(const char* table) override;
171 
175  bool IsSupported(int feature) override;
176 
180  vtkStringArray* GetDatabases();
181 
186  bool CreateDatabase(const char* dbName, bool dropExisting = false);
187 
192  bool DropDatabase(const char* dbName);
193 
201  vtkSQLDatabaseSchema* schema, int tblHandle, int colHandle) override;
202 
208  bool ParseURL(const char* url) override;
209 
210 protected:
212  ~vtkPostgreSQLDatabase() override;
213 
228  void UpdateDataTypeMap();
229 
230  vtkSetStringMacro(DatabaseType);
231  vtkSetStringMacro(LastErrorText);
232  void NullTrailingWhitespace(char* msg);
233  bool OpenInternal(const char* connectionOptions);
234 
240  char* HostName;
241  char* User;
242  char* Password;
247 
248 private:
250  void operator=(const vtkPostgreSQLDatabase&) = delete;
251 };
252 
253 // This is basically the body of the SetStringMacro but with a
254 // call to update an additional vtkTimeStamp. We inline the implementation
255 // so that wrapping will work.
256 #define vtkSetStringPlusMTimeMacro(className, name, timeStamp) \
257  inline void className::Set##name(const char* _arg) \
258  { \
259  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting " << #name " to " \
260  << (_arg ? _arg : "(null)")); \
261  if (this->name == nullptr && _arg == nullptr) \
262  { \
263  return; \
264  } \
265  if (this->name && _arg && (!strcmp(this->name, _arg))) \
266  { \
267  return; \
268  } \
269  delete[] this->name; \
270  if (_arg) \
271  { \
272  size_t n = strlen(_arg) + 1; \
273  char* cp1 = new char[n]; \
274  const char* cp2 = (_arg); \
275  this->name = cp1; \
276  do \
277  { \
278  *cp1++ = *cp2++; \
279  } while (--n); \
280  } \
281  else \
282  { \
283  this->name = nullptr; \
284  } \
285  this->Modified(); \
286  this->timeStamp.Modified(); \
287  this->Close(); /* Force a re-open on next query */ \
288  }
289 
293 vtkSetStringPlusMTimeMacro(vtkPostgreSQLDatabase, DatabaseName, URLMTime);
294 vtkSetStringPlusMTimeMacro(vtkPostgreSQLDatabase, ConnectOptions, URLMTime);
295 
297 {
298  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting ServerPort to " << _arg);
299  if (this->ServerPort != (_arg < 0 ? 0 : (_arg > VTK_INT_MAX ? VTK_INT_MAX : _arg)))
300  {
301  this->ServerPort = (_arg < 0 ? 0 : (_arg > VTK_INT_MAX ? VTK_INT_MAX : _arg));
302  this->Modified();
303  this->URLMTime.Modified();
304  this->Close(); // Force a re-open on next query
305  }
306 }
307 
308 #endif // vtkPostgreSQLDatabase_h
vtkPostgreSQLDatabase::Password
char * Password
Definition: vtkPostgreSQLDatabase.h:242
vtkSQLDatabase::GetRecord
virtual vtkStringArray * GetRecord(const char *table)=0
Get the list of fields for a particular table.
vtkPostgreSQLDatabasePrivate
internal details of a connection to a PostgreSQL database
Definition: vtkPostgreSQLDatabasePrivate.h:46
vtkSQLDatabase::IsSupported
virtual bool IsSupported(int vtkNotUsed(feature))
Return whether a feature is supported by the database.
Definition: vtkSQLDatabase.h:145
VTK_INT_MAX
#define VTK_INT_MAX
Definition: vtkType.h:147
vtkSQLDatabase::Close
virtual void Close()=0
Close the connection to the database.
vtkSQLDatabase::HasError
virtual bool HasError()=0
Did the last operation generate an error.
vtkPostgreSQLDatabase::HostName
char * HostName
Definition: vtkPostgreSQLDatabase.h:240
vtkPostgreSQLDatabase::GetServerPortMinValue
virtual int GetServerPortMinValue()
Definition: vtkPostgreSQLDatabase.h:149
vtkObject::New
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
vtkTimeStamp
record modification and/or execution time
Definition: vtkTimeStamp.h:32
vtkObject::Modified
virtual void Modified()
Update the modification time for this object.
vtkPostgreSQLDatabase
maintain a connection to a PostgreSQL database
Definition: vtkPostgreSQLDatabase.h:57
vtkPostgreSQLDatabase::DatabaseType
char * DatabaseType
Definition: vtkPostgreSQLDatabase.h:239
vtkPostgreSQLDatabase::URLMTime
vtkTimeStamp URLMTime
Definition: vtkPostgreSQLDatabase.h:235
vtkSQLDatabase::GetQueryInstance
virtual vtkSQLQuery * GetQueryInstance()=0
Return an empty query on this database.
vtkPostgreSQLDatabase::ServerPort
int ServerPort
Definition: vtkPostgreSQLDatabase.h:244
vtkPostgreSQLDatabase::Connection
vtkPostgreSQLDatabasePrivate * Connection
Definition: vtkPostgreSQLDatabase.h:236
vtkSQLDatabase.h
vtkPostgreSQLDatabase::SetServerPort
virtual void SetServerPort(int)
The port used for connecting to the database.
Definition: vtkPostgreSQLDatabase.h:296
vtkSQLDatabase::IsOpen
virtual bool IsOpen()=0
Return whether the database has an open connection.
vtkPostgreSQLDatabase::User
char * User
Definition: vtkPostgreSQLDatabase.h:241
vtkPostgreSQLDatabase::Tables
vtkStringArray * Tables
Definition: vtkPostgreSQLDatabase.h:238
vtkSQLDatabase::Open
virtual bool Open(const char *password)=0
Open a new connection to the database.
vtkPostgreSQLDatabase::ConnectOptions
char * ConnectOptions
Definition: vtkPostgreSQLDatabase.h:245
vtkObjectBase::GetClassName
const char * GetClassName() const
Return the class name as a string.
vtkTimeStamp::Modified
void Modified()
Set this objects time to the current time.
vtkSQLDatabase::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkPostgreSQLDatabase::ConnectionMTime
vtkTimeStamp ConnectionMTime
Definition: vtkPostgreSQLDatabase.h:237
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkPostgreSQLQuery
vtkSQLQuery implementation for PostgreSQL databases
Definition: vtkPostgreSQLQuery.h:50
vtkPostgreSQLDatabase::Close
void Close() override
Close the connection to the database.
vtkPostgreSQLDatabase::GetServerPortMaxValue
virtual int GetServerPortMaxValue()
Definition: vtkPostgreSQLDatabase.h:150
vtkX3D::url
Definition: vtkX3D.h:239
vtkSQLDatabase::GetTables
virtual vtkStringArray * GetTables()=0
Get the list of tables from the database.
vtkSQLDatabaseSchema
represent an SQL database schema
Definition: vtkSQLDatabaseSchema.h:61
vtkSQLDatabase::GetURL
virtual vtkStdString GetURL()=0
Get the URL of the database.
vtkSQLDatabase::GetColumnSpecification
virtual vtkStdString GetColumnSpecification(vtkSQLDatabaseSchema *schema, int tblHandle, int colHandle)
Return the SQL string with the syntax to create a column inside a "CREATE TABLE" SQL statement.
vtkPostgreSQLDatabase::LastErrorText
char * LastErrorText
Definition: vtkPostgreSQLDatabase.h:246
vtkStringArray
a vtkAbstractArray subclass for strings
Definition: vtkStringArray.h:36
vtkPostgreSQLDatabase::GetDatabaseType
const char * GetDatabaseType() override
String representing database type (e.g.
Definition: vtkPostgreSQLDatabase.h:104
vtkSQLDatabase::ParseURL
virtual bool ParseURL(const char *url)=0
Subclasses should override this method to determine connection parameters given the URL.
vtkSetStringPlusMTimeMacro
#define vtkSetStringPlusMTimeMacro(className, name, timeStamp)
Definition: vtkPostgreSQLDatabase.h:256
vtkStdString
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:34
vtkSQLDatabase::GetLastErrorText
virtual const char * GetLastErrorText()=0
Get the last error text from the database I'm using const so that people do NOT use the standard vtkG...
vtkSQLQuery
executes an sql query and retrieves results
Definition: vtkSQLQuery.h:68
vtkPostgreSQLDatabase::DatabaseName
char * DatabaseName
Definition: vtkPostgreSQLDatabase.h:243
vtkSQLDatabase
maintain a connection to an sql database
Definition: vtkSQLDatabase.h:84