Skip to content

Commit 5fa4c17

Browse files
author
g_j_m
committed
Capture a few unusual occurences with respect to views with unique
constraints but non-unique data. In response to ticket #143. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5514 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 3fcb926 commit 5fa4c17

File tree

1 file changed

+52
-53
lines changed

1 file changed

+52
-53
lines changed

src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,70 +1167,69 @@ QString QgsPostgresProvider::chooseViewColumn(const tableCols& cols)
11671167
// indexed, else pick one called 'oid' if it exists, else
11681168
// pick the first one. If there are none we return an empty string.
11691169

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)
11711173
{
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;
11751185
#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";
11781188
#endif
1189+
break;
11791190
}
1191+
PQclear(result);
11801192
}
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-
}
12061193

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))
12081201
{
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;
12161203
#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.";
12211208
#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)
12261215
{
1227-
key = suitable.begin()->first;
1216+
if (uniqueData(mSchemaName, mTableName, i->first))
1217+
{
1218+
key = i->first;
12281219
#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.";
12331224
#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+
}
12341233
}
12351234
}
12361235
}
@@ -1248,7 +1247,7 @@ QString QgsPostgresProvider::chooseViewColumn(const tableCols& cols)
12481247
"have a unique constraint on it, or be a PostgreSQL "
12491248
"oid column. To improve "
12501249
"performance the column should also be indexed.\n"));
1251-
log.prepend(tr("The view ") + "'" + mSchemaName + mTableName + "'" +
1250+
log.prepend(tr("The view ") + "'" + mSchemaName + '.' + mTableName + "' " +
12521251
tr("has no column suitable for use as a unique key.\n"));
12531252
showMessageBox(tr("No suitable key column in view"), log);
12541253
}

0 commit comments

Comments
 (0)