Skip to content

Commit

Permalink
Add optimized version of QgsPostgresProvider::empty()
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jun 4, 2018
1 parent 381a766 commit b568d6b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Expand Up @@ -937,6 +937,18 @@ Number of features rendered with specified legend key. Features must be first
calculated by countSymbolFeatures()

:return: number of features rendered by symbol or -1 if failed or counts are not available
%End

virtual bool empty() const;

%Docstring
Determines if this vector layer is empty.
A layer is considered empty if either the data provider or
the edit buffer have features in them.
This means, in case the data provider contains features which have all
been deleted in the edit buffer, the result will be false.

.. versionadded:: 3.2
%End

void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, bool loadDefaultStyleFlag = false ) /Deprecated/;
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -704,6 +704,8 @@ long QgsVectorLayer::featureCount( const QString &legendKey ) const
return mSymbolFeatureCountMap.value( legendKey );
}



QgsVectorLayerFeatureCounter *QgsVectorLayer::countSymbolFeatures()
{
if ( mSymbolFeatureCounted || mFeatureCounter )
Expand Down Expand Up @@ -2763,6 +2765,11 @@ long QgsVectorLayer::featureCount() const
( mEditBuffer ? mEditBuffer->mAddedFeatures.size() - mEditBuffer->mDeletedFeatureIds.size() : 0 );
}

bool QgsVectorLayer::empty() const
{
return mDataProvider->empty() && ( !mEditBuffer || mEditBuffer->addedFeatures().empty() );
}

bool QgsVectorLayer::commitChanges()
{
mCommitErrors.clear();
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -929,6 +929,17 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
long featureCount( const QString &legendKey ) const;

/**
* Determines if this vector layer is empty.
* A layer is considered empty if either the data provider or
* the edit buffer have features in them.
* This means, in case the data provider contains features which have all
* been deleted in the edit buffer, the result will be false.
*
* \since QGIS 3.2
*/
bool empty() const override;

/**
* Update the data source of the layer. The layer's renderer and legend will be preserved only
* if the geometry type of the new data source matches the current geometry type of the layer.
Expand Down
13 changes: 13 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -3187,6 +3187,19 @@ long QgsPostgresProvider::featureCount() const
return num;
}

bool QgsPostgresProvider::empty() const
{
QString sql = QStringLiteral( "SELECT EXISTS (SELECT * FROM %1%2 LIMIT 1)" ).arg( mQuery, filterWhereClause() );
QgsPostgresResult res( connectionRO()->PQexec( sql ) );
if ( res.PQresultStatus() != PGRES_TUPLES_OK )
{
pushError( res.PQresultErrorMessage() );
return false;
}

return res.PQgetvalue( 0, 0 ) != 't';
}

QgsRectangle QgsPostgresProvider::extent() const
{
if ( mGeometryColumn.isNull() )
Expand Down
10 changes: 10 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -109,6 +109,16 @@ class QgsPostgresProvider : public QgsVectorDataProvider

long featureCount() const override;

/**
* Determines if there is at least one feature avaiable on this table.
*
* \note In contrast to the featureCount() method, this method is not
* affected by estimated metadata.
*
* \since QGIS 3.2
*/
bool empty() const override;

/**
* Returns a string representation of the endian-ness for the layer
*/
Expand Down

0 comments on commit b568d6b

Please sign in to comment.