Skip to content

Commit

Permalink
fix #4470 and #7889
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed May 24, 2013
1 parent 8742a9e commit f990886
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/providers/postgres/qgscolumntypethread.cpp
Expand Up @@ -77,7 +77,7 @@ void QgsGeomColumnTypeThread::run()

if ( !layerProperty.geometryColName.isNull() &&
( layerProperty.types.value( 0, QGis::WKBUnknown ) == QGis::WKBUnknown ||
layerProperty.srids.value( 0, 0 ) == 0 ) )
layerProperty.srids.value( 0, 0 ) <= 0 ) )
{
if ( dontResolveType )
{
Expand Down
6 changes: 3 additions & 3 deletions src/providers/postgres/qgspgtablemodel.cpp
Expand Up @@ -56,7 +56,7 @@ void QgsPgTableModel::addTableEntry( QgsPostgresLayerProperty layerProperty )
wkbType = QGis::WKBNoGeometry;
}

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

QStandardItem *schemaNameItem = new QStandardItem( layerProperty.schemaName );
QStandardItem *typeItem = new QStandardItem( iconForWkbType( wkbType ), wkbType == QGis::WKBUnknown ? tr( "Select..." ) : QgsPostgresConn::displayStringForWkbType( wkbType ) );
Expand All @@ -70,7 +70,7 @@ void QgsPgTableModel::addTableEntry( QgsPostgresLayerProperty layerProperty )
QStandardItem *tableItem = new QStandardItem( layerProperty.tableName );
QStandardItem *geomItem = new QStandardItem( layerProperty.geometryColName );
QStandardItem *sridItem = new QStandardItem( wkbType != QGis::WKBNoGeometry ? QString::number( srid ) : "" );
sridItem->setEditable( wkbType != QGis::WKBNoGeometry && srid == 0 );
sridItem->setEditable( wkbType != QGis::WKBNoGeometry && srid < 0 );
if ( sridItem->isEditable() )
{
sridItem->setText( tr( "Enter..." ) );
Expand Down Expand Up @@ -250,7 +250,7 @@ bool QgsPgTableModel::setData( const QModelIndex &idx, const QVariant &value, in
if ( ok && geomType != QGis::WKBNoGeometry )
{
int srid = idx.sibling( idx.row(), dbtmSrid ).data().toInt( &ok );
ok &= srid != 0;
ok &= srid >= 0;
}

QStringList pkCols = idx.sibling( idx.row(), dbtmPkCol ).data( Qt::UserRole + 1 ).toStringList();
Expand Down
4 changes: 3 additions & 1 deletion src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -380,6 +380,8 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
QString type = result.PQgetvalue( idx, 3 );
QString srid = result.PQgetvalue( idx, 4 );
QString relkind = result.PQgetvalue( idx, 5 );
if ( srid.isEmpty() || srid == "0" )

This comment has been minimized.

Copy link
@strk

strk May 25, 2013

Contributor

I guess the check here should be for srid <= 0, as PostGIS 1.x will use -1 for unknown.

srid = "-1";

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

layerProperty.types = QList<QGis::WkbType>() << QGis::WKBUnknown;
layerProperty.srids = QList<int>() << 0;
layerProperty.srids = QList<int>() << -1;
layerProperty.schemaName = schema;
layerProperty.tableName = table;
layerProperty.geometryColName = QString::null;
Expand Down
58 changes: 27 additions & 31 deletions src/providers/postgres/qgspostgresfeatureiterator.cpp
Expand Up @@ -232,47 +232,43 @@ bool QgsPostgresFeatureIterator::close()
QString QgsPostgresFeatureIterator::whereClauseRect()
{
QgsRectangle rect = mRequest.filterRect();
QString whereClause;
if ( P->mSpatialColType == sctGeography )
{
rect = QgsRectangle( -180.0, -90.0, 180.0, 90.0 ).intersect( &rect );
if ( !rect.isFinite() )
whereClause = "false";
return "false";
}

if ( whereClause.isEmpty() )
QString qBox;
if ( P->mConnectionRO->majorVersion() < 2 )
{
QString qBox;
if ( P->mConnectionRO->majorVersion() < 2 )
{
qBox = QString( "setsrid('BOX3D(%1)'::box3d,%2)" )
.arg( rect.asWktCoordinates() )
.arg( P->mRequestedSrid.isEmpty() ? P->mDetectedSrid : P->mRequestedSrid );
}
else
{
qBox = QString( "st_makeenvelope(%1,%2,%3,%4,%5)" )
.arg( rect.xMinimum(), 0, 'f', 16 )
.arg( rect.yMinimum(), 0, 'f', 16 )
.arg( rect.xMaximum(), 0, 'f', 16 )
.arg( rect.yMaximum(), 0, 'f', 16 )
.arg( P->mRequestedSrid.isEmpty() ? P->mDetectedSrid : P->mRequestedSrid );
}
qBox = QString( "setsrid('BOX3D(%1)'::box3d,%2)" )
.arg( rect.asWktCoordinates() )
.arg( P->mRequestedSrid.isEmpty() ? P->mDetectedSrid : P->mRequestedSrid );
}
else
{
qBox = QString( "st_makeenvelope(%1,%2,%3,%4,%5)" )
.arg( rect.xMinimum(), 0, 'f', 16 )
.arg( rect.yMinimum(), 0, 'f', 16 )
.arg( rect.xMaximum(), 0, 'f', 16 )
.arg( rect.yMaximum(), 0, 'f', 16 )
.arg( P->mRequestedSrid.isEmpty() ? P->mDetectedSrid : P->mRequestedSrid );
}

whereClause = QString( "%1 && %2" )
.arg( P->quotedIdentifier( P->mGeometryColumn ) )
.arg( qBox );
if ( mRequest.flags() & QgsFeatureRequest::ExactIntersect )
{
whereClause += QString( " AND %1(%2%3,%4)" )
.arg( P->mConnectionRO->majorVersion() < 2 ? "intersects" : "st_intersects" )
.arg( P->quotedIdentifier( P->mGeometryColumn ) )
.arg( P->mSpatialColType == sctGeography ? "::geometry" : "" )
.arg( qBox );
}
QString whereClause = QString( "%1 && %2" )
.arg( P->quotedIdentifier( P->mGeometryColumn ) )
.arg( qBox );
if ( mRequest.flags() & QgsFeatureRequest::ExactIntersect )
{
whereClause += QString( " AND %1(%2%3,%4)" )
.arg( P->mConnectionRO->majorVersion() < 2 ? "intersects" : "st_intersects" )
.arg( P->quotedIdentifier( P->mGeometryColumn ) )
.arg( P->mSpatialColType == sctGeography ? "::geometry" : "" )
.arg( qBox );
}

if ( !P->mRequestedSrid.isEmpty() && P->mRequestedSrid != P->mDetectedSrid )
if ( !P->mRequestedSrid.isEmpty() && ( P->mRequestedSrid != P->mDetectedSrid || P->mRequestedSrid.toInt() == 0 ) )
{
whereClause += QString( " AND %1(%2%3)=%4" )
.arg( P->mConnectionRO->majorVersion() < 2 ? "srid" : "st_srid" )
Expand Down
54 changes: 36 additions & 18 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -516,6 +516,35 @@ QString QgsPostgresProvider::whereClause( QgsFeatureId featureId ) const
return whereClause;
}

QString QgsPostgresProvider::filterWhereClause() const
{
QString where;
QString delim = " WHERE ";

if ( !mSqlWhereClause.isEmpty() )
{
where += delim + "(" + mSqlWhereClause + ")";
delim = " AND ";
}

if ( !mRequestedSrid.isEmpty() && ( mRequestedSrid != mDetectedSrid || mRequestedSrid.toInt() == 0 ) )
{
where += delim + QString( "%1(%2%3)=%4" )
.arg( mConnectionRO->majorVersion() < 2 ? "srid" : "st_srid" )
.arg( quotedIdentifier( mGeometryColumn ) )
.arg( mSpatialColType == sctGeography ? "::geography" : "" )
.arg( mRequestedSrid );
delim = " AND ";
}

if ( mRequestedGeomType != QGis::WKBUnknown && mRequestedGeomType != mDetectedGeomType )
{
where += delim + QgsPostgresConn::postgisTypeFilter( mGeometryColumn, mRequestedGeomType, mSpatialColType == sctGeography );
delim = " AND ";
}

return where;
}

void QgsPostgresProvider::setExtent( QgsRectangle& newExtent )
{
Expand Down Expand Up @@ -1128,14 +1157,10 @@ bool QgsPostgresProvider::uniqueData( QString query, QString colName )
{
Q_UNUSED( query );
// Check to see if the given column contains unique data
QString sql = QString( "SELECT count(distinct %1)=count(%1) FROM %2" )
QString sql = QString( "SELECT count(distinct %1)=count(%1) FROM %2%3" )
.arg( quotedIdentifier( colName ) )
.arg( mQuery );

if ( !mSqlWhereClause.isEmpty() )
{
sql += " WHERE " + mSqlWhereClause;
}
.arg( mQuery )
.arg( filterWhereClause() );

QgsPostgresResult unique = mConnectionRO->PQexec( sql );

Expand Down Expand Up @@ -2323,12 +2348,7 @@ long QgsPostgresProvider::featureCount() const
}
else
{
sql = QString( "SELECT count(*) FROM %1" ).arg( mQuery );

if ( !mSqlWhereClause.isEmpty() )
{
sql += " WHERE " + mSqlWhereClause;
}
sql = QString( "SELECT count(*) FROM %1%2" ).arg( mQuery ).arg( filterWhereClause() );
}

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

if ( ext.isEmpty() )
{
sql = QString( "SELECT %1(%2) FROM %3" )
sql = QString( "SELECT %1(%2) FROM %3%4" )
.arg( mConnectionRO->majorVersion() < 2 ? "extent" : "st_extent" )
.arg( quotedIdentifier( mGeometryColumn ) )
.arg( mQuery );

if ( !mSqlWhereClause.isEmpty() )
sql += QString( " WHERE %1" ).arg( mSqlWhereClause );
.arg( mQuery )
.arg( filterWhereClause() );

result = mConnectionRO->PQexec( sql );
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
Expand Down
1 change: 1 addition & 0 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -309,6 +309,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
*/
QString pkParamWhereClause( int offset, const char* alias = 0 ) const;
QString whereClause( QgsFeatureId featureId ) const;
QString filterWhereClause() const;

bool hasSufficientPermsAndCapabilities();

Expand Down

1 comment on commit f990886

@strk
Copy link
Contributor

@strk strk commented on f990886 May 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.