diff --git a/src/providers/postgres/qgspostgresprovider.cpp b/src/providers/postgres/qgspostgresprovider.cpp index a891dd7dcc0..1d68f00a6be 100644 --- a/src/providers/postgres/qgspostgresprovider.cpp +++ b/src/providers/postgres/qgspostgresprovider.cpp @@ -1285,11 +1285,9 @@ bool QgsPostgresProvider::determinePrimaryKey() // If the relation is a view try to find a suitable column to use as // the primary key. - sql = QStringLiteral( "SELECT relkind FROM pg_class WHERE oid=regclass(%1)::oid" ).arg( quotedValue( mQuery ) ); - res = connectionRO()->PQexec( sql ); - QString type = res.PQgetvalue( 0, 0 ); + QgsPostgresProvider::Relkind type = relkind(); - if ( type == QLatin1String( "r" ) ) // the relation is a table + if ( type == Relkind::OrdinaryTable ) { QgsDebugMsg( "Relation is a table. Checking to see if it has an oid column." ); @@ -1324,13 +1322,15 @@ bool QgsPostgresProvider::determinePrimaryKey() } } } - else if ( type == QLatin1String( "v" ) || type == QLatin1String( "m" ) ) // the relation is a view + else if ( type == Relkind::View || type == Relkind::MaterializedView ) { determinePrimaryKeyFromUriKeyColumn(); } else { - QgsMessageLog::logMessage( tr( "Unexpected relation type '%1'." ).arg( type ), tr( "PostGIS" ) ); + const QMetaEnum metaEnum( QMetaEnum::fromType() ); + QString typeName = metaEnum.valueToKey( type ); + QgsMessageLog::logMessage( tr( "Unexpected relation type '%1'." ).arg( typeName ), tr( "PostGIS" ) ); } } else @@ -4247,6 +4247,50 @@ QgsAttrPalIndexNameHash QgsPostgresProvider::palAttributeIndexNames() const return mAttrPalIndexName; } +QgsPostgresProvider::Relkind QgsPostgresProvider::relkind() const +{ + QString sql = QStringLiteral( "SELECT relkind FROM pg_class WHERE oid=regclass(%1)::oid" ).arg( quotedValue( mQuery ) ); + QgsPostgresResult res( connectionRO()->PQexec( sql ) ); + QString type = res.PQgetvalue( 0, 0 ); + + QgsPostgresProvider::Relkind kind = Relkind::Unknown; + + if ( type == QLatin1String( "r" ) ) + { + kind = Relkind::OrdinaryTable; + } + else if ( type == QLatin1String( "i" ) ) + { + kind = Relkind::Index; + } + else if ( type == QLatin1String( "s" ) ) + { + kind = Relkind::Sequence; + } + else if ( type == QLatin1String( "v" ) ) + { + kind = Relkind::View; + } + else if ( type == QLatin1String( "m" ) ) + { + kind = Relkind::MaterializedView; + } + else if ( type == QLatin1String( "c" ) ) + { + kind = Relkind::CompositeType; + } + else if ( type == QLatin1String( "t" ) ) + { + kind = Relkind::ToastTable; + } + else if ( type == QLatin1String( "f" ) ) + { + kind = Relkind::ForeignTable; + } + + return kind; +} + /** * Class factory to return a pointer to a newly created * QgsPostgresProvider object diff --git a/src/providers/postgres/qgspostgresprovider.h b/src/providers/postgres/qgspostgresprovider.h index 9a264a9f92f..959f34dff5d 100644 --- a/src/providers/postgres/qgspostgresprovider.h +++ b/src/providers/postgres/qgspostgresprovider.h @@ -48,6 +48,19 @@ class QgsPostgresProvider : public QgsVectorDataProvider Q_OBJECT public: + enum Relkind + { + Unknown, + OrdinaryTable, // r + Index, // i + Sequence, // s + View, // v + MaterializedView, // m + CompositeType, // c + ToastTable, // t + ForeignTable // f + }; + Q_ENUM( Relkind ); /** Import a vector layer into the database * \param options options for provider, specified via a map of option name @@ -206,6 +219,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider void repaintRequested(); private: + Relkind relkind() const; bool declareCursor( const QString &cursorName, const QgsAttributeList &fetchAttributes,