Skip to content

Commit 5b5dc95

Browse files
committedSep 6, 2017
Add an enum for postgres relation kind
1 parent 088acdb commit 5b5dc95

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed
 

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,11 +1285,9 @@ bool QgsPostgresProvider::determinePrimaryKey()
12851285
// If the relation is a view try to find a suitable column to use as
12861286
// the primary key.
12871287

1288-
sql = QStringLiteral( "SELECT relkind FROM pg_class WHERE oid=regclass(%1)::oid" ).arg( quotedValue( mQuery ) );
1289-
res = connectionRO()->PQexec( sql );
1290-
QString type = res.PQgetvalue( 0, 0 );
1288+
QgsPostgresProvider::Relkind type = relkind();
12911289

1292-
if ( type == QLatin1String( "r" ) ) // the relation is a table
1290+
if ( type == Relkind::OrdinaryTable )
12931291
{
12941292
QgsDebugMsg( "Relation is a table. Checking to see if it has an oid column." );
12951293

@@ -1324,13 +1322,15 @@ bool QgsPostgresProvider::determinePrimaryKey()
13241322
}
13251323
}
13261324
}
1327-
else if ( type == QLatin1String( "v" ) || type == QLatin1String( "m" ) ) // the relation is a view
1325+
else if ( type == Relkind::View || type == Relkind::MaterializedView )
13281326
{
13291327
determinePrimaryKeyFromUriKeyColumn();
13301328
}
13311329
else
13321330
{
1333-
QgsMessageLog::logMessage( tr( "Unexpected relation type '%1'." ).arg( type ), tr( "PostGIS" ) );
1331+
const QMetaEnum metaEnum( QMetaEnum::fromType<Relkind>() );
1332+
QString typeName = metaEnum.valueToKey( type );
1333+
QgsMessageLog::logMessage( tr( "Unexpected relation type '%1'." ).arg( typeName ), tr( "PostGIS" ) );
13341334
}
13351335
}
13361336
else
@@ -4247,6 +4247,50 @@ QgsAttrPalIndexNameHash QgsPostgresProvider::palAttributeIndexNames() const
42474247
return mAttrPalIndexName;
42484248
}
42494249

4250+
QgsPostgresProvider::Relkind QgsPostgresProvider::relkind() const
4251+
{
4252+
QString sql = QStringLiteral( "SELECT relkind FROM pg_class WHERE oid=regclass(%1)::oid" ).arg( quotedValue( mQuery ) );
4253+
QgsPostgresResult res( connectionRO()->PQexec( sql ) );
4254+
QString type = res.PQgetvalue( 0, 0 );
4255+
4256+
QgsPostgresProvider::Relkind kind = Relkind::Unknown;
4257+
4258+
if ( type == QLatin1String( "r" ) )
4259+
{
4260+
kind = Relkind::OrdinaryTable;
4261+
}
4262+
else if ( type == QLatin1String( "i" ) )
4263+
{
4264+
kind = Relkind::Index;
4265+
}
4266+
else if ( type == QLatin1String( "s" ) )
4267+
{
4268+
kind = Relkind::Sequence;
4269+
}
4270+
else if ( type == QLatin1String( "v" ) )
4271+
{
4272+
kind = Relkind::View;
4273+
}
4274+
else if ( type == QLatin1String( "m" ) )
4275+
{
4276+
kind = Relkind::MaterializedView;
4277+
}
4278+
else if ( type == QLatin1String( "c" ) )
4279+
{
4280+
kind = Relkind::CompositeType;
4281+
}
4282+
else if ( type == QLatin1String( "t" ) )
4283+
{
4284+
kind = Relkind::ToastTable;
4285+
}
4286+
else if ( type == QLatin1String( "f" ) )
4287+
{
4288+
kind = Relkind::ForeignTable;
4289+
}
4290+
4291+
return kind;
4292+
}
4293+
42504294
/**
42514295
* Class factory to return a pointer to a newly created
42524296
* QgsPostgresProvider object

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ class QgsPostgresProvider : public QgsVectorDataProvider
4848
Q_OBJECT
4949

5050
public:
51+
enum Relkind
52+
{
53+
Unknown,
54+
OrdinaryTable, // r
55+
Index, // i
56+
Sequence, // s
57+
View, // v
58+
MaterializedView, // m
59+
CompositeType, // c
60+
ToastTable, // t
61+
ForeignTable // f
62+
};
63+
Q_ENUM( Relkind );
5164

5265
/** Import a vector layer into the database
5366
* \param options options for provider, specified via a map of option name
@@ -206,6 +219,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
206219
void repaintRequested();
207220

208221
private:
222+
Relkind relkind() const;
209223

210224
bool declareCursor( const QString &cursorName,
211225
const QgsAttributeList &fetchAttributes,

0 commit comments

Comments
 (0)
Please sign in to comment.