Skip to content
Permalink
Browse files

[ogr] Compile make_datetime, make_date, make_time (#36636)

* [ogr] Compile make_datetime, make_date, make_time

* Update test
  • Loading branch information
nirvn committed May 22, 2020
1 parent 12dd3ef commit 0618b2e4064f37e9e78c0de87fb8a1590a5ced2b
@@ -73,6 +73,24 @@ QgsSqlExpressionCompiler::Result QgsOgrExpressionCompiler::compileNode( const Qg
}

case QgsExpressionNode::ntFunction:
{
const QgsExpressionNodeFunction *n = static_cast<const QgsExpressionNodeFunction *>( node );
QgsExpressionFunction *fd = QgsExpression::Functions()[n->fnIndex()];

if ( fd->name() == QLatin1String( "make_datetime" ) || fd->name() == QLatin1String( "make_date" ) || fd->name() == QLatin1String( "make_time" ) )
{
const auto constList = n->args()->list();
for ( const QgsExpressionNode *ln : constList )
{
if ( ln->nodeType() != QgsExpressionNode::ntLiteral )
return Fail;
}
return QgsSqlExpressionCompiler::compileNode( node, result );
}
//generally not support by OGR
return Fail;
}

case QgsExpressionNode::ntCondition:
//not support by OGR
return Fail;
@@ -88,6 +106,45 @@ QgsSqlExpressionCompiler::Result QgsOgrExpressionCompiler::compileNode( const Qg
return QgsSqlExpressionCompiler::compileNode( node, result );
}

QString QgsOgrExpressionCompiler::sqlFunctionFromFunctionName( const QString &fnName ) const
{
static const QMap<QString, QString> FN_NAMES
{
{ "make_datetime", "" },
{ "make_date", "" },
{ "make_time", "" },
};

return FN_NAMES.value( fnName, QString() );
}

QStringList QgsOgrExpressionCompiler::sqlArgumentsFromFunctionName( const QString &fnName, const QStringList &fnArgs ) const
{
QStringList args( fnArgs );
if ( fnName == QLatin1String( "make_datetime" ) )
{
args = QStringList( QStringLiteral( "'%1-%2-%3T%4:%5:%6Z'" ).arg( args[0].rightJustified( 4, '0' ) )
.arg( args[1].rightJustified( 2, '0' ) )
.arg( args[2].rightJustified( 2, '0' ) )
.arg( args[3].rightJustified( 2, '0' ) )
.arg( args[4].rightJustified( 2, '0' ) )
.arg( args[5].rightJustified( 2, '0' ) ) );
}
else if ( fnName == QLatin1String( "make_date" ) )
{
args = QStringList( QStringLiteral( "'%1-%2-%3'" ).arg( args[0].rightJustified( 4, '0' ) )
.arg( args[1].rightJustified( 2, '0' ) )
.arg( args[2].rightJustified( 2, '0' ) ) );
}
else if ( fnName == QLatin1String( "make_time" ) )
{
args = QStringList( QStringLiteral( "'%1:%2:%3'" ).arg( args[0].rightJustified( 2, '0' ) )
.arg( args[1].rightJustified( 2, '0' ) )
.arg( args[2].rightJustified( 2, '0' ) ) );
}
return args;
}

QString QgsOgrExpressionCompiler::quotedIdentifier( const QString &identifier )
{
return QgsOgrProviderUtils::quotedIdentifier( identifier.toUtf8(), mSource->mDriverName );
@@ -39,6 +39,8 @@ class QgsOgrExpressionCompiler : public QgsSqlExpressionCompiler
Result compileNode( const QgsExpressionNode *node, QString &str ) override;
QString quotedIdentifier( const QString &identifier ) override;
QString quotedValue( const QVariant &value, bool &ok ) override;
QString sqlFunctionFromFunctionName( const QString &fnName ) const override;
QStringList sqlArgumentsFromFunctionName( const QString &fnName, const QStringList &fnArgs ) const override;
QString castToReal( const QString &value ) const override;
QString castToInt( const QString &value ) const override;

@@ -212,8 +212,6 @@ def uncompiledFilters(self):
'"dt" <= format_date(make_datetime(2020, 5, 4, 12, 13, 14), \'yyyy-MM-dd hh:mm:ss\')',
'"dt" < format_date(make_date(2020, 5, 4), \'yyyy-MM-dd hh:mm:ss\')',
'"dt" = format_date(to_datetime(\'000www14ww13ww12www4ww5ww2020\',\'zzzwwwsswwmmwwhhwwwdwwMwwyyyy\'),\'yyyy-MM-dd hh:mm:ss\')',
'"date" <= make_datetime(2020, 5, 4, 12, 13, 14)',
'"date" >= make_date(2020, 5, 4)',
'"date" = to_date(\'www4ww5ww2020\',\'wwwdwwMwwyyyy\')',
'to_time("time") >= make_time(12, 14, 14)',
'to_time("time") = to_time(\'000www14ww13ww12www\',\'zzzwwwsswwmmwwhhwww\')',

0 comments on commit 0618b2e

Please sign in to comment.
You can’t perform that action at this time.