Skip to content

Commit

Permalink
Add an enum for postgres relation kind
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Sep 6, 2017
1 parent 088acdb commit 5b5dc95
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
56 changes: 50 additions & 6 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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." );

Expand Down Expand Up @@ -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<Relkind>() );
QString typeName = metaEnum.valueToKey( type );
QgsMessageLog::logMessage( tr( "Unexpected relation type '%1'." ).arg( typeName ), tr( "PostGIS" ) );
}
}
else
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -206,6 +219,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
void repaintRequested();

private:
Relkind relkind() const;

bool declareCursor( const QString &cursorName,
const QgsAttributeList &fetchAttributes,
Expand Down

0 comments on commit 5b5dc95

Please sign in to comment.