Skip to content

Commit 86ab6c2

Browse files
committed
Moved the code to determine the primary key for a view using the URI keyColumn to a separate function.
1 parent 6a1f1fd commit 86ab6c2

File tree

2 files changed

+92
-83
lines changed

2 files changed

+92
-83
lines changed

src/providers/postgres/qgspostgresprovider.cpp

+88-83
Original file line numberDiff line numberDiff line change
@@ -1175,89 +1175,7 @@ bool QgsPostgresProvider::determinePrimaryKey()
11751175
}
11761176
else if ( type == "v" || type == "m" ) // the relation is a view
11771177
{
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();
12611179
}
12621180
else
12631181
{
@@ -1324,6 +1242,93 @@ bool QgsPostgresProvider::determinePrimaryKey()
13241242
return mValid;
13251243
}
13261244

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+
13271332
bool QgsPostgresProvider::uniqueData( QString query, QString quotedColName )
13281333
{
13291334
Q_UNUSED( query );

src/providers/postgres/qgspostgresprovider.h

+4
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ class QgsPostgresProvider : public QgsVectorDataProvider
136136
*/
137137
bool determinePrimaryKey();
138138

139+
/** Determine the fields making up the primary key from the uri attribute keyColumn
140+
*/
141+
void determinePrimaryKeyFromUriKeyColumn();
142+
139143
/**
140144
* Get the field information for the layer
141145
* @return vector of QgsField objects

0 commit comments

Comments
 (0)