Index: src/providers/postgres/qgspostgresprovider.cpp =================================================================== --- src/providers/postgres/qgspostgresprovider.cpp (revision 13405) +++ src/providers/postgres/qgspostgresprovider.cpp (working copy) @@ -853,18 +853,13 @@ QString fieldElem = QString::fromUtf8( PQgetvalue( oidResult, 0, 2 ) ); int fieldSize = QString::fromUtf8( PQgetvalue( oidResult, 0, 3 ) ).toInt(); - if ( tableoid >= 0 ) + if ( tableoid > 0 ) { - sql = QString( "SELECT attnum FROM pg_attribute WHERE attrelid=%1 AND attname=%2" ) + sql = QString( "SELECT d.description FROM pg_description AS d JOIN pg_attribute AS a ON d.objoid=a.attrelid" + " WHERE d.objoid=%1 AND a.attname=%2 AND objsubid=a.attnum" ) .arg( tableoid ).arg( quotedValue( fieldName ) ); Result tresult = connectionRO->PQexec( sql ); - QString attnum = QString::fromUtf8( PQgetvalue( tresult, 0, 0 ) ); - - sql = QString( "SELECT description FROM pg_description WHERE objoid=%1 AND objsubid=%2" ) - .arg( tableoid ).arg( attnum ); - - tresult = connectionRO->PQexec( sql ); if ( PQntuples( tresult ) > 0 ) fieldComment = QString::fromUtf8( PQgetvalue( tresult, 0, 0 ) ); } @@ -2940,7 +2935,7 @@ for ( i = 0; i < PQnfields( res ); i++ ) { int tableoid = PQftable( res, i ); - if ( tableoid >= 0 ) + if ( tableoid > 0 ) { oidValue = QString::number( tableoid ); break; @@ -3000,16 +2995,49 @@ valid = false; QStringList log; + QString tableName; + QString schemaName; + Result result; QString sql; - if ( !isQuery ) + // get the geometry column's schema and table + if ( isQuery ) { + sql = QString( "select %1 from %2 LIMIT 0" ) + .arg( quotedIdentifier( geometryColumn ) ) + .arg( mQuery ); + + result = connectionRO->PQexec( sql ); + + int tableoid = PQftable( result, 0 ); + if ( tableoid > 0 ) + { + sql = QString( "SELECT t.relname, n.nspname FROM pg_class AS t JOIN pg_namespace AS n ON t.relnamespace = n.oid" + " WHERE t.oid=%1" ) + .arg( tableoid ); + + result = connectionRO->PQexec( sql ); + if ( PQntuples( result ) > 0 ) + { + tableName = QString::fromUtf8( PQgetvalue( result, 0, 0 ) ); + schemaName = QString::fromUtf8( PQgetvalue( result, 0, 1 ) ); + } + } + } + else + { + tableName = mTableName; + schemaName = mSchemaName; + } + + if ( !tableName.isEmpty() ) + { sql = QString( "select type,srid from geometry_columns" " where f_table_name=%1 and f_geometry_column=%2 and f_table_schema=%3" ) - .arg( quotedValue( mTableName ) ) + .arg( quotedValue( tableName ) ) .arg( quotedValue( geometryColumn ) ) - .arg( quotedValue( mSchemaName ) ); + .arg( quotedValue( schemaName ) ); QgsDebugMsg( "Getting geometry column: " + sql );