@@ -1285,11 +1285,9 @@ bool QgsPostgresProvider::determinePrimaryKey()
1285
1285
// If the relation is a view try to find a suitable column to use as
1286
1286
// the primary key.
1287
1287
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 ();
1291
1289
1292
- if ( type == QLatin1String ( " r " ) ) // the relation is a table
1290
+ if ( type == Relkind::OrdinaryTable )
1293
1291
{
1294
1292
QgsDebugMsg ( " Relation is a table. Checking to see if it has an oid column." );
1295
1293
@@ -1324,13 +1322,15 @@ bool QgsPostgresProvider::determinePrimaryKey()
1324
1322
}
1325
1323
}
1326
1324
}
1327
- else if ( type == QLatin1String ( " v " ) || type == QLatin1String ( " m " ) ) // the relation is a view
1325
+ else if ( type == Relkind::View || type == Relkind::MaterializedView )
1328
1326
{
1329
1327
determinePrimaryKeyFromUriKeyColumn ();
1330
1328
}
1331
1329
else
1332
1330
{
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" ) );
1334
1334
}
1335
1335
}
1336
1336
else
@@ -4247,6 +4247,50 @@ QgsAttrPalIndexNameHash QgsPostgresProvider::palAttributeIndexNames() const
4247
4247
return mAttrPalIndexName ;
4248
4248
}
4249
4249
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
+
4250
4294
/* *
4251
4295
* Class factory to return a pointer to a newly created
4252
4296
* QgsPostgresProvider object
0 commit comments