Skip to content
Permalink
Browse files

Merge pull request #34295 from elpaso/bugfix-gh34167-pg-views-pk-3_10

Fix PG views PK
  • Loading branch information
elpaso committed Feb 6, 2020
2 parents 423ef5b + 8671f6f commit 6609651cb23c8a12c3e1346fb1df02a51b280e02
@@ -45,7 +45,7 @@ QgsPgTableModel::QgsPgTableModel()

void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProperty )
{
QgsDebugMsg( layerProperty.toString() );
QgsDebugMsgLevel( layerProperty.toString(), 3 );

// is there already a root item with the given scheme Name?
QStandardItem *schemaItem = nullptr;
@@ -553,7 +553,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
// The following query returns only tables that exist and the user has SELECT privilege on.
// Can't use regclass here because table must exist, else error occurs.
sql = QString( "SELECT %1,%2,%3,%4,%5,%6,c.relkind,obj_description(c.oid),"
"array_agg(a.attname), "
"array_agg(a.attname ORDER BY a.attnum), "
"count(CASE WHEN t.typname IN (%9) THEN 1 ELSE NULL END) "
", %8 "
" FROM %7 l,pg_class c,pg_namespace n,pg_attribute a,pg_type t"
@@ -824,7 +824,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
",pg_namespace.nspname"
",pg_class.relkind"
",obj_description(pg_class.oid)"
",array_agg(a.attname)"
",array_agg(a.attname ORDER BY a.attnum)"
" FROM "
" pg_class"
",pg_namespace"
@@ -1789,7 +1789,7 @@ void QgsPostgresConn::retrieveLayerTypes( QVector<QgsPostgresLayerProperty *> &l

}

QgsDebugMsg( "Layer types,srids and dims query: " + query );
QgsDebugMsgLevel( "Layer types,srids and dims query: " + query, 3 );

QgsPostgresResult res( PQexec( query ) );
if ( res.PQresultStatus() != PGRES_TUPLES_OK )
@@ -1804,31 +1804,33 @@ void QgsPostgresConn::retrieveLayerTypes( QVector<QgsPostgresLayerProperty *> &l
auto srids_and_types = QgsPostgresStringUtils::parseArray( res.PQgetvalue( i, 1 ) );
QgsPostgresLayerProperty &layerProperty = *layerProperties[idx];

QgsDebugMsg( QStringLiteral(
"Layer %1.%2.%3 has %4 srid/type combinations"
)
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( srids_and_types.length() )
);
QgsDebugMsgLevel( QStringLiteral(
"Layer %1.%2.%3 has %4 srid/type combinations"
)
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( srids_and_types.length() )
, 3
);

/* Gather found types */
QList< std::pair<QgsWkbTypes::Type, int> > foundCombinations;
for ( const auto &sridAndTypeVariant : srids_and_types )
{
QString sridAndTypeString = sridAndTypeVariant.toString();

QgsDebugMsg( QStringLiteral(
"Analyzing layer's %1.%2.%3 sridAndType %4"
" against %6 found combinations"
)
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( sridAndTypeString )
.arg( foundCombinations.length() )
);
QgsDebugMsgLevel( QStringLiteral(
"Analyzing layer's %1.%2.%3 sridAndType %4"
" against %6 found combinations"
)
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( sridAndTypeString )
.arg( foundCombinations.length() )
, 3
);

if ( sridAndTypeString == "NULL" ) continue;

@@ -1862,84 +1864,84 @@ void QgsPostgresConn::retrieveLayerTypes( QVector<QgsPostgresLayerProperty *> &l

if ( multiCurveType == knownMultiCurveType )
{
QgsDebugMsg( QStringLiteral(
"Upgrading type[%1] of layer %2.%3.%4 "
"to multiCurved type %5" )
.arg( j )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( multiCurveType )
);
QgsDebugMsgLevel( QStringLiteral(
"Upgrading type[%1] of layer %2.%3.%4 "
"to multiCurved type %5" )
.arg( j )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( multiCurveType ), 3
);
foundCombinations[j].first = multiCurveType;
break;
}
else if ( multiType == knownMultiType )
{
QgsDebugMsg( QStringLiteral(
"Upgrading type[%1] of layer %2.%3.%4 "
"to multi type %5" )
.arg( j )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( multiType )
);
QgsDebugMsgLevel( QStringLiteral(
"Upgrading type[%1] of layer %2.%3.%4 "
"to multi type %5" )
.arg( j )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( multiType ), 3
);
foundCombinations[j].first = multiType;
break;
}
else if ( curveType == knownCurveType )
{
QgsDebugMsg( QStringLiteral(
"Upgrading type[%1] of layer %2.%3.%4 "
"to curved type %5" )
.arg( j )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( multiType )
);
QgsDebugMsgLevel( QStringLiteral(
"Upgrading type[%1] of layer %2.%3.%4 "
"to curved type %5" )
.arg( j )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( multiType ), 3
);
foundCombinations[j].first = curveType;
break;
}
}

if ( j < foundCombinations.length() )
{
QgsDebugMsg( QStringLiteral(
"Pre-existing compatible combination %1/%2 "
"found for layer %3.%4.%5 "
)
.arg( j ) .arg( foundCombinations.length() )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
);
QgsDebugMsgLevel( QStringLiteral(
"Pre-existing compatible combination %1/%2 "
"found for layer %3.%4.%5 "
)
.arg( j ) .arg( foundCombinations.length() )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName ), 3
);
continue; // already found
}

QgsDebugMsg( QStringLiteral(
"Setting typeSridCombination[%1] of layer %2.%3.%4 "
"to srid %5 and type %6" )
.arg( j )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( srid )
.arg( type )
);
QgsDebugMsgLevel( QStringLiteral(
"Setting typeSridCombination[%1] of layer %2.%3.%4 "
"to srid %5 and type %6" )
.arg( j )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( srid )
.arg( type ), 3
);

foundCombinations << std::make_pair( type, srid );
}

QgsDebugMsg( QStringLiteral(
"Completed scan of %1 srid/type combinations "
"for layer of layer %2.%3.%4 " )
.arg( srids_and_types.length() )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
);
QgsDebugMsgLevel( QStringLiteral(
"Completed scan of %1 srid/type combinations "
"for layer of layer %2.%3.%4 " )
.arg( srids_and_types.length() )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName ), 2
);

/* Rewrite srids and types to match found combinations
* of srids and types */
@@ -1950,20 +1952,20 @@ void QgsPostgresConn::retrieveLayerTypes( QVector<QgsPostgresLayerProperty *> &l
layerProperty.types << comb.first;
layerProperty.srids << comb.second;
}
QgsDebugMsg( QStringLiteral(
"Final layer %1.%2.%3 types: %4" )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( layerProperty.types.length() )
);
QgsDebugMsg( QStringLiteral(
"Final layer %1.%2.%3 srids: %4" )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( layerProperty.srids.length() )
);
QgsDebugMsgLevel( QStringLiteral(
"Final layer %1.%2.%3 types: %4" )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( layerProperty.types.length() ), 2
);
QgsDebugMsgLevel( QStringLiteral(
"Final layer %1.%2.%3 srids: %4" )
.arg( layerProperty.schemaName,
layerProperty.tableName,
layerProperty.geometryColName )
.arg( layerProperty.srids.length() ), 2
);
}
}

@@ -164,6 +164,14 @@ def test_postgis_raster_rename(self):
self.assertFalse('Raster2' in table_names)
self.assertTrue('Raster1' in table_names)

def test_pk_cols_order(self):
"""Test that PKs are returned in consistent order: see GH #34167"""

md = QgsProviderRegistry.instance().providerMetadata(self.providerKey)
conn = md.createConnection(self.uri, {})
self.assertEqual(self._table_by_name(conn.tables(), 'bikes_view').primaryKeyColumns(), ['pk', 'name'])
self.assertEqual(self._table_by_name(conn.tables(), 'some_poly_data_view').primaryKeyColumns(), ['pk', 'geom'])


if __name__ == '__main__':
unittest.main()

0 comments on commit 6609651

Please sign in to comment.
You can’t perform that action at this time.