Skip to content

Commit 151e0cc

Browse files
committed
Fix spatialite provider for Null QVariant instead of Invalid QVariant
1 parent a2886cc commit 151e0cc

File tree

2 files changed

+61
-59
lines changed

2 files changed

+61
-59
lines changed

src/core/qgsvectordataprovider.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ QVariant QgsVectorDataProvider::convertValue( QVariant::Type type, QString value
396396
{
397397
QVariant v( value );
398398

399-
if ( !v.convert( type ) )
400-
v = QVariant( QString::null );
399+
if ( !v.convert( type ) || value.isNull() )
400+
v = QVariant( type );
401401

402402
return v;
403403
}

src/providers/spatialite/qgsspatialiteprovider.cpp

+59-57
Original file line numberDiff line numberDiff line change
@@ -3270,7 +3270,7 @@ QString QgsSpatiaLiteProvider::description() const
32703270
return SPATIALITE_DESCRIPTION;
32713271
} // QgsSpatiaLiteProvider::description()
32723272

3273-
const QgsFields & QgsSpatiaLiteProvider::fields() const
3273+
const QgsFields& QgsSpatiaLiteProvider::fields() const
32743274
{
32753275
return attributeFields;
32763276
}
@@ -3290,7 +3290,7 @@ QVariant QgsSpatiaLiteProvider::minimumValue( int index )
32903290
try
32913291
{
32923292
// get the field name
3293-
const QgsField & fld = field( index );
3293+
const QgsField& fld = field( index );
32943294

32953295
sql = QString( "SELECT Min(%1) FROM %2" ).arg( quotedIdentifier( fld.name() ) ).arg( mQuery );
32963296

@@ -3301,41 +3301,44 @@ QVariant QgsSpatiaLiteProvider::minimumValue( int index )
33013301

33023302
ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
33033303
if ( ret != SQLITE_OK )
3304-
goto error;
3305-
if ( rows < 1 )
3306-
;
3307-
else
33083304
{
3309-
for ( i = 1; i <= rows; i++ )
3305+
QgsMessageLog::logMessage( tr( "SQLite error: %2\nSQL: %1" ).arg( sql ).arg( errMsg ? errMsg : tr( "unknown cause" ) ), tr( "SpatiaLite" ) );
3306+
// unexpected error
3307+
if ( errMsg != NULL )
33103308
{
3311-
minValue = results[( i * columns ) + 0];
3309+
sqlite3_free( errMsg );
33123310
}
3313-
}
3314-
sqlite3_free_table( results );
3315-
3316-
if ( minValue.isEmpty() )
3317-
{
3318-
// NULL or not found
3319-
return QVariant( QString::null );
3311+
minValue = QString();
33203312
}
33213313
else
33223314
{
3323-
return convertValue( fld.type(), minValue );
3315+
if ( rows < 1 )
3316+
;
3317+
else
3318+
{
3319+
for ( i = 1; i <= rows; i++ )
3320+
{
3321+
minValue = results[( i * columns ) + 0];
3322+
}
3323+
}
3324+
sqlite3_free_table( results );
3325+
3326+
if ( minValue.isEmpty() )
3327+
{
3328+
// NULL or not found
3329+
minValue = QString();
3330+
}
33243331
}
3332+
3333+
return convertValue( fld.type(), minValue );
33253334
}
33263335
catch ( SLFieldNotFound )
33273336
{
3328-
return QVariant( QString::null );
3337+
return QVariant( QVariant::Int );
33293338
}
33303339

3331-
error:
3332-
QgsMessageLog::logMessage( tr( "SQLite error: %2\nSQL: %1" ).arg( sql ).arg( errMsg ? errMsg : tr( "unknown cause" ) ), tr( "SpatiaLite" ) );
3333-
// unexpected error
3334-
if ( errMsg != NULL )
3335-
{
3336-
sqlite3_free( errMsg );
3337-
}
3338-
return QVariant( QString::null );
3340+
// dummy return, so compiler is quiet
3341+
return QVariant();
33393342
}
33403343

33413344
// Returns the maximum value of an attribute
@@ -3364,41 +3367,45 @@ QVariant QgsSpatiaLiteProvider::maximumValue( int index )
33643367

