Skip to content

Commit 4ec97c3

Browse files
committed
[postgres] Update primary key on insert with pktFidMap
Helps to retrieve generated keys with views
1 parent 40f214b commit 4ec97c3

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/providers/postgres/qgspostgresprovider.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -1918,8 +1918,21 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
19181918

19191919
insert += values + ')';
19201920

1921+
if ( mPrimaryKeyType == pktFidMap )
1922+
{
1923+
insert += " RETURNING ";
1924+
1925+
QString delim;
1926+
Q_FOREACH ( int idx, mPrimaryKeyAttrs )
1927+
{
1928+
insert += delim + quotedIdentifier( mAttributeFields.at( idx ).name() );
1929+
delim = ',';
1930+
}
1931+
}
1932+
19211933
QgsDebugMsg( QString( "prepare addfeatures: %1" ).arg( insert ) );
19221934
QgsPostgresResult stmt( conn->PQprepare( "addfeatures", insert, fieldId.size() + offset - 1, nullptr ) );
1935+
19231936
if ( stmt.PQresultStatus() != PGRES_COMMAND_OK )
19241937
throw PGException( stmt );
19251938

@@ -1961,7 +1974,16 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
19611974
}
19621975

19631976
QgsPostgresResult result( conn->PQexecPrepared( "addfeatures", params ) );
1964-
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
1977+
1978+
if ( result.PQresultStatus() == PGRES_TUPLES_OK )
1979+
{
1980+
for ( int i = 0; i < mPrimaryKeyAttrs.size(); ++i )
1981+
{
1982+
int idx = mPrimaryKeyAttrs.at( i );
1983+
features->setAttribute( idx, convertValue( mAttributeFields.at( idx ).type(), result.PQgetvalue( 0, i ) ) );
1984+
}
1985+
}
1986+
else if ( result.PQresultStatus() != PGRES_COMMAND_OK )
19651987
throw PGException( result );
19661988

19671989
if ( mPrimaryKeyType == pktOid )

0 commit comments

Comments
 (0)