@@ -61,7 +61,7 @@ QgsOracleFeatureIterator::QgsOracleFeatureIterator( QgsOracleFeatureSource* sour
6161 else
6262 mAttributeList = mSource ->mFields .allAttributesList ();
6363
64-
64+ bool limitAtProvider = ( mRequest . limit () >= 0 );
6565 QString whereClause;
6666
6767 if ( !mSource ->mGeometryColumn .isNull () )
@@ -139,15 +139,7 @@ QgsOracleFeatureIterator::QgsOracleFeatureIterator( QgsOracleFeatureSource* sour
139139 break ;
140140
141141 case QgsFeatureRequest::FilterExpression:
142- if ( QSettings ().value ( " /qgis/compileExpressions" , true ).toBool () )
143- {
144- QgsOracleExpressionCompiler compiler ( mSource );
145- if ( compiler.compile ( mRequest .filterExpression () ) == QgsSqlExpressionCompiler::Complete )
146- {
147- whereClause = QgsOracleUtils::andWhereClauses ( whereClause, compiler.result () );
148- mExpressionCompiled = true ;
149- }
150- }
142+ // handled below
151143 break ;
152144
153145 case QgsFeatureRequest::FilterRect:
@@ -163,22 +155,70 @@ QgsOracleFeatureIterator::QgsOracleFeatureIterator( QgsOracleFeatureSource* sour
163155 whereClause += QgsOracleConn::databaseTypeFilter ( " FEATUREREQUEST" , mSource ->mGeometryColumn , mSource ->mRequestedGeomType );
164156 }
165157
166- if ( mRequest . limit () >= 0 )
158+ if ( ! mSource -> mSqlWhereClause . isEmpty () )
167159 {
168160 if ( !whereClause.isEmpty () )
169161 whereClause += " AND " ;
162+ whereClause += " (" + mSource ->mSqlWhereClause + " )" ;
163+ }
170164
171- whereClause += QString ( " rownum<=%1" ).arg ( mRequest .limit () );
165+ // NOTE - must be last added!
166+ mExpressionCompiled = false ;
167+ mCompileStatus = NoCompilation;
168+ QString fallbackStatement;
169+ bool useFallback = false ;
170+ if ( request.filterType () == QgsFeatureRequest::FilterExpression )
171+ {
172+ if ( QSettings ().value ( " /qgis/compileExpressions" , true ).toBool () )
173+ {
174+ QgsOracleExpressionCompiler compiler ( mSource );
175+ QgsSqlExpressionCompiler::Result result = compiler.compile ( mRequest .filterExpression () );
176+ if ( result == QgsSqlExpressionCompiler::Complete || result == QgsSqlExpressionCompiler::Partial )
177+ {
178+ fallbackStatement = whereClause;
179+ useFallback = true ;
180+ whereClause = QgsOracleUtils::andWhereClauses ( whereClause, compiler.result () );
181+
182+ // if only partial success when compiling expression, we need to double-check results using QGIS' expressions
183+ mExpressionCompiled = ( result == QgsSqlExpressionCompiler::Complete );
184+ mCompileStatus = ( mExpressionCompiled ? Compiled : PartiallyCompiled );
185+ limitAtProvider = mExpressionCompiled ;
186+ }
187+ else
188+ {
189+ limitAtProvider = false ;
190+ }
191+ }
192+ else
193+ {
194+ limitAtProvider = false ;
195+ }
172196 }
173197
174- if ( !mSource ->mSqlWhereClause .isEmpty () )
198+ if ( !mRequest .orderBy ().isEmpty () )
199+ {
200+ limitAtProvider = false ;
201+ }
202+
203+ if ( mRequest .limit () >= 0 && limitAtProvider )
175204 {
176205 if ( !whereClause.isEmpty () )
177206 whereClause += " AND " ;
178- whereClause += " (" + mSource ->mSqlWhereClause + " )" ;
207+
208+ whereClause += QString ( " rownum<=%1" ).arg ( mRequest .limit () );
209+ fallbackStatement += QString ( " rownum<=%1" ).arg ( mRequest .limit () );
179210 }
180211
181- openQuery ( whereClause );
212+ bool result = openQuery ( whereClause, !useFallback );
213+ if ( !result && useFallback )
214+ {
215+ result = openQuery ( fallbackStatement );
216+ if ( result )
217+ {
218+ mExpressionCompiled = false ;
219+ mCompileStatus = NoCompilation;
220+ }
221+ }
182222}
183223
184224QgsOracleFeatureIterator::~QgsOracleFeatureIterator ()
@@ -392,7 +432,7 @@ bool QgsOracleFeatureIterator::close()
392432 return true ;
393433}
394434
395- bool QgsOracleFeatureIterator::openQuery ( QString whereClause )
435+ bool QgsOracleFeatureIterator::openQuery ( QString whereClause, bool showLog )
396436{
397437 try
398438 {
@@ -447,10 +487,13 @@ bool QgsOracleFeatureIterator::openQuery( QString whereClause )
447487 mSql = query;
448488 if ( !QgsOracleProvider::exec ( mQry , query ) )
449489 {
450- QgsMessageLog::logMessage ( QObject::tr ( " Fetching features failed.\n SQL:%1\n Error: %2" )
451- .arg ( mQry .lastQuery () )
452- .arg ( mQry .lastError ().text () ),
453- QObject::tr ( " Oracle" ) );
490+ if ( showLog )
491+ {
492+ QgsMessageLog::logMessage ( QObject::tr ( " Fetching features failed.\n SQL:%1\n Error: %2" )
493+ .arg ( mQry .lastQuery () )
494+ .arg ( mQry .lastError ().text () ),
495+ QObject::tr ( " Oracle" ) );
496+ }
454497 return false ;
455498 }
456499 }
0 commit comments