Skip to content

Commit

Permalink
Fix #6146 (Offline editing tool broken)
Browse files Browse the repository at this point in the history
Adding of features back to remote database didn't work in case of primary keys
being auto-generated. With the fix, if the user did not set any value (i.e. it is NULL),
the value will be set to provider's default (and thus use the sequence generator)
  • Loading branch information
wonder-sk committed Feb 7, 2014
1 parent 9a4e701 commit 8b8eb56
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/core/qgsofflineediting.cpp
Expand Up @@ -411,7 +411,7 @@ void QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlite3* db, con
{
QString dataType = "";
QVariant::Type type = fields[idx].type();
if ( type == QVariant::Int )
if ( type == QVariant::Int || type == QVariant::LongLong )
{
dataType = "INTEGER";
}
Expand Down Expand Up @@ -622,6 +622,15 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVec
QString sql = QString( "SELECT \"fid\" FROM 'log_added_features' WHERE \"layer_id\" = %1" ).arg( layerId );
QList<int> newFeatureIds = sqlQueryInts( db, sql );

// get default value for each field
const QgsFields& remoteFlds = remoteLayer->pendingFields();
QVector<QVariant> defaultValues( remoteFlds.count() );
for ( int i = 0; i < remoteFlds.count(); ++i )
{
if ( remoteFlds.fieldOrigin( i ) == QgsFields::OriginProvider )
defaultValues[i] = remoteLayer->dataProvider()->defaultValue( remoteFlds.fieldOriginIndex( i ) );
}

// get new features from offline layer
QgsFeatureList features;
for ( int i = 0; i < newFeatureIds.size(); i++ )
Expand Down Expand Up @@ -651,6 +660,15 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer* offlineLayer, QgsVec
{
newAttrs[ attrLookup[ it ] ] = attrs[ it ];
}

// try to use default value from the provider
// (important especially e.g. for postgis primary key generated from a sequence)
for ( int k = 0; k < newAttrs.count(); ++k )
{
if ( newAttrs[k].isNull() && !defaultValues[k].isNull() )
newAttrs[k] = defaultValues[k];
}

f.setAttributes( newAttrs );

remoteLayer->addFeature( f, false );
Expand Down

0 comments on commit 8b8eb56

Please sign in to comment.