Skip to content

Commit 744d8a4

Browse files
author
jef
committed
apply #2730
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13922 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 1b1fa94 commit 744d8a4

File tree

3 files changed

+207
-93
lines changed

3 files changed

+207
-93
lines changed

src/app/postgres/qgspgsourceselect.cpp

Lines changed: 127 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -606,66 +606,105 @@ QStringList QgsPgSourceSelect::pkCandidates( PGconn *pg, QString schemaName, QSt
606606

607607
bool QgsPgSourceSelect::getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly, bool searchPublicOnly )
608608
{
609-
int n = 0;
609+
int nColumns = 0;
610+
int nGTables = 0;
610611
QApplication::setOverrideCursor( Qt::WaitCursor );
611612

612-
// The following query returns only tables that exist and the user has SELECT privilege on.
613-
// Can't use regclass here because table must exist, else error occurs.
614-
QString sql = "select "
615-
"f_table_name,"
616-
"f_table_schema,"
617-
"f_geometry_column,"
618-
"type,"
619-
"pg_class.relkind"
620-
" from "
621-
"geometry_columns,"
622-
"pg_class,"
623-
"pg_namespace"
624-
" where "
625-
"relname=f_table_name"
626-
" and f_table_schema=nspname"
627-
" and pg_namespace.oid=pg_class.relnamespace"
628-
" and has_schema_privilege(pg_namespace.nspname,'usage')"
629-
" and has_table_privilege('\"'||pg_namespace.nspname||'\".\"'||pg_class.relname||'\"','select')" // user has select privilege
630-
" order by "
631-
"f_table_schema,f_table_name,f_geometry_column";
632-
633-
PGresult *result = PQexec( pg, sql.toUtf8() );
634-
if ( result )
613+
PGresult *result = 0;
614+
615+
for ( int i = 0; i < 2; i++ )
635616
{
636-
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
617+
QString gtableName, columnName;
618+
619+
if ( i == 0 )
637620
{
638-
QMessageBox::warning( this,
639-
tr( "Accessible tables could not be determined" ),
640-
tr( "Database connection was successful, but the accessible tables could not be determined.\n\n"
641-
"The error message from the database was:\n%1\n" )
642-
.arg( QString::fromUtf8( PQresultErrorMessage( result ) ) ) );
643-
n = -1;
621+
gtableName = "geometry_columns";
622+
columnName = "f_geometry_column";
644623
}
645-
else if ( PQntuples( result ) > 0 )
624+
else if ( i == 1 )
625+
{
626+
gtableName = "geography_columns";
627+
columnName = "f_geography_column";
628+
}
629+
630+
// The following query returns only tables that exist and the user has SELECT privilege on.
631+
// Can't use regclass here because table must exist, else error occurs.
632+
QString sql = QString( "select "
633+
"f_table_name,"
634+
"f_table_schema,"
635+
"%2,"
636+
"upper(type),"
637+
"pg_class.relkind"
638+
" from "
639+
"%1,"
640+
"pg_class,"
641+
"pg_namespace"
642+
" where "
643+
"relname=f_table_name"
644+
" and f_table_schema=nspname"
645+
" and pg_namespace.oid=pg_class.relnamespace"
646+
" and has_schema_privilege(pg_namespace.nspname,'usage')"
647+
" and has_table_privilege('\"'||pg_namespace.nspname||'\".\"'||pg_class.relname||'\"','select')" // user has select privilege
648+
" order by "
649+
"f_table_schema,f_table_name,%2" ).arg( gtableName ).arg( columnName );
650+
651+
QgsDebugMsg( "sql: " + sql );
652+
653+
result = PQexec( pg, sql.toUtf8() );
654+
if ( result )
646655
{
647-
for ( int idx = 0; idx < PQntuples( result ); idx++ )
656+
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
657+
{
658+
PGresult *r = PQexec( pg, "COMMIT" );
659+
if ( r )
660+
PQclear( r );
661+
}
662+
else
648663
{
649-
QString tableName = QString::fromUtf8( PQgetvalue( result, idx, 0 ) );
650-
QString schemaName = QString::fromUtf8( PQgetvalue( result, idx, 1 ) );
651-
QString column = QString::fromUtf8( PQgetvalue( result, idx, 2 ) );
652-
QString type = QString::fromUtf8( PQgetvalue( result, idx, 3 ) );
653-
QString relkind = QString::fromUtf8( PQgetvalue( result, idx, 4 ) );
654-
655-
QString as = "";
656-
if ( type == "GEOMETRY" && !searchGeometryColumnsOnly )
664+
nGTables++;
665+
666+
if ( PQntuples( result ) > 0 )
657667
{
658-
addSearchGeometryColumn( schemaName, tableName, column );
659-
as = type = "WAITING";
660-
}
661668

662-
mTableModel.addTableEntry( type, schemaName, tableName, column, relkind == "v" ? pkCandidates( pg, schemaName, tableName ) : QStringList(), "" );
663-
n++;
669+
for ( int idx = 0; idx < PQntuples( result ); idx++ )
670+
{
671+
QString tableName = QString::fromUtf8( PQgetvalue( result, idx, 0 ) );
672+
QString schemaName = QString::fromUtf8( PQgetvalue( result, idx, 1 ) );
673+
QString column = QString::fromUtf8( PQgetvalue( result, idx, 2 ) );
674+
QString type = QString::fromUtf8( PQgetvalue( result, idx, 3 ) );
675+
QString relkind = QString::fromUtf8( PQgetvalue( result, idx, 4 ) );
676+
677+
QgsDebugMsg( QString( "%1 %2.%3.%4: %5 %6" )
678+
.arg( gtableName )
679+
.arg( schemaName ).arg( tableName ).arg( column )
680+
.arg( type )
681+
.arg( relkind ) );
682+
683+
QString as = "";
684+
if ( type == "GEOMETRY" && !searchGeometryColumnsOnly )
685+
{
686+
addSearchGeometryColumn( schemaName, tableName, column );
687+
as = type = "WAITING";
688+
}
689+
690+
mTableModel.addTableEntry( type, schemaName, tableName, column, relkind == "v" ? pkCandidates( pg, schemaName, tableName ) : QStringList(), "" );
691+
nColumns++;
692+
}
693+
}
664694
}
665695
}
696+
697+
PQclear( result );
698+
result = 0;
666699
}
667700

