Skip to content
Permalink
Browse files

Merge pull request #3384 from wonder-sk/fix-postgres-qt5

Fix postgres pkey map in Qt5 (fixes #15223)
  • Loading branch information
3nids committed Aug 15, 2016
2 parents ea4e5cb + be3c780 commit 1cb48ddd65a98f8b17d880edbf4dd90d62fe7a91
@@ -733,7 +733,7 @@ bool QgsPostgresFeatureIterator::getFeature( QgsPostgresResult &queryResult, int

case pktFidMap:
{
QList<QVariant> primaryKeyVals;
QVariantList primaryKeyVals;

Q_FOREACH ( int idx, mSource->mPrimaryKeyAttrs )
{
@@ -748,7 +748,7 @@ bool QgsPostgresFeatureIterator::getFeature( QgsPostgresResult &queryResult, int
col++;
}

fid = mSource->mShared->lookupFid( QVariant( primaryKeyVals ) );
fid = mSource->mShared->lookupFid( primaryKeyVals );

}
break;
@@ -319,6 +319,8 @@ QString QgsPostgresProvider::storageType() const
}

// Qt5 has that built in
// ... BUT it does not behave exactly the same way as our implementation
// (e.g. comparison of QVariantList does not work)
#if QT_VERSION < 0x050000
static bool operator<( const QVariant &a, const QVariant &b )
{
@@ -401,6 +403,17 @@ static bool operator<( const QVariant &a, const QVariant &b )
}
#endif

#if QT_VERSION >= 0x050000 && QT_VERSION < 0x050600
#include <algorithm>
template <typename T>
bool operator<( const QList<T> &lhs, const QList<T> &rhs )
{
return std::lexicographical_compare( lhs.begin(), lhs.end(),
rhs.begin(), rhs.end() );
}
#endif


QgsFeatureIterator QgsPostgresProvider::getFeatures( const QgsFeatureRequest& request ) const
{
if ( !mValid )
@@ -488,11 +501,9 @@ void QgsPostgresProvider::appendPkParams( QgsFeatureId featureId, QStringList &p

case pktFidMap:
{
QVariant pkValsVariant = mShared->lookupKey( featureId );
QList<QVariant> pkVals;
if ( !pkValsVariant.isNull() )
QVariantList pkVals = mShared->lookupKey( featureId );
if ( !pkVals.isEmpty() )
{
pkVals = pkValsVariant.toList();
Q_ASSERT( pkVals.size() == mPrimaryKeyAttrs.size() );
}

@@ -554,11 +565,9 @@ QString QgsPostgresUtils::whereClause( QgsFeatureId featureId, const QgsFields&

case pktFidMap:
{
QVariant pkValsVariant = sharedData->lookupKey( featureId );
if ( !pkValsVariant.isNull() )
QVariantList pkVals = sharedData->lookupKey( featureId );
if ( !pkVals.isEmpty() )
{
QList<QVariant> pkVals = pkValsVariant.toList();

Q_ASSERT( pkVals.size() == pkAttrs.size() );

QString delim = "";
@@ -2058,14 +2067,14 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
}
else
{
QList<QVariant> primaryKeyVals;
QVariantList primaryKeyVals;

Q_FOREACH ( int idx, mPrimaryKeyAttrs )
{
primaryKeyVals << attrs.at( idx );
}

features->setFeatureId( mShared->lookupFid( QVariant( primaryKeyVals ) ) );
features->setFeatureId( mShared->lookupFid( primaryKeyVals ) );
}
QgsDebugMsgLevel( QString( "new fid=%1" ).arg( features->id() ), 4 );
}
@@ -2416,9 +2425,7 @@ bool QgsPostgresProvider::changeAttributeValues( const QgsChangedAttributesMap &
// update feature id map if key was changed
if ( pkChanged && mPrimaryKeyType == pktFidMap )
{
QVariant v = mShared->removeFid( fid );

QList<QVariant> k = v.toList();
QVariantList k = mShared->removeFid( fid );

for ( int i = 0; i < mPrimaryKeyAttrs.size(); i++ )
{
@@ -2766,9 +2773,7 @@ bool QgsPostgresProvider::changeFeatures( const QgsChangedAttributesMap &attr_ma
// update feature id map if key was changed
if ( pkChanged && mPrimaryKeyType == pktFidMap )
{
QVariant v = mShared->removeFid( fid );

QList<QVariant> k = v.toList();
QVariantList k = mShared->removeFid( fid );

for ( int i = 0; i < mPrimaryKeyAttrs.size(); i++ )
{
@@ -4291,11 +4296,11 @@ void QgsPostgresSharedData::setFeaturesCounted( long count )
}


QgsFeatureId QgsPostgresSharedData::lookupFid( const QVariant &v )
QgsFeatureId QgsPostgresSharedData::lookupFid( const QVariantList& v )
{
QMutexLocker locker( &mMutex );

QMap<QVariant, QgsFeatureId>::const_iterator it = mKeyToFid.constFind( v );
QMap<QVariantList, QgsFeatureId>::const_iterator it = mKeyToFid.constFind( v );

if ( it != mKeyToFid.constEnd() )
{
@@ -4309,30 +4314,30 @@ QgsFeatureId QgsPostgresSharedData::lookupFid( const QVariant &v )
}


QVariant QgsPostgresSharedData::removeFid( QgsFeatureId fid )
QVariantList QgsPostgresSharedData::removeFid( QgsFeatureId fid )
{
QMutexLocker locker( &mMutex );

QVariant v = mFidToKey[ fid ];
QVariantList v = mFidToKey[ fid ];
mFidToKey.remove( fid );
mKeyToFid.remove( v );
return v;
}

void QgsPostgresSharedData::insertFid( QgsFeatureId fid, const QVariant& k )
void QgsPostgresSharedData::insertFid( QgsFeatureId fid, const QVariantList& k )
{
QMutexLocker locker( &mMutex );

mFidToKey.insert( fid, k );
mKeyToFid.insert( k, fid );
}

QVariant QgsPostgresSharedData::lookupKey( QgsFeatureId featureId )
QVariantList QgsPostgresSharedData::lookupKey( QgsFeatureId featureId )
{
QMutexLocker locker( &mMutex );

QMap<QgsFeatureId, QVariant>::const_iterator it = mFidToKey.constFind( featureId );
QMap<QgsFeatureId, QVariantList>::const_iterator it = mFidToKey.constFind( featureId );
if ( it != mFidToKey.constEnd() )
return it.value();
return QVariant();
return QVariantList();
}
@@ -508,19 +508,19 @@ class QgsPostgresSharedData
void ensureFeaturesCountedAtLeast( long fetched );

// FID lookups
QgsFeatureId lookupFid( const QVariant &v ); // lookup existing mapping or add a new one
QVariant removeFid( QgsFeatureId fid );
void insertFid( QgsFeatureId fid, const QVariant& k );
QVariant lookupKey( QgsFeatureId featureId );
QgsFeatureId lookupFid( const QVariantList& v ); // lookup existing mapping or add a new one
QVariantList removeFid( QgsFeatureId fid );
void insertFid( QgsFeatureId fid, const QVariantList& k );
QVariantList lookupKey( QgsFeatureId featureId );

protected:
QMutex mMutex; //!< Access to all data members is guarded by the mutex

long mFeaturesCounted; //! Number of features in the layer

QgsFeatureId mFidCounter; // next feature id if map is used
QMap<QVariant, QgsFeatureId> mKeyToFid; // map key values to feature id
QMap<QgsFeatureId, QVariant> mFidToKey; // map feature back to fea
QMap<QVariantList, QgsFeatureId> mKeyToFid; // map key values to feature id
QMap<QgsFeatureId, QVariantList> mFidToKey; // map feature id back to key values
};

#endif

1 comment on commit 1cb48dd

@m-kuhn

This comment has been minimized.

Copy link
Member

@m-kuhn m-kuhn commented on 1cb48dd Aug 16, 2016

Good job!

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