Skip to content

Commit 012a29d

Browse files
committed
[oracle] Fix feature request when expression compilation fails,
fix incorrect provider side use of limit when expression compilation could not be used On behalf of Faunalia, sponsored by ENEL (cherry-picked from d089cba)
1 parent e6dd24a commit 012a29d

File tree

2 files changed

+64
-21
lines changed

2 files changed

+64
-21
lines changed

src/providers/oracle/qgsoraclefeatureiterator.cpp

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

184224
QgsOracleFeatureIterator::~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.\nSQL:%1\nError: %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.\nSQL:%1\nError: %2" )
493+
.arg( mQry.lastQuery() )
494+
.arg( mQry.lastError().text() ),
495+
QObject::tr( "Oracle" ) );
496+
}
454497
return false;
455498
}
456499
}

src/providers/oracle/qgsoraclefeatureiterator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class QgsOracleFeatureIterator : public QgsAbstractFeatureIteratorFromSource<Qgs
7676
//! fetch next feature filter expression
7777
bool nextFeatureFilterExpression( QgsFeature& f ) override;
7878

79-
bool openQuery( QString whereClause );
79+
bool openQuery( QString whereClause, bool showLog = true );
8080

8181
QgsOracleConn *mConnection;
8282
QSqlQuery mQry;

0 commit comments

Comments
 (0)