Skip to content
Permalink
Browse files

Implement provider side feature IDs filtering for mssql

  • Loading branch information
nyalldawson committed Dec 10, 2015
1 parent a6cf547 commit c6c076b082c85f07d52d9328f524816bd48aed30
Showing with 44 additions and 1 deletion.
  1. +21 −1 src/providers/mssql/qgsmssqlfeatureiterator.cpp
  2. +23 −0 tests/src/python/providertestbase.py
@@ -119,7 +119,7 @@ void QgsMssqlFeatureIterator::BuildStatement( const QgsFeatureRequest& request )
// set fid filter
if ( request.filterType() == QgsFeatureRequest::FilterFid && !mSource->mFidColName.isEmpty() )
{
QString fidfilter = QString( " [%1] = %2" ).arg( mSource->mFidColName, QString::number( request.filterFid() ) );
QString fidfilter = QString( " [%1] = %2" ).arg( mSource->mFidColName, FID_TO_STRING( request.filterFid() ) );
// set attribute filter
if ( !filterAdded )
mStatement += " WHERE ";
@@ -129,6 +129,26 @@ void QgsMssqlFeatureIterator::BuildStatement( const QgsFeatureRequest& request )
mStatement += fidfilter;
filterAdded = true;
}
else if ( request.filterType() == QgsFeatureRequest::FilterFids && !mSource->mFidColName.isEmpty()
&& !mRequest.filterFids().isEmpty() )
{
QString delim;
QString inClause = QString( "%1 IN (" ).arg( mSource->mFidColName );
Q_FOREACH ( QgsFeatureId featureId, mRequest.filterFids() )
{
inClause += delim + FID_TO_STRING( featureId );
delim = ',';
}
inClause.append( ')' );

if ( !filterAdded )
mStatement += " WHERE ";
else
mStatement += " AND ";

mStatement += inClause;
filterAdded = true;
}

if ( !mSource->mSqlWhereClause.isEmpty() )
{
@@ -120,6 +120,29 @@ def testGetFeaturesCompiled(self):
except AttributeError:
print 'Provider does not support compiling'

def testGetFeaturesFidTests(self):
fids = [f.id() for f in self.provider.getFeatures()]
assert len(fids) == 5, 'Expected 5 features, got {} instead'.format(len(fids))
for id in fids:
result = [f.id() for f in self.provider.getFeatures(QgsFeatureRequest().setFilterFid(id))]
expected = [id]
assert result == expected, 'Expected {} and got {} when testing for feature ID filter'.format(expected, result)

def testGetFeaturesFidsTests(self):
fids = [f.id() for f in self.provider.getFeatures()]

result = set([f.id() for f in self.provider.getFeatures(QgsFeatureRequest().setFilterFids([fids[0], fids[2]]))])
expected = set([fids[0], fids[2]])
assert result == expected, 'Expected {} and got {} when testing for feature IDs filter'.format(expected, result)

result = set([f.id() for f in self.provider.getFeatures(QgsFeatureRequest().setFilterFids([fids[1], fids[3], fids[4]]))])
expected = set([fids[1], fids[3], fids[4]])
assert result == expected, 'Expected {} and got {} when testing for feature IDs filter'.format(expected, result)

result = set([f.id() for f in self.provider.getFeatures(QgsFeatureRequest().setFilterFids([]))])
expected = set([])
assert result == expected, 'Expected {} and got {} when testing for feature IDs filter'.format(expected, result)

def testGetFeaturesFilterRectTests(self):
extent = QgsRectangle(-70, 67, -60, 80)
features = [f['pk'] for f in self.provider.getFeatures(QgsFeatureRequest().setFilterRect(extent))]

0 comments on commit c6c076b

Please sign in to comment.
You can’t perform that action at this time.