3232
3333
3434QgsOgrFeatureIterator::QgsOgrFeatureIterator ( QgsOgrProvider* p, const QgsFeatureRequest& request )
35- : QgsAbstractFeatureIterator( request ), P( p )
35+ : QgsAbstractFeatureIterator( request ), P( p ), ogrDataSource( 0 ), ogrLayer( 0 ), mSubsetStringSet( false )
3636{
37- // make sure that only one iterator is active
38- if ( P->mActiveIterator )
37+ mFeatureFetched = false ;
38+ P->mActiveIterators .insert ( this );
39+
40+ ogrDataSource = OGROpen ( TO8F ( P->filePath () ), false , NULL );
41+
42+ if ( P->layerName ().isNull () )
3943 {
40- QgsMessageLog::logMessage ( QObject::tr ( " Already active iterator on this provider was closed." ), QObject::tr ( " OGR" ) );
41- P->mActiveIterator ->close ();
44+ ogrLayer = OGR_DS_GetLayer ( ogrDataSource, P->layerIndex () );
45+ }
46+ else
47+ {
48+ ogrLayer = OGR_DS_GetLayerByName ( ogrDataSource, TO8 ( p->layerName () ) );
4249 }
43- P->mActiveIterator = this ;
4450
45- mFeatureFetched = false ;
51+ if ( !P->subsetString ().isEmpty () )
52+ {
53+ QString sql = QString ( " SELECT * FROM %1 WHERE %2" )
54+ .arg ( P->quotedIdentifier ( FROM8 ( OGR_FD_GetName ( OGR_L_GetLayerDefn ( ogrLayer ) ) ) ) )
55+ .arg ( P->subsetString () );
56+ QgsDebugMsg ( QString ( " SQL: %1" ).arg ( sql ) );
57+ ogrLayer = OGR_DS_ExecuteSQL ( ogrDataSource, P->textEncoding ()->fromUnicode ( sql ).constData (), NULL , NULL );
58+ mSubsetStringSet = true ;
59+ }
4660
4761 ensureRelevantFields ();
4862
@@ -56,12 +70,12 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrProvider* p, const QgsFeatur
5670
5771 OGR_G_CreateFromWkt (( char ** )&wktText, NULL , &filter );
5872 QgsDebugMsg ( " Setting spatial filter using " + wktExtent );
59- OGR_L_SetSpatialFilter ( P-> ogrLayer , filter );
73+ OGR_L_SetSpatialFilter ( ogrLayer, filter );
6074 OGR_G_DestroyGeometry ( filter );
6175 }
6276 else
6377 {
64- OGR_L_SetSpatialFilter ( P-> ogrLayer , 0 );
78+ OGR_L_SetSpatialFilter ( ogrLayer, 0 );
6579 }
6680
6781 // start with first feature
@@ -94,7 +108,7 @@ bool QgsOgrFeatureIterator::nextFeature( QgsFeature& feature )
94108
95109 if ( mRequest .filterType () == QgsFeatureRequest::FilterFid )
96110 {
97- OGRFeatureH fet = OGR_L_GetFeature ( P-> ogrLayer , FID_TO_NUMBER ( mRequest .filterFid () ) );
111+ OGRFeatureH fet = OGR_L_GetFeature ( ogrLayer, FID_TO_NUMBER ( mRequest .filterFid () ) );
98112 if ( !fet )
99113 {
100114 close ();
@@ -111,7 +125,7 @@ bool QgsOgrFeatureIterator::nextFeature( QgsFeature& feature )
111125
112126 OGRFeatureH fet;
113127
114- while (( fet = OGR_L_GetNextFeature ( P-> ogrLayer ) ) )
128+ while (( fet = OGR_L_GetNextFeature ( ogrLayer ) ) )
115129 {
116130 if ( !readFeature ( fet, feature ) )
117131 continue ;
@@ -135,7 +149,7 @@ bool QgsOgrFeatureIterator::rewind()
135149 if ( mClosed )
136150 return false ;
137151
138- OGR_L_ResetReading ( P-> ogrLayer );
152+ OGR_L_ResetReading ( ogrLayer );
139153
140154 return true ;
141155}
@@ -146,10 +160,17 @@ bool QgsOgrFeatureIterator::close()
146160 if ( mClosed )
147161 return false ;
148162
149- // tell provider that this iterator is not active anymore
150- P->mActiveIterator = 0 ;
163+ P->mActiveIterators .remove ( this );
164+
165+ if (mSubsetStringSet )
166+ {
167+ OGR_DS_ReleaseResultSet (ogrDataSource, ogrLayer );
168+ }
169+
170+ OGR_DS_Destroy ( ogrDataSource );
151171
152172 mClosed = true ;
173+ ogrDataSource = 0 ;
153174 return true ;
154175}
155176
0 commit comments