Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions app/filter/filtercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "coreutils.h"

const QString TIMESTAMP_FORMAT = QStringLiteral( "yyyy-MM-ddTHH:mm:ss.zzzZ" );
const QString TIMESTAMP_FORMAT_NO_MS = QStringLiteral( "yyyy-MM-ddTHH:mm:ssZ" );
const QString DATE_FORMAT = QStringLiteral( "yyyy-MM-dd" );

FilterController::FilterController( QObject *parent )
Expand Down Expand Up @@ -262,11 +263,39 @@ QString FilterController::buildFieldExpression( const FieldFilter &filter ) cons

if ( value.typeId() == QMetaType::QDateTime )
if ( isDateFilterDateTime( filter.filterId ) )
expressionCopy.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedString( value.toDateTime().toUTC().toString( TIMESTAMP_FORMAT ) ) );
{
const QDateTime utcDateTime = value.toDateTime().toUTC();
if ( utcDateTime.time().msec() != 0 )
{
expressionCopy.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedString( utcDateTime.toString( TIMESTAMP_FORMAT ) ) );
}
else
{
// if the msec() is 0, it could mean that either there are 0 milliseconds or none at all
// in this case add both filter queries and use 'OR' between them
QStringList dateTimeExpressions;

// filter expression with milliseconds
QString exprWithMs( expressionCopy );
exprWithMs.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedString( utcDateTime.toString( TIMESTAMP_FORMAT ) ) );
dateTimeExpressions << QStringLiteral( "(%1)" ).arg( exprWithMs );

// filter expression without milliseconds
QString exprNoMs( expressionCopy );
exprNoMs.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedString( utcDateTime.toString( TIMESTAMP_FORMAT_NO_MS ) ) );
dateTimeExpressions << QStringLiteral( "(%1)" ).arg( exprNoMs );

expressionCopy = dateTimeExpressions.join( QStringLiteral( " OR " ) );
}
}
else
{
expressionCopy.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedString( value.toDate().toString( DATE_FORMAT ) ) );
}
else
{
expressionCopy.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedValue( value ) );
}
}
break;
}
Expand Down Expand Up @@ -357,14 +386,39 @@ QString FilterController::buildFieldExpression( const FieldFilter &filter ) cons
{
// QML always returns QDateTime so we can't differentiate based on type
if ( value.typeId() == QMetaType::QDateTime )
{
if ( isDateFilterDateTime( filter.filterId ) )
expressionTemplate.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedString( value.toDateTime().toUTC().toString( TIMESTAMP_FORMAT ) ) );
{
const QDateTime utcDateTime = value.toDateTime().toUTC();
if ( utcDateTime.time().msec() != 0 )
{
expressionTemplate.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedString( utcDateTime.toString( TIMESTAMP_FORMAT ) ) );
expressions << QStringLiteral( "(%1)" ).arg( expressionTemplate );
}
else
{
// if the msec() is 0, it could mean that either there are 0 milliseconds or none at all
// in this case, we create two filter expressions which are already joined by OR for multi-select
QString exprWithMs( expressionTemplate );
exprWithMs.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedString( utcDateTime.toString( TIMESTAMP_FORMAT ) ) );
expressions << QStringLiteral( "(%1)" ).arg( exprWithMs );

QString exprNoMs( expressionTemplate );
exprNoMs.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedString( utcDateTime.toString( TIMESTAMP_FORMAT_NO_MS ) ) );
expressions << QStringLiteral( "(%1)" ).arg( exprNoMs );
}
}
else
{
expressionTemplate.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedString( value.toDate().toString( DATE_FORMAT ) ) );
expressions << QStringLiteral( "(%1)" ).arg( expressionTemplate );
}
}
else
{
expressionTemplate.replace( QStringLiteral( "@@value@@" ), QgsExpression::quotedValue( value ) );

expressions << QStringLiteral( "(%1)" ).arg( expressionTemplate );
expressions << QStringLiteral( "(%1)" ).arg( expressionTemplate );
}
}
}
expressionCopy = expressions.join( QStringLiteral( " OR " ) );
Expand Down
Loading