Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
postgres provider: lazy feature count
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13524 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed May 18, 2010
1 parent 9a21aac commit 69a153e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
24 changes: 11 additions & 13 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -139,7 +139,7 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )

deduceEndian();
calculateExtents();
getFeatureCount();
featuresCounted = -1;

// set the primary key
getPrimaryKey();
Expand Down Expand Up @@ -735,14 +735,6 @@ QGis::WkbType QgsPostgresProvider::geometryType() const
return geomType;
}

/**
* Return the feature type
*/
long QgsPostgresProvider::featureCount() const
{
return featuresCounted;
}

const QgsField &QgsPostgresProvider::field( int index ) const
{
QgsFieldMap::const_iterator it = attributeFields.find( index );
Expand Down Expand Up @@ -2753,14 +2745,20 @@ bool QgsPostgresProvider::setSubsetString( QString theSQL )
setDataSourceUri( mUri.uri() );

// need to recalculate the number of features...
getFeatureCount();
featuresCounted = -1;
calculateExtents();

return true;
}

long QgsPostgresProvider::getFeatureCount()
/**
* Return the feature count
*/
long QgsPostgresProvider::featureCount() const
{
if( featuresCounted >= 0 )
return featuresCounted;

// get total number of features

// First get an approximate count; then delegate to
Expand All @@ -2783,12 +2781,12 @@ long QgsPostgresProvider::getFeatureCount()

Result result = connectionRO->PQexec( sql );

QgsDebugMsg( "Approximate Number of features as text: " +
QgsDebugMsg( "number of features as text: " +
QString::fromUtf8( PQgetvalue( result, 0, 0 ) ) );

featuresCounted = QString::fromUtf8( PQgetvalue( result, 0, 0 ) ).toLong();

QgsDebugMsg( "Approximate Number of features: " + QString::number( featuresCounted ) );
QgsDebugMsg( "number of features: " + QString::number( featuresCounted ) );

return featuresCounted;
}
Expand Down
5 changes: 1 addition & 4 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -446,7 +446,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
/**
* Number of features in the layer
*/
long featuresCounted;
mutable long featuresCounted;

/**
* Feature queue that GetNextFeature will retrieve from
Expand Down Expand Up @@ -576,9 +576,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider

int enabledCapabilities;

//! Get the feature count based on the where clause
long getFeatureCount();

//! Calculate the extents of the layer
void calculateExtents();

Expand Down

0 comments on commit 69a153e

Please sign in to comment.