1 /***************************************************************************
2   qgsdb2featureiterator.h - DB2 spatial feature processing
3   --------------------------------------
4   Date      : 2016-01-27
5   Copyright : (C) 2016 by David Adler
6                           Shirley Xiao, David Nguyen
7   Email     : dadler at adtechgeospatial.com
8               xshirley2012 at yahoo.com, davidng0123 at gmail.com
9 ****************************************************************************
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  ***************************************************************************/
17 
18 #ifndef QGSDB2FEATUREITERATOR_H
19 #define QGSDB2FEATUREITERATOR_H
20 
21 #include "qgsfields.h"
22 #include "qgsfeatureiterator.h"
23 #include <QtSql/QSqlDatabase>
24 #include <QtSql/QSqlQuery>
25 #include <QtSql/QSqlError>
26 
27 class QgsDb2Provider;
28 
29 class QgsDb2FeatureSource final: public QgsAbstractFeatureSource
30 {
31   public:
32     explicit QgsDb2FeatureSource( const QgsDb2Provider *p );
33 
34     QgsFeatureIterator getFeatures( const QgsFeatureRequest &request ) override;
35 
36   private:
37     QgsFields mFields;
38     QString mFidColName;
39     long mSRId;
40 
41     QString mGeometryColName;
42     QString mGeometryColType;
43 
44     // current layer name
45     QString mSchemaName;
46     QString mTableName;
47 
48     // server access
49     QString mConnInfo;
50 
51     // SQL statement used to limit the features retrieved
52     QString mSqlWhereClause;
53 
54     QgsCoordinateReferenceSystem mCrs;
55 
56     // Return True if this feature source has spatial attributes.
isSpatial()57     bool isSpatial() { return !mGeometryColName.isEmpty() || !mGeometryColType.isEmpty(); }
58 
59     friend class QgsDb2FeatureIterator;
60     friend class QgsDb2ExpressionCompiler;
61 };
62 
63 class QgsDb2FeatureIterator final: public QgsAbstractFeatureIteratorFromSource<QgsDb2FeatureSource>
64 {
65   public:
66     QgsDb2FeatureIterator( QgsDb2FeatureSource *source, bool ownSource, const QgsFeatureRequest &request );
67 
68     ~QgsDb2FeatureIterator() override;
69 
70     bool rewind() override;
71     bool close() override;
72 
73   protected:
74     void BuildStatement( const QgsFeatureRequest &request );
75 
76     bool fetchFeature( QgsFeature &feature ) override;
77     bool nextFeatureFilterExpression( QgsFeature &f ) override;
78 
79   private:
80 
81     bool prepareOrderBy( const QList<QgsFeatureRequest::OrderByClause> &orderBys ) override;
82 
83 
84     // The current database
85     QSqlDatabase mDatabase;
86     QString mOrderByClause;
87 
88     // The current sql query
89     std::unique_ptr< QSqlQuery > mQuery;
90 
91     // The current sql statement
92     QString mStatement;
93 
94     // Field index of FID column
95     long mFidCol;
96 
97     // List of attribute indices to fetch with nextFeature calls
98     QgsAttributeList mAttributesToFetch;
99 
100     bool mExpressionCompiled;
101     bool mOrderByCompiled;
102 
103     int mFetchCount = 0;
104 
105     QgsCoordinateTransform mTransform;
106     QgsRectangle mFilterRect;
107 };
108 
109 #endif // QGSDB2FEATUREITERATOR_H
110