Skip to content

Commit

Permalink
Merge pull request #2458 from SebDieBln/Postgres_GeomTypeDirect
Browse files Browse the repository at this point in the history
[postgresprovider] Determine geometry type and srid directly from column type
  • Loading branch information
jef-n committed Nov 18, 2015
2 parents f6e4e75 + cfa308d commit 8e5ba3b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/providers/postgres/qgspostgresconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ int QgsPostgresResult::PQftype( int col )
return ::PQftype( mRes, col );
}

int QgsPostgresResult::PQfmod( int col )
{
Q_ASSERT( mRes );
return ::PQfmod( mRes, col );
}

Oid QgsPostgresResult::PQoidValue()
{
Q_ASSERT( mRes );
Expand Down
1 change: 1 addition & 0 deletions src/providers/postgres/qgspostgresconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class QgsPostgresResult
QString PQfname( int col );
int PQftable( int col );
int PQftype( int col );
int PQfmod( int col );
int PQftablecol( int col );
Oid PQoidValue();

Expand Down
38 changes: 37 additions & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2915,7 +2915,43 @@ bool QgsPostgresProvider::getGeometryDetails()
}
else
{
detectedType = mRequestedGeomType == QGis::WKBUnknown ? "" : QgsPostgresConn::postgisWkbTypeName( mRequestedGeomType );
sql = QString( "SELECT %1 FROM %2 LIMIT 0" ).arg( quotedIdentifier( mGeometryColumn ), mQuery );
result = connectionRO()->PQexec( sql );
if ( PGRES_TUPLES_OK == result.PQresultStatus() )
{
sql = QString( "SELECT (SELECT t.typname FROM pg_type t WHERE oid = %1), upper(postgis_typmod_type(%2)), postgis_typmod_srid(%2)" )
.arg( QString::number( result.PQftype( 0 ) ), QString::number( result.PQfmod( 0 ) ) );
result = connectionRO()->PQexec( sql );
if ( result.PQntuples() == 1 )
{
geomColType = result.PQgetvalue( 0, 0 );
detectedType = result.PQgetvalue( 0, 1 );
detectedSrid = result.PQgetvalue( 0, 2 );
if ( geomColType == "geometry" )
mSpatialColType = sctGeometry;
else if ( geomColType == "geography" )
mSpatialColType = sctGeography;
else if ( geomColType == "topogeometry" )
mSpatialColType = sctTopoGeometry;
else if ( geomColType == "pcpatch" )
mSpatialColType = sctPcPatch;
else
{
detectedType = mRequestedGeomType == QGis::WKBUnknown ? "" : QgsPostgresConn::postgisWkbTypeName( mRequestedGeomType );
detectedSrid = mRequestedSrid;
}
}
else
{
connectionRO()->PQexecNR( "COMMIT" );
detectedType = mRequestedGeomType == QGis::WKBUnknown ? "" : QgsPostgresConn::postgisWkbTypeName( mRequestedGeomType );
}
}
else
{
mValid = false;
return false;
}
}

mDetectedGeomType = QgsPostgresConn::wkbTypeFromPostgis( detectedType );
Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/test_provider_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def testDefaultValue(self):
assert self.provider.defaultValue(1) == NULL
assert self.provider.defaultValue(2) == '\'qgis\'::text'

def testQueryLayers(self):
def test_query(dbconn, query, key):
ql = QgsVectorLayer('%s srid=4326 table="%s" (geom) key=\'%s\' sql=' % (dbconn, query.replace('"', '\\"'), key), "testgeom", "postgres")
print query, key
assert(ql.isValid())

test_query(self.dbconn, '(SELECT NULL::integer "Id1", NULL::integer "Id2", NULL::geometry(Point, 4326) geom LIMIT 0)', '"Id1","Id2"')

def testWkbTypes(self):
def test_table(dbconn, table_name, wkt):
vl = QgsVectorLayer('%s srid=4326 table="qgis_test".%s (geom) sql=' % (dbconn, table_name), "testgeom", "postgres")
Expand Down

0 comments on commit 8e5ba3b

Please sign in to comment.