Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Only shift 32bit int identifiers when negative
Fixes existing tests to retain expectances
  • Loading branch information
strk committed Jun 9, 2016
1 parent e763b41 commit f4a135b
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -42,10 +42,20 @@
const QString POSTGRES_KEY = "postgres";
const QString POSTGRES_DESCRIPTION = "PostgreSQL/PostGIS data provider";

// Add this offset to the database integer primary keys
// to support the whole range of values (including negative)
// We convert signed 32bit integers to unsigned 64bit integers
// to support the whole range of int32 values (including negative)
// See http://hub.qgis.org/issues/14262
#define PKINT_OFFSET (2^32)
inline uint64_t PKINT2FID( int32_t x )
{
return static_cast<uint64_t>( x );
}

inline int32_t FID2PKINT( int64_t x )
{
return static_cast<int32_t>( x );
}



QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
: QgsVectorDataProvider( uri )
Expand Down Expand Up @@ -453,7 +463,7 @@ void QgsPostgresProvider::appendPkParams( QgsFeatureId featureId, QStringList &p
break;

case pktInt:
params << QString::number( featureId - PKINT_OFFSET );
params << QString::number( FID2PKINT( featureId ) );
break;

case pktTid:
Expand Down Expand Up @@ -518,7 +528,7 @@ QString QgsPostgresUtils::whereClause( QgsFeatureId featureId, const QgsFields&

case pktInt:
Q_ASSERT( pkAttrs.size() == 1 );
whereClause = QString( "%1=%2" ).arg( QgsPostgresConn::quotedIdentifier( fields[ pkAttrs[0] ].name() ) ).arg( featureId - PKINT_OFFSET );
whereClause = QString( "%1=%2" ).arg( QgsPostgresConn::quotedIdentifier( fields[ pkAttrs[0] ].name() ) ).arg( FID2PKINT( featureId ) );
break;

case pktUint64:
Expand Down Expand Up @@ -585,7 +595,7 @@ QString QgsPostgresUtils::whereClause( const QgsFeatureIds& featureIds, const Qg

Q_FOREACH ( const QgsFeatureId featureId, featureIds )
{
expr += delim + FID_TO_STRING( pkType == pktOid ? featureId : pkType == pktUint64 ? featureId : featureId - PKINT_OFFSET );
expr += delim + FID_TO_STRING( pkType == pktOid ? featureId : pkType == pktUint64 ? featureId : FID2PKINT( featureId ) );
delim = ',';
}
expr += ')';
Expand Down Expand Up @@ -2070,7 +2080,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
}
else if ( mPrimaryKeyType == pktInt )
{
features->setFeatureId( STRING_TO_FID( attrs.at( mPrimaryKeyAttrs.at( 0 ) ) ) + PKINT_OFFSET );
features->setFeatureId( PKINT2FID( STRING_TO_FID( attrs.at( mPrimaryKeyAttrs.at( 0 ) ) ) ) );
}
else
{
Expand Down

0 comments on commit f4a135b

Please sign in to comment.