/
qgspostgresconn.h
323 lines (250 loc) · 9.33 KB
/
qgspostgresconn.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/***************************************************************************
qgspostgresconn.h - connection class to PostgreSQL/PostGIS
-------------------
begin : 2011/01/28
copyright : (C) 2011 by Juergen E. Fischer
email : jef at norbit dot de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSPOSTGRESCONN_H
#define QGSPOSTGRESCONN_H
#include <QString>
#include <QStringList>
#include <QVector>
#include <QMap>
#include "qgis.h"
#include "qgsdatasourceuri.h"
extern "C"
{
#include <libpq-fe.h>
}
class QgsField;
enum QgsPostgresGeometryColumnType
{
sctNone,
sctGeometry,
sctGeography,
sctTopoGeometry
};
/** Layer Property structure */
// TODO: Fill to Postgres/PostGIS specifications
struct QgsPostgresLayerProperty
{
// Postgres/PostGIS layer properties
QList<QGis::WkbType> types;
QString schemaName;
QString tableName;
QString geometryColName;
QgsPostgresGeometryColumnType geometryColType;
QStringList pkCols;
QList<int> srids;
unsigned int nSpCols;
QString sql;
// TODO: rename this !
int size() const { Q_ASSERT( types.size() == srids.size() ); return types.size(); }
QString defaultName() const {
QString n = tableName;
if ( nSpCols > 1 ) n += "." + geometryColName;
return n;
}
QgsPostgresLayerProperty at( int i ) const
{
QgsPostgresLayerProperty property;
Q_ASSERT( i >= 0 && i < size() );
property.types << types[ i ];
property.srids << srids[ i ];
property.schemaName = schemaName;
property.tableName = tableName;
property.geometryColName = geometryColName;
property.geometryColType = geometryColType;
property.pkCols = pkCols;
property.nSpCols = nSpCols;
property.sql = sql;
return property;
}
#if QGISDEBUG
QString toString() const
{
QString typeString;
foreach ( QGis::WkbType type, types )
{
if ( !typeString.isEmpty() )
typeString += "|";
typeString += QString::number( type );
}
QString sridString;
foreach ( int srid, srids )
{
if ( !sridString.isEmpty() )
sridString += "|";
sridString += QString::number( srid );
}
return QString( "%1.%2.%3 type=%4 srid=%5 pkCols=%6 sql=%7 nSpCols=%8" )
.arg( schemaName )
.arg( tableName )
.arg( geometryColName )
.arg( typeString )
.arg( sridString )
.arg( pkCols.join( "|" ) )
.arg( sql )
.arg( nSpCols );
}
#endif
};
class QgsPostgresResult
{
public:
QgsPostgresResult( PGresult *theRes = 0 ) : mRes( theRes ) {}
~QgsPostgresResult();
QgsPostgresResult &operator=( PGresult *theRes );
QgsPostgresResult &operator=( const QgsPostgresResult &src );
ExecStatusType PQresultStatus();
QString PQresultErrorMessage();
int PQntuples();
QString PQgetvalue( int row, int col );
bool PQgetisnull( int row, int col );
int PQnfields();
QString PQfname( int col );
int PQftable( int col );
int PQftype( int col );
int PQftablecol( int col );
Oid PQoidValue();
PGresult *result() const { return mRes; }
private:
PGresult *mRes;
};
class QgsPostgresConn : public QObject
{
Q_OBJECT;
public:
static QgsPostgresConn *connectDb( QString connInfo, bool readOnly );
void disconnect();
//! get postgis version string
QString postgisVersion();
//! get status of GEOS capability
bool hasGEOS();
//! get status of topology capability
bool hasTopology();
//! get status of GIST capability
bool hasGIST();
//! get status of PROJ4 capability
bool hasPROJ();
//! encode wkb in hex
bool useWkbHex() { return mUseWkbHex; }
//! major PostGIS version
int majorVersion() { return mPostgisVersionMajor; }
//! minor PostGIS version
int minorVersion() { return mPostgisVersionMinor; }
//! PostgreSQL version
int pgVersion() { return mPostgresqlVersion; }
//! run a query and free result buffer
bool PQexecNR( QString query, bool retry = true );
//! cursor handling
bool openCursor( QString cursorName, QString declare );
bool closeCursor( QString cursorName );
#if 0
PGconn *pgConnection() { return mConn; }
#endif
//
// libpq wrapper
//
// run a query and check for errors
PGresult *PQexec( QString query, bool logError = true );
void PQfinish();
QString PQerrorMessage();
int PQsendQuery( QString query );
int PQstatus();
PGresult *PQgetResult();
PGresult *PQprepare( QString stmtName, QString query, int nParams, const Oid *paramTypes );
PGresult *PQexecPrepared( QString stmtName, const QStringList ¶ms );
// cancel running query
bool cancel();
/** Double quote a PostgreSQL identifier for placement in a SQL string.
*/
static QString quotedIdentifier( QString ident );
/** Quote a value for placement in a SQL string.
*/
static QString quotedValue( QVariant value );
//! Get the list of supported layers
bool supportedLayers( QVector<QgsPostgresLayerProperty> &layers,
bool searchGeometryColumnsOnly = true,
bool searchPublicOnly = true,
bool allowGeometrylessTables = false );
void retrieveLayerTypes( QgsPostgresLayerProperty &layerProperty, bool useEstimatedMetadata );
/** Gets information about the spatial tables */
bool getTableInfo( bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables );
qint64 getBinaryInt( QgsPostgresResult &queryResult, int row, int col );
QString fieldExpression( const QgsField &fld );
QString connInfo() const { return mConnInfo; }
static const int sGeomTypeSelectLimit;
static QString displayStringForWkbType( QGis::WkbType wkbType );
static QString displayStringForGeomType( QgsPostgresGeometryColumnType geomType );
static QGis::WkbType wkbTypeFromPostgis( QString dbType );
static QString postgisWkbTypeName( QGis::WkbType wkbType );
static int postgisWkbTypeDim( QGis::WkbType wkbType );
static void postgisWkbType( QGis::WkbType wkbType, QString &geometryType, int &dim );
static QString postgisTypeFilter( QString geomCol, QGis::WkbType wkbType, bool isGeography );
static QGis::WkbType wkbTypeFromGeomType( QGis::GeometryType geomType );
static QStringList connectionList();
static QString selectedConnection();
static void setSelectedConnection( QString theConnName );
static QgsDataSourceURI connUri( QString theConnName );
static bool publicSchemaOnly( QString theConnName );
static bool geometryColumnsOnly( QString theConnName );
static bool dontResolveType( QString theConnName );
static bool allowGeometrylessTables( QString theConnName );
static void deleteConnection( QString theConnName );
private:
QgsPostgresConn( QString conninfo, bool readOnly );
~QgsPostgresConn();
int mRef;
int mOpenCursors;
PGconn *mConn;
QString mConnInfo;
//! GEOS capability
bool mGeosAvailable;
//! Topology capability
bool mTopologyAvailable;
//! PostGIS version string
QString mPostgisVersionInfo;
//! Are mPostgisVersionMajor, mPostgisVersionMinor, mGeosAvailable, mGistAvailable, mProjAvailable, mTopologyAvailable valid?
bool mGotPostgisVersion;
//! PostgreSQL version
int mPostgresqlVersion;
//! PostGIS major version
int mPostgisVersionMajor;
//! PostGIS minor version
int mPostgisVersionMinor;
//! GIST capability
bool mGistAvailable;
//! PROJ4 capability
bool mProjAvailable;
//! encode wkb in hex
bool mUseWkbHex;
bool mReadOnly;
static QMap<QString, QgsPostgresConn *> sConnectionsRW;
static QMap<QString, QgsPostgresConn *> sConnectionsRO;
/** count number of spatial columns in a given relation */
void addColumnInfo( QgsPostgresLayerProperty& layerProperty, const QString& schemaName, const QString& viewName, bool fetchPkCandidates );
//! List of the supported layers
QVector<QgsPostgresLayerProperty> mLayersSupported;
/**
* Flag indicating whether data from binary cursors must undergo an
* endian conversion prior to use
@note
XXX Umm, it'd be helpful to know what we're swapping from and to.
XXX Presumably this means swapping from big-endian (network) byte order
XXX to little-endian; but the inverse transaction is possible, too, and
XXX that's not reflected in this variable
*/
bool mSwapEndian;
void deduceEndian();
};
#endif