Skip to content

Commit d45877b

Browse files
Sandro Santillijef-n
Sandro Santilli
authored andcommitted
Lookup TopoGeometry columns metadata in topology.layer
Closes #4532
1 parent f4451c0 commit d45877b

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

src/providers/postgres/qgspostgresconn.cpp

+75-1
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,76 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
386386
}
387387
}
388388

389+
if ( hasTopology() )
390+
{
391+
// Look in topology.layer
392+
// The following query returns only tables that exist
393+
// and the user has SELECT privilege on.
394+
QString sql = QString( "SELECT "
395+
"l.table_name,"
396+
"l.schema_name,"
397+
"l.feature_column,"
398+
"CASE WHEN l.feature_type = 1 THEN 'MULTIPOINT' "
399+
" WHEN l.feature_type = 2 THEN 'MULTILINESTRING' "
400+
" WHEN l.feature_type = 3 THEN 'MULTIPOLYGON' "
401+
" WHEN l.feature_type = 4 THEN 'GEOMETRYCOLLECTION' "
402+
"END as type,"
403+
"t.srid"
404+
" FROM "
405+
"topology.layer l, pg_class c, pg_namespace n, topology.topology t"
406+
" WHERE l.topology_id = t.id"
407+
" AND c.relname=l.table_name"
408+
" AND l.schema_name=n.nspname"
409+
" AND n.oid=c.relnamespace"
410+
" AND has_schema_privilege(n.nspname,'usage')"
411+
// user has select privilege
412+
" AND has_table_privilege('\"'||n.nspname||'\".\"'||c.relname||'\"','select')"
413+
);
414+
415+
if ( searchPublicOnly )
416+
sql += " AND n.nspname='public'";
417+
418+
sql += QString( " ORDER BY n.nspname,c.relname,l.feature_column" );
419+
420+
QgsDebugMsg( "getting topology.layer info: " + sql );
421+
result = PQexec( sql, true );
422+
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
423+
{
424+
PQexecNR( "COMMIT" );
425+
return false;
426+
}
427+
428+
QString gtableName = "topology.layer";
429+
430+
for ( int idx = 0; idx < result.PQntuples(); idx++ )
431+
{
432+
QString tableName = result.PQgetvalue( idx, 0 );
433+
QString schemaName = result.PQgetvalue( idx, 1 );
434+
QString column = result.PQgetvalue( idx, 2 );
435+
QString type = result.PQgetvalue( idx, 3 );
436+
QString srid = result.PQgetvalue( idx, 4 );
437+
//QString relkind = result.PQgetvalue( idx, 5 );
438+
439+
QgsDebugMsg( QString( "%1 : %2.%3.%4: %5 %6 " )
440+
.arg( gtableName )
441+
.arg( schemaName ).arg( tableName ).arg( column )
442+
.arg( type )
443+
.arg( srid ) );
444+
445+
layerProperty.type = type;
446+
layerProperty.schemaName = schemaName;
447+
layerProperty.tableName = tableName;
448+
layerProperty.geometryColName = column;
449+
layerProperty.pkCols = QStringList(); // relkind == "v" ? pkCandidates( schemaName, tableName ) : QStringList();
450+
layerProperty.srid = srid;
451+
layerProperty.sql = "";
452+
layerProperty.isGeography = false; // TODO: use an enum for the type!
453+
454+
mLayersSupported << layerProperty;
455+
nColumns++;
456+
}
457+
}
458+
389459
if ( nColumns == 0 )
390460
{
391461
QgsMessageLog::logMessage( tr( "Database connection was successful, but the accessible tables could not be determined." ), tr( "PostGIS" ) );
@@ -425,7 +495,11 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
425495
sql += " AND (pg_namespace.nspname,pg_class.relname,pg_attribute.attname) NOT IN (SELECT f_table_schema,f_table_name,f_geography_column FROM geography_columns)";
426496
}
427497

428-
// TODO: handle this for the topogeometry case (once we lookup topology.layer)
498+
if ( hasTopology() )
499+
{
500+
// TODO: do not lookup tables found in topology.layer (but why?)
501+
sql += " AND (pg_namespace.nspname,pg_class.relname,pg_attribute.attname) NOT IN (SELECT schema_name,table_name,feature_column FROM topology.layer)";
502+
}
429503
}
430504

431505
sql += " AND pg_class.relkind IN ('v','r')"; // only from views and relations (tables)

0 commit comments

Comments
 (0)