Skip to content

Commit 0618b2e

Browse files
authoredMay 22, 2020
[ogr] Compile make_datetime, make_date, make_time (#36636)
* [ogr] Compile make_datetime, make_date, make_time * Update test
1 parent 12dd3ef commit 0618b2e

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed
 

‎src/core/providers/ogr/qgsogrexpressioncompiler.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ QgsSqlExpressionCompiler::Result QgsOgrExpressionCompiler::compileNode( const Qg
7373
}
7474

7575
case QgsExpressionNode::ntFunction:
76+
{
77+
const QgsExpressionNodeFunction *n = static_cast<const QgsExpressionNodeFunction *>( node );
78+
QgsExpressionFunction *fd = QgsExpression::Functions()[n->fnIndex()];
79+
80+
if ( fd->name() == QLatin1String( "make_datetime" ) || fd->name() == QLatin1String( "make_date" ) || fd->name() == QLatin1String( "make_time" ) )
81+
{
82+
const auto constList = n->args()->list();
83+
for ( const QgsExpressionNode *ln : constList )
84+
{
85+
if ( ln->nodeType() != QgsExpressionNode::ntLiteral )
86+
return Fail;
87+
}
88+
return QgsSqlExpressionCompiler::compileNode( node, result );
89+
}
90+
//generally not support by OGR
91+
return Fail;
92+
}
93+
7694
case QgsExpressionNode::ntCondition:
7795
//not support by OGR
7896
return Fail;
@@ -88,6 +106,45 @@ QgsSqlExpressionCompiler::Result QgsOgrExpressionCompiler::compileNode( const Qg
88106
return QgsSqlExpressionCompiler::compileNode( node, result );
89107
}
90108

109+
QString QgsOgrExpressionCompiler::sqlFunctionFromFunctionName( const QString &fnName ) const
110+
{
111+
static const QMap<QString, QString> FN_NAMES
112+
{
113+
{ "make_datetime", "" },
114+
{ "make_date", "" },
115+
{ "make_time", "" },
116+
};
117+
118+
return FN_NAMES.value( fnName, QString() );
119+
}
120+
121+
QStringList QgsOgrExpressionCompiler::sqlArgumentsFromFunctionName( const QString &fnName, const QStringList &fnArgs ) const
122+
{
123+
QStringList args( fnArgs );
124+
if ( fnName == QLatin1String( "make_datetime" ) )
125+
{
126+
args = QStringList( QStringLiteral( "'%1-%2-%3T%4:%5:%6Z'" ).arg( args[0].rightJustified( 4, '0' ) )
127+
.arg( args[1].rightJustified( 2, '0' ) )
128+
.arg( args[2].rightJustified( 2, '0' ) )
129+
.arg( args[3].rightJustified( 2, '0' ) )
130+
.arg( args[4].rightJustified( 2, '0' ) )
131+
.arg( args[5].rightJustified( 2, '0' ) ) );
132+
}
133+
else if ( fnName == QLatin1String( "make_date" ) )
134+
{
135+
args = QStringList( QStringLiteral( "'%1-%2-%3'" ).arg( args[0].rightJustified( 4, '0' ) )
136+
.arg( args[1].rightJustified( 2, '0' ) )
137+
.arg( args[2].rightJustified( 2, '0' ) ) );
138+
}
139+
else if ( fnName == QLatin1String( "make_time" ) )
140+
{
141+
args = QStringList( QStringLiteral( "'%1:%2:%3'" ).arg( args[0].rightJustified( 2, '0' ) )
142+
.arg( args[1].rightJustified( 2, '0' ) )
143+
.arg( args[2].rightJustified( 2, '0' ) ) );
144+
}
145+
return args;
146+
}
147+
91148
QString QgsOgrExpressionCompiler::quotedIdentifier( const QString &identifier )
92149
{
93150
return QgsOgrProviderUtils::quotedIdentifier( identifier.toUtf8(), mSource->mDriverName );

‎src/core/providers/ogr/qgsogrexpressioncompiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class QgsOgrExpressionCompiler : public QgsSqlExpressionCompiler
3939
Result compileNode( const QgsExpressionNode *node, QString &str ) override;
4040
QString quotedIdentifier( const QString &identifier ) override;
4141
QString quotedValue( const QVariant &value, bool &ok ) override;
42+
QString sqlFunctionFromFunctionName( const QString &fnName ) const override;
43+
QStringList sqlArgumentsFromFunctionName( const QString &fnName, const QStringList &fnArgs ) const override;
4244
QString castToReal( const QString &value ) const override;
4345
QString castToInt( const QString &value ) const override;
4446

‎tests/src/python/test_provider_shapefile.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,6 @@ def uncompiledFilters(self):
212212
'"dt" <= format_date(make_datetime(2020, 5, 4, 12, 13, 14), \'yyyy-MM-dd hh:mm:ss\')',
213213
'"dt" < format_date(make_date(2020, 5, 4), \'yyyy-MM-dd hh:mm:ss\')',
214214
'"dt" = format_date(to_datetime(\'000www14ww13ww12www4ww5ww2020\',\'zzzwwwsswwmmwwhhwwwdwwMwwyyyy\'),\'yyyy-MM-dd hh:mm:ss\')',
215-
'"date" <= make_datetime(2020, 5, 4, 12, 13, 14)',
216-
'"date" >= make_date(2020, 5, 4)',
217215
'"date" = to_date(\'www4ww5ww2020\',\'wwwdwwMwwyyyy\')',
218216
'to_time("time") >= make_time(12, 14, 14)',
219217
'to_time("time") = to_time(\'000www14ww13ww12www\',\'zzzwwwsswwmmwwhhwww\')',

0 commit comments

Comments
 (0)
Please sign in to comment.