Skip to content

Commit a9cccd2

Browse files
committedMay 27, 2015
postgres provider: cast result of a subquery in min/max/uniqueValue(s) (fixes #12630; followup bf56457 ff)
(cherry picked from commit e509fc5)
1 parent 91b1785 commit a9cccd2

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
@@ -1234,15 +1234,17 @@ QVariant QgsPostgresProvider::minimumValue( int index )
12341234
{
12351235
// get the field name
12361236
const QgsField &fld = field( index );
1237-
QString sql = QString( "SELECT %1 FROM %2" )
1238-
.arg( connectionRO()->fieldExpression( fld, "min(%1)" ) )
1237+
QString sql = QString( "SELECT min(%1) AS %1 FROM %2" )
1238+
.arg( quotedIdentifier( fld.name() ) )
12391239
.arg( mQuery );
12401240

12411241
if ( !mSqlWhereClause.isEmpty() )
12421242
{
12431243
sql += QString( " WHERE %1" ).arg( mSqlWhereClause );
12441244
}
12451245

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

12681270
if ( !mSqlWhereClause.isEmpty() )
12691271
{
12701272
sql += QString( " WHERE %1" ).arg( mSqlWhereClause );
12711273
}
12721274

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

12751277
if ( limit >= 0 )
12761278
{
12771279
sql += QString( " LIMIT %1" ).arg( limit );
12781280
}
12791281

1282+
sql = QString( "SELECT %1 FROM (%2) foo" ).arg( connectionRO()->fieldExpression( fld ) ).arg( sql );
1283+
12801284
QgsPostgresResult res = connectionRO()->PQexec( sql );
12811285
if ( res.PQresultStatus() == PGRES_TUPLES_OK )
12821286
{
@@ -1406,15 +1410,17 @@ QVariant QgsPostgresProvider::maximumValue( int index )
14061410
{
14071411
// get the field name
14081412
const QgsField &fld = field( index );
1409-
QString sql = QString( "SELECT %1 FROM %2" )
1410-
.arg( connectionRO()->fieldExpression( fld, "max(%1)" ) )
1413+
QString sql = QString( "SELECT max(%1) AS %1 FROM %2" )
1414+
.arg( quotedIdentifier( fld.name() ) )
14111415
.arg( mQuery );
14121416

14131417
if ( !mSqlWhereClause.isEmpty() )
14141418
{
14151419
sql += QString( " WHERE %1" ).arg( mSqlWhereClause );
14161420
}
14171421

1422+
sql = QString( "SELECT %1 FROM (%2) foo" ).arg( connectionRO()->fieldExpression( fld ) ).arg( sql );
1423+
14181424
QgsPostgresResult rmax = connectionRO()->PQexec( sql );
14191425
return convertValue( fld.type(), rmax.PQgetvalue( 0, 0 ) );
14201426
}

0 commit comments

Comments
 (0)
Please sign in to comment.