@@ -1175,89 +1175,7 @@ bool QgsPostgresProvider::determinePrimaryKey()
1175
1175
}
1176
1176
else if ( type == " v" || type == " m" ) // the relation is a view
1177
1177
{
1178
- QString primaryKey = mUri .keyColumn ();
1179
- mPrimaryKeyType = pktUnknown;
1180
-
1181
- if ( !primaryKey.isEmpty () )
1182
- {
1183
- QStringList cols;
1184
-
1185
- // remove quotes from key list
1186
- if ( primaryKey.startsWith ( ' "' ) && primaryKey.endsWith ( ' "' ) )
1187
- {
1188
- int i = 1 ;
1189
- QString col;
1190
- while ( i < primaryKey.size () )
1191
- {
1192
- if ( primaryKey[i] == ' "' )
1193
- {
1194
- if ( i + 1 < primaryKey.size () && primaryKey[i+1 ] == ' "' )
1195
- {
1196
- i++;
1197
- }
1198
- else
1199
- {
1200
- cols << col;
1201
- col = " " ;
1202
-
1203
- if ( ++i == primaryKey.size () )
1204
- break ;
1205
-
1206
- Q_ASSERT ( primaryKey[i] == ' ,' );
1207
- i++;
1208
- Q_ASSERT ( primaryKey[i] == ' "' );
1209
- i++;
1210
- col = " " ;
1211
- continue ;
1212
- }
1213
- }
1214
-
1215
- col += primaryKey[i++];
1216
- }
1217
- }
1218
- else if ( primaryKey.contains ( " ," ) )
1219
- {
1220
- cols = primaryKey.split ( " ," );
1221
- }
1222
- else
1223
- {
1224
- cols << primaryKey;
1225
- primaryKey = quotedIdentifier ( primaryKey );
1226
- }
1227
-
1228
- Q_FOREACH ( const QString& col, cols )
1229
- {
1230
- int idx = fieldNameIndex ( col );
1231
- if ( idx < 0 )
1232
- {
1233
- QgsMessageLog::logMessage ( tr ( " Key field '%1' for view not found." ).arg ( col ), tr ( " PostGIS" ) );
1234
- mPrimaryKeyAttrs .clear ();
1235
- break ;
1236
- }
1237
-
1238
- mPrimaryKeyAttrs << idx;
1239
- }
1240
-
1241
- if ( mPrimaryKeyAttrs .size () > 0 )
1242
- {
1243
- if ( mUseEstimatedMetadata || uniqueData ( mQuery , primaryKey ) )
1244
- {
1245
- mPrimaryKeyType = ( mPrimaryKeyAttrs .size () == 1 && ( mAttributeFields [ mPrimaryKeyAttrs [0 ] ].type () == QVariant::Int || mAttributeFields [ mPrimaryKeyAttrs [0 ] ].type () == QVariant::LongLong ) ) ? pktInt : pktFidMap;
1246
- }
1247
- else
1248
- {
1249
- QgsMessageLog::logMessage ( tr ( " Primary key field '%1' for view not unique." ).arg ( primaryKey ), tr ( " PostGIS" ) );
1250
- }
1251
- }
1252
- else
1253
- {
1254
- QgsMessageLog::logMessage ( tr ( " Keys for view undefined." ).arg ( primaryKey ), tr ( " PostGIS" ) );
1255
- }
1256
- }
1257
- else
1258
- {
1259
- QgsMessageLog::logMessage ( tr ( " No key field for view given." ), tr ( " PostGIS" ) );
1260
- }
1178
+ determinePrimaryKeyFromUriKeyColumn ();
1261
1179
}
1262
1180
else
1263
1181
{
@@ -1324,6 +1242,93 @@ bool QgsPostgresProvider::determinePrimaryKey()
1324
1242
return mValid ;
1325
1243
}
1326
1244
1245
+ void QgsPostgresProvider::determinePrimaryKeyFromUriKeyColumn ()
1246
+ {
1247
+ QString primaryKey = mUri .keyColumn ();
1248
+ mPrimaryKeyType = pktUnknown;
1249
+
1250
+ if ( !primaryKey.isEmpty () )
1251
+ {
1252
+ QStringList cols;
1253
+
1254
+ // remove quotes from key list
1255
+ if ( primaryKey.startsWith ( ' "' ) && primaryKey.endsWith ( ' "' ) )
1256
+ {
1257
+ int i = 1 ;
1258
+ QString col;
1259
+ while ( i < primaryKey.size () )
1260
+ {
1261
+ if ( primaryKey[i] == ' "' )
1262
+ {
1263
+ if ( i + 1 < primaryKey.size () && primaryKey[i+1 ] == ' "' )
1264
+ {
1265
+ i++;
1266
+ }
1267
+ else
1268
+ {
1269
+ cols << col;
1270
+ col = " " ;
1271
+
1272
+ if ( ++i == primaryKey.size () )
1273
+ break ;
1274
+
1275
+ Q_ASSERT ( primaryKey[i] == ' ,' );
1276
+ i++;
1277
+ Q_ASSERT ( primaryKey[i] == ' "' );
1278
+ i++;
1279
+ col = " " ;
1280
+ continue ;
1281
+ }
1282
+ }
1283
+
1284
+ col += primaryKey[i++];
1285
+ }
1286
+ }
1287
+ else if ( primaryKey.contains ( " ," ) )
1288
+ {
1289
+ cols = primaryKey.split ( " ," );
1290
+ }
1291
+ else
1292
+ {
1293
+ cols << primaryKey;
1294
+ primaryKey = quotedIdentifier ( primaryKey );
1295
+ }
1296
+
1297
+ Q_FOREACH ( const QString& col, cols )
1298
+ {
1299
+ int idx = fieldNameIndex ( col );
1300
+ if ( idx < 0 )
1301
+ {
1302
+ QgsMessageLog::logMessage ( tr ( " Key field '%1' for view not found." ).arg ( col ), tr ( " PostGIS" ) );
1303
+ mPrimaryKeyAttrs .clear ();
1304
+ break ;
1305
+ }
1306
+
1307
+ mPrimaryKeyAttrs << idx;
1308
+ }
1309
+
1310
+ if ( mPrimaryKeyAttrs .size () > 0 )
1311
+ {
1312
+ if ( mUseEstimatedMetadata || uniqueData ( mQuery , primaryKey ) )
1313
+ {
1314
+ mPrimaryKeyType = ( mPrimaryKeyAttrs .size () == 1 && ( mAttributeFields [ mPrimaryKeyAttrs [0 ] ].type () == QVariant::Int || mAttributeFields [ mPrimaryKeyAttrs [0 ] ].type () == QVariant::LongLong ) ) ? pktInt : pktFidMap;
1315
+ }
1316
+ else
1317
+ {
1318
+ QgsMessageLog::logMessage ( tr ( " Primary key field '%1' for view not unique." ).arg ( primaryKey ), tr ( " PostGIS" ) );
1319
+ }
1320
+ }
1321
+ else
1322
+ {
1323
+ QgsMessageLog::logMessage ( tr ( " Keys for view undefined." ).arg ( primaryKey ), tr ( " PostGIS" ) );
1324
+ }
1325
+ }
1326
+ else
1327
+ {
1328
+ QgsMessageLog::logMessage ( tr ( " No key field for view given." ), tr ( " PostGIS" ) );
1329
+ }
1330
+ }
1331
+
1327
1332
bool QgsPostgresProvider::uniqueData ( QString query, QString quotedColName )
1328
1333
{
1329
1334
Q_UNUSED ( query );
0 commit comments