@@ -37,6 +37,11 @@ QgsSpatiaLiteFeatureIterator::QgsSpatiaLiteFeatureIterator( QgsSpatiaLiteFeature
3737
3838 QStringList whereClauses;
3939 QString whereClause;
40+
41+ // beware - limitAtProvider needs to be set to false if the request cannot be completely handled
42+ // by the provider (eg utilising QGIS expression filters)
43+ bool limitAtProvider = ( mRequest .limit () >= 0 );
44+
4045 if ( !request.filterRect ().isNull () && !mSource ->mGeometryColumn .isNull () )
4146 {
4247 // some kind of MBR spatial filtering is required
@@ -63,23 +68,34 @@ QgsSpatiaLiteFeatureIterator::QgsSpatiaLiteFeatureIterator( QgsSpatiaLiteFeature
6368 whereClauses.append ( whereClause );
6469 }
6570 }
66- else if ( request.filterType () == QgsFeatureRequest::FilterExpression
67- && QSettings ().value ( " /qgis/compileExpressions" , true ).toBool () )
71+ else if ( request.filterType () == QgsFeatureRequest::FilterExpression )
6872 {
69- QgsSpatiaLiteExpressionCompiler compiler = QgsSpatiaLiteExpressionCompiler ( source );
73+ if ( QSettings ().value ( " /qgis/compileExpressions" , true ).toBool () )
74+ {
75+ QgsSpatiaLiteExpressionCompiler compiler = QgsSpatiaLiteExpressionCompiler ( source );
7076
71- QgsSqlExpressionCompiler::Result result = compiler.compile ( request.filterExpression () );
77+ QgsSqlExpressionCompiler::Result result = compiler.compile ( request.filterExpression () );
7278
73- if ( result == QgsSqlExpressionCompiler::Complete || result == QgsSqlExpressionCompiler::Partial )
74- {
75- whereClause = compiler.result ();
76- if ( !whereClause.isEmpty () )
79+ if ( result == QgsSqlExpressionCompiler::Complete || result == QgsSqlExpressionCompiler::Partial )
80+ {
81+ whereClause = compiler.result ();
82+ if ( !whereClause.isEmpty () )
83+ {
84+ whereClauses.append ( whereClause );
85+ // if only partial success when compiling expression, we need to double-check results using QGIS' expressions
86+ mExpressionCompiled = ( result == QgsSqlExpressionCompiler::Complete );
87+ }
88+ }
89+ if ( result != QgsSqlExpressionCompiler::Complete )
7790 {
78- whereClauses.append ( whereClause );
79- // if only partial success when compiling expression, we need to double-check results using QGIS' expressions
80- mExpressionCompiled = ( result == QgsSqlExpressionCompiler::Complete );
91+ // can't apply limit at provider side as we need to check all results using QGIS expressions
92+ limitAtProvider = false ;
8193 }
8294 }
95+ else
96+ {
97+ limitAtProvider = false ;
98+ }
8399 }
84100
85101 if ( !mSource ->mSubsetString .isEmpty () )
@@ -94,7 +110,7 @@ QgsSpatiaLiteFeatureIterator::QgsSpatiaLiteFeatureIterator( QgsSpatiaLiteFeature
94110 whereClause = whereClauses.join ( " AND " );
95111
96112 // preparing the SQL statement
97- if ( !prepareStatement ( whereClause ) )
113+ if ( !prepareStatement ( whereClause, limitAtProvider ? mRequest . limit () : - 1 ) )
98114 {
99115 // some error occurred
100116 sqliteStatement = NULL ;
@@ -183,7 +199,7 @@ bool QgsSpatiaLiteFeatureIterator::close()
183199// //
184200
185201
186- bool QgsSpatiaLiteFeatureIterator::prepareStatement ( const QString& whereClause )
202+ bool QgsSpatiaLiteFeatureIterator::prepareStatement ( const QString& whereClause, long limit )
187203{
188204 if ( !mHandle )
189205 return false ;
@@ -222,6 +238,9 @@ bool QgsSpatiaLiteFeatureIterator::prepareStatement( const QString& whereClause
222238 if ( !whereClause.isEmpty () )
223239 sql += QString ( " WHERE %1" ).arg ( whereClause );
224240
241+ if ( limit >= 0 )
242+ sql += QString ( " LIMIT %1" ).arg ( limit );
243+
225244 if ( sqlite3_prepare_v2 ( mHandle ->handle (), sql.toUtf8 ().constData (), -1 , &sqliteStatement, NULL ) != SQLITE_OK )
226245 {
227246 // some error occurred
0 commit comments