Skip to content

Commit 65ee183

Browse files
committed
[FEATURE] oracle provider: initial expression compiler support
1 parent 5e4312b commit 65ee183

7 files changed

+142
-7
lines changed

src/providers/oracle/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ SET(ORACLE_SRCS
1414
qgsoraclecolumntypethread.cpp
1515
qgsoraclefeatureiterator.cpp
1616
qgsoracleconnpool.cpp
17+
qgsoracleexpressioncompiler.cpp
1718
)
1819

1920
SET(ORACLE_MOC_HDRS
@@ -25,6 +26,7 @@ SET(ORACLE_MOC_HDRS
2526
qgsoracletablemodel.h
2627
qgsoraclecolumntypethread.h
2728
qgsoracleconnpool.h
29+
qgsoracleexpressioncompiler.h
2830
)
2931

3032

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/***************************************************************************
2+
qgsoracleexpressioncompiler.cpp
3+
----------------------------------------------------
4+
date : December 2015
5+
copyright : (C) 2015 by Juergen E. Fischer
6+
email : jef at norbit dot de
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgsoracleexpressioncompiler.h"
17+
#include "qgssqlexpressioncompiler.h"
18+
19+
QgsOracleExpressionCompiler::QgsOracleExpressionCompiler( QgsOracleFeatureSource* source )
20+
: QgsSqlExpressionCompiler( source->mFields )
21+
{
22+
}
23+
24+
QgsSqlExpressionCompiler::Result QgsOracleExpressionCompiler::compileNode( const QgsExpression::Node* node, QString& result )
25+
{
26+
if ( node->nodeType() == QgsExpression::ntBinaryOperator )
27+
{
28+
const QgsExpression::NodeBinaryOperator *bin( static_cast<const QgsExpression::NodeBinaryOperator*>( node ) );
29+
30+
switch ( bin->op() )
31+
{
32+
case QgsExpression::boPow:
33+
case QgsExpression::boRegexp:
34+
{
35+
QString op1, op2;
36+
37+
if ( compileNode( bin->opLeft(), op1 ) != Complete ||
38+
compileNode( bin->opRight(), op2 ) != Complete )
39+
return Fail;
40+
41+
switch ( bin->op() )
42+
{
43+
case QgsExpression::boPow:
44+
result = QString( "power(%1,%2)" ).arg( op1, op2 );
45+
return Complete;
46+
47+
case QgsExpression::boRegexp:
48+
result = QString( "regexp_like(%1,%2)" ).arg( op1, op2 );
49+
return Complete;
50+
51+
default:
52+
break;
53+
}
54+
}
55+
56+
default:
57+
break;
58+
}
59+
}
60+
61+
//fallback to default handling
62+
return QgsSqlExpressionCompiler::compileNode( node, result );
63+
}
64+
65+
QString QgsOracleExpressionCompiler::quotedIdentifier( const QString& identifier )
66+
{
67+
return QgsOracleConn::quotedIdentifier( identifier );
68+
}
69+
70+
QString QgsOracleExpressionCompiler::quotedValue( const QVariant& value )
71+
{
72+
return QgsOracleConn::quotedValue( value );
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/***************************************************************************
2+
qgsoracleexpressioncompiler.h
3+
----------------------------------------------------
4+
date : December 2015
5+
copyright : (C) 2015 by Jürgen E. Fischer
6+
email : jef at norbit dot de
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSORACLEEXPRESSIONCOMPILER_H
17+
#define QGSORACLEEXPRESSIONCOMPILER_H
18+
19+
#include "qgssqlexpressioncompiler.h"
20+
#include "qgsexpression.h"
21+
#include "qgsoraclefeatureiterator.h"
22+
23+
class QgsOracleExpressionCompiler : public QgsSqlExpressionCompiler
24+
{
25+
public:
26+
27+
explicit QgsOracleExpressionCompiler( QgsOracleFeatureSource* source );
28+
29+
protected:
30+
virtual Result compileNode( const QgsExpression::Node* node, QString& result ) override;
31+
virtual QString quotedIdentifier( const QString& identifier ) override;
32+
virtual QString quotedValue( const QVariant& value ) override;
33+
};
34+
35+
#endif // QGSORACLEEXPRESSIONCOMPILER_H

src/providers/oracle/qgsoraclefeatureiterator.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616
#include "qgsoraclefeatureiterator.h"
1717
#include "qgsoracleprovider.h"
1818
#include "qgsoracleconnpool.h"
19+
#include "qgsoracleexpressioncompiler.h"
1920

2021
#include "qgslogger.h"
2122
#include "qgsmessagelog.h"
2223
#include "qgsgeometry.h"
2324

2425
#include <QObject>
26+
#include <QSettings>
2527

2628
QgsOracleFeatureIterator::QgsOracleFeatureIterator( QgsOracleFeatureSource* source, bool ownSource, const QgsFeatureRequest &request )
2729
: QgsAbstractFeatureIteratorFromSource<QgsOracleFeatureSource>( source, ownSource, request )
2830
, mRewind( false )
31+
, mExpressionCompiled( false )
2932
{
3033
mConnection = QgsOracleConnPool::instance()->acquireConnection( mSource->mUri.connectionInfo() );
3134
if ( !mConnection )
@@ -90,6 +93,15 @@ QgsOracleFeatureIterator::QgsOracleFeatureIterator( QgsOracleFeatureSource* sour
9093
break;
9194

9295
case QgsFeatureRequest::FilterExpression:
96+
if ( QSettings().value( "/qgis/compileExpressions", true ).toBool() )
97+
{
98+
QgsOracleExpressionCompiler compiler( mSource );
99+
if ( compiler.compile( request.filterExpression() ) == QgsSqlExpressionCompiler::Complete )
100+
{
101+
whereClause = QgsOracleUtils::andWhereClauses( whereClause, compiler.result() );
102+
mExpressionCompiled = true;
103+
}
104+
}
93105
break;
94106

95107
case QgsFeatureRequest::FilterRect:
@@ -128,6 +140,14 @@ QgsOracleFeatureIterator::~QgsOracleFeatureIterator()
128140
close();
129141
}
130142

143+
bool QgsOracleFeatureIterator::nextFeatureFilterExpression( QgsFeature& f )
144+
{
145+
if ( !mExpressionCompiled )
146+
return QgsAbstractFeatureIterator::nextFeatureFilterExpression( f );
147+
else
148+
return fetchFeature( f );
149+
}
150+
131151
bool QgsOracleFeatureIterator::fetchFeature( QgsFeature& feature )
132152
{
133153
feature.setValid( false );

src/providers/oracle/qgsoraclefeatureiterator.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class QgsOracleFeatureSource : public QgsAbstractFeatureSource
5252
QSharedPointer<QgsOracleSharedData> mShared;
5353

5454
friend class QgsOracleFeatureIterator;
55+
friend class QgsOracleExpressionCompiler;
5556
};
5657

5758

@@ -63,20 +64,24 @@ class QgsOracleFeatureIterator : public QgsAbstractFeatureIteratorFromSource<Qgs
6364
~QgsOracleFeatureIterator();
6465

6566
//! reset the iterator to the starting position
66-
virtual bool rewind();
67+
virtual bool rewind() override;
6768

6869
//! end of iterating: free the resources / lock
69-
virtual bool close();
70+
virtual bool close() override;
7071

7172
protected:
7273
//! fetch next feature, return true on success
73-
virtual bool fetchFeature( QgsFeature& feature );
74+
virtual bool fetchFeature( QgsFeature& feature ) override;
75+
76+
//! fetch next feature filter expression
77+
bool nextFeatureFilterExpression( QgsFeature& f ) override;
7478

7579
bool openQuery( QString whereClause );
7680

7781
QgsOracleConn *mConnection;
7882
QSqlQuery mQry;
7983
bool mRewind;
84+
bool mExpressionCompiled;
8085
QgsAttributeList mAttributeList;
8186
};
8287

src/providers/postgres/qgspostgresexpressioncompiler.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***************************************************************************
2-
3-
----------------------------------------------------
2+
qgspostgresexpressioncompiler.cpp
3+
----------------------------------------------------
44
date : 22.4.2015
55
copyright : (C) 2015 by Matthias Kuhn
66
email : matthias (at) opengis.ch

src/providers/postgres/qgspostgresexpressioncompiler.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***************************************************************************
2-
3-
----------------------------------------------------
2+
qgspostgresexpressioncompiler.h
3+
----------------------------------------------------
44
date : 22.4.2015
55
copyright : (C) 2015 by Matthias Kuhn
66
email : matthias (at) opengis.ch

0 commit comments

Comments
 (0)