Skip to content

Commit b0a9d99

Browse files
author
jef
committed
fix #2511
git-svn-id: http://svn.osgeo.org/qgis/trunk@13014 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 3e33939 commit b0a9d99

File tree

2 files changed

+59
-45
lines changed

2 files changed

+59
-45
lines changed

src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,26 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
115115
return;
116116
}
117117

118-
sql = QString( "SELECT "
119-
"has_table_privilege(%1,'DELETE'),"
120-
"has_table_privilege(%1,'UPDATE'),"
121-
"has_table_privilege(%1,'INSERT'),"
122-
"current_schema()" )
123-
.arg( quotedValue( mSchemaTableName ) );
118+
if ( connectionRO->pgVersion() >= 80400 )
119+
{
120+
sql = QString( "SELECT "
121+
"has_table_privilege(%1,'DELETE'),"
122+
"has_any_column_privilege(%1,'UPDATE'),"
123+
"has_column_privilege(%1,%2,'UPDATE'),"
124+
"has_table_privilege(%1,'INSERT'),"
125+
"current_schema()" )
126+
.arg( quotedValue( mSchemaTableName ) ).arg( quotedValue( geometryColumn ) );
127+
}
128+
else
129+
{
130+
sql = QString( "SELECT "
131+
"has_table_privilege(%1,'DELETE'),"
132+
"has_table_privilege(%1,'UPDATE'),"
133+
"has_table_privilege(%1,'UPDATE'),"
134+
"has_table_privilege(%1,'INSERT'),"
135+
"current_schema()" )
136+
.arg( quotedValue( mSchemaTableName ) );
137+
}
124138

125139
testAccess = connectionRO->PQexec( sql );
126140
if ( PQresultStatus( testAccess ) != PGRES_TUPLES_OK )
@@ -148,16 +162,22 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
148162
if ( QString::fromUtf8( PQgetvalue( testAccess, 0, 1 ) ) == "t" )
149163
{
150164
// UPDATE
151-
enabledCapabilities |= QgsVectorDataProvider::ChangeGeometries | QgsVectorDataProvider::ChangeAttributeValues;
165+
enabledCapabilities |= QgsVectorDataProvider::ChangeAttributeValues;
152166
}
153167

154168
if ( QString::fromUtf8( PQgetvalue( testAccess, 0, 2 ) ) == "t" )
169+
{
170+
// UPDATE
171+
enabledCapabilities |= QgsVectorDataProvider::ChangeGeometries;
172+
}
173+
174+
if ( QString::fromUtf8( PQgetvalue( testAccess, 0, 3 ) ) == "t" )
155175
{
156176
// INSERT
157177
enabledCapabilities |= QgsVectorDataProvider::AddFeatures;
158178
}
159179

