Skip to content

Commit

Permalink
[mssql] Fix incorrect feature sort order when combined with subset
Browse files Browse the repository at this point in the history
of attributes request

Fixes a failing provider conformance test
  • Loading branch information
nyalldawson committed Oct 4, 2018
1 parent f377958 commit 7ac86d2
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/providers/mssql/qgsmssqlfeatureiterator.cpp
Expand Up @@ -81,16 +81,28 @@ void QgsMssqlFeatureIterator::BuildStatement( const QgsFeatureRequest &request )
bool subsetOfAttributes = mRequest.flags() & QgsFeatureRequest::SubsetOfAttributes;
QgsAttributeList attrs = subsetOfAttributes ? mRequest.subsetOfAttributes() : mSource->mFields.allAttributesList();

// ensure that all attributes required for expression filter are being fetched
if ( subsetOfAttributes && request.filterType() == QgsFeatureRequest::FilterExpression )
if ( subsetOfAttributes )
{
//ensure that all fields required for filter expressions are prepared
QSet<int> attributeIndexes = request.filterExpression()->referencedAttributeIndexes( mSource->mFields );
attributeIndexes += attrs.toSet();
attrs = attributeIndexes.toList();
// ensure that all attributes required for expression filter are being fetched
if ( request.filterType() == QgsFeatureRequest::FilterExpression )
{
//ensure that all fields required for filter expressions are prepared
QSet<int> attributeIndexes = request.filterExpression()->referencedAttributeIndexes( mSource->mFields );
attributeIndexes += attrs.toSet();
attrs = attributeIndexes.toList();
}

// ensure that all attributes required for order by are fetched
const QSet< QString > orderByAttributes = mRequest.orderBy().usedAttributes();
for ( const QString &attr : orderByAttributes )
{
int attrIndex = mSource->mFields.lookupField( attr );
if ( !attrs.contains( attrIndex ) )
attrs << attrIndex;
}
}

Q_FOREACH ( int i, attrs )
for ( int i : qgis::as_const( attrs ) )
{
QString fieldname = mSource->mFields.at( i ).name();
if ( mSource->mFidColName == fieldname )
Expand Down Expand Up @@ -269,7 +281,7 @@ void QgsMssqlFeatureIterator::BuildStatement( const QgsFeatureRequest &request )
mOrderByClause = QStringLiteral( " ORDER BY %1" ).arg( orderByParts.join( QStringLiteral( "," ) ) );
}

QgsDebugMsg( mStatement );
QgsDebugMsg( mStatement + " " + mOrderByClause );
#if 0
if ( fieldCount == 0 )
{
Expand Down

0 comments on commit 7ac86d2

Please sign in to comment.