668-
PQclear( result );
701+
if ( nColumns == 0 )
702+
{
703+
QMessageBox::warning( this,
704+
tr( "Accessible tables could not be determined" ),
705+
tr( "Database connection was successful, but the accessible tables could not be determined." ) );
706+
nColumns = -1;
707+
}
669708

670709
//search for geometry columns in tables that are not in the geometry_columns metatable
671710
QApplication::restoreOverrideCursor();
@@ -676,38 +715,46 @@ bool QgsPgSourceSelect::getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly
676715
// geometry_columns table. This code is specific to postgresql,
677716
// but an equivalent query should be possible in other
678717
// databases.
679-
sql = "select "
680-
"pg_class.relname,"
681-
"pg_namespace.nspname,"
682-
"pg_attribute.attname,"
683-
"pg_class.relkind"
684-
" from "
685-
"pg_attribute,"
686-
"pg_class,"
687-
"pg_namespace"
688-
" where "
689-
"pg_namespace.oid = pg_class.relnamespace"
690-
" and pg_attribute.attrelid = pg_class.oid "
691-
" and ("
692-
"pg_attribute.atttypid = regtype('geometry')"
693-
" or pg_attribute.atttypid IN (select oid FROM pg_type WHERE typbasetype=regtype('geometry'))"
694-
")"
695-
" and has_schema_privilege(pg_namespace.nspname,'usage')"
696-
" and has_table_privilege('\"'||pg_namespace.nspname||'\".\"'||pg_class.relname||'\"','select')";
718+
QString sql = "select "
719+
"pg_class.relname"
720+
",pg_namespace.nspname"
721+
",pg_attribute.attname"
722+
",pg_class.relkind"
723+
" from "
724+
"pg_attribute"
725+
",pg_class"
726+
",pg_namespace"
727+
" where "
728+
"pg_namespace.oid=pg_class.relnamespace"
729+
" and pg_attribute.attrelid = pg_class.oid"
730+
" and ("
731+
"pg_attribute.atttypid::regtype::text IN ('geometry','geography')"
732+
" or pg_attribute.atttypid IN (select oid FROM pg_type WHERE typbasetype::regtype::text IN ('geometry','geography'))"
733+
")"
734+
" and has_schema_privilege( pg_namespace.nspname, 'usage' )"
735+
" and has_table_privilege( '\"' || pg_namespace.nspname || '\".\"' || pg_class.relname || '\"', 'select' )";
736+
697737
// user has select privilege
698738
if ( searchPublicOnly )
699739
sql += " and pg_namespace.nspname = 'public'";
700740

