@@ -438,12 +438,19 @@ bool QgsPostgresProvider::declareCursor(
438
438
if ( !whereClause.isEmpty () )
439
439
query += QString ( " where %1" ).arg ( whereClause );
440
440
441
- return connectionRO->openCursor ( cursorName, query );
441
+ if ( !connectionRO->openCursor ( cursorName, query ) )
442
+ {
443
+ // reloading the fields might help next time around
444
+ rewind ();
445
+ return false ;
446
+ }
442
447
}
443
448
catch ( PGFieldNotFound )
444
449
{
445
450
return false ;
446
451
}
452
+
453
+ return true ;
447
454
}
448
455
449
456
bool QgsPostgresProvider::getFeature ( PGresult *queryResult, int row, bool fetchGeometry,
@@ -2937,25 +2944,37 @@ QgsRectangle QgsPostgresProvider::extent()
2937
2944
{
2938
2945
if ( QString::fromUtf8 ( PQgetvalue ( result, 0 , 0 ) ).toInt () > 0 )
2939
2946
{
2940
- sql = QString ( " select %1(%2,%3,%4)" )
2941
- .arg ( connectionRO->majorVersion () < 2 ? " estimated_extent" : " st_estimated_extent" )
2942
- .arg ( quotedValue ( mSchemaName ) )
2943
- .arg ( quotedValue ( mTableName ) )
2944
- .arg ( quotedValue ( geometryColumn ) );
2947
+ sql = QString ( " select reltuples::int from pg_catalog.pg_class where oid=regclass(%1)::oid" ).arg ( quotedValue ( mQuery ) );
2945
2948
result = connectionRO->PQexec ( sql );
2946
- if ( PQresultStatus ( result ) == PGRES_TUPLES_OK && PQntuples ( result ) == 1 )
2949
+ if ( PQresultStatus ( result ) == PGRES_TUPLES_OK &&
2950
+ PQntuples ( result ) == 1 &&
2951
+ QString::fromUtf8 ( PQgetvalue ( result, 0 , 0 ) ).toLong () > 0 )
2947
2952
{
2948
- ext = PQgetvalue ( result, 0 , 0 );
2949
-
2950
- // fix for what might be a postgis bug: when the extent crosses the
2951
- // dateline extent() returns -180 to 180 (which appears right), but
2952
- // estimated_extent() returns eastern bound of data (>-180) and
2953
- // 180 degrees.
2954
- if ( !ext. startsWith ( " -180 " ) && ext. contains ( " ,180 " ) )
2953
+ sql = QString ( " select %1(%2,%3,%4) " )
2954
+ . arg ( connectionRO-> majorVersion () < 2 ? " estimated_extent " : " st_estimated_extent " )
2955
+ . arg ( quotedValue ( mSchemaName ) )
2956
+ . arg ( quotedValue ( mTableName ) )
2957
+ . arg ( quotedValue ( geometryColumn ) );
2958
+ result = connectionRO-> PQexec ( sql );
2959
+ if ( PQresultStatus ( result ) == PGRES_TUPLES_OK && PQntuples ( result ) == 1 )
2955
2960
{
2956
- ext.clear ();
2961
+ ext = PQgetvalue ( result, 0 , 0 );
2962
+
2963
+ // fix for what might be a postgis bug: when the extent crosses the
2964
+ // dateline extent() returns -180 to 180 (which appears right), but
2965
+ // estimated_extent() returns eastern bound of data (>-180) and
2966
+ // 180 degrees.
2967
+ if ( !ext.startsWith ( " -180 " ) && ext.contains ( " ,180 " ) )
2968
+ {
2969
+ ext.clear ();
2970
+ }
2957
2971
}
2958
2972
}
2973
+ else
2974
+ {
2975
+ // no features => ignore estimated extent
2976
+ ext.clear ();
2977
+ }
2959
2978
}
2960
2979
}
2961
2980
else
@@ -3322,8 +3341,8 @@ QString QgsPostgresProvider::quotedValue( QString value ) const
3322
3341
if ( value.isNull () )
3323
3342
return " NULL" ;
3324
3343
3325
- // FIXME: use PQescapeStringConn
3326
3344
value.replace ( " '" , " ''" );
3345
+ value.replace ( " \\\" " , " \\\\\" " );
3327
3346
return value.prepend ( " '" ).append ( " '" );
3328
3347
}
3329
3348
@@ -3482,7 +3501,7 @@ QString QgsPostgresProvider::subsetString()
3482
3501
return sqlWhereClause;
3483
3502
}
3484
3503
3485
- PGconn * QgsPostgresProvider::pgConnection ()
3504
+ PGconn *QgsPostgresProvider::pgConnection ()
3486
3505
{
3487
3506
connectRW ();
3488
3507
return connectionRW->pgConnection ();
0 commit comments