Skip to content

Commit f990886

Browse files
committedMay 24, 2013
fix #4470 and #7889
1 parent 8742a9e commit f990886

File tree

6 files changed

+71
-54
lines changed

6 files changed

+71
-54
lines changed
 

‎src/providers/postgres/qgscolumntypethread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void QgsGeomColumnTypeThread::run()
7777

7878
if ( !layerProperty.geometryColName.isNull() &&
7979
( layerProperty.types.value( 0, QGis::WKBUnknown ) == QGis::WKBUnknown ||
80-
layerProperty.srids.value( 0, 0 ) == 0 ) )
80+
layerProperty.srids.value( 0, 0 ) <= 0 ) )
8181
{
8282
if ( dontResolveType )
8383
{

‎src/providers/postgres/qgspgtablemodel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void QgsPgTableModel::addTableEntry( QgsPostgresLayerProperty layerProperty )
5656
wkbType = QGis::WKBNoGeometry;
5757
}
5858

59-
bool selectable = wkbType == QGis::WKBNoGeometry || ( wkbType != QGis::WKBUnknown && srid != 0 );
59+
bool selectable = wkbType == QGis::WKBNoGeometry || ( wkbType != QGis::WKBUnknown && srid > 0 );
6060

6161
QStandardItem *schemaNameItem = new QStandardItem( layerProperty.schemaName );
6262
QStandardItem *typeItem = new QStandardItem( iconForWkbType( wkbType ), wkbType == QGis::WKBUnknown ? tr( "Select..." ) : QgsPostgresConn::displayStringForWkbType( wkbType ) );
@@ -70,7 +70,7 @@ void QgsPgTableModel::addTableEntry( QgsPostgresLayerProperty layerProperty )
7070
QStandardItem *tableItem = new QStandardItem( layerProperty.tableName );
7171
QStandardItem *geomItem = new QStandardItem( layerProperty.geometryColName );
7272
QStandardItem *sridItem = new QStandardItem( wkbType != QGis::WKBNoGeometry ? QString::number( srid ) : "" );
73-
sridItem->setEditable( wkbType != QGis::WKBNoGeometry && srid == 0 );
73+
sridItem->setEditable( wkbType != QGis::WKBNoGeometry && srid < 0 );
7474
if ( sridItem->isEditable() )
7575
{
7676
sridItem->setText( tr( "Enter..." ) );
@@ -250,7 +250,7 @@ bool QgsPgTableModel::setData( const QModelIndex &idx, const QVariant &value, in
250250
if ( ok && geomType != QGis::WKBNoGeometry )
251251
{
252252
int srid = idx.sibling( idx.row(), dbtmSrid ).data().toInt( &ok );
253-
ok &= srid != 0;
253+
ok &= srid >= 0;
254254
}
255255

256256
QStringList pkCols = idx.sibling( idx.row(), dbtmPkCol ).data( Qt::UserRole + 1 ).toStringList();

‎src/providers/postgres/qgspostgresconn.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
380380
QString type = result.PQgetvalue( idx, 3 );
381381
QString srid = result.PQgetvalue( idx, 4 );
382382
QString relkind = result.PQgetvalue( idx, 5 );
383+
if ( srid.isEmpty() || srid == "0" )
Code has comments. Press enter to view.
384+
srid = "-1";
383385

384386
QgsDebugMsg( QString( "%1 : %2.%3.%4: %5 %6 %7" )
385387
.arg( gtableName )
@@ -570,7 +572,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
570572
QgsDebugMsg( QString( "%1.%2: %3" ).arg( schema ).arg( table ).arg( relkind ) );
571573

572574
layerProperty.types = QList<QGis::WkbType>() << QGis::WKBUnknown;
573-
layerProperty.srids = QList<int>() << 0;
575+
layerProperty.srids = QList<int>() << -1;
574576
layerProperty.schemaName = schema;
575577
layerProperty.tableName = table;
576578
layerProperty.geometryColName = QString::null;

‎src/providers/postgres/qgspostgresfeatureiterator.cpp

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -232,47 +232,43 @@ bool QgsPostgresFeatureIterator::close()
232232
QString QgsPostgresFeatureIterator::whereClauseRect()
233233
{
234234
QgsRectangle rect = mRequest.filterRect();
235-
QString whereClause;
236235
if ( P->mSpatialColType == sctGeography )
237236
{
238237
rect = QgsRectangle( -180.0, -90.0, 180.0, 90.0 ).intersect( &rect );
239238
if ( !rect.isFinite() )
240-
whereClause = "false";
239+
return "false";
241240
}
242241

243-
if ( whereClause.isEmpty() )
242+
QString qBox;
243+
if ( P->mConnectionRO->majorVersion() < 2 )
244244
{
245-
QString qBox;
246-
if ( P->mConnectionRO->majorVersion() < 2 )
247-
{
248-
qBox = QString( "setsrid('BOX3D(%1)'::box3d,%2)" )
249-
.arg( rect.asWktCoordinates() )
250-
.arg( P->mRequestedSrid.isEmpty() ? P->mDetectedSrid : P->mRequestedSrid );
251-
}
252-
else
253-
{
254-
qBox = QString( "st_makeenvelope(%1,%2,%3,%4,%5)" )
255-
.arg( rect.xMinimum(), 0, 'f', 16 )
256-
.arg( rect.yMinimum(), 0, 'f', 16 )
257-
.arg( rect.xMaximum(), 0, 'f', 16 )
258-
.arg( rect.yMaximum(), 0, 'f', 16 )
259-
.arg( P->mRequestedSrid.isEmpty() ? P->mDetectedSrid : P->mRequestedSrid );
260-
}
245+
qBox = QString( "setsrid('BOX3D(%1)'::box3d,%2)" )
246+
.arg( rect.asWktCoordinates() )
247+
.arg( P->mRequestedSrid.isEmpty() ? P->mDetectedSrid : P->mRequestedSrid );
248+
}
249+
else
250+
{
251+
qBox = QString( "st_makeenvelope(%1,%2,%3,%4,%5)" )
252+
.arg( rect.xMinimum(), 0, 'f', 16 )
253+
.arg( rect.yMinimum(), 0, 'f', 16 )
254+
.arg( rect.xMaximum(), 0, 'f', 16 )
255+
.arg( rect.yMaximum(), 0, 'f', 16 )
256+
.arg( P->mRequestedSrid.isEmpty() ? P->mDetectedSrid : P->mRequestedSrid );
257+
}
261258

262-
whereClause = QString( "%1 && %2" )
263-
.arg( P->quotedIdentifier( P->mGeometryColumn ) )
264-
.arg( qBox );
265-
if ( mRequest.flags() & QgsFeatureRequest::ExactIntersect )
266-
{
267-
whereClause += QString( " AND %1(%2%3,%4)" )
268-
.arg( P->mConnectionRO->majorVersion() < 2 ? "intersects" : "st_intersects" )
269-
.arg( P->quotedIdentifier( P->mGeometryColumn ) )
270-
.arg( P->mSpatialColType == sctGeography ? "::geometry" : "" )
271-
.arg( qBox );
272-
}
259+
QString whereClause = QString( "%1 && %2" )
260+
.arg( P->quotedIdentifier( P->mGeometryColumn ) )
261+
.arg( qBox );
262+
if ( mRequest.flags() & QgsFeatureRequest::ExactIntersect )
263+
{
264+
whereClause += QString( " AND %1(%2%3,%4)" )
265+
.arg( P->mConnectionRO->majorVersion() < 2 ? "intersects" : "st_intersects" )
266+
.arg( P->quotedIdentifier( P->mGeometryColumn ) )
267+
.arg( P->mSpatialColType == sctGeography ? "::geometry" : "" )
268+
.arg( qBox );
273269
}
274270

275-
if ( !P->mRequestedSrid.isEmpty() && P->mRequestedSrid != P->mDetectedSrid )
271+
if ( !P->mRequestedSrid.isEmpty() && ( P->mRequestedSrid != P->mDetectedSrid || P->mRequestedSrid.toInt() == 0 ) )
276272
{
277273
whereClause += QString( " AND %1(%2%3)=%4" )
278274
.arg( P->mConnectionRO->majorVersion() < 2 ? "srid" : "st_srid" )

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,35 @@ QString QgsPostgresProvider::whereClause( QgsFeatureId featureId ) const
516516
return whereClause;
517517
}
518518

519+
QString QgsPostgresProvider::filterWhereClause() const
520+
{
521+
QString where;
522+
QString delim = " WHERE ";
523+
524+
if ( !mSqlWhereClause.isEmpty() )
525+
{
526+
where += delim + "(" + mSqlWhereClause + ")";
527+
delim = " AND ";
528+
}
529+
530+
if ( !mRequestedSrid.isEmpty() && ( mRequestedSrid != mDetectedSrid || mRequestedSrid.toInt() == 0 ) )
531+
{
532+
where += delim + QString( "%1(%2%3)=%4" )
533+
.arg( mConnectionRO->majorVersion() < 2 ? "srid" : "st_srid" )
534+
.arg( quotedIdentifier( mGeometryColumn ) )
535+
.arg( mSpatialColType == sctGeography ? "::geography" : "" )
536+
.arg( mRequestedSrid );
537+
delim = " AND ";
538+
}
539+
540+
if ( mRequestedGeomType != QGis::WKBUnknown && mRequestedGeomType != mDetectedGeomType )
541+
{
542+
where += delim + QgsPostgresConn::postgisTypeFilter( mGeometryColumn, mRequestedGeomType, mSpatialColType == sctGeography );
543+
delim = " AND ";
544+
}
545+
546+
return where;
547+
}
519548

520549
void QgsPostgresProvider::setExtent( QgsRectangle& newExtent )
521550
{
@@ -1128,14 +1157,10 @@ bool QgsPostgresProvider::uniqueData( QString query, QString colName )
11281157
{
11291158
Q_UNUSED( query );
11301159
// Check to see if the given column contains unique data
1131-
QString sql = QString( "SELECT count(distinct %1)=count(%1) FROM %2" )
1160+
QString sql = QString( "SELECT count(distinct %1)=count(%1) FROM %2%3" )
11321161
.arg( quotedIdentifier( colName ) )
1133-
.arg( mQuery );
1134-
1135-
if ( !mSqlWhereClause.isEmpty() )
1136-
{
1137-
sql += " WHERE " + mSqlWhereClause;
1138-
}
1162+
.arg( mQuery )
1163+
.arg( filterWhereClause() );
11391164

11401165
QgsPostgresResult unique = mConnectionRO->PQexec( sql );
11411166

@@ -2323,12 +2348,7 @@ long QgsPostgresProvider::featureCount() const
23232348
}
23242349
else
23252350
{
2326-
sql = QString( "SELECT count(*) FROM %1" ).arg( mQuery );
2327-
2328-
if ( !mSqlWhereClause.isEmpty() )
2329-
{
2330-
sql += " WHERE " + mSqlWhereClause;
2331-
}
2351+
sql = QString( "SELECT count(*) FROM %1%2" ).arg( mQuery ).arg( filterWhereClause() );
23322352
}
23332353

23342354
QgsPostgresResult result = mConnectionRO->PQexec( sql );
@@ -2410,13 +2430,11 @@ QgsRectangle QgsPostgresProvider::extent()
24102430

24112431
if ( ext.isEmpty() )
24122432
{
2413-
sql = QString( "SELECT %1(%2) FROM %3" )
2433+
sql = QString( "SELECT %1(%2) FROM %3%4" )
24142434
.arg( mConnectionRO->majorVersion() < 2 ? "extent" : "st_extent" )
24152435
.arg( quotedIdentifier( mGeometryColumn ) )
2416-
.arg( mQuery );
2417-
2418-
if ( !mSqlWhereClause.isEmpty() )
2419-
sql += QString( " WHERE %1" ).arg( mSqlWhereClause );
2436+
.arg( mQuery )
2437+
.arg( filterWhereClause() );
24202438

24212439
result = mConnectionRO->PQexec( sql );
24222440
if ( result.PQresultStatus() != PGRES_TUPLES_OK )

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
309309
*/
310310
QString pkParamWhereClause( int offset, const char* alias = 0 ) const;
311311
QString whereClause( QgsFeatureId featureId ) const;
312+
QString filterWhereClause() const;
312313

313314
bool hasSufficientPermsAndCapabilities();
314315

1 commit comments

Comments
 (1)

strk commented on May 25, 2013

@strk
Contributor

Does not fix http://hub.qgis.org/issues/7889 in that columns with SRID=0 cannot be added to the map.

Please sign in to comment.