@@ -74,6 +74,7 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
74
74
mTableName = mUri .table ();
75
75
geometryColumn = mUri .geometryColumn ();
76
76
sqlWhereClause = mUri .sql ();
77
+ primaryKey = mUri .keyColumn ();
77
78
78
79
// Keep a schema qualified table name for convenience later on.
79
80
mSchemaTableName = mUri .quotedTablename ();
@@ -483,15 +484,15 @@ bool QgsPostgresProvider::getFeature( PGresult *queryResult, int row, bool fetch
483
484
484
485
if ( block > 0xffff )
485
486
{
486
- qWarning ( " block number %x exceed 16 bit" , block );
487
+ QgsDebugMsg ( QString ( " block number %1 exceeds 16 bit" ). arg ( block ) );
487
488
return false ;
488
489
}
489
490
490
491
oid = ( block << 16 ) + offset;
491
492
}
492
493
else
493
494
{
494
- qWarning ( " expecting 6 bytes for tid (found %d bytes)" , PQgetlength ( queryResult, row, 0 ) );
495
+ QgsDebugMsg ( QString ( " expecting 6 bytes for tid (found %1 bytes)" ). arg ( PQgetlength ( queryResult, row, 0 ) ) );
495
496
return false ;
496
497
}
497
498
@@ -623,7 +624,7 @@ bool QgsPostgresProvider::nextFeature( QgsFeature& feature )
623
624
QString fetch = QString ( " fetch forward %1 from %2" ).arg ( mFeatureQueueSize ).arg ( cursorName );
624
625
if ( connectionRO->PQsendQuery ( fetch ) == 0 ) // fetch features asynchronously
625
626
{
626
- qWarning ( " PQsendQuery failed (1)" );
627
+ QgsDebugMsg ( " PQsendQuery failed (1)" );
627
628
}
628
629
629
630
Result queryResult;
@@ -948,12 +949,12 @@ QString QgsPostgresProvider::getPrimaryKey()
948
949
Result tableType = connectionRO->PQexec ( sql );
949
950
QString type = QString::fromUtf8 ( PQgetvalue ( tableType, 0 , 0 ) );
950
951
951
- primaryKey = " " ;
952
-
953
952
if ( type == " r" ) // the relation is a table
954
953
{
955
954
QgsDebugMsg ( " Relation is a table. Checking to see if it has an oid column." );
956
955
956
+ primaryKey = " " ;
957
+
957
958
// If there is an oid on the table, use that instead,
958
959
// otherwise give up
959
960
sql = QString ( " SELECT attname FROM pg_attribute WHERE attname='oid' AND attrelid=regclass(%1)" )
@@ -1008,15 +1009,40 @@ QString QgsPostgresProvider::getPrimaryKey()
1008
1009
}
1009
1010
else if ( type == " v" ) // the relation is a view
1010
1011
{
1011
- // Have a poke around the view to see if any of the columns
1012
- // could be used as the primary key.
1013
- tableCols cols;
1014
- // Given a schema.view, populate the cols variable with the
1015
- // schema.table.column's that underly the view columns.
1016
- findColumns ( cols );
1017
- // From the view columns, choose one for which the underlying
1018
- // column is suitable for use as a key into the view.
1019
- primaryKey = chooseViewColumn ( cols );
1012
+ if ( !primaryKey.isEmpty () )
1013
+ {
1014
+ // check last used candidate
1015
+ sql = QString ( " select pg_type.typname from pg_attribute,pg_type where atttypid=pg_type.oid and attname=%1 and attrelid=regclass(%2)" )
1016
+ .arg ( quotedValue ( primaryKey ) ).arg ( quotedValue ( mSchemaTableName ) );
1017
+
1018
+ QgsDebugMsg ( " checking candidate: " + sql );
1019
+
1020
+ Result result = connectionRO->PQexec ( sql );
1021
+
1022
+ if ( PQresultStatus ( result ) != PGRES_TUPLES_OK ||
1023
+ PQntuples ( result ) != 1 ||
1024
+ QString ( PQgetvalue ( result, 0 , 0 ) ) != " int4" ||
1025
+ !uniqueData ( mSchemaName , mTableName , primaryKey ) )
1026
+ {
1027
+ primaryKey = " " ;
1028
+ }
1029
+ }
1030
+
1031
+ if ( primaryKey.isEmpty () )
1032
+ {
1033
+ // Have a poke around the view to see if any of the columns
1034
+ // could be used as the primary key.
1035
+ tableCols cols;
1036
+ // Given a schema.view, populate the cols variable with the
1037
+ // schema.table.column's that underly the view columns.
1038
+ findColumns ( cols );
1039
+ // From the view columns, choose one for which the underlying
1040
+ // column is suitable for use as a key into the view.
1041
+ primaryKey = chooseViewColumn ( cols );
1042
+
1043
+ mUri .setKeyColumn ( primaryKey );
1044
+ setDataSourceUri ( mUri .uri () );
1045
+ }
1020
1046
}
1021
1047
else
1022
1048
QgsDebugMsg ( " Unexpected relation type of '" + type + " '." );
0 commit comments