@@ -234,28 +234,6 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
234
234
235
235
// Kick off the long running threads
236
236
237
- #ifdef POSTGRESQL_THREADS
238
- QgsDebugMsg ( " About to touch mExtentThread" );
239
- mExtentThread .setConnInfo ( mUri .connectionInfo );
240
- mExtentThread .setTableName ( mTableName );
241
- mExtentThread .setSqlWhereClause ( sqlWhereClause );
242
- mExtentThread .setGeometryColumn ( geometryColumn );
243
- mExtentThread .setCallback ( this );
244
- QgsDebugMsg ( " About to start mExtentThread" );
245
- mExtentThread .start ();
246
- QgsDebugMsg ( " Main thread just dispatched mExtentThread" );
247
-
248
- QgsDebugMsg ( " About to touch mCountThread" );
249
- mCountThread .setConnInfo ( mUri .connectionInfo );
250
- mCountThread .setTableName ( mTableName );
251
- mCountThread .setSqlWhereClause ( sqlWhereClause );
252
- mCountThread .setGeometryColumn ( geometryColumn );
253
- mCountThread .setCallback ( this );
254
- QgsDebugMsg ( " About to start mCountThread" );
255
- mCountThread .start ();
256
- QgsDebugMsg ( " Main thread just dispatched mCountThread" );
257
- #endif
258
-
259
237
// fill type names into sets
260
238
mNativeTypes
261
239
// integer types
@@ -292,25 +270,6 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
292
270
293
271
QgsPostgresProvider::~QgsPostgresProvider ()
294
272
{
295
- #ifdef POSTGRESQL_THREADS
296
- QgsDebugMsg ( " About to wait for mExtentThread" );
297
-
298
- mExtentThread .wait ();
299
-
300
- QgsDebugMsg ( " Finished waiting for mExtentThread" );
301
-
302
- QgsDebugMsg ( " About to wait for mCountThread" );
303
-
304
- mCountThread .wait ();
305
-
306
- QgsDebugMsg ( " Finished waiting for mCountThread" );
307
-
308
- // Make sure all events from threads have been processed
309
- // (otherwise they will get destroyed prematurely)
310
- QApplication::sendPostedEvents ( this , QGis::ProviderExtentCalcEvent );
311
- QApplication::sendPostedEvents ( this , QGis::ProviderCountCalcEvent );
312
- #endif
313
-
314
273
disconnectDb ();
315
274
316
275
QgsDebugMsg ( " deconstructing." );
@@ -2730,68 +2689,38 @@ long QgsPostgresProvider::getFeatureCount()
2730
2689
return featuresCounted;
2731
2690
}
2732
2691
2733
- // TODO: use the estimateExtents procedure of PostGIS and PostgreSQL 8
2734
- // This tip thanks to #qgis irc nick "creeping"
2735
2692
void QgsPostgresProvider::calculateExtents ()
2736
2693
{
2737
- #ifdef POSTGRESQL_THREADS
2738
- // get the approximate extent by retrieving the bounding box
2739
- // of the first few items with a geometry
2740
-
2741
- QString sql = QString ( " select box3d(%1) from %2 where " )
2742
- .arg ( quotedIdentifier ( geometryColumn ) )
2743
- .arg ( mSchemaTableName );
2744
-
2745
- if ( sqlWhereClause.length () > 0 )
2746
- {
2747
- sql += QString ( " (%1) and " ).arg ( sqlWhereClause );
2748
- }
2749
-
2750
- sql += QString ( " not IsEmpty(%1) limit 5" ).arg ( quotedIdentifier ( geometryColumn ) );
2751
-
2752
- QgsDebugMsg ( " Getting approximate extent using: '" + sql + " '" );
2753
-
2754
- Result result = connectionRO->PQexec ( sql );
2755
-
2756
- // TODO: Guard against the result having no rows
2757
- for ( int i = 0 ; i < PQntuples ( result ); i++ )
2758
- {
2759
- QString box3d = PQgetvalue ( result, i, 0 );
2760
-
2761
- if ( 0 == i )
2762
- {
2763
- // create the initial extent
2764
- layerExtent = QgsPostGisBox3d ( box3d );
2765
- }
2766
- else
2767
- {
2768
- // extend the initial extent
2769
- QgsPostGisBox3d b = QgsPostGisBox3d ( box3d );
2770
- layerExtent.combineExtentWith ( &b );
2771
- }
2772
-
2773
- QgsDebugMsg ( QString ( " After row %1, extent is %2" ).arg ( i ).arg ( layerExtent.toString () ) );
2774
- }
2775
-
2776
- #else // non-postgresql threads version
2777
2694
QString sql;
2778
2695
Result result;
2779
2696
QString ext;
2780
2697
2781
2698
// get the extents
2782
- if (( mUseEstimatedMetadata || sqlWhereClause.isEmpty () ) && !connectionRO-> hasNoExtentEstimate () )
2699
+ if ( mUseEstimatedMetadata || sqlWhereClause.isEmpty () )
2783
2700
{
2784
- result = connectionRO->PQexec ( QString ( " select estimated_extent(%1,%2,%3)" )
2785
- .arg ( quotedValue ( mSchemaName ) )
2786
- .arg ( quotedValue ( mTableName ) )
2787
- .arg ( quotedValue ( geometryColumn ) ) );
2788
- if ( PQresultStatus ( result ) != PGRES_TUPLES_OK )
2701
+ // do stats exists?
2702
+ sql = QString ( " SELECT COUNT(*) FROM pg_stats WHERE schemaname=%1 AND tablename=%2 AND attname=%3" )
2703
+ .arg ( quotedValue ( mSchemaName ) )
2704
+ .arg ( quotedValue ( mTableName ) )
2705
+ .arg ( quotedValue ( geometryColumn ) );
2706
+ result = connectionRO->PQexec ( sql );
2707
+ if ( PQresultStatus ( result ) == PGRES_TUPLES_OK && PQntuples ( result ) == 1 )
2789
2708
{
2790
- connectionRO->PQexecNR ( " ROLLBACK" );
2791
- connectionRO->setNoExtentEstimate ();
2709
+ if ( QString::fromUtf8 ( PQgetvalue ( result, 0 , 0 ) ).toInt () > 0 )
2710
+ {
2711
+ sql = QString ( " select estimated_extent(%1,%2,%3)" )
2712
+ .arg ( quotedValue ( mSchemaName ) )
2713
+ .arg ( quotedValue ( mTableName ) )
2714
+ .arg ( quotedValue ( geometryColumn ) );
2715
+ result = connectionRO->PQexec ( sql );
2716
+ if ( PQresultStatus ( result ) == PGRES_TUPLES_OK && PQntuples ( result ) == 1 )
2717
+ ext = PQgetvalue ( result, 0 , 0 );
2718
+ }
2719
+ }
2720
+ else
2721
+ {
2722
+ QgsDebugMsg ( QString ( " no column statistics for %1.%2.%3" ).arg ( mSchemaName ).arg ( mTableName ).arg ( geometryColumn ) );
2792
2723
}
2793
- else if ( PQntuples ( result ) == 1 )
2794
- ext = PQgetvalue ( result, 0 , 0 );
2795
2724
}
2796
2725
2797
2726
if ( ext.isEmpty () )
@@ -2810,7 +2739,7 @@ void QgsPostgresProvider::calculateExtents()
2810
2739
ext = PQgetvalue ( result, 0 , 0 );
2811
2740
}
2812
2741
2813
- QgsDebugMsg ( " Getting extents using schema.table : " + sql );
2742
+ QgsDebugMsg ( " Got extents using: " + sql );
2814
2743
2815
2744
QRegExp rx ( " \\ ((.+) (.+),(.+) (.+)\\ )" );
2816
2745
if ( ext.contains ( rx ) )
@@ -2826,7 +2755,6 @@ void QgsPostgresProvider::calculateExtents()
2826
2755
{
2827
2756
QgsDebugMsg ( " extents query failed" );
2828
2757
}
2829
- #endif
2830
2758
2831
2759
QgsDebugMsg ( " Set extents to: " + layerExtent.toString () );
2832
2760
}
0 commit comments