VTK  9.7.1
vtkPostgreSQLDatabase.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-FileCopyrightText: Copyright 2008 Sandia Corporation
3 // SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov
32 #ifndef vtkPostgreSQLDatabase_h
33 #define vtkPostgreSQLDatabase_h
34 
35 #include "vtkIOPostgreSQLModule.h" // For export macro
36 #include "vtkSQLDatabase.h"
37 
38 VTK_ABI_NAMESPACE_BEGIN
39 class vtkPostgreSQLQuery;
40 class vtkStringArray;
42 struct PQconn;
43 
44 class VTKIOPOSTGRESQL_EXPORT vtkPostgreSQLDatabase : public vtkSQLDatabase
45 {
46 
47  friend class vtkPostgreSQLQuery;
48  friend class vtkPostgreSQLQueryPrivate;
49 
50 public:
52  void PrintSelf(ostream& os, vtkIndent indent) override;
53  static vtkPostgreSQLDatabase* New();
54 
60  bool Open(const char* password = nullptr) override;
61 
65  void Close() override;
66 
70  bool IsOpen() override;
71 
75  vtkSQLQuery* GetQueryInstance() override;
76 
80  bool HasError() override;
81 
85  const char* GetLastErrorText() override;
86 
88 
91  const char* GetDatabaseType() override { return this->DatabaseType; }
93 
95 
98  virtual void SetHostName(const char*);
99  vtkGetStringMacro(HostName);
101 
103 
106  virtual void SetUser(const char*);
107  vtkGetStringMacro(User);
109 
113  virtual void SetPassword(const char*);
114 
116 
119  virtual void SetDatabaseName(const char*);
120  vtkGetStringMacro(DatabaseName);
122 
124 
127  virtual void SetConnectOptions(const char*);
128  vtkGetStringMacro(ConnectOptions);
130 
132 
135  virtual void SetServerPort(int);
136  virtual int GetServerPortMinValue() { return 0; }
137  virtual int GetServerPortMaxValue() { return VTK_INT_MAX; }
138  vtkGetMacro(ServerPort, int);
140 
147  vtkStdString GetURL() override;
148 
152  vtkStringArray* GetTables() override;
153 
157  vtkStringArray* GetRecord(const char* table) override;
158 
162  bool IsSupported(int feature) override;
163 
167  vtkStringArray* GetDatabases();
168 
173  bool CreateDatabase(const char* dbName, bool dropExisting = false);
174 
179  bool DropDatabase(const char* dbName);
180 
190  vtkSQLDatabaseSchema* schema, int tblHandle, int colHandle) override;
191 
197  bool ParseURL(const char* url) override;
198 
199 protected:
201  ~vtkPostgreSQLDatabase() override;
202 
217  void UpdateDataTypeMap();
218 
219  vtkSetStringMacro(DatabaseType);
220  vtkSetStringMacro(LastErrorText);
221  void NullTrailingWhitespace(char* msg);
222  bool OpenInternal(const char* connectionOptions);
223 
229  char* HostName;
230  char* User;
231  char* Password;
236 
237 private:
239  void operator=(const vtkPostgreSQLDatabase&) = delete;
240 };
241 
242 // This is basically the body of the SetStringMacro but with a
243 // call to update an additional vtkTimeStamp. We inline the implementation
244 // so that wrapping will work.
245 #define vtkSetStringPlusMTimeMacro(className, name, timeStamp) \
246  inline void className::Set##name(const char* _arg) \
247  { \
248  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting " << #name " to " \
249  << (_arg ? _arg : "(null)")); \
250  if (this->name == nullptr && _arg == nullptr) \
251  { \
252  return; \
253  } \
254  if (this->name && _arg && (!strcmp(this->name, _arg))) \
255  { \
256  return; \
257  } \
258  delete[] this->name; \
259  if (_arg) \
260  { \
261  size_t n = strlen(_arg) + 1; \
262  char* cp1 = new char[n]; \
263  const char* cp2 = (_arg); \
264  this->name = cp1; \
265  do \
266  { \
267  *cp1++ = *cp2++; \
268  } while (--n); \
269  } \
270  else \
271  { \
272  this->name = nullptr; \
273  } \
274  this->Modified(); \
275  this->timeStamp.Modified(); \
276  this->Close(); /* Force a re-open on next query */ \
277  }
278 
282 vtkSetStringPlusMTimeMacro(vtkPostgreSQLDatabase, DatabaseName, URLMTime);
283 vtkSetStringPlusMTimeMacro(vtkPostgreSQLDatabase, ConnectOptions, URLMTime);
284 
286 {
287  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting ServerPort to " << _arg);
288  _arg = std::min(std::max(_arg, 0), VTK_INT_MAX);
289  if (this->ServerPort != _arg)
290  {
291  this->ServerPort = _arg;
292  this->Modified();
293  this->URLMTime.Modified();
294  this->Close(); // Force a re-open on next query
295  }
296 }
297 
298 VTK_ABI_NAMESPACE_END
299 #endif // vtkPostgreSQLDatabase_h
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:27
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
#define VTK_INT_MAX
Definition: vtkType.h:192
record modification and/or execution time
Definition: vtkTimeStamp.h:24
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
void Modified()
Set this objects time to the current time.
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.
virtual bool IsSupported(int feature)
Return whether a feature is supported by the database.
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:28
const char * GetClassName() const
Return the class name as a string.
vtkSQLQuery implementation for PostgreSQL databases
void Close() override
Close the connection to the database.
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.
#define vtkSetStringPlusMTimeMacro(className, name, timeStamp)
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...
executes an sql query and retrieves results
Definition: vtkSQLQuery.h:59
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.
#define max(a, b)