Skip to content

Commit

Permalink
skip views without pk candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Oct 19, 2012
1 parent 0e889ea commit 3420e1d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/providers/postgres/qgspgsourceselect.cpp
Expand Up @@ -519,6 +519,11 @@ void QgsPgSourceSelect::finishList()
mTablesTreeView->sortByColumn( QgsPgTableModel::dbtmTable, Qt::AscendingOrder );
mTablesTreeView->sortByColumn( QgsPgTableModel::dbtmSchema, Qt::AscendingOrder );

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

}

void QgsPgSourceSelect::columnThreadFinished()
Expand Down
41 changes: 29 additions & 12 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -345,15 +345,15 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP

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

if ( searchPublicOnly )
sql += " AND n.nspname='public'";
Expand Down Expand Up @@ -388,7 +388,16 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
layerProperty.schemaName = schemaName;
layerProperty.tableName = tableName;
layerProperty.geometryColName = column;
layerProperty.pkCols = relkind == "v" ? pkCandidates( schemaName, tableName ) : QStringList();

if ( relkind == "v" )
{
layerProperty.pkCols = pkCandidates( schemaName, tableName );
if ( layerProperty.pkCols.isEmpty() )
{
QgsDebugMsg( "no key columns found." );
continue;
}
}
layerProperty.srid = srid;
layerProperty.sql = "";
layerProperty.isGeography = i == 1;
Expand Down Expand Up @@ -472,18 +481,26 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
// Make the assumption that the geometry type for the first
// row is the same as for all other rows.

QString table = result.PQgetvalue( i, 0 ); // relname
QString schema = result.PQgetvalue( i, 1 ); // nspname
QString column = result.PQgetvalue( i, 2 ); // attname
QString relkind = result.PQgetvalue( i, 3 ); // relation kind
QString tableName = result.PQgetvalue( i, 0 ); // relname
QString schemaName = result.PQgetvalue( i, 1 ); // nspname
QString column = result.PQgetvalue( i, 2 ); // attname
QString relkind = result.PQgetvalue( i, 3 ); // relation kind

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

layerProperty.type = QString::null;
layerProperty.schemaName = schema;
layerProperty.tableName = table;
layerProperty.schemaName = schemaName;
layerProperty.tableName = tableName;
layerProperty.geometryColName = column;
layerProperty.pkCols = relkind == "v" ? pkCandidates( schema, table ) : QStringList();
if ( relkind == "v" )
{
layerProperty.pkCols = pkCandidates( schemaName, tableName );
if ( layerProperty.pkCols.isEmpty() )
{
QgsDebugMsg( "no key columns found." );
continue;
}
}
layerProperty.sql = "";
layerProperty.isGeography = false; // TODO might be geography after all

Expand Down

0 comments on commit 3420e1d

Please sign in to comment.