Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
PG views: be tolerant for unsupported field type ...
... if it's in URI key

Fixes #31799
  • Loading branch information
elpaso committed Sep 17, 2019
1 parent b7693d1 commit f7d1971
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -179,7 +179,7 @@ QgsPostgresProvider::QgsPostgresProvider( QString const &uri, const ProviderOpti
if ( !getGeometryDetails() ) // gets srid, geometry and data type
{
// the table is not a geometry table
QgsMessageLog::logMessage( tr( "invalid PostgreSQL layer" ), tr( "PostGIS" ) );
QgsMessageLog::logMessage( tr( "Invalid PostgreSQL layer" ), tr( "PostGIS" ) );
disconnectDb();
return;
}
Expand All @@ -191,7 +191,7 @@ QgsPostgresProvider::QgsPostgresProvider( QString const &uri, const ProviderOpti
{
if ( !getTopoLayerInfo() ) // gets topology name and layer id
{
QgsMessageLog::logMessage( tr( "invalid PostgreSQL topology layer" ), tr( "PostGIS" ) );
QgsMessageLog::logMessage( tr( "Invalid PostgreSQL topology layer" ), tr( "PostGIS" ) );
mValid = false;
disconnectDb();
return;
Expand Down Expand Up @@ -939,7 +939,7 @@ bool QgsPostgresProvider::loadFields()
}
else if ( formattedFieldType != QLatin1String( "numeric" ) )
{
QgsMessageLog::logMessage( tr( "unexpected formatted field type '%1' for field %2" )
QgsMessageLog::logMessage( tr( "Unexpected formatted field type '%1' for field %2" )
.arg( formattedFieldType,
fieldName ),
tr( "PostGIS" ) );
Expand Down Expand Up @@ -1010,7 +1010,7 @@ bool QgsPostgresProvider::loadFields()
}
else
{
QgsDebugMsg( QStringLiteral( "unexpected formatted field type '%1' for field %2" )
QgsDebugMsg( QStringLiteral( "Unexpected formatted field type '%1' for field %2" )
.arg( formattedFieldType,
fieldName ) );
fieldSize = -1;
Expand All @@ -1028,7 +1028,7 @@ bool QgsPostgresProvider::loadFields()
}
else
{
QgsMessageLog::logMessage( tr( "unexpected formatted field type '%1' for field %2" )
QgsMessageLog::logMessage( tr( "Unexpected formatted field type '%1' for field %2" )
.arg( formattedFieldType,
fieldName ) );
fieldSize = -1;
Expand All @@ -1049,8 +1049,19 @@ bool QgsPostgresProvider::loadFields()
}
else
{
QgsMessageLog::logMessage( tr( "Field %1 ignored, because of unsupported type %2" ).arg( fieldName, fieldTypeName ), tr( "PostGIS" ) );
continue;
// be tolerant in case of views: this might be a field used as a key
const QgsPostgresProvider::Relkind type = relkind();
if ( ( type == Relkind::View || type == Relkind::MaterializedView ) && mUri.keyColumn().remove( '"' ) == fieldName )
{
// Assume it is convertible to text
fieldType = QVariant::String;
fieldSize = -1;
}
else
{
QgsMessageLog::logMessage( tr( "Field %1 ignored, because of unsupported type %2" ).arg( fieldName, fieldTType ), tr( "PostGIS" ) );
continue;
}
}

if ( isArray )
Expand Down Expand Up @@ -1368,7 +1379,7 @@ bool QgsPostgresProvider::determinePrimaryKey()
// If the relation is a view try to find a suitable column to use as
// the primary key.

QgsPostgresProvider::Relkind type = relkind();
const QgsPostgresProvider::Relkind type = relkind();

if ( type == Relkind::OrdinaryTable || type == Relkind::PartitionedTable )
{
Expand Down
9 changes: 9 additions & 0 deletions tests/testdata/provider/testdata_pg.sql
Expand Up @@ -653,3 +653,12 @@ CREATE VIEW qgis_test.some_poly_data_shift_bbox AS
0.0
) AS shiftbox
FROM qgis_test.some_poly_data;


---------------------------------------------
--
-- View with tid PK field
--

CREATE TABLE qgis_test.b31799_test_table AS (SELECT (ST_DumpPoints(ST_GeneratePoints(ST_Expand('SRID=4326;POINT(0 0)'::geometry,90),10))).geom, random());
CREATE VIEW qgis_test.b31799_test_view_ctid AS (SELECT ctid, geom, random() FROM qgis_test.b31799_test_table, pg_sleep(0.1));

0 comments on commit f7d1971

Please sign in to comment.