701-
if ( n > 0 )
741+
if ( nColumns > 0 )
702742
{
703743
sql += " and not exists (select * from geometry_columns WHERE pg_namespace.nspname=f_table_schema AND pg_class.relname=f_table_name)";
744+
745+
if ( nGTables > 1 )
746+
{
747+
sql += " and not exists (select * from geography_columns WHERE pg_namespace.nspname=f_table_schema AND pg_class.relname=f_table_name)";
748+
}
704749
}
705750
else
706751
{
707-
n = 0;
752+
nColumns = 0;
708753
}
709754

710-
sql += " and pg_class.relkind in ('v', 'r')"; // only from views and relations (tables)
755+
sql += " and pg_class.relkind in( 'v', 'r' )"; // only from views and relations (tables)
756+
757+
QgsDebugMsg( "sql: " + sql );
711758

712759
result = PQexec( pg, sql.toUtf8() );
713760

@@ -718,8 +765,8 @@ bool QgsPgSourceSelect::getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly
718765
tr( "Database connection was successful, but the accessible tables could not be determined.\n\n"
719766
"The error message from the database was:\n%1\n" )
720767
.arg( QString::fromUtf8( PQresultErrorMessage( result ) ) ) );
721-
if ( n == 0 )
722-
n = -1;
768+
if ( nColumns == 0 )
769+
nColumns = -1;
723770
}
724771
else if ( PQntuples( result ) > 0 )
725772
{
@@ -737,17 +784,20 @@ bool QgsPgSourceSelect::getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly
737784
QString column = QString::fromUtf8( PQgetvalue( result, i, 2 ) ); // attname
738785
QString relkind = QString::fromUtf8( PQgetvalue( result, i, 3 ) ); // relation kind
739786

787+
QgsDebugMsg( QString( "%1.%2.%3: %4" ).arg( schema ).arg( table ).arg( column ).arg( relkind ) );
788+
740789
addSearchGeometryColumn( schema, table, column );
741790
//details.push_back(geomPair(fullDescription(schema, table, column, "WAITING"), "WAITING"));
742-
mTableModel.addTableEntry( "Waiting", schema, table, column, relkind == "v" ? pkCandidates( pg, schema, table ) : QStringList(), "" );
743-
n++;
791+
mTableModel.addTableEntry( tr( "Waiting" ), schema, table, column, relkind == "v" ? pkCandidates( pg, schema, table ) : QStringList(), "" );
792+
nColumns++;
744793
}
745794
}
746795

747796
PQclear( result );
797+
result = 0;
748798
}
749799

750-
if ( n == 0 )
800+
if ( nColumns == 0 )
751801
{
752802
QMessageBox::warning( this,
753803
tr( "No accessible tables found" ),
@@ -756,7 +806,7 @@ bool QgsPgSourceSelect::getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly
756806
"geometry." ) );
757807
}
758808

759-
return n > 0;
809+
return nColumns > 0;
760810
}
761811

762812
QString QgsPgSourceSelect::fullDescription( QString schema, QString table,
@@ -852,7 +902,9 @@ void QgsGeomColumnTypeThread::getLayerTypes()
852902
query += "\"" + schemas[i] + "\".\"" + tables[i] + "\"";
853903
}
854904

855-
PGresult* gresult = PQexec( pd, query.toUtf8() );
905+
QgsDebugMsg( "sql: " + query );
906+
907+
PGresult *gresult = PQexec( pd, query.toUtf8() );
856908
QString type;
857909
if ( PQresultStatus( gresult ) == PGRES_TUPLES_OK )
858910
{

0 commit comments

Comments
 (0)