Skip to content

Commit ebc93b7

Browse files
author
jef
committed
fix #2681
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13406 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 89e80b3 commit ebc93b7

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

src/providers/postgres/qgspostgresprovider.cpp

+51-11
Original file line numberDiff line numberDiff line change
@@ -3003,27 +3003,67 @@ bool QgsPostgresProvider::getGeometryDetails()
30033003
Result result;
30043004
QString sql;
30053005

3006-
if ( !isQuery )
3006+
QString schemaName = mSchemaName;
3007+
QString tableName = mTableName;
3008+
QString geomCol = geometryColumn;
3009+
3010+
if ( isQuery )
30073011
{
3008-
sql = QString( "select type,srid from geometry_columns"
3009-
" where f_table_name=%1 and f_geometry_column=%2 and f_table_schema=%3" )
3010-
.arg( quotedValue( mTableName ) )
3011-
.arg( quotedValue( geometryColumn ) )
3012-
.arg( quotedValue( mSchemaName ) );
3012+
sql = QString( "select %1 from %2 limit 0" ).arg( quotedIdentifier( geometryColumn ) ).arg( mQuery );
30133013

30143014
QgsDebugMsg( "Getting geometry column: " + sql );
30153015

30163016
Result result = connectionRO->PQexec( sql );
3017+
if ( PGRES_TUPLES_OK == PQresultStatus( result ) )
3018+
{
3019+
Oid tableOid = PQftable( result, 0 );
3020+
int column = PQftablecol( result, 0 );
30173021

3018-
QgsDebugMsg( "geometry column query returned " + QString::number( PQntuples( result ) ) );
3022+
result = connectionRO->PQexec( sql );
3023+
if ( tableOid >= 0 && PGRES_TUPLES_OK == PQresultStatus( result ) )
3024+
{
3025+
sql = QString( "SELECT pg_namespace.nspname,pg_class.relname FROM pg_class,pg_namespace WHERE pg_class.relnamespace=pg_namespace.oid AND pg_class.oid=%1" ).arg( tableOid );
3026+
result = connectionRO->PQexec( sql );
30193027

3020-
if ( PQntuples( result ) > 0 )
3021-
{
3022-
fType = QString::fromUtf8( PQgetvalue( result, 0, 0 ) );
3023-
srid = QString::fromUtf8( PQgetvalue( result, 0, 1 ) );
3028+
if ( PGRES_TUPLES_OK == PQresultStatus( result ) && 1 == PQntuples( result ) )
3029+
{
3030+
schemaName = QString::fromUtf8( PQgetvalue( result, 0, 0 ) );
3031+
tableName = QString::fromUtf8( PQgetvalue( result, 0, 1 ) );
3032+
3033+
sql = QString( "SELECT attname FROM pg_attribute WHERE attrelid=%1 AND attnum=%2" ).arg( tableOid ).arg( column );
3034+
result = connectionRO->PQexec( sql );
3035+
if ( PGRES_TUPLES_OK == PQresultStatus( result ) && 1 == PQntuples( result ) )
3036+
{
3037+
geomCol = QString::fromUtf8( PQgetvalue( result, 0, 0 ) );
3038+
}
3039+
else
3040+
{
3041+
schemaName = mSchemaName;
3042+
tableName = mTableName;
3043+
}
3044+
}
3045+
}
30243046
}
30253047
}
30263048

3049+
sql = QString( "select type,srid from geometry_columns"
3050+
" where f_table_name=%1 and f_geometry_column=%2 and f_table_schema=%3" )
3051+
.arg( quotedValue( tableName ) )
3052+
.arg( quotedValue( geomCol ) )
3053+
.arg( quotedValue( schemaName ) );
3054+
3055+
QgsDebugMsg( "Getting geometry column: " + sql );
3056+
3057+
result = connectionRO->PQexec( sql );
3058+
3059+
QgsDebugMsg( "geometry column query returned " + QString::number( PQntuples( result ) ) );
3060+
3061+
if ( PQntuples( result ) > 0 )
3062+
{
3063+
fType = QString::fromUtf8( PQgetvalue( result, 0, 0 ) );
3064+
srid = QString::fromUtf8( PQgetvalue( result, 0, 1 ) );
3065+
}
3066+
30273067
if ( srid.isEmpty() || fType.isEmpty() )
30283068
{
30293069
// Didn't find what we need in the geometry_columns table, so

0 commit comments

Comments
 (0)