Version: 0.6.0
postgisdataset.h
1 /******************************************************************************
2  * Project: wxGIS (GIS Catalog)
3  * Purpose: PostresDataSource class.
4  * Author: Dmitry Baryshnikov (aka Bishop), polimax@mail.ru
5  ******************************************************************************
6 * Copyright (C) 2010-2013 Dmitry Baryshnikov
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  ****************************************************************************/
21 #pragma once
22 
23 #include "wxgis/datasource/datasource.h"
24 
25 #ifdef wxGIS_USE_POSTGRES
26 
27 #include "libpq-fe.h"
28 #include "wxgis/datasource/featuredataset.h"
29 #include "wxgis/datasource/filter.h"
30 
31 class OGRPGDataSource : public OGRDataSource
32 {
33  typedef struct
34  {
35  int nMajor;
36  int nMinor;
37  int nRelease;
38  } PGver;
39 
40  void **papoLayers;
41  int nLayers;
42 
43  char *pszName;
44  char *pszDBName;
45 
46  int bDSUpdate;
47  int bHavePostGIS;
48  int bHaveGeography;
49 
50  int nSoftTransactionLevel;
51 
52  PGconn *hPGConn;
53 
54  int DeleteLayer(int iLayer);
55 
56  Oid nGeometryOID;
57  Oid nGeographyOID;
58 
59  // We maintain a list of known SRID to reduce the number of trips to
60  // the database to get SRSes.
61  int nKnownSRID;
62  int *panSRID;
63  OGRSpatialReference **papoSRS;
64 
65  void *poLayerInCopyMode;
66 
67  CPLString osCurrentSchema;
68 
69  int nUndefinedSRID;
70 
71 public:
72  PGver sPostgreSQLVersion;
73  PGver sPostGISVersion;
74 
75  int bUseBinaryCursor;
76  int bBinaryTimeFormatIsInt8;
77  int bUseEscapeStringSyntax;
78 
79 public:
80  PGconn *GetPGConn() { return hPGConn; }
81 };
82 
89 class WXDLLIMPEXP_GIS_DS wxGISPostgresDataSource :
90  public wxGISDataset
91 {
92  DECLARE_CLASS(wxGISPostgresDataSource)
93 public:
94  wxGISPostgresDataSource(const wxString &sName, const wxString &sPass, const wxString &sPort = wxT("5432"), const wxString &sAddres = wxT("localhost"), const wxString &sDBName = wxT("postgres"), const wxString &sConnectTimeout = wxT("30"), bool bIsBinaryCursor = false);
95  wxGISPostgresDataSource(const CPLString &szPath);
96  virtual ~wxGISPostgresDataSource(void);
97 
98  //wxGISDataset
99  virtual void Close(void);
100  virtual size_t GetSubsetsCount(void) const;
101  virtual wxGISDataset* GetSubset(size_t nIndex);
102  virtual wxGISDataset* GetSubset(const wxString &sTableName);
103  virtual wxString GetName(void) const;
104  virtual bool Open(bool bUpdate = true, bool bShared = true);
105  //wxGISPostGISDataset
106  bool ExecuteSQL(const wxString &sStatement);
107  bool CreateDatabase(const wxString &sDBName, const wxString &sTemplate = wxT("template_postgis"), const wxString &sOwner = wxT("postgres"), const wxString &sEncoding = wxT("UTF8"));
108  wxGISDataset* ExecuteSQL2(const wxString &sStatement, const wxString &sDialect = wxT("OGRSQL"));
109  //the geometry in spatial filter should have the same SpaRef as the target layer
110  wxGISDataset* ExecuteSQL2(const wxGISSpatialFilter &SpatialFilter, const wxString &sDialect = wxT("OGRSQL"));
111 
112  //bool PGExecuteSQL(const wxString &sStatement);
113  bool CreateSchema(const wxString &sSchemaName);
114  bool DeleteSchema(const wxString &sSchemaName);
115  bool RenameSchema(const wxString &sSchemaName, const wxString &sSchemaNewName);
116  bool RenameTable(const wxString &sSchemaName, const wxString &sTableName, const wxString &sTableNewName, const wxString &sPkIdxName = wxEmptyString, const wxString &sGeomIdxName = wxEmptyString);
117  bool MoveTable(const wxString &sTableName, const wxString &sSchemaName, const wxString &sSchemaNewName);
118  //wxGISDataset
119  virtual bool Rename(const wxString &sNewName);
120  virtual bool Copy(const CPLString &szDestPath, ITrackCancel* const pTrackCancel = NULL);
121  virtual bool Move(const CPLString &szDestPath, ITrackCancel* const pTrackCancel = NULL);
122  virtual void Cache(ITrackCancel* const pTrackCancel = NULL);
123  virtual char **GetFileList();
124  //
125  virtual OGRDataSource* const GetDataSourceRef(void) const { return m_poDS; };
126  virtual wxFontEncoding GetEncoding() const { return m_Encoding; };
127  static wxString NormalizeTableName(const wxString &sSrcName);
128  virtual void ReadConnectionFile();
129 protected:
130  wxGISDataset* GetDatasetFromOGRLayer(const CPLString &sPath, OGRLayer* poLayer);
131 protected:
132  OGRDataSource *m_poDS;
133  OGRDataSource *m_poDS4SQL;
134  wxString m_sName, m_sPass, m_sPort, m_sAddres, m_sDBName, m_sConnectTimeout;
135  bool m_bIsBinaryCursor, m_bPathPresent;
136  wxFontEncoding m_Encoding;
137 };
138 
145 class WXDLLIMPEXP_GIS_DS wxGISPostgresFeatureDataset :
146  public wxGISFeatureDataset
147 {
148  DECLARE_CLASS(wxGISPostgresFeatureDataset)
149 public:
150  wxGISPostgresFeatureDataset(const CPLString &sPath, OGRLayer* poLayer = NULL, OGRDataSource* poDS = NULL);
151  virtual ~wxGISPostgresFeatureDataset(void);
152  //wxGISDataset
153  virtual bool CanDelete(void);
154  virtual bool Delete(ITrackCancel* const pTrackCancel = NULL);
155 protected:
156  CPLString m_sLayerName;
157 };
158 
165 class WXDLLIMPEXP_GIS_DS wxGISPostgresTable :
166  public wxGISTable
167 {
168  DECLARE_CLASS(wxGISPostgresTable)
169 public:
170  wxGISPostgresTable(const CPLString &sPath, OGRLayer* poLayer = NULL, OGRDataSource* poDS = NULL);
171  virtual ~wxGISPostgresTable(void);
172  //wxGISDataset
173  virtual bool CanDelete(void);
174  virtual bool Delete(ITrackCancel* const pTrackCancel = NULL);
175 protected:
176  CPLString m_sLayerName;
177 };
178 
179 #endif //wxGIS_USE_POSTGRES
180 
Definition: table.h:37
A TrackCancel interface class.
Definition: core.h:144
Definition: dataset.h:33
Definition: filter.h:49
Definition: featuredataset.h:32