VTK
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 -------------------------------------------------------------------------*/
49 #ifndef vtkPostgreSQLDatabase_h
50 #define vtkPostgreSQLDatabase_h
51 
52 #include "vtkIOPostgreSQLModule.h" // For export macro
53 #include "vtkSQLDatabase.h"
54 
55 class vtkPostgreSQLQuery;
56 class vtkStringArray;
58 struct PQconn;
59 
60 class VTKIOPOSTGRESQL_EXPORT vtkPostgreSQLDatabase : public vtkSQLDatabase
61 {
62 
63  friend class vtkPostgreSQLQuery;
64  friend class vtkPostgreSQLQueryPrivate;
65 
66 public:
68  void PrintSelf(ostream& os, vtkIndent indent) override;
69  static vtkPostgreSQLDatabase *New();
70 
76  bool Open( const char* password = 0 ) override;
77 
81  void Close() override;
82 
86  bool IsOpen() override;
87 
91  vtkSQLQuery* GetQueryInstance() override;
92 
96  virtual bool HasError() override;
97 
101  const char* GetLastErrorText() override;
102 
104 
107  const char* GetDatabaseType() override
108  {
109  return this->DatabaseType;
110  }
112 
114 
117  virtual void SetHostName( const char* );
118  vtkGetStringMacro(HostName);
120 
122 
125  virtual void SetUser( const char* );
126  vtkGetStringMacro(User);
128 
132  virtual void SetPassword( const char* );
133 
135 
138  virtual void SetDatabaseName( const char* );
139  vtkGetStringMacro(DatabaseName);
141 
143 
146  virtual void SetConnectOptions( const char* );
147  vtkGetStringMacro(ConnectOptions);
149 
151 
154  virtual void SetServerPort( int );
155  virtual int GetServerPortMinValue()
156  {
157  return 0;
158  }
159  virtual int GetServerPortMaxValue()
160  {
161  return VTK_INT_MAX;
162  }
163  vtkGetMacro(ServerPort, int);
165 
172  vtkStdString GetURL() override;
173 
177  vtkStringArray* GetTables() override;
178 
182  vtkStringArray* GetRecord( const char* table ) override;
183 
187  bool IsSupported( int feature ) override;
188 
192  vtkStringArray* GetDatabases();
193 
198  bool CreateDatabase( const char* dbName, bool dropExisting = false );
199 
204  bool DropDatabase( const char* dbName );
205 
213  vtkSQLDatabaseSchema* schema, int tblHandle, int colHandle ) override;
214 
220  bool ParseURL(const char* url) override;
221 
222 protected:
225 
240  void UpdateDataTypeMap();
241 
242  vtkSetStringMacro(DatabaseType);
243  vtkSetStringMacro(LastErrorText);
244  void NullTrailingWhitespace( char* msg );
245  bool OpenInternal( const char* connectionOptions );
246 
252  char* HostName;
253  char* User;
254  char* Password;
259 
260 private:
261  vtkPostgreSQLDatabase( const vtkPostgreSQLDatabase& ) = delete;
262  void operator = ( const vtkPostgreSQLDatabase& ) = delete;
263 };
264 
265 // This is basically the body of the SetStringMacro but with a
266 // call to update an additional vtkTimeStamp. We inline the implementation
267 // so that wrapping will work.
268 #define vtkSetStringPlusMTimeMacro(className,name,timeStamp) \
269  inline void className::Set##name (const char* _arg) \
270  { \
271  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting " << #name " to " << (_arg?_arg:"(null)") ); \
272  if ( this->name == nullptr && _arg == nullptr) { return;} \
273  if ( this->name && _arg && (!strcmp(this->name,_arg))) { return;} \
274  delete [] this->name; \
275  if (_arg) \
276  { \
277  size_t n = strlen(_arg) + 1; \
278  char *cp1 = new char[n]; \
279  const char *cp2 = (_arg); \
280  this->name = cp1; \
281  do { *cp1++ = *cp2++; } while ( --n ); \
282  } \
283  else \
284  { \
285  this->name = nullptr; \
286  } \
287  this->Modified(); \
288  this->timeStamp.Modified(); \
289  this->Close(); /* Force a re-open on next query */ \
290  }
291 
296 vtkSetStringPlusMTimeMacro(vtkPostgreSQLDatabase,ConnectOptions,URLMTime);
297 
299 {
300  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting ServerPort to " << _arg );
301  if ( this->ServerPort != ( _arg < 0 ? 0 : ( _arg > VTK_INT_MAX ? VTK_INT_MAX : _arg ) ) )
302  {
303  this->ServerPort = ( _arg < 0 ? 0 : ( _arg > VTK_INT_MAX ? VTK_INT_MAX : _arg ) );
304  this->Modified();
305  this->URLMTime.Modified();
306  this->Close(); // Force a re-open on next query
307  }
308 }
309 
310 #endif // vtkPostgreSQLDatabase_h
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:40
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...
const char * GetDatabaseType() override
String representing database type (e.g.
maintain a connection to a PostgreSQL database
virtual bool IsSupported(int vtkNotUsed(feature))
Return whether a feature is supported by the database.
#define VTK_INT_MAX
Definition: vtkType.h:159
record modification and/or execution time
Definition: vtkTimeStamp.h:35
virtual bool Open(const char *password)=0
Open a new connection to the database.
virtual bool ParseURL(const char *url)=0
Subclasses should override this method to determine connection parameters given the URL...
internal details of a connection to a PostgreSQL database
virtual bool HasError()=0
Did the last operation generate an error.
a vtkAbstractArray subclass for strings
virtual vtkStdString GetURL()=0
Get the URL of the database.
represent an SQL database schema
virtual vtkStringArray * GetRecord(const char *table)=0
Get the list of fields for a particular table.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
#define vtkSetStringPlusMTimeMacro(className, name, timeStamp)
virtual int GetServerPortMaxValue()
The port used for connecting to the database.
virtual int GetServerPortMinValue()
The port used for connecting to the database.
maintain a connection to an sql database
virtual void Close()=0
Close the connection to the database.
vtkPostgreSQLDatabasePrivate * Connection
a simple class to control print indentation
Definition: vtkIndent.h:39
const char * GetClassName() const
Return the class name as a string.
vtkSQLQuery implementation for PostgreSQL databases
virtual vtkSQLQuery * GetQueryInstance()=0
Return an empty query on this database.
virtual bool IsOpen()=0
Return whether the database has an open connection.
virtual void Modified()
Update the modification time for this object.
virtual const char * GetLastErrorText()=0
Get the last error text from the database I&#39;m using const so that people do NOT use the standard vtkG...
executes an sql query and retrieves results
Definition: vtkSQLQuery.h:74
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
virtual vtkStringArray * GetTables()=0
Get the list of tables from the database.
virtual void SetServerPort(int)
The port used for connecting to the database.