Skip to content

Commit 3420e1d

Browse files
committed
skip views without pk candidate
1 parent 0e889ea commit 3420e1d

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/providers/postgres/qgspgsourceselect.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,11 @@ void QgsPgSourceSelect::finishList()
519519
mTablesTreeView->sortByColumn( QgsPgTableModel::dbtmTable, Qt::AscendingOrder );
520520
mTablesTreeView->sortByColumn( QgsPgTableModel::dbtmSchema, Qt::AscendingOrder );
521521

522+
if ( mTablesTreeView->model()->rowCount() == 0 )
523+
QMessageBox::information( this,
524+
tr( "Postgres/PostGIS Provider" ),
525+
tr( "No accessible tables or views found. Check the message log for possible errors." ) );
526+
522527
}
523528

524529
void QgsPgSourceSelect::columnThreadFinished()

src/providers/postgres/qgspostgresconn.cpp

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,15 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
345345

346346
// The following query returns only tables that exist and the user has SELECT privilege on.
347347
// Can't use regclass here because table must exist, else error occurs.
348-
sql = QString( "SELECT %1,%2,%3,%4,c.relkind"
349-
" FROM %5 l,pg_class c,pg_namespace n"
348+
sql = QString( "SELECT %1,%2,%3,%4,%5,c.relkind"
349+
" FROM %6 l,pg_class c,pg_namespace n"
350350
" WHERE c.relname=%1"
351351
" AND %2=n.nspname"
352352
" AND n.oid=c.relnamespace"
353353
" AND has_schema_privilege(n.nspname,'usage')"
354354
" AND has_table_privilege('\"'||n.nspname||'\".\"'||c.relname||'\"','select')" // user has select privilege
355355
)
356-
.arg( tableName ).arg( schemaName ).arg( columnName ).arg( typeName ).arg( gtableName );
356+
.arg( tableName ).arg( schemaName ).arg( columnName ).arg( typeName ).arg( sridName ).arg( gtableName );
357357

358358
if ( searchPublicOnly )
359359
sql += " AND n.nspname='public'";
@@ -388,7 +388,16 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
388388
layerProperty.schemaName = schemaName;
389389
layerProperty.tableName = tableName;
390390
layerProperty.geometryColName = column;
391-
layerProperty.pkCols = relkind == "v" ? pkCandidates( schemaName, tableName ) : QStringList();
391+
392+
if ( relkind == "v" )
393+
{
394+
layerProperty.pkCols = pkCandidates( schemaName, tableName );
395+
if ( layerProperty.pkCols.isEmpty() )
396+
{
397+
QgsDebugMsg( "no key columns found." );
398+
continue;
399+
}
400+
}
392401
layerProperty.srid = srid;
393402
layerProperty.sql = "";
394403
layerProperty.isGeography = i == 1;
@@ -472,18 +481,26 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
472481
// Make the assumption that the geometry type for the first
473482
// row is the same as for all other rows.
474483

475-
QString table = result.PQgetvalue( i, 0 ); // relname
476-
QString schema = result.PQgetvalue( i, 1 ); // nspname
477-
QString column = result.PQgetvalue( i, 2 ); // attname
478-
QString relkind = result.PQgetvalue( i, 3 ); // relation kind
484+
QString tableName = result.PQgetvalue( i, 0 ); // relname
485+
QString schemaName = result.PQgetvalue( i, 1 ); // nspname
486+
QString column = result.PQgetvalue( i, 2 ); // attname
487+
QString relkind = result.PQgetvalue( i, 3 ); // relation kind
479488

480-
QgsDebugMsg( QString( "%1.%2.%3: %4" ).arg( schema ).arg( table ).arg( column ).arg( relkind ) );
489+
QgsDebugMsg( QString( "%1.%2.%3: %4" ).arg( schemaName ).arg( tableName ).arg( column ).arg( relkind ) );
481490

482491
layerProperty.type = QString::null;
483-
layerProperty.schemaName = schema;
484-
layerProperty.tableName = table;
492+
layerProperty.schemaName = schemaName;
493+
layerProperty.tableName = tableName;
485494
layerProperty.geometryColName = column;
486-
layerProperty.pkCols = relkind == "v" ? pkCandidates( schema, table ) : QStringList();
495+
if ( relkind == "v" )
496+
{
497+
layerProperty.pkCols = pkCandidates( schemaName, tableName );
498+
if ( layerProperty.pkCols.isEmpty() )
499+
{
500+
QgsDebugMsg( "no key columns found." );
501+
continue;
502+
}
503+
}
487504
layerProperty.sql = "";
488505
layerProperty.isGeography = false; // TODO might be geography after all
489506

0 commit comments

Comments
 (0)