Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgis: cache information about enum fields #9219

Merged
merged 2 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1683,42 +1683,50 @@ QStringList QgsPostgresProvider::uniqueStringsMatching( int index, const QString

void QgsPostgresProvider::enumValues( int index, QStringList &enumList ) const
{
enumList.clear();

if ( index < 0 || index >= mAttributeFields.count() )
return;

if ( ! mShared->fieldSupportsEnumValuesIsSet( index ) )
{
mShared->setFieldSupportsEnumValues( index, true );
}
else if ( ! mShared->fieldSupportsEnumValues( index ) )
{
return;
}

//find out type of index
QString fieldName = mAttributeFields.at( index ).name();
const QString fieldName = mAttributeFields.at( index ).name();
QString typeName = mAttributeFields.at( index ).typeName();

// Remove schema extension from typeName
typeName.remove( QRegularExpression( "^([^.]+\\.)+" ) );

//is type an enum?
QString typeSql = QStringLiteral( "SELECT typtype FROM pg_type WHERE typname=%1" ).arg( quotedValue( typeName ) );
const QString typeSql = QStringLiteral( "SELECT typtype FROM pg_type WHERE typname=%1" ).arg( quotedValue( typeName ) );
QgsPostgresResult typeRes( connectionRO()->PQexec( typeSql ) );
if ( typeRes.PQresultStatus() != PGRES_TUPLES_OK || typeRes.PQntuples() < 1 )
{
mShared->setFieldSupportsEnumValues( index, false );
return;
}


QString typtype = typeRes.PQgetvalue( 0, 0 );
const QString typtype = typeRes.PQgetvalue( 0, 0 );
if ( typtype.compare( QLatin1String( "e" ), Qt::CaseInsensitive ) == 0 )
{
//try to read enum_range of attribute
if ( !parseEnumRange( enumList, fieldName ) )
{
enumList.clear();
mShared->setFieldSupportsEnumValues( index, false );
}
}
else
{
//is there a domain check constraint for the attribute?
if ( !parseDomainCheckConstraint( enumList, fieldName ) )
{
enumList.clear();
mShared->setFieldSupportsEnumValues( index, false );
}
}
}
Expand Down Expand Up @@ -5166,3 +5174,22 @@ void QgsPostgresSharedData::clear()
mFeaturesCounted = -1;
mFidCounter = 0;
}

bool QgsPostgresSharedData::fieldSupportsEnumValuesIsSet( int index )
{
QMutexLocker locker( &mMutex );
return mFieldSupportsEnumValues.contains( index );
}

bool QgsPostgresSharedData::fieldSupportsEnumValues( int index )
{
QMutexLocker locker( &mMutex );
return mFieldSupportsEnumValues.contains( index ) && mFieldSupportsEnumValues[ index ];
}

void QgsPostgresSharedData::setFieldSupportsEnumValues( int index, bool isSupported )
{
QMutexLocker locker( &mMutex );
mFieldSupportsEnumValues[ index ] = isSupported;
}

5 changes: 5 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ class QgsPostgresSharedData
QVariantList lookupKey( QgsFeatureId featureId );
void clear();

bool fieldSupportsEnumValuesIsSet( int index );
bool fieldSupportsEnumValues( int index );
void setFieldSupportsEnumValues( int index, bool isSupported );

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

Expand All @@ -530,6 +534,7 @@ class QgsPostgresSharedData
QgsFeatureId mFidCounter = 0; // next feature id if map is used
QMap<QVariantList, QgsFeatureId> mKeyToFid; // map key values to feature id
QMap<QgsFeatureId, QVariantList> mFidToKey; // map feature id back to key values
QMap<int, bool> mFieldSupportsEnumValues; // map field index to bool flag supports enum values
elpaso marked this conversation as resolved.
Show resolved Hide resolved
};

// clazy:excludeall=qstring-allocations
Expand Down