/
qgsdb2provider.h
163 lines (127 loc) · 5.21 KB
/
qgsdb2provider.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
/***************************************************************************
qgsdb2provider.h - Data provider for DB2 server
--------------------------------------
Date : 2016-01-27
Copyright : (C) 2016 by David Adler
Shirley Xiao, David Nguyen
Email : dadler at adtechgeospatial.com
xshirley2012 at yahoo.com, davidng0123 at gmail.com
****************************************************************************
*
* 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 QGSDB2PROVIDER_H
#define QGSDB2PROVIDER_H
#include "qgsvectordataprovider.h"
#include "qgsvectorlayerexporter.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsgeometry.h"
#include "qgsfields.h"
#include <QtSql>
#include <QMutex>
/**
* \class QgsDb2Provider
* \brief Data provider for DB2 server.
*/
class QgsDb2Provider : public QgsVectorDataProvider
{
Q_OBJECT
public:
explicit QgsDb2Provider( const QString &uri, const QgsDataProvider::ProviderOptions &options );
~QgsDb2Provider() override;
/**
* Returns a QSqlDatabase object that can connect to DB2 for LUW or z/OS.
*
* If service is provided, then username and password is required.
* If service is not provided, the remaining arguments are required.
*
* \param connInfo A string containing all connection information.
*/
static QSqlDatabase getDatabase( const QString &connInfo, QString &errMsg );
static bool openDatabase( QSqlDatabase db );
QgsAbstractFeatureSource *featureSource() const override;
QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
QgsWkbTypes::Type wkbType() const override;
long featureCount() const override;
/**
* Update the extent for this layer.
*/
void updateStatistics() const;
QgsFields fields() const override;
QgsCoordinateReferenceSystem crs() const override;
QgsRectangle extent() const override;
bool isValid() const override;
QString subsetString() const override;
bool setSubsetString( const QString &theSQL, bool updateFeatureCount = true ) override;
bool supportsSubsetString() const override { return true; }
QString name() const override;
QString description() const override;
QgsAttributeList pkAttributeIndexes() const override;
QgsVectorDataProvider::Capabilities capabilities() const override;
bool addFeatures( QgsFeatureList &flist, QgsFeatureSink::Flags flags = nullptr ) override;
bool deleteFeatures( const QgsFeatureIds &id ) override;
bool changeAttributeValues( const QgsChangedAttributesMap &attr_map ) override;
bool changeGeometryValues( const QgsGeometryMap &geometry_map ) override;
//! Import a vector layer into the database
static QgsVectorLayerExporter::ExportError createEmptyLayer(
const QString &uri,
const QgsFields &fields,
QgsWkbTypes::Type wkbType,
const QgsCoordinateReferenceSystem &srs,
bool overwrite,
QMap<int, int> *oldToNewAttrIdxMap,
QString *errorMessage = nullptr,
const QMap<QString, QVariant> *options = nullptr
);
//! Convert a QgsField to work with DB2
static bool convertField( QgsField &field );
//! Convert a QgsField to work with DB2
static QString qgsFieldToDb2Field( const QgsField &field );
protected:
//! Loads fields from input file to member attributeFields
QVariant::Type decodeSqlType( int typeId );
void loadMetadata();
void loadFields();
private:
static void db2WkbTypeAndDimension( QgsWkbTypes::Type wkbType, QString &geometryType, int &dim );
static QString db2TypeName( int typeId );
/**
* Returns a thread-safe connection name for use with QSqlDatabase
*/
static QString dbConnectionName( const QString &name );
static QMutex sMutex;
QgsFields mAttributeFields; //fields
QMap<int, QVariant> mDefaultValues;
mutable QgsRectangle mExtent; //layer extent
bool mValid;
bool mUseEstimatedMetadata;
bool mSkipFailures;
long mNumberFeatures = 0;
int mFidColIdx = -1;
QString mFidColName;
QString mExtents;
mutable long mSRId;
mutable int mEnvironment;
mutable QString mSrsName;
mutable QString mGeometryColName, mGeometryColType;
QString mLastError; //string containing the last reported error message
mutable QgsCoordinateReferenceSystem mCrs; //coordinate reference system
QgsWkbTypes::Type mWkbType = QgsWkbTypes::Unknown;
QSqlQuery mQuery; //current SQL query
QString mConnInfo; // full connection information
QString mSchemaName, mTableName; //current layer schema/name
QString mSqlWhereClause; //SQL statement used to limit the features retrieved
QSqlDatabase mDatabase; //the database object
static int sConnectionId;
//sets the error messages
void setLastError( const QString &error )
{
mLastError = error;
}
friend class QgsDb2FeatureSource;
};
#endif