Skip to content
Permalink
Browse files

Add an enum for postgres relation kind

  • Loading branch information
pblottiere committed Aug 21, 2017
1 parent 088acdb commit 5b5dc9545d9828db098d1e467f97dba5d242a6de
Showing with 64 additions and 6 deletions.
  1. +50 −6 src/providers/postgres/qgspostgresprovider.cpp
  2. +14 −0 src/providers/postgres/qgspostgresprovider.h
@@ -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<Relkind>() );
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
@@ -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,

0 comments on commit 5b5dc95

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