Skip to content

Commit e509fc5

Browse files
committedMay 10, 2015
postgres provider: cast result of a subquery in min/max/uniqueValue(s) (fixes #12630; followup bf56457 ff)
1 parent e371219 commit e509fc5

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed
 

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,15 +1279,17 @@ QVariant QgsPostgresProvider::minimumValue( int index )
12791279
{
12801280
// get the field name
12811281
const QgsField &fld = field( index );
1282-
QString sql = QString( "SELECT %1 FROM %2" )
1283-
.arg( connectionRO()->fieldExpression( fld, "min(%1)" ) )
1282+
QString sql = QString( "SELECT min(%1) AS %1 FROM %2" )
1283+
.arg( quotedIdentifier( fld.name() ) )
12841284
.arg( mQuery );
12851285

12861286
if ( !mSqlWhereClause.isEmpty() )
12871287
{
12881288
sql += QString( " WHERE %1" ).arg( mSqlWhereClause );
12891289
}
12901290

1291+
sql = QString( "SELECT %1 FROM (%2) foo" ).arg( connectionRO()->fieldExpression( fld ) ).arg( sql );
1292+
12911293
QgsPostgresResult rmin = connectionRO()->PQexec( sql );
12921294
return convertValue( fld.type(), rmin.PQgetvalue( 0, 0 ) );
12931295
}
@@ -1307,21 +1309,23 @@ void QgsPostgresProvider::uniqueValues( int index, QList<QVariant> &uniqueValues
13071309
// get the field name
13081310
const QgsField &fld = field( index );
13091311
QString sql = QString( "SELECT DISTINCT %1 FROM %2" )
1310-
.arg( connectionRO()->fieldExpression( fld ) )
1312+
.arg( quotedIdentifier( fld.name() ) )
13111313
.arg( mQuery );
13121314

13131315
if ( !mSqlWhereClause.isEmpty() )
13141316
{
13151317
sql += QString( " WHERE %1" ).arg( mSqlWhereClause );
13161318
}
13171319

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

13201322
if ( limit >= 0 )
13211323
{
13221324
sql += QString( " LIMIT %1" ).arg( limit );
13231325
}
13241326

1327+
sql = QString( "SELECT %1 FROM (%2) foo" ).arg( connectionRO()->fieldExpression( fld ) ).arg( sql );
1328+
13251329
QgsPostgresResult res = connectionRO()->PQexec( sql );
13261330
if ( res.PQresultStatus() == PGRES_TUPLES_OK )
13271331
{
@@ -1451,15 +1455,17 @@ QVariant QgsPostgresProvider::maximumValue( int index )
14511455
{
14521456
// get the field name
14531457
const QgsField &fld = field( index );
1454-
QString sql = QString( "SELECT %1 FROM %2" )
1455-
.arg( connectionRO()->fieldExpression( fld, "max(%1)" ) )
1458+
QString sql = QString( "SELECT max(%1) AS %1 FROM %2" )
1459+
.arg( quotedIdentifier( fld.name() ) )
14561460
.arg( mQuery );
14571461

14581462
if ( !mSqlWhereClause.isEmpty() )
14591463
{
14601464
sql += QString( " WHERE %1" ).arg( mSqlWhereClause );
14611465
}
14621466

1467+
sql = QString( "SELECT %1 FROM (%2) foo" ).arg( connectionRO()->fieldExpression( fld ) ).arg( sql );
1468+
14631469
QgsPostgresResult rmax = connectionRO()->PQexec( sql );
14641470
return convertValue( fld.type(), rmax.PQgetvalue( 0, 0 ) );
14651471
}

0 commit comments

Comments
 (0)