Skip to content
Permalink
Browse files

[postgres] Update primary key on insert with pktFidMap

Helps to retrieve generated keys with views
  • Loading branch information
m-kuhn committed Apr 7, 2016
1 parent 40f214b commit 4ec97c3362154171bbfd251e99784948081bbabe
Showing with 23 additions and 1 deletion.
  1. +23 −1 src/providers/postgres/qgspostgresprovider.cpp
@@ -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 );

@@ -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 )

1 comment on commit 4ec97c3

@3nids

This comment has been minimized.

Copy link
Member

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

you rock!

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