Skip to content

Commit

Permalink
postgres provider: cast result of a subquery in min/max/uniqueValue(s) (
Browse files Browse the repository at this point in the history
fixes #12630; followup bf56457 ff)
  • Loading branch information
jef-n committed May 10, 2015
1 parent e371219 commit e509fc5
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,15 +1279,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 ) );
}
Expand All @@ -1307,21 +1309,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 )
{
Expand Down Expand Up @@ -1451,15 +1455,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 ) );
}
Expand Down

0 comments on commit e509fc5

Please sign in to comment.