@@ -81,16 +81,28 @@ void QgsMssqlFeatureIterator::BuildStatement( const QgsFeatureRequest &request )
81
81
bool subsetOfAttributes = mRequest .flags () & QgsFeatureRequest::SubsetOfAttributes;
82
82
QgsAttributeList attrs = subsetOfAttributes ? mRequest .subsetOfAttributes () : mSource ->mFields .allAttributesList ();
83
83
84
- // ensure that all attributes required for expression filter are being fetched
85
- if ( subsetOfAttributes && request.filterType () == QgsFeatureRequest::FilterExpression )
84
+ if ( subsetOfAttributes )
86
85
{
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
+ }
91
103
}
92
104
93
- Q_FOREACH ( int i, attrs )
105
+ for ( int i : qgis::as_const ( attrs ) )
94
106
{
95
107
QString fieldname = mSource ->mFields .at ( i ).name ();
96
108
if ( mSource ->mFidColName == fieldname )
@@ -269,7 +281,7 @@ void QgsMssqlFeatureIterator::BuildStatement( const QgsFeatureRequest &request )
269
281
mOrderByClause = QStringLiteral ( " ORDER BY %1" ).arg ( orderByParts.join ( QStringLiteral ( " ," ) ) );
270
282
}
271
283
272
- QgsDebugMsg ( mStatement );
284
+ QgsDebugMsg ( mStatement + " " + mOrderByClause );
273
285
#if 0
274
286
if ( fieldCount == 0 )
275
287
{
0 commit comments