Skip to content

Commit

Permalink
postgres provider: allow detection of empty views (fixes #13638)
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Oct 20, 2015
1 parent b20d316 commit 441f6f8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -588,9 +588,6 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
// catalog doesn't exist in postgresql so we ignore that, but we
// do need to get the geometry type.

// Make the assumption that the geometry type for the first
// row is the same as for all other rows.

QString tableName = result.PQgetvalue( i, 0 ); // relname
QString schemaName = result.PQgetvalue( i, 1 ); // nspname
QString column = result.PQgetvalue( i, 2 ); // attname
Expand All @@ -601,8 +598,8 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP

//QgsDebugMsg( QString( "%1.%2.%3: %4" ).arg( schemaName ).arg( tableName ).arg( column ).arg( relkind ) );

layerProperty.types.clear();
layerProperty.srids.clear();
layerProperty.types = QList<QGis::WkbType>() << QGis::WKBUnknown;
layerProperty.srids = QList<int>() << INT_MIN;
layerProperty.schemaName = schemaName;
layerProperty.tableName = tableName;
layerProperty.geometryColName = column;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -1318,7 +1318,7 @@ bool QgsPostgresProvider::uniqueData( QString query, QString quotedColNames )
{
Q_UNUSED( query );
// Check to see if the given columns contain unique data
QString sql = QString( "SELECT count(distinct (%1))=count((%1)) AND bool_and((%1) IS NOT NULL) FROM %2%3" )
QString sql = QString( "SELECT count(distinct (%1))=count((%1)) FROM %2%3" )
.arg( quotedColNames,
mQuery,
filterWhereClause() );
Expand Down

3 comments on commit 441f6f8

@SebDieBln
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jef-n Your change now not only allows empty queries/views but it also allows parts of the key to be NULL. Is this intended?
If not, I would suggest simply adding the clause IS NOT FALSE to the bool_and() function to avoid the problematic NULL result for this special case.

The following query does the same check as the one before except it returns true for an empty relation:

SELECT
   count(distinct (%1)) = count((%1))
   AND
   bool_and((%1) IS NOT NULL) IS NOT FALSE
FROM %2%3

@jef-n
Copy link
Member Author

@jef-n jef-n commented on 441f6f8 Oct 20, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the problem with keys being partly NULL in the first place?

@SebDieBln
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usual comparison semantics don't apply to NULL values. E.g. "Attribute" = NULL never yields TRUE, even if the attribute is NULL. It always yields NULL.

So at least here would be a problem. Maybe somewhere else, too.

See also my newly filed bug 13641.

Please sign in to comment.