Skip to content
Permalink
Browse files

Merge pull request #2458 from SebDieBln/Postgres_GeomTypeDirect

[postgresprovider] Determine geometry type and srid directly from column type
  • Loading branch information
jef-n committed Nov 18, 2015
2 parents f6e4e75 + cfa308d commit 8e5ba3bf925ba893c60bacb8ae1b25ad9edcb729
@@ -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 );
@@ -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();

@@ -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 );
@@ -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")

0 comments on commit 8e5ba3b

Please sign in to comment.
You can’t perform that action at this time.