@@ -1478,7 +1478,7 @@ bool QgsPostgresProvider::determinePrimaryKey()
1478
1478
continue ;
1479
1479
}
1480
1480
1481
- if ( isInt && ( mAttributeFields [j].type () == QVariant::Int || mAttributeFields [j].type () == QVariant::LongLong ) )
1481
+ if ( isInt && mAttributeFields [j].type () != QVariant::Int && mAttributeFields [j].type () != QVariant::LongLong )
1482
1482
isInt = false ;
1483
1483
1484
1484
mPrimaryKeyAttrs << j;
@@ -1774,7 +1774,7 @@ QString QgsPostgresProvider::paramValue( QString fieldValue, const QString &defa
1774
1774
if ( fieldValue == defaultValue && !defaultValue.isNull () )
1775
1775
{
1776
1776
QgsPostgresResult result = mConnectionRW ->PQexec ( QString ( " SELECT %1" ).arg ( defaultValue ) );
1777
- if ( result.PQresultStatus () == PGRES_FATAL_ERROR )
1777
+ if ( result.PQresultStatus () != PGRES_TUPLES_OK )
1778
1778
throw PGException ( result );
1779
1779
1780
1780
return result.PQgetvalue ( 0 , 0 );
@@ -1936,8 +1936,6 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
1936
1936
if ( stmt.PQresultStatus () == PGRES_FATAL_ERROR )
1937
1937
throw PGException ( stmt );
1938
1938
1939
- QList<int > newIds;
1940
-
1941
1939
for ( QgsFeatureList::iterator features = flist.begin (); features != flist.end (); features++ )
1942
1940
{
1943
1941
const QgsAttributeMap &attributevec = features->attributeMap ();
@@ -1949,16 +1947,62 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
1949
1947
}
1950
1948
1951
1949
for ( int i = 0 ; i < fieldId.size (); i++ )
1952
- params << paramValue ( attributevec.value ( fieldId[i], defaultValues[i] ).toString (), defaultValues[i] );
1950
+ {
1951
+ QgsAttributeMap::const_iterator attr = attributevec.find ( fieldId[i] );
1952
+
1953
+ QString v;
1954
+ if ( attr == attributevec.end () )
1955
+ {
1956
+ v = paramValue ( defaultValues[i], defaultValues[i] );
1957
+ features->addAttribute ( fieldId[i], v );
1958
+ }
1959
+ else
1960
+ {
1961
+ v = paramValue ( attr.value ().toString (), defaultValues[i] );
1962
+
1963
+ if ( v != attr.value ().toString () )
1964
+ features->changeAttribute ( fieldId[i], v );
1965
+ }
1966
+
1967
+ params << v;
1968
+ }
1953
1969
1954
1970
QgsPostgresResult result = mConnectionRW ->PQexecPrepared ( " addfeatures" , params );
1955
- if ( result.PQresultStatus () == PGRES_FATAL_ERROR )
1971
+ if ( result.PQresultStatus () != PGRES_COMMAND_OK )
1956
1972
throw PGException ( result );
1973
+
1974
+ if ( mPrimaryKeyType == pktOid )
1975
+ {
1976
+ features->setFeatureId ( result.PQoidValue () );
1977
+ QgsDebugMsgLevel ( QString ( " new fid=%1" ).arg ( features->id () ), 4 );
1978
+ }
1957
1979
}
1958
1980
1959
- if ( flist.size () == newIds.size () )
1960
- for ( int i = 0 ; i < flist.size (); i++ )
1961
- flist[i].setFeatureId ( newIds[i] );
1981
+ // update feature ids
1982
+ if ( mPrimaryKeyType == pktInt || mPrimaryKeyType == pktFidMap )
1983
+ {
1984
+ for ( QgsFeatureList::iterator features = flist.begin (); features != flist.end (); features++ )
1985
+ {
1986
+ const QgsAttributeMap &attributevec = features->attributeMap ();
1987
+
1988
+ if ( mPrimaryKeyType == pktInt )
1989
+ {
1990
+ features->setFeatureId ( STRING_TO_FID ( attributevec[ mPrimaryKeyAttrs [0 ] ] ) );
1991
+ }
1992
+ else
1993
+ {
1994
+ QList<QVariant> primaryKeyVals;
1995
+
1996
+ foreach ( int idx, mPrimaryKeyAttrs )
1997
+ {
1998
+ primaryKeyVals << attributevec[ mPrimaryKeyAttrs [idx] ];
1999
+ }
2000
+
2001
+ features->setFeatureId ( lookupFid ( QVariant ( primaryKeyVals ) ) );
2002
+ }
2003
+ QgsDebugMsgLevel ( QString ( " new fid=%1" ).arg ( features->id () ), 4 );
2004
+ }
2005
+ }
1962
2006
1963
2007
mConnectionRW ->PQexecNR ( " DEALLOCATE addfeatures" );
1964
2008
mConnectionRW ->PQexecNR ( " COMMIT" );
@@ -2704,7 +2748,7 @@ bool QgsPostgresProvider::getGeometryDetails()
2704
2748
QgsDebugMsg ( " Requested SRID is " + mRequestedSrid );
2705
2749
QgsDebugMsg ( " Detected type is " + QString::number ( mDetectedGeomType ) );
2706
2750
QgsDebugMsg ( " Requested type is " + QString::number ( mRequestedGeomType ) );
2707
- QgsDebugMsg ( " Feature type name is " + QString ( QGis::qgisFeatureTypes[ geometryType () ] ) );
2751
+ QgsDebugMsg ( " Feature type name is " + QString ( QGis::qgisFeatureTypes[ geometryType ()] ) );
2708
2752
QgsDebugMsg ( " Geometry is geography " + mIsGeography );
2709
2753
}
2710
2754
else
0 commit comments