Skip to content

Commit

Permalink
Fix spatialite provider for Null QVariant instead of Invalid QVariant
Browse files Browse the repository at this point in the history
  • 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 ); QVariant v( value );


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


return v; 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; return SPATIALITE_DESCRIPTION;
} // QgsSpatiaLiteProvider::description() } // QgsSpatiaLiteProvider::description()


const QgsFields & QgsSpatiaLiteProvider::fields() const const QgsFields& QgsSpatiaLiteProvider::fields() const
{ {
return attributeFields; return attributeFields;
} }
Expand All @@ -3290,7 +3290,7 @@ QVariant QgsSpatiaLiteProvider::minimumValue( int index )
try try
{ {
// get the field name // 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 ); 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 ); ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
if ( ret != SQLITE_OK ) 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 );
} }
} minValue = QString();
sqlite3_free_table( results );

if ( minValue.isEmpty() )
{
// NULL or not found
return QVariant( QString::null );
} }
else 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 ) catch ( SLFieldNotFound )
{ {
return QVariant( QString::null ); return QVariant( QVariant::Int );
} }


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


// Returns the maximum value of an attribute // 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 ); ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
if ( ret != SQLITE_OK ) 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 );
} }
} maxValue = QString();
sqlite3_free_table( results );

if ( maxValue.isEmpty() )
{
// NULL or not found
return QVariant( QString::null );
} }
else 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 ) catch ( SLFieldNotFound )
{ {
return QVariant( QString::null ); return QVariant( QVariant::Int );
} }


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


// Returns the list of unique values of an attribute // 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 ) for ( int i = 0; i < attributevec.count(); ++i )
{ {
if ( !attributevec[i].isValid() ) if ( !attributevec[i].isNull() )
continue; continue;


if ( i >= attributeFields.count() ) 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++ ) for ( QgsFeatureList::iterator features = flist.begin(); features != flist.end(); features++ )
{ {
// looping on each feature to insert // looping on each feature to insert
const QgsAttributes & attributevec = features->attributes(); const QgsAttributes& attributevec = features->attributes();


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


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


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


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


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

0 comments on commit 151e0cc

Please sign in to comment.