@@ -55,7 +55,8 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
55
55
: QgsVectorDataProvider( uri ),
56
56
mFetching( false ),
57
57
geomType( QGis::WKBUnknown ),
58
- mFeatureQueueSize( 200 )
58
+ mFeatureQueueSize( 200 ),
59
+ mPrimaryKeyDefault( QString::null )
59
60
{
60
61
// assume this is a valid layer until we determine otherwise
61
62
valid = true ;
@@ -1064,13 +1065,13 @@ QString QgsPostgresProvider::getPrimaryKey()
1064
1065
}
1065
1066
else
1066
1067
{
1067
- primaryKeyDefault = defaultValue ( primaryKey ).toString ();
1068
- if ( primaryKeyDefault .isNull () )
1068
+ mPrimaryKeyDefault = defaultValue ( primaryKey ).toString ();
1069
+ if ( mPrimaryKeyDefault .isNull () )
1069
1070
{
1070
- primaryKeyDefault = QString ( " max(%1)+1 from %2.%3" )
1071
- .arg ( quotedIdentifier ( primaryKey ) )
1072
- .arg ( quotedIdentifier ( mSchemaName ) )
1073
- .arg ( quotedIdentifier ( mTableName ) );
1071
+ mPrimaryKeyDefault = QString ( " max(%1)+1 from %2.%3" )
1072
+ .arg ( quotedIdentifier ( primaryKey ) )
1073
+ .arg ( quotedIdentifier ( mSchemaName ) )
1074
+ .arg ( quotedIdentifier ( mTableName ) );
1074
1075
}
1075
1076
}
1076
1077
}
@@ -1093,39 +1094,18 @@ QString QgsPostgresProvider::getPrimaryKey()
1093
1094
type = PQgetvalue ( result, 0 , 0 );
1094
1095
}
1095
1096
1097
+ // mPrimaryKeyDefault stays null and is retrieved later on demand
1098
+
1096
1099
if (( type != " int4" && type != " oid" ) ||
1097
1100
!uniqueData ( mSchemaName , mTableName , primaryKey ) )
1098
1101
{
1099
1102
primaryKey = " " ;
1100
1103
}
1101
1104
}
1102
1105
1103
- // Have a poke around the view to see if any of the columns
1104
- // could be used as the primary key.
1105
- tableCols cols;
1106
-
1107
- // Given a schema.view, populate the cols variable with the
1108
- // schema.table.column's that underly the view columns.
1109
- findColumns ( cols );
1110
-
1111
1106
if ( primaryKey.isEmpty () )
1112
1107
{
1113
- // From the view columns, choose one for which the underlying
1114
- // column is suitable for use as a key into the view.
1115
- primaryKey = chooseViewColumn ( cols );
1116
- }
1117
-
1118
- tableCols::const_iterator it = cols.find ( primaryKey );
1119
- if ( it != cols.end () )
1120
- {
1121
- primaryKeyDefault = defaultValue ( it->second .column , it->second .relation , it->second .schema ).toString ();
1122
- if ( primaryKeyDefault.isNull () )
1123
- {
1124
- primaryKeyDefault = QString ( " max(%1)+1 from %2.%3" )
1125
- .arg ( quotedIdentifier ( it->second .column ) )
1126
- .arg ( quotedIdentifier ( it->second .schema ) )
1127
- .arg ( quotedIdentifier ( it->second .relation ) );
1128
- }
1108
+ parseView ();
1129
1109
}
1130
1110
}
1131
1111
else
@@ -1227,13 +1207,13 @@ QString QgsPostgresProvider::getPrimaryKey()
1227
1207
}
1228
1208
else
1229
1209
{
1230
- primaryKeyDefault = defaultValue ( primaryKey ).toString ();
1231
- if ( primaryKeyDefault .isNull () )
1210
+ mPrimaryKeyDefault = defaultValue ( primaryKey ).toString ();
1211
+ if ( mPrimaryKeyDefault .isNull () )
1232
1212
{
1233
- primaryKeyDefault = QString ( " max(%1)+1 from %2.%3" )
1234
- .arg ( quotedIdentifier ( primaryKey ) )
1235
- .arg ( quotedIdentifier ( mSchemaName ) )
1236
- .arg ( quotedIdentifier ( mTableName ) );
1213
+ mPrimaryKeyDefault = QString ( " max(%1)+1 from %2.%3" )
1214
+ .arg ( quotedIdentifier ( primaryKey ) )
1215
+ .arg ( quotedIdentifier ( mSchemaName ) )
1216
+ .arg ( quotedIdentifier ( mTableName ) );
1237
1217
}
1238
1218
}
1239
1219
}
@@ -1250,11 +1230,58 @@ QString QgsPostgresProvider::getPrimaryKey()
1250
1230
return primaryKey;
1251
1231
}
1252
1232
1233
+ void QgsPostgresProvider::parseView ()
1234
+ {
1235
+ // Have a poke around the view to see if any of the columns
1236
+ // could be used as the primary key.
1237
+ tableCols cols;
1238
+
1239
+ // Given a schema.view, populate the cols variable with the
1240
+ // schema.table.column's that underly the view columns.
1241
+ findColumns ( cols );
1242
+
1243
+ // pick the primary key, if we don't have one yet
1244
+ if ( primaryKey.isEmpty () )
1245
+ {
1246
+ // From the view columns, choose one for which the underlying
1247
+ // column is suitable for use as a key into the view.
1248
+ primaryKey = chooseViewColumn ( cols );
1249
+ }
1250
+
1251
+ tableCols::const_iterator it = cols.find ( primaryKey );
1252
+ if ( it != cols.end () )
1253
+ {
1254
+ mPrimaryKeyDefault = defaultValue ( it->second .column , it->second .relation , it->second .schema ).toString ();
1255
+ if ( mPrimaryKeyDefault .isNull () )
1256
+ {
1257
+ mPrimaryKeyDefault = QString ( " max(%1)+1 from %2.%3" )
1258
+ .arg ( quotedIdentifier ( it->second .column ) )
1259
+ .arg ( quotedIdentifier ( it->second .schema ) )
1260
+ .arg ( quotedIdentifier ( it->second .relation ) );
1261
+ }
1262
+ }
1263
+ else
1264
+ {
1265
+ mPrimaryKeyDefault = QString ( " max(%1)+1 from %2.%3" )
1266
+ .arg ( quotedIdentifier ( primaryKey ) )
1267
+ .arg ( quotedIdentifier ( mSchemaName ) )
1268
+ .arg ( quotedIdentifier ( mTableName ) );
1269
+ }
1270
+ }
1271
+
1272
+ QString QgsPostgresProvider::primaryKeyDefault ()
1273
+ {
1274
+ if ( mPrimaryKeyDefault .isNull () )
1275
+ parseView ();
1276
+
1277
+ return mPrimaryKeyDefault ;
1278
+ }
1279
+
1253
1280
// Given the table and column that each column in the view refers to,
1254
1281
// choose one. Prefers column with an index on them, but will
1255
1282
// otherwise choose something suitable.
1256
1283
1257
- QString QgsPostgresProvider::chooseViewColumn ( const tableCols& cols )
1284
+ QString QgsPostgresProvider::chooseViewColumn ( const tableCols & cols )
1258
1285
{
1259
1286
// For each relation name and column name need to see if it
1260
1287
// has unique constraints on it, or is a primary key (if not,
@@ -2206,7 +2233,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
2206
2233
2207
2234
if ( primaryKeyType != " tid" )
2208
2235
{
2209
- int id = paramValue ( primaryKeyDefault, primaryKeyDefault ).toInt ();
2236
+ int id = paramValue ( primaryKeyDefault () , primaryKeyDefault () ).toInt ();
2210
2237
params << QString::number ( id );
2211
2238
newIds << id;
2212
2239
}
0 commit comments