Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix spatialite provider for Null QVariant instead of Invalid QVariant
  • Loading branch information
m-kuhn committed Sep 5, 2013
1 parent a2886cc commit 151e0cc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/core/qgsvectordataprovider.cpp
Expand Up @@ -396,8 +396,8 @@ QVariant QgsVectorDataProvider::convertValue( QVariant::Type type, QString value
{
QVariant v( value );

if ( !v.convert( type ) )
v = QVariant( QString::null );
if ( !v.convert( type ) || value.isNull() )
v = QVariant( type );

return v;
}
Expand Down
116 changes: 59 additions & 57 deletions src/providers/spatialite/qgsspatialiteprovider.cpp
Expand Up @@ -3270,7 +3270,7 @@ QString QgsSpatiaLiteProvider::description() const
return SPATIALITE_DESCRIPTION;
} // QgsSpatiaLiteProvider::description()

const QgsFields & QgsSpatiaLiteProvider::fields() const
const QgsFields& QgsSpatiaLiteProvider::fields() const
{
return attributeFields;
}
Expand All @@ -3290,7 +3290,7 @@ QVariant QgsSpatiaLiteProvider::minimumValue( int index )
try
{
// get the field name
const QgsField & fld = field( index );
const QgsField& fld = field( index );

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

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

ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
if ( ret != SQLITE_OK )
goto error;
if ( rows < 1 )
;
else
{
for ( i = 1; i <= rows; i++ )
QgsMessageLog::logMessage( tr( "SQLite error: %2\nSQL: %1" ).arg( sql ).arg( errMsg ? errMsg : tr( "unknown cause" ) ), tr( "SpatiaLite" ) );
// unexpected error
if ( errMsg != NULL )
{
minValue = results[( i * columns ) + 0];
sqlite3_free( errMsg );
}
}
sqlite3_free_table( results );

if ( minValue.isEmpty() )
{
// NULL or not found
return QVariant( QString::null );
minValue = QString();
}
else
{
return convertValue( fld.type(), minValue );
if ( rows < 1 )
;
else
{
for ( i = 1; i <= rows; i++ )
{
minValue = results[( i * columns ) + 0];
}
}
sqlite3_free_table( results );

if ( minValue.isEmpty() )
{
// NULL or not found
minValue = QString();
}
}

return convertValue( fld.type(), minValue );
}
catch ( SLFieldNotFound )
{
return QVariant( QString::null );
return QVariant( QVariant::Int );
}

error:
QgsMessageLog::logMessage( tr( "SQLite error: %2\nSQL: %1" ).arg( sql ).arg( errMsg ? errMsg : tr( "unknown cause" ) ), tr( "SpatiaLite" ) );
// unexpected error
if ( errMsg != NULL )
{
sqlite3_free( errMsg );
}
return QVariant( QString::null );
// dummy return, so compiler is quiet
return QVariant();
}

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

ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
if ( ret != SQLITE_OK )
goto error;
if ( rows < 1 )
;
else
{
for ( i = 1; i <= rows; i++ )
QgsMessageLog::logMessage( tr( "SQLite error: %2\nSQL: %1" ).arg( sql ).arg( errMsg ? errMsg : tr( "unknown cause" ) ), tr( "SpatiaLite" ) );
// unexpected error
if ( errMsg != NULL )
{
maxValue = results[( i * columns ) + 0];
sqlite3_free( errMsg );
}
}
sqlite3_free_table( results );

if ( maxValue.isEmpty() )
{
// NULL or not found
return QVariant( QString::null );
maxValue = QString();
}
else
{
return convertValue( fld.type(), maxValue );

if ( rows < 1 )
;
else
{
for ( i = 1; i <= rows; i++ )
{
maxValue = results[( i * columns ) + 0];
}
}
sqlite3_free_table( results );

if ( maxValue.isEmpty() )
{
// NULL or not found
maxValue = QString();
}
}

return convertValue( fld.type(), maxValue );
}
catch ( SLFieldNotFound )
{
return QVariant( QString::null );
return QVariant( QVariant::Int );
}

error:
QgsMessageLog::logMessage( tr( "SQLite error: %2\nSQL: %1" ).arg( sql ).arg( errMsg ? errMsg : tr( "unknown cause" ) ), tr( "SpatiaLite" ) );
// unexpected error
if ( errMsg != NULL )
{
sqlite3_free( errMsg );
}
return QVariant( QString::null );
// dummy return, so compiler is quiet
return QVariant();
}

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

for ( int i = 0; i < attributevec.count(); ++i )
{
if ( !attributevec[i].isValid() )
if ( !attributevec[i].isNull() )
continue;

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

// resetting Prepared Statement and bindings
sqlite3_reset( stmt );
Expand Down Expand Up @@ -3627,23 +3634,18 @@ bool QgsSpatiaLiteProvider::addFeatures( QgsFeatureList & flist )
for ( int i = 0; i < attributevec.count(); ++i )
{
QVariant v = attributevec[i];
if ( !v.isValid() )
if ( !v.isNull() )
continue;

// binding values for each attribute
if ( i >= attributeFields.count() )
continue;
break;

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

QVariant::Type type = attributeFields[i].type();
if ( v.toString().isEmpty() )
{
// assuming to be a NULL value
type = QVariant::Invalid;
}

if ( type == QVariant::Int )
{
Expand Down

0 comments on commit 151e0cc

Please sign in to comment.