Skip to content

Commit 0c88963

Browse files
committed
Set filter on iterator:
1 parent 6eadd80 commit 0c88963

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/providers/ogr/qgsogrfeatureiterator.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232

3333

3434
QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrProvider* p, const QgsFeatureRequest& request )
35-
: QgsAbstractFeatureIterator( request ), P( p ), ogrDataSource(0), ogrLayer(0), ogrDriver(0)
35+
: QgsAbstractFeatureIterator( request ), P( p ), ogrDataSource(0), ogrLayer(0), mSubsetStringSet(false)
3636
{
37+
mFeatureFetched = false;
3738

3839
ogrDataSource = OGROpen( TO8F( P->filePath() ), false, NULL );
3940

@@ -46,7 +47,15 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrProvider* p, const QgsFeatur
4647
ogrLayer = OGR_DS_GetLayerByName( ogrDataSource, TO8( p->layerName() ) );
4748
}
4849

49-
mFeatureFetched = false;
50+
if ( !P->subsetString().isEmpty() )
51+
{
52+
QString sql = QString( "SELECT * FROM %1 WHERE %2" )
53+
.arg( P->quotedIdentifier( FROM8( OGR_FD_GetName( OGR_L_GetLayerDefn( ogrLayer ) ) ) ) )
54+
.arg( P->subsetString() );
55+
QgsDebugMsg( QString( "SQL: %1" ).arg( sql ) );
56+
ogrLayer = OGR_DS_ExecuteSQL( ogrDataSource, P->textEncoding()->fromUnicode( sql ).constData(), NULL, NULL );
57+
mSubsetStringSet = true;
58+
}
5059

5160
ensureRelevantFields();
5261

@@ -150,6 +159,11 @@ bool QgsOgrFeatureIterator::close()
150159
if ( mClosed )
151160
return false;
152161

162+
if (mSubsetStringSet)
163+
{
164+
OGR_DS_ReleaseResultSet(ogrDataSource, ogrLayer );
165+
}
166+
153167
OGR_DS_Destroy( ogrDataSource );
154168

155169
mClosed = true;

src/providers/ogr/qgsogrfeatureiterator.h

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class QgsOgrFeatureIterator : public QgsAbstractFeatureIterator
5151

5252
OGRDataSourceH ogrDataSource;
5353
OGRLayerH ogrLayer;
54+
55+
bool mSubsetStringSet;
5456
};
5557

5658

src/providers/ogr/qgsogrprovider.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ email : sherman at mrcc.com
1515
* *
1616
***************************************************************************/
1717

18+
#include "QTextCodec"
19+
1820
#include "qgsrectangle.h"
1921
#include "qgsvectordataprovider.h"
2022
#include "qgsvectorfilewriter.h"
@@ -256,6 +258,10 @@ class QgsOgrProvider : public QgsVectorDataProvider
256258

257259
int layerIndex() { return mLayerIndex; }
258260

261+
QTextCodec* textEncoding() { return mEncoding; }
262+
263+
QString quotedIdentifier( QString field );
264+
259265
protected:
260266
/** loads fields from input file to member attributeFields */
261267
void loadFields();
@@ -329,8 +335,6 @@ class QgsOgrProvider : public QgsVectorDataProvider
329335
/**Deletes one feature*/
330336
bool deleteFeature( QgsFeatureId id );
331337

332-
QString quotedIdentifier( QString field );
333-
334338
/**Calls OGR_L_SyncToDisk and recreates the spatial index if present*/
335339
bool syncToDisc();
336340

0 commit comments

Comments
 (0)