Skip to content
Permalink
Browse files

Fix mssql provider returns garbage values for min/max/uniquevalues on…

… time fields
  • Loading branch information
nyalldawson committed May 14, 2020
1 parent 098c578 commit ef46d8eca70b79a5a171d1f5104bf2d90f6cb6d9
Showing with 31 additions and 1 deletion.
  1. +31 −1 src/providers/mssql/qgsmssqlprovider.cpp
@@ -506,6 +506,27 @@ QString QgsMssqlProvider::storageType() const
return QStringLiteral( "MSSQL spatial database" );
}

QVariant convertTimeValue( const QVariant &value )
{
if ( value.isValid() && value.type() == QVariant::ByteArray )
{
// time fields can be returned as byte arrays... woot
const QByteArray ba = value.toByteArray();
if ( ba.length() >= 5 )
{
const int hours = ba.at( 0 );
const int mins = ba.at( 2 );
const int seconds = ba.at( 4 );
QVariant t = QTime( hours, mins, seconds );
if ( !t.isValid() ) // can't handle it
t = QVariant( QVariant::Time );
return t;
}
return QVariant( QVariant::Time );
}
return value;
}

// Returns the minimum value of an attribute
QVariant QgsMssqlProvider::minimumValue( int index ) const
{
@@ -537,6 +558,9 @@ QVariant QgsMssqlProvider::minimumValue( int index ) const

if ( query.isActive() && query.next() )
{
if ( fld.type() == QVariant::Time )
return convertTimeValue( query.value( 0 ) );

return query.value( 0 );
}

@@ -573,6 +597,9 @@ QVariant QgsMssqlProvider::maximumValue( int index ) const

if ( query.isActive() && query.next() )
{
if ( fld.type() == QVariant::Time )
return convertTimeValue( query.value( 0 ) );

return query.value( 0 );
}

@@ -620,7 +647,10 @@ QSet<QVariant> QgsMssqlProvider::uniqueValues( int index, int limit ) const
// read all features
while ( query.next() )
{
uniqueValues.insert( query.value( 0 ) );
if ( fld.type() == QVariant::Time )
uniqueValues.insert( convertTimeValue( query.value( 0 ) ) );
else
uniqueValues.insert( query.value( 0 ) );
}
}
return uniqueValues;

0 comments on commit ef46d8e

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