Skip to content

Commit eec8ca4

Browse files
authoredMay 22, 2020
[expression] Compile make_{datetime,date,time} for sqlite
1 parent 73e888d commit eec8ca4

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed
 

‎src/core/qgssqlexpressioncompiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ QgsSqlExpressionCompiler::Result QgsSqlExpressionCompiler::compileNode( const Qg
372372
// get sql function to compile node expression
373373
QString nd = sqlFunctionFromFunctionName( fd->name() );
374374
// if no sql function the node can't be compiled
375-
if ( nd.isEmpty() )
375+
if ( nd.isNull() )
376376
return Fail;
377377

378378
// compile arguments
@@ -397,7 +397,7 @@ QgsSqlExpressionCompiler::Result QgsSqlExpressionCompiler::compileNode( const Qg
397397
args = sqlArgumentsFromFunctionName( fd->name(), args );
398398

399399
// build result
400-
result = QStringLiteral( "%1(%2)" ).arg( nd, args.join( ',' ) );
400+
result = !nd.isEmpty() ? QStringLiteral( "%1(%2)" ).arg( nd, args.join( ',' ) ) : QStringLiteral( "%1" ).arg( args.join( ',' ) );
401401
return inResult == Partial ? Partial : Complete;
402402
}
403403

‎src/core/qgssqliteexpressioncompiler.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
#include "qgssqliteexpressioncompiler.h"
1919
#include "qgssqlexpressioncompiler.h"
20+
#include "qgsexpressionfunction.h"
2021
#include "qgsexpressionnodeimpl.h"
22+
#include "qgsexpression.h"
2123
#include "qgssqliteutils.h"
2224

2325
QgsSQLiteExpressionCompiler::QgsSQLiteExpressionCompiler( const QgsFields &fields )
@@ -61,6 +63,24 @@ QgsSqlExpressionCompiler::Result QgsSQLiteExpressionCompiler::compileNode( const
6163
}
6264
}
6365

66+
case QgsExpressionNode::ntFunction:
67+
{
68+
const QgsExpressionNodeFunction *n = static_cast<const QgsExpressionNodeFunction *>( node );
69+
QgsExpressionFunction *fd = QgsExpression::Functions()[n->fnIndex()];
70+
71+
if ( fd->name() == QLatin1String( "make_datetime" ) || fd->name() == QLatin1String( "make_date" ) || fd->name() == QLatin1String( "make_time" ) )
72+
{
73+
const auto constList = n->args()->list();
74+
for ( const QgsExpressionNode *ln : constList )
75+
{
76+
if ( ln->nodeType() != QgsExpressionNode::ntLiteral )
77+
return Fail;
78+
}
79+
}
80+
81+
return QgsSqlExpressionCompiler::compileNode( node, result );
82+
}
83+
6484
default:
6585
break;
6686
}
@@ -90,11 +110,41 @@ QString QgsSQLiteExpressionCompiler::sqlFunctionFromFunctionName( const QString
90110
{ "round", "round" },
91111
{ "trim", "trim" },
92112
{ "upper", "upper" },
113+
{ "make_datetime", "" },
114+
{ "make_date", "" },
115+
{ "make_time", "" },
93116
};
94117

95118
return FN_NAMES.value( fnName, QString() );
96119
}
97120

121+
QStringList QgsSQLiteExpressionCompiler::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+
98148
QString QgsSQLiteExpressionCompiler::castToReal( const QString &value ) const
99149
{
100150
return QStringLiteral( "CAST((%1) AS REAL)" ).arg( value );

‎src/core/qgssqliteexpressioncompiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class CORE_EXPORT QgsSQLiteExpressionCompiler : public QgsSqlExpressionCompiler
5050
QString quotedIdentifier( const QString &identifier ) override;
5151
QString quotedValue( const QVariant &value, bool &ok ) override;
5252
QString sqlFunctionFromFunctionName( const QString &fnName ) const override;
53+
QStringList sqlArgumentsFromFunctionName( const QString &fnName, const QStringList &fnArgs ) const override;
5354
QString castToReal( const QString &value ) const override;
5455
QString castToInt( const QString &value ) const override;
5556
QString castToText( const QString &value ) const override;

‎tests/src/python/test_provider_gpkg.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,7 @@ def uncompiledFilters(self):
154154
'overlaps(buffer($geometry,1),geom_from_wkt( \'Polygon ((-75.1 76.1, -75.1 81.6, -68.8 81.6, -68.8 76.1, -75.1 76.1))\'))',
155155
'intersects(centroid($geometry),geom_from_wkt( \'Polygon ((-74.4 78.2, -74.4 79.1, -66.8 79.1, -66.8 78.2, -74.4 78.2))\'))',
156156
'intersects(point_on_surface($geometry),geom_from_wkt( \'Polygon ((-74.4 78.2, -74.4 79.1, -66.8 79.1, -66.8 78.2, -74.4 78.2))\'))',
157-
'"dt" <= make_datetime(2020, 5, 4, 12, 13, 14)',
158-
'"dt" < make_date(2020, 5, 4)',
159157
'"dt" = to_datetime(\'000www14ww13ww12www4ww5ww2020\',\'zzzwwwsswwmmwwhhwwwdwwMwwyyyy\')',
160-
'"date" <= make_datetime(2020, 5, 4, 12, 13, 14)',
161-
'"date" >= make_date(2020, 5, 4)',
162158
'to_time("time") >= make_time(12, 14, 14)',
163159
'to_time("time") = to_time(\'000www14ww13ww12www\',\'zzzwwwsswwmmwwhhwww\')',
164160
'"date" = to_date(\'www4ww5ww2020\',\'wwwdwwMwwyyyy\')'

‎tests/src/python/test_provider_spatialite.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,6 @@ def uncompiledFilters(self):
361361
'"dt" <= format_date(make_datetime(2020, 5, 4, 12, 13, 14), \'yyyy-MM-dd hh:mm:ss\')',
362362
'"dt" < format_date(make_date(2020, 5, 4), \'yyyy-MM-dd hh:mm:ss\')',
363363
'"dt" = format_date(to_datetime(\'000www14ww13ww12www4ww5ww2020\',\'zzzwwwsswwmmwwhhwwwdwwMwwyyyy\'),\'yyyy-MM-dd hh:mm:ss\')',
364-
'"date" <= make_datetime(2020, 5, 4, 12, 13, 14)',
365-
'"date" >= make_date(2020, 5, 4)',
366364
'to_time("time") >= make_time(12, 14, 14)',
367365
'to_time("time") = to_time(\'000www14ww13ww12www\',\'zzzwwwsswwmmwwhhwww\')',
368366
'"date" = to_date(\'www4ww5ww2020\',\'wwwdwwMwwyyyy\')'

0 commit comments

Comments
 (0)
Please sign in to comment.