@@ -1167,70 +1167,69 @@ QString QgsPostgresProvider::chooseViewColumn(const tableCols& cols)
1167
1167
// indexed, else pick one called 'oid' if it exists, else
1168
1168
// pick the first one. If there are none we return an empty string.
1169
1169
1170
- if (suitable.size () == 1 )
1170
+ // Search for one with an index
1171
+ tableCols::const_iterator i = suitable.begin ();
1172
+ for (; i != suitable.end (); ++i)
1171
1173
{
1172
- if (uniqueData (mSchemaName , mTableName , suitable.begin ()->first ))
1173
- {
1174
- key = suitable.begin ()->first ;
1174
+ // Get the relation oid from our cache.
1175
+ QString rel_oid = relOid[i->first ];
1176
+ // And see if the column has an index
1177
+ sql = " select * from pg_index where indrelid = " + rel_oid +
1178
+ " and indkey[0] = (select attnum from pg_attribute where "
1179
+ " attrelid = " + rel_oid + " and attname = '" + i->second .column + " ')" ;
1180
+ PGresult* result = PQexec (connection, (const char *)(sql.utf8 ()));
1181
+
1182
+ if (PQntuples (result) > 0 && uniqueData (mSchemaName , mTableName , i->first ))
1183
+ { // Got one. Use it.
1184
+ key = i->first ;
1175
1185
#ifdef QGISDEBUG
1176
- std::cerr << " Picked column " << key.toLocal8Bit ().data ()
1177
- << " as it is the only one that was suitable .\n " ;
1186
+ std::cerr << " Picked column ' " << key.toLocal8Bit ().data ()
1187
+ << " ' because it has an index .\n " ;
1178
1188
#endif
1189
+ break ;
1179
1190
}
1191
+ PQclear (result);
1180
1192
}
1181
- else if (suitable.size () > 1 )
1182
- {
1183
- // Search for one with an index
1184
- tableCols::const_iterator i = suitable.begin ();
1185
- for (; i != suitable.end (); ++i)
1186
- {
1187
- // Get the relation oid from our cache.
1188
- QString rel_oid = relOid[i->first ];
1189
- // And see if the column has an index
1190
- sql = " select * from pg_index where indrelid = " + rel_oid +
1191
- " and indkey[0] = (select attnum from pg_attribute where "
1192
- " attrelid = " + rel_oid + " and attname = '" + i->second .column + " ')" ;
1193
- PGresult* result = PQexec (connection, (const char *)(sql.utf8 ()));
1194
-
1195
- if (PQntuples (result) > 0 && uniqueData (mSchemaName , mTableName , i->first ))
1196
- { // Got one. Use it.
1197
- key = i->first ;
1198
- #ifdef QGISDEBUG
1199
- std::cerr << " Picked column '" << key.toLocal8Bit ().data ()
1200
- << " ' because it has an index.\n " ;
1201
- #endif
1202
- break ;
1203
- }
1204
- PQclear (result);
1205
- }
1206
1193
1207
- if (key.isEmpty ())
1194
+ if (key.isEmpty ())
1195
+ {
1196
+ // If none have indices, choose one that is called 'oid' (if it
1197
+ // exists). This is legacy support and could be removed in
1198
+ // future.
1199
+ i = suitable.find (" oid" );
1200
+ if (i != suitable.end () && uniqueData (mSchemaName , mTableName , i->first ))
1208
1201
{
1209
- // If none have indices, choose one that is called 'oid' (if it
1210
- // exists). This is legacy support and could be removed in
1211
- // future.
1212
- i = suitable.find (" oid" );
1213
- if (i != suitable.end () && uniqueData (mSchemaName , mTableName , i->first ))
1214
- {
1215
- key = i->first ;
1202
+ key = i->first ;
1216
1203
#ifdef QGISDEBUG
1217
- std::cerr << " Picked column " << key.toLocal8Bit ().data ()
1218
- << " as it is probably the postgresql object id "
1219
- << " column (which contains unique values) and there are no"
1220
- << " columns with indices to choose from\n ." ;
1204
+ std::cerr << " Picked column " << key.toLocal8Bit ().data ()
1205
+ << " as it is probably the postgresql object id "
1206
+ << " column (which contains unique values) and there are no"
1207
+ << " columns with indices to choose from\n ." ;
1221
1208
#endif
1222
- }
1223
- // else choose the first one in the container, ensuring that it
1224
- // contains unique data
1225
- else if (uniqueData (mSchemaName , mTableName , suitable.begin ()->first ))
1209
+ }
1210
+ // else choose the first one in the container that has unique data
1211
+ else
1212
+ {
1213
+ tableCols::const_iterator i = suitable.begin ();
1214
+ for (; i != suitable.end (); ++i)
1226
1215
{
1227
- key = suitable.begin ()->first ;
1216
+ if (uniqueData (mSchemaName , mTableName , i->first ))
1217
+ {
1218
+ key = i->first ;
1228
1219
#ifdef QGISDEBUG
1229
- std::cerr << " Picked column " << key.toLocal8Bit ().data ()
1230
- << " as it was the first suitable column found"
1231
- << " and there are no"
1232
- << " columns with indices to choose from\n ." ;
1220
+ std::cerr << " Picked column " << key.toLocal8Bit ().data ()
1221
+ << " as it was the first suitable column found"
1222
+ << " with unique data and were are no"
1223
+ << " columns with indices to choose from\n ." ;
1233
1224
#endif
1225
+ break ;
1226
+ }
1227
+ else
1228
+ {
1229
+ log << QString (tr (" Note: " ) + " '" + key + " '"
1230
+ + tr (" initially appeared suitable but does not "
1231
+ " contain unique data, so is not suitable.\n " ));
1232
+ }
1234
1233
}
1235
1234
}
1236
1235
}
@@ -1248,7 +1247,7 @@ QString QgsPostgresProvider::chooseViewColumn(const tableCols& cols)
1248
1247
" have a unique constraint on it, or be a PostgreSQL "
1249
1248
" oid column. To improve "
1250
1249
" performance the column should also be indexed.\n " ));
1251
- log .prepend (tr (" The view " ) + " '" + mSchemaName + mTableName + " '" +
1250
+ log .prepend (tr (" The view " ) + " '" + mSchemaName + ' . ' + mTableName + " ' " +
1252
1251
tr (" has no column suitable for use as a unique key.\n " ));
1253
1252
showMessageBox (tr (" No suitable key column in view" ), log );
1254
1253
}
0 commit comments