Skip to content

Commit

Permalink
[postgres] Update primary key on insert with pktFidMap
Browse files Browse the repository at this point in the history
Helps to retrieve generated keys with views
  • Loading branch information
m-kuhn committed Apr 7, 2016
1 parent 40f214b commit 4ec97c3
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -1918,8 +1918,21 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )

insert += values + ')';

if ( mPrimaryKeyType == pktFidMap )
{
insert += " RETURNING ";

QString delim;
Q_FOREACH ( int idx, mPrimaryKeyAttrs )
{
insert += delim + quotedIdentifier( mAttributeFields.at( idx ).name() );
delim = ',';
}
}

QgsDebugMsg( QString( "prepare addfeatures: %1" ).arg( insert ) );
QgsPostgresResult stmt( conn->PQprepare( "addfeatures", insert, fieldId.size() + offset - 1, nullptr ) );

if ( stmt.PQresultStatus() != PGRES_COMMAND_OK )
throw PGException( stmt );

Expand Down Expand Up @@ -1961,7 +1974,16 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
}

QgsPostgresResult result( conn->PQexecPrepared( "addfeatures", params ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )

if ( result.PQresultStatus() == PGRES_TUPLES_OK )
{
for ( int i = 0; i < mPrimaryKeyAttrs.size(); ++i )
{
int idx = mPrimaryKeyAttrs.at( i );
features->setAttribute( idx, convertValue( mAttributeFields.at( idx ).type(), result.PQgetvalue( 0, i ) ) );
}
}
else if ( result.PQresultStatus() != PGRES_COMMAND_OK )
throw PGException( result );

if ( mPrimaryKeyType == pktOid )
Expand Down

1 comment on commit 4ec97c3

@3nids
Copy link
Member

@3nids 3nids commented on 4ec97c3 Apr 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you rock!

Please sign in to comment.