Skip to content
Permalink
Browse files

Fix spatialite provider for Null QVariant instead of Invalid QVariant

  • Loading branch information
m-kuhn committed Aug 30, 2013
1 parent a2886cc commit 151e0cc394204cd102b0e1fad95dd0a2c7193e00
Showing with 61 additions and 59 deletions.
  1. +2 −2 src/core/qgsvectordataprovider.cpp
  2. +59 −57 src/providers/spatialite/qgsspatialiteprovider.cpp
@@ -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;
}
@@ -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;
}
@@ -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 );

@@ -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
@@ -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
@@ -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() )
@@ -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 );
@@ -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 )
{

0 comments on commit 151e0cc

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