160-
mCurrentSchema = QString::fromUtf8( PQgetvalue( testAccess, 0, 3 ) );
180+
mCurrentSchema = QString::fromUtf8( PQgetvalue( testAccess, 0, 4 ) );
161181
if ( mCurrentSchema == mSchemaName )
162182
{
163183
mUri.clearSchema();
@@ -2055,6 +2075,8 @@ bool QgsPostgresProvider::Conn::hasGEOS()
20552075
/* Functions for determining available features in postGIS */
20562076
QString QgsPostgresProvider::Conn::postgisVersion()
20572077
{
2078+
postgresqlVersion = PQserverVersion( conn );
2079+
20582080
Result result = PQexec( "select postgis_version()" );
20592081
if ( PQntuples( result ) != 1 )
20602082
{
@@ -2694,15 +2716,6 @@ void QgsPostgresProvider::calculateExtents()
26942716

26952717
sql += QString( "not IsEmpty(%1) limit 5" ).arg( quotedIdentifier( geometryColumn ) );
26962718

2697-
#if WASTE_TIME
2698-
sql = QString( "select "
2699-
"xmax(extent(%1)) as xmax,"
2700-
"xmin(extent(%1)) as xmin,"
2701-
"ymax(extent(%1)) as ymax,"
2702-
"ymin(extent(%1)) as ymin"
2703-
" from %2" ).arg( quotedIdentifier( geometryColumn ) ).arg( mSchemaTableName );
2704-
#endif
2705-
27062719
QgsDebugMsg( "Getting approximate extent using: '" + sql + "'" );
27072720

27082721
Result result = connectionRO->PQexec( sql );
@@ -2733,13 +2746,18 @@ void QgsPostgresProvider::calculateExtents()
27332746
QString ext;
27342747

27352748
// get the extents
2736-
if ( sqlWhereClause.isEmpty() )
2749+
if ( sqlWhereClause.isEmpty() && !connectionRO->hasNoExtentEstimate() )
27372750
{
27382751
result = connectionRO->PQexec( QString( "select estimated_extent(%1,%2,%3)" )
27392752
.arg( quotedValue( mSchemaName ) )
27402753
.arg( quotedValue( mTableName ) )
27412754
.arg( quotedValue( geometryColumn ) ) );
2742-
if ( PQntuples( result ) == 1 )
2755+
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
2756+
{
2757+
connectionRO->PQexecNR( "ROLLBACK" );
2758+
connectionRO->setNoExtentEstimate();
2759+
}
2760+
else if ( PQntuples( result ) == 1 )
27432761
ext = PQgetvalue( result, 0, 0 );
27442762
}
27452763

@@ -2753,19 +2771,12 @@ void QgsPostgresProvider::calculateExtents()
27532771
sql += QString( "where %1" ).arg( sqlWhereClause );
27542772

27552773
result = connectionRO->PQexec( sql );
2756-
if ( PQntuples( result ) == 1 )
2774+
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
2775+
connectionRO->PQexecNR( "ROLLBACK" );
2776+
else if ( PQntuples( result ) == 1 )
27572777
ext = PQgetvalue( result, 0, 0 );
27582778
}
27592779

2760-
#if WASTE_TIME
2761-
sql = QString( "select "
2762-
"xmax(extent(%1)) as xmax,"
2763-
"xmin(extent(%1)) as xmin,"
2764-
"ymax(extent(%1)) as ymax,"
2765-
"ymin(extent(%1)) as ymin"
2766-
" from %2" ).arg( quotedIdentifier( geometryColumn ) ).arg( mSchemaTableName );
2767-
#endif
2768-
27692780
QgsDebugMsg( "Getting extents using schema.table: " + sql );
27702781

27712782
QRegExp rx( "\\((.+) (.+),(.+) (.+)\\)" );
@@ -2784,19 +2795,6 @@ void QgsPostgresProvider::calculateExtents()
27842795
}
27852796
#endif
27862797

2787-
#if 0
2788-
#ifdef QGISDEBUG
2789-
QString xMsg;
2790-
QTextOStream( &xMsg ).precision( 18 );
2791-
QTextOStream( &xMsg ).width( 18 );
2792-
QTextOStream( &xMsg ) << "QgsPostgresProvider: Set extents to: "
2793-
<< layerExtent.xMinimum() << ", "
2794-
<< layerExtent.yMinimum() << " "
2795-
<< layerExtent.xMaximum() << ", "
2796-
<< layerExtent.yMaximum();
2797-
QgsDebugMsg( xMsg );
2798-
#endif
2799-
#endif
28002798
QgsDebugMsg( "Set extents to: " + layerExtent.toString() );
28012799
}
28022800

src/providers/postgres/qgspostgresprovider.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,8 @@ class QgsPostgresProvider : public QgsVectorDataProvider
581581
ref( 1 ),
582582
openCursors( 0 ),
583583
conn( connection ),
584-
gotPostgisVersion( false )
584+
gotPostgisVersion( false ),
585+
mHasNoExtentEstimate( false )
585586
{
586587
}
587588

@@ -603,10 +604,19 @@ class QgsPostgresProvider : public QgsVectorDataProvider
603604
//! major PostgreSQL version
604605
int majorVersion() { return postgisVersionMajor; }
605606

606-
// run a query and free result buffer
607+
//! PostgreSQL version
608+
int pgVersion() { return postgresqlVersion; }
609+
610+
//! has PostGIS no extent estimate?
611+
bool hasNoExtentEstimate() { return mHasNoExtentEstimate; }
612+
613+
//! PostGIS does not have a extent estimate
614+
void setNoExtentEstimate( bool flag = true ) { mHasNoExtentEstimate = flag; }
615+
616+
//! run a query and free result buffer
607617
bool PQexecNR( QString query );
608618

609-
// cursor handling
619+
//! cursor handling
610620
bool openCursor( QString cursorName, QString declare );
611621
bool closeCursor( QString cursorName );
612622

@@ -643,6 +653,9 @@ class QgsPostgresProvider : public QgsVectorDataProvider
643653
//! Are postgisVersionMajor, postgisVersionMinor, geosAvailable, gistAvailable, projAvailable valid?
644654
bool gotPostgisVersion;
645655

656+
//! PostgreSQL version
657+
int postgresqlVersion;
658+
646659
//! PostGIS major version
647660
int postgisVersionMajor;
648661

@@ -658,6 +671,9 @@ class QgsPostgresProvider : public QgsVectorDataProvider
658671
//! encode wkb in hex
659672
bool mUseWkbHex;
660673

674+
//! PostGIS doesn't have extent estimates
675+
bool mHasNoExtentEstimate;
676+
661677
static QMap<QString, Conn *> connectionsRW;
662678
static QMap<QString, Conn *> connectionsRO;
663679
static QMap<QString, QString> passwordCache;

0 commit comments

Comments
 (0)