@@ -37,6 +37,11 @@ QgsSpatiaLiteFeatureIterator::QgsSpatiaLiteFeatureIterator( QgsSpatiaLiteFeature
37
37
38
38
QStringList whereClauses;
39
39
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
+
40
45
if ( !request.filterRect ().isNull () && !mSource ->mGeometryColumn .isNull () )
41
46
{
42
47
// some kind of MBR spatial filtering is required
@@ -63,23 +68,34 @@ QgsSpatiaLiteFeatureIterator::QgsSpatiaLiteFeatureIterator( QgsSpatiaLiteFeature
63
68
whereClauses.append ( whereClause );
64
69
}
65
70
}
66
- else if ( request.filterType () == QgsFeatureRequest::FilterExpression
67
- && QSettings ().value ( " /qgis/compileExpressions" , true ).toBool () )
71
+ else if ( request.filterType () == QgsFeatureRequest::FilterExpression )
68
72
{
69
- QgsSpatiaLiteExpressionCompiler compiler = QgsSpatiaLiteExpressionCompiler ( source );
73
+ if ( QSettings ().value ( " /qgis/compileExpressions" , true ).toBool () )
74
+ {
75
+ QgsSpatiaLiteExpressionCompiler compiler = QgsSpatiaLiteExpressionCompiler ( source );
70
76
71
- QgsSqlExpressionCompiler::Result result = compiler.compile ( request.filterExpression () );
77
+ QgsSqlExpressionCompiler::Result result = compiler.compile ( request.filterExpression () );
72
78
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 )
77
90
{
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 ;
81
93
}
82
94
}
95
+ else
96
+ {
97
+ limitAtProvider = false ;
98
+ }
83
99
}
84
100
85
101
if ( !mSource ->mSubsetString .isEmpty () )
@@ -94,7 +110,7 @@ QgsSpatiaLiteFeatureIterator::QgsSpatiaLiteFeatureIterator( QgsSpatiaLiteFeature
94
110
whereClause = whereClauses.join ( " AND " );
95
111
96
112
// preparing the SQL statement
97
- if ( !prepareStatement ( whereClause ) )
113
+ if ( !prepareStatement ( whereClause, limitAtProvider ? mRequest . limit () : - 1 ) )
98
114
{
99
115
// some error occurred
100
116
sqliteStatement = NULL ;
@@ -183,7 +199,7 @@ bool QgsSpatiaLiteFeatureIterator::close()
183
199
// //
184
200
185
201
186
- bool QgsSpatiaLiteFeatureIterator::prepareStatement ( const QString& whereClause )
202
+ bool QgsSpatiaLiteFeatureIterator::prepareStatement ( const QString& whereClause, long limit )
187
203
{
188
204
if ( !mHandle )
189
205
return false ;
@@ -222,6 +238,9 @@ bool QgsSpatiaLiteFeatureIterator::prepareStatement( const QString& whereClause
222
238
if ( !whereClause.isEmpty () )
223
239
sql += QString ( " WHERE %1" ).arg ( whereClause );
224
240
241
+ if ( limit >= 0 )
242
+ sql += QString ( " LIMIT %1" ).arg ( limit );
243
+
225
244
if ( sqlite3_prepare_v2 ( mHandle ->handle (), sql.toUtf8 ().constData (), -1 , &sqliteStatement, NULL ) != SQLITE_OK )
226
245
{
227
246
// some error occurred
0 commit comments