Add an enum for postgres relation kind

This commit is contained in:
Blottiere Paul 2017-08-21 03:47:25 +01:00
parent 088acdba40
commit 5b5dc9545d
2 changed files with 64 additions and 6 deletions

View File

@ -1285,11 +1285,9 @@ bool QgsPostgresProvider::determinePrimaryKey()
// If the relation is a view try to find a suitable column to use as // If the relation is a view try to find a suitable column to use as
// the primary key. // the primary key.
sql = QStringLiteral( "SELECT relkind FROM pg_class WHERE oid=regclass(%1)::oid" ).arg( quotedValue( mQuery ) ); QgsPostgresProvider::Relkind type = relkind();
res = connectionRO()->PQexec( sql );
QString type = res.PQgetvalue( 0, 0 );
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." ); 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(); determinePrimaryKeyFromUriKeyColumn();
} }
else else
{ {
QgsMessageLog::logMessage( tr( "Unexpected relation type '%1'." ).arg( type ), tr( "PostGIS" ) ); const QMetaEnum metaEnum( QMetaEnum::fromType<Relkind>() );
QString typeName = metaEnum.valueToKey( type );
QgsMessageLog::logMessage( tr( "Unexpected relation type '%1'." ).arg( typeName ), tr( "PostGIS" ) );
} }
} }
else else
@ -4247,6 +4247,50 @@ QgsAttrPalIndexNameHash QgsPostgresProvider::palAttributeIndexNames() const
return mAttrPalIndexName; 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 * Class factory to return a pointer to a newly created
* QgsPostgresProvider object * QgsPostgresProvider object

View File

@ -48,6 +48,19 @@ class QgsPostgresProvider : public QgsVectorDataProvider
Q_OBJECT Q_OBJECT
public: 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 /** Import a vector layer into the database
* \param options options for provider, specified via a map of option name * \param options options for provider, specified via a map of option name
@ -206,6 +219,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
void repaintRequested(); void repaintRequested();
private: private:
Relkind relkind() const;
bool declareCursor( const QString &cursorName, bool declareCursor( const QString &cursorName,
const QgsAttributeList &fetchAttributes, const QgsAttributeList &fetchAttributes,