33653368
ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
33663369
if ( ret != SQLITE_OK )
3367-
goto error;
3368-
if ( rows < 1 )
3369-
;
3370-
else
33713370
{
3372-
for ( i = 1; i <= rows; i++ )
3371+
QgsMessageLog::logMessage( tr( "SQLite error: %2\nSQL: %1" ).arg( sql ).arg( errMsg ? errMsg : tr( "unknown cause" ) ), tr( "SpatiaLite" ) );
3372+
// unexpected error
3373+
if ( errMsg != NULL )
33733374
{
3374-
maxValue = results[( i * columns ) + 0];
3375+
sqlite3_free( errMsg );
33753376
}
3376-
}
3377-
sqlite3_free_table( results );
3378-
3379-
if ( maxValue.isEmpty() )
3380-
{
3381-
// NULL or not found
3382-
return QVariant( QString::null );
3377+
maxValue = QString();
33833378
}
33843379
else
33853380
{
3386-
return convertValue( fld.type(), maxValue );
3381+
3382+
if ( rows < 1 )
3383+
;
3384+
else
3385+
{
3386+
for ( i = 1; i <= rows; i++ )
3387+
{
3388+
maxValue = results[( i * columns ) + 0];
3389+
}
3390+
}
3391+
sqlite3_free_table( results );
3392+
3393+
if ( maxValue.isEmpty() )
3394+
{
3395+
// NULL or not found
3396+
maxValue = QString();
3397+
}
33873398
}
3399+
3400+
return convertValue( fld.type(), maxValue );
33883401
}
33893402
catch ( SLFieldNotFound )
33903403
{
3391-
return QVariant( QString::null );
3404+
return QVariant( QVariant::Int );
33923405
}
33933406

3394-
error:
3395-
QgsMessageLog::logMessage( tr( "SQLite error: %2\nSQL: %1" ).arg( sql ).arg( errMsg ? errMsg : tr( "unknown cause" ) ), tr( "SpatiaLite" ) );
3396-
// unexpected error
3397-
if ( errMsg != NULL )
3398-
{
3399-
sqlite3_free( errMsg );
3400-
}
3401-
return QVariant( QString::null );
3407+
// dummy return, so compiler is quiet
3408+
return QVariant();
34023409
}
34033410

34043411
// Returns the list of unique values of an attribute
@@ -3565,7 +3572,7 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList & flist )
35653572

35663573
for ( int i = 0; i < attributevec.count(); ++i )
35673574
{
3568-
if ( !attributevec[i].isValid() )
3575+
if ( !attributevec[i].isNull() )
35693576
continue;
35703577

35713578
if ( i >= attributeFields.count() )
@@ -3594,7 +3601,7 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList & flist )
35943601
for ( QgsFeatureList::iterator features = flist.begin(); features != flist.end(); features++ )
35953602
{
35963603
// looping on each feature to insert
3597-
const QgsAttributes & attributevec = features->attributes();
3604+
const QgsAttributes& attributevec = features->attributes();
35983605

35993606
// resetting Prepared Statement and bindings
36003607
sqlite3_reset( stmt );
@@ -3627,23 +3634,18 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList & flist )
36273634
for ( int i = 0; i < attributevec.count(); ++i )
36283635
{
36293636
QVariant v = attributevec[i];
3630-
if ( !v.isValid() )
3637+
if ( !v.isNull() )
36313638
continue;
36323639

36333640
// binding values for each attribute
36343641
if ( i >= attributeFields.count() )
3635-
continue;
3642+
break;
36363643

36373644
QString fieldname = attributeFields[i].name();
36383645
if ( fieldname.isEmpty() || fieldname == mGeometryColumn )
36393646
continue;
36403647

36413648
QVariant::Type type = attributeFields[i].type();
3642-
if ( v.toString().isEmpty() )
3643-
{
3644-
// assuming to be a NULL value
3645-
type = QVariant::Invalid;
3646-
}
36473649

36483650
if ( type == QVariant::Int )
36493651
{

0 commit comments

Comments
 (0)