Skip to content
Permalink
Browse files

Use FidMap for int64 primary keys

Also rename pktInt64 to pktUint64, in case we'll be able to know
in advance the database contains only positive integers (not
implemented in this commit).
  • Loading branch information
Sandro Santilli authored and strk committed Apr 27, 2016
1 parent a7a0eef commit e763b41c08b699a2e26a9f3ae114fb8d90fca3e1
@@ -49,7 +49,7 @@ enum QgsPostgresPrimaryKeyType
{
pktUnknown,
pktInt,
pktInt64,
pktUint64,
pktTid,
pktOid,
pktFidMap
@@ -581,7 +581,7 @@ bool QgsPostgresFeatureIterator::declareCursor( const QString& whereClause, long
break;

case pktInt:
case pktInt64:
case pktUint64:
query += delim + QgsPostgresConn::quotedIdentifier( mSource->mFields.at( mSource->mPrimaryKeyAttrs.at( 0 ) ).name() );
delim = ',';
break;
@@ -716,7 +716,7 @@ bool QgsPostgresFeatureIterator::getFeature( QgsPostgresResult &queryResult, int
break;

case pktInt:
case pktInt64:
case pktUint64:
fid = mConn->getBinaryInt( queryResult, row, col++ );
if ( !subsetOfAttributes || fetchAttributes.contains( mSource->mPrimaryKeyAttrs.at( 0 ) ) )
{
@@ -198,7 +198,7 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
key = "tid";
break;
case pktInt:
case pktInt64:
case pktUint64:
Q_ASSERT( mPrimaryKeyAttrs.size() == 1 );
Q_ASSERT( mPrimaryKeyAttrs[0] >= 0 && mPrimaryKeyAttrs[0] < mAttributeFields.count() );
key = mAttributeFields.at( mPrimaryKeyAttrs.at( 0 ) ).name();
@@ -407,7 +407,7 @@ QString QgsPostgresProvider::pkParamWhereClause( int offset, const char *alias )
break;

case pktInt:
case pktInt64:
case pktUint64:
Q_ASSERT( mPrimaryKeyAttrs.size() == 1 );
whereClause = QString( "%3%1=$%2" ).arg( quotedIdentifier( field( mPrimaryKeyAttrs[0] ).name() ) ).arg( offset ).arg( aliased );
break;
@@ -448,7 +448,7 @@ void QgsPostgresProvider::appendPkParams( QgsFeatureId featureId, QStringList &p
switch ( mPrimaryKeyType )
{
case pktOid:
case pktInt64:
case pktUint64:
params << QString::number( featureId );
break;

@@ -521,7 +521,7 @@ QString QgsPostgresUtils::whereClause( QgsFeatureId featureId, const QgsFields&
whereClause = QString( "%1=%2" ).arg( QgsPostgresConn::quotedIdentifier( fields[ pkAttrs[0] ].name() ) ).arg( featureId - PKINT_OFFSET );
break;

case pktInt64:
case pktUint64:
Q_ASSERT( pkAttrs.size() == 1 );
whereClause = QString( "%1=%2" ).arg( QgsPostgresConn::quotedIdentifier( fields[ pkAttrs[0] ].name() ) ).arg( featureId );
break;
@@ -573,7 +573,7 @@ QString QgsPostgresUtils::whereClause( const QgsFeatureIds& featureIds, const Qg
{
case pktOid:
case pktInt:
case pktInt64:
case pktUint64:
{
QString expr;

@@ -585,7 +585,7 @@ QString QgsPostgresUtils::whereClause( const QgsFeatureIds& featureIds, const Qg

Q_FOREACH ( const QgsFeatureId featureId, featureIds )
{
expr += delim + FID_TO_STRING( pkType == pktOid ? featureId : pkType == pktInt64 ? featureId : featureId - PKINT_OFFSET );
expr += delim + FID_TO_STRING( pkType == pktOid ? featureId : pkType == pktUint64 ? featureId : featureId - PKINT_OFFSET );
delim = ',';
}
expr += ')';
@@ -1332,7 +1332,11 @@ bool QgsPostgresProvider::determinePrimaryKey()
}
else if ( fld.type() == QVariant::LongLong )
{
mPrimaryKeyType = pktInt64; // 64bit integer
// unless we can guarantee all values are unsigned
// (in which case we could use pktUint64)
// we'll have to use a Map type.
// See http://hub.qgis.org/issues/14262
mPrimaryKeyType = pktFidMap; // pktUint64

This comment has been minimized.

Copy link
@nyalldawson

nyalldawson Jul 5, 2016

Contributor

@strk I've tracked down an issue with the qt5 builds to this change - on a qt5 build it's impossible to select features from a postgres layer (all features are selected) and the attribute table shows the same row repeated. Any ideas on why this occurs?

This comment has been minimized.

Copy link
@strk

strk via email Jul 5, 2016

Contributor
}
else if ( fld.type() == QVariant::Int )
{
@@ -1435,7 +1439,7 @@ void QgsPostgresProvider::determinePrimaryKeyFromUriKeyColumn()
const QgsField& fld = mAttributeFields.at( 0 );
if ( fld.type() == QVariant::LongLong )
{
mPrimaryKeyType = pktInt64; // 64bit integer
mPrimaryKeyType = pktUint64; // 64bit integer
}
else if ( fld.type() == QVariant::Int )
{
@@ -1869,7 +1873,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
delim = ',';
}

if ( mPrimaryKeyType == pktInt || mPrimaryKeyType == pktFidMap || mPrimaryKeyType == pktInt64 )
if ( mPrimaryKeyType == pktInt || mPrimaryKeyType == pktFidMap || mPrimaryKeyType == pktUint64 )
{
Q_FOREACH ( int idx, mPrimaryKeyAttrs )
{
@@ -1978,7 +1982,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )

insert += values + ')';

if ( mPrimaryKeyType == pktFidMap || mPrimaryKeyType == pktInt || mPrimaryKeyType == pktInt64 )
if ( mPrimaryKeyType == pktFidMap || mPrimaryKeyType == pktInt || mPrimaryKeyType == pktUint64 )
{
insert += " RETURNING ";

@@ -2054,13 +2058,13 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
}

// update feature ids
if ( mPrimaryKeyType == pktInt || mPrimaryKeyType == pktFidMap || mPrimaryKeyType == pktInt64 )
if ( mPrimaryKeyType == pktInt || mPrimaryKeyType == pktFidMap || mPrimaryKeyType == pktUint64 )
{
for ( QgsFeatureList::iterator features = flist.begin(); features != flist.end(); ++features )
{
QgsAttributes attrs = features->attributes();

if ( mPrimaryKeyType == pktInt64 )
if ( mPrimaryKeyType == pktUint64 )
{
features->setFeatureId( STRING_TO_FID( attrs.at( mPrimaryKeyAttrs.at( 0 ) ) ) );
}

0 comments on commit e763b41

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