Skip to content
Permalink
Browse files

Add optimized version of QgsPostgresProvider::empty()

  • Loading branch information
m-kuhn committed Jun 4, 2018
1 parent 381a766 commit b568d6be5577db4fce943e2b96b752b91249247b
@@ -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/;
@@ -704,6 +704,8 @@ long QgsVectorLayer::featureCount( const QString &legendKey ) const
return mSymbolFeatureCountMap.value( legendKey );
}



QgsVectorLayerFeatureCounter *QgsVectorLayer::countSymbolFeatures()
{
if ( mSymbolFeatureCounted || mFeatureCounter )
@@ -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();
@@ -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.
@@ -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() )
@@ -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
*/

0 comments on commit b568d6b

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