Skip to content

Commit 18ac13c

Browse files
committed
postgres provider: fix lookup of aliased columns
1 parent 9d05631 commit 18ac13c

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ bool QgsPostgresProvider::loadFields()
637637
int fieldPrec = -1;
638638
QString fieldComment( "" );
639639
int tableoid = result.PQftable( i );
640+
int attnum = result.PQftablecol( i );
640641

641642
sql = QString( "SELECT typname,typtype,typelem,typlen FROM pg_type WHERE oid=%1" ).arg( typOid );
642643
// just oid; needs more work to support array type
@@ -652,22 +653,19 @@ bool QgsPostgresProvider::loadFields()
652653
QString formattedFieldType;
653654
if ( tableoid > 0 )
654655
{
655-
sql = QString( "SELECT attnum,pg_catalog.format_type(atttypid,atttypmod) FROM pg_attribute WHERE attrelid=%1 AND attname=%2" )
656-
.arg( tableoid ).arg( quotedValue( fieldName ) );
656+
sql = QString( "SELECT pg_catalog.format_type(atttypid,atttypmod) FROM pg_attribute WHERE attrelid=%1 AND attnum=%2" )
657+
.arg( tableoid ).arg( quotedValue( attnum ) );
657658

658659
QgsPostgresResult tresult = mConnectionRO->PQexec( sql );
659-
QString attnum = tresult.PQgetvalue( 0, 0 );
660-
formattedFieldType = tresult.PQgetvalue( 0, 1 );
660+
if ( tresult.PQntuples() > 0 )
661+
formattedFieldType = tresult.PQgetvalue( 0, 0 );
661662

662-
if ( !attnum.isEmpty() )
663-
{
664-
sql = QString( "SELECT description FROM pg_description WHERE objoid=%1 AND objsubid=%2" )
665-
.arg( tableoid ).arg( attnum );
663+
sql = QString( "SELECT description FROM pg_description WHERE objoid=%1 AND objsubid=%2" )
664+
.arg( tableoid ).arg( attnum );
666665

667-
tresult = mConnectionRO->PQexec( sql );
668-
if ( tresult.PQntuples() > 0 )
669-
fieldComment = tresult.PQgetvalue( 0, 0 );
670-
}
666+
tresult = mConnectionRO->PQexec( sql );
667+
if ( tresult.PQntuples() > 0 )
668+
fieldComment = tresult.PQgetvalue( 0, 0 );
671669
}
672670

673671
QVariant::Type fieldType;

0 commit comments

Comments
 (0)