Skip to content
Permalink
Browse files
postgres provider: cast result of a subquery in min/max/uniqueValue(s) (
fixes #12630; followup bf56457 ff)

(cherry picked from commit e509fc5)
  • Loading branch information
jef-n committed May 27, 2015
1 parent 91b1785 commit a9cccd2ca3c4a8bc19ed49f3f3f1def7c96d91a9
Showing with 12 additions and 6 deletions.
  1. +12 −6 src/providers/postgres/qgspostgresprovider.cpp
@@ -1234,15 +1234,17 @@ QVariant QgsPostgresProvider::minimumValue( int index )
{
// get the field name
const QgsField &fld = field( index );
QString sql = QString( "SELECT %1 FROM %2" )
.arg( connectionRO()->fieldExpression( fld, "min(%1)" ) )
QString sql = QString( "SELECT min(%1) AS %1 FROM %2" )
.arg( quotedIdentifier( fld.name() ) )
.arg( mQuery );

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

sql = QString( "SELECT %1 FROM (%2) foo" ).arg( connectionRO()->fieldExpression( fld ) ).arg( sql );

QgsPostgresResult rmin = connectionRO()->PQexec( sql );
return convertValue( fld.type(), rmin.PQgetvalue( 0, 0 ) );
}
@@ -1262,21 +1264,23 @@ void QgsPostgresProvider::uniqueValues( int index, QList<QVariant> &uniqueValues
// get the field name
const QgsField &fld = field( index );
QString sql = QString( "SELECT DISTINCT %1 FROM %2" )
.arg( connectionRO()->fieldExpression( fld ) )
.arg( quotedIdentifier( fld.name() ) )
.arg( mQuery );

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

sql += QString( " ORDER BY %1" ).arg( connectionRO()->fieldExpression( fld ) );
sql += QString( " ORDER BY %1" ).arg( quotedIdentifier( fld.name() ) );

if ( limit >= 0 )
{
sql += QString( " LIMIT %1" ).arg( limit );
}

sql = QString( "SELECT %1 FROM (%2) foo" ).arg( connectionRO()->fieldExpression( fld ) ).arg( sql );

QgsPostgresResult res = connectionRO()->PQexec( sql );
if ( res.PQresultStatus() == PGRES_TUPLES_OK )
{
@@ -1406,15 +1410,17 @@ QVariant QgsPostgresProvider::maximumValue( int index )
{
// get the field name
const QgsField &fld = field( index );
QString sql = QString( "SELECT %1 FROM %2" )
.arg( connectionRO()->fieldExpression( fld, "max(%1)" ) )
QString sql = QString( "SELECT max(%1) AS %1 FROM %2" )
.arg( quotedIdentifier( fld.name() ) )
.arg( mQuery );

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

sql = QString( "SELECT %1 FROM (%2) foo" ).arg( connectionRO()->fieldExpression( fld ) ).arg( sql );

QgsPostgresResult rmax = connectionRO()->PQexec( sql );
return convertValue( fld.type(), rmax.PQgetvalue( 0, 0 ) );
}

0 comments on commit a9cccd2

Please sign in to comment.