Skip to content

Commit 7ac86d2

Browse files
committed
[mssql] Fix incorrect feature sort order when combined with subset
of attributes request Fixes a failing provider conformance test
1 parent f377958 commit 7ac86d2

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/providers/mssql/qgsmssqlfeatureiterator.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,28 @@ void QgsMssqlFeatureIterator::BuildStatement( const QgsFeatureRequest &request )
8181
bool subsetOfAttributes = mRequest.flags() & QgsFeatureRequest::SubsetOfAttributes;
8282
QgsAttributeList attrs = subsetOfAttributes ? mRequest.subsetOfAttributes() : mSource->mFields.allAttributesList();
8383

84-
// ensure that all attributes required for expression filter are being fetched
85-
if ( subsetOfAttributes && request.filterType() == QgsFeatureRequest::FilterExpression )
84+
if ( subsetOfAttributes )
8685
{
87-
//ensure that all fields required for filter expressions are prepared
88-
QSet<int> attributeIndexes = request.filterExpression()->referencedAttributeIndexes( mSource->mFields );
89-
attributeIndexes += attrs.toSet();
90-
attrs = attributeIndexes.toList();
86+
// ensure that all attributes required for expression filter are being fetched
87+
if ( request.filterType() == QgsFeatureRequest::FilterExpression )
88+
{
89+
//ensure that all fields required for filter expressions are prepared
90+
QSet<int> attributeIndexes = request.filterExpression()->referencedAttributeIndexes( mSource->mFields );
91+
attributeIndexes += attrs.toSet();
92+
attrs = attributeIndexes.toList();
93+
}
94+
95+
// ensure that all attributes required for order by are fetched
96+
const QSet< QString > orderByAttributes = mRequest.orderBy().usedAttributes();
97+
for ( const QString &attr : orderByAttributes )
98+
{
99+
int attrIndex = mSource->mFields.lookupField( attr );
100+
if ( !attrs.contains( attrIndex ) )
101+
attrs << attrIndex;
102+
}
91103
}
92104

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

272-
QgsDebugMsg( mStatement );
284+
QgsDebugMsg( mStatement + " " + mOrderByClause );
273285
#if 0
274286
if ( fieldCount == 0 )
275287
{

0 commit comments

Comments
 (0)