@@ -606,66 +606,105 @@ QStringList QgsPgSourceSelect::pkCandidates( PGconn *pg, QString schemaName, QSt
606
606
607
607
bool QgsPgSourceSelect::getTableInfo ( PGconn *pg, bool searchGeometryColumnsOnly, bool searchPublicOnly )
608
608
{
609
- int n = 0 ;
609
+ int nColumns = 0 ;
610
+ int nGTables = 0 ;
610
611
QApplication::setOverrideCursor ( Qt::WaitCursor );
611
612
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++ )
635
616
{
636
- if ( PQresultStatus ( result ) != PGRES_TUPLES_OK )
617
+ QString gtableName, columnName;
618
+
619
+ if ( i == 0 )
637
620
{
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" ;
644
623
}
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 )
646
655
{
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
648
663
{
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 )
657
667
{
658
- addSearchGeometryColumn ( schemaName, tableName, column );
659
- as = type = " WAITING" ;
660
- }
661
668
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
+ }
664
694
}
665
695
}
696
+
697
+ PQclear ( result );
698
+ result = 0 ;
666
699
}
667
700
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
+ }
669
708
670
709
// search for geometry columns in tables that are not in the geometry_columns metatable
671
710
QApplication::restoreOverrideCursor ();
@@ -676,38 +715,46 @@ bool QgsPgSourceSelect::getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly
676
715
// geometry_columns table. This code is specific to postgresql,
677
716
// but an equivalent query should be possible in other
678
717
// 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
+
697
737
// user has select privilege
698
738
if ( searchPublicOnly )
699
739
sql += " and pg_namespace.nspname = 'public'" ;
700
740
701
- if ( n > 0 )
741
+ if ( nColumns > 0 )
702
742
{
703
743
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
+ }
704
749
}
705
750
else
706
751
{
707
- n = 0 ;
752
+ nColumns = 0 ;
708
753
}
709
754
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 );
711
758
712
759
result = PQexec ( pg, sql.toUtf8 () );
713
760
@@ -718,8 +765,8 @@ bool QgsPgSourceSelect::getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly
718
765
tr ( " Database connection was successful, but the accessible tables could not be determined.\n\n "
719
766
" The error message from the database was:\n %1\n " )
720
767
.arg ( QString::fromUtf8 ( PQresultErrorMessage ( result ) ) ) );
721
- if ( n == 0 )
722
- n = -1 ;
768
+ if ( nColumns == 0 )
769
+ nColumns = -1 ;
723
770
}
724
771
else if ( PQntuples ( result ) > 0 )
725
772
{
@@ -737,17 +784,20 @@ bool QgsPgSourceSelect::getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly
737
784
QString column = QString::fromUtf8 ( PQgetvalue ( result, i, 2 ) ); // attname
738
785
QString relkind = QString::fromUtf8 ( PQgetvalue ( result, i, 3 ) ); // relation kind
739
786
787
+ QgsDebugMsg ( QString ( " %1.%2.%3: %4" ).arg ( schema ).arg ( table ).arg ( column ).arg ( relkind ) );
788
+
740
789
addSearchGeometryColumn ( schema, table, column );
741
790
// 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 ++;
744
793
}
745
794
}
746
795
747
796
PQclear ( result );
797
+ result = 0 ;
748
798
}
749
799
750
- if ( n == 0 )
800
+ if ( nColumns == 0 )
751
801
{
752
802
QMessageBox::warning ( this ,
753
803
tr ( " No accessible tables found" ),
@@ -756,7 +806,7 @@ bool QgsPgSourceSelect::getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly
756
806
" geometry." ) );
757
807
}
758
808
759
- return n > 0 ;
809
+ return nColumns > 0 ;
760
810
}
761
811
762
812
QString QgsPgSourceSelect::fullDescription ( QString schema, QString table,
@@ -852,7 +902,9 @@ void QgsGeomColumnTypeThread::getLayerTypes()
852
902
query += " \" " + schemas[i] + " \" .\" " + tables[i] + " \" " ;
853
903
}
854
904
855
- PGresult* gresult = PQexec ( pd, query.toUtf8 () );
905
+ QgsDebugMsg ( " sql: " + query );
906
+
907
+ PGresult *gresult = PQexec ( pd, query.toUtf8 () );
856
908
QString type;
857
909
if ( PQresultStatus ( gresult ) == PGRES_TUPLES_OK )
858
910
{
0 commit comments