@@ -125,6 +125,16 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
125
125
return ;
126
126
}
127
127
128
+ if ( mSpatialColType == sctTopoGeometry )
129
+ {
130
+ if ( !getTopoLayerInfo () ) // gets topology name and layer id
131
+ {
132
+ QgsMessageLog::logMessage ( tr ( " invalid PostgreSQL topology layer" ), tr ( " PostGIS" ) );
133
+ disconnectDb ();
134
+ return ;
135
+ }
136
+ }
137
+
128
138
mLayerExtent .setMinimal ();
129
139
mFeaturesCounted = -1 ;
130
140
@@ -1817,38 +1827,38 @@ QString QgsPostgresProvider::paramValue( QString fieldValue, const QString &defa
1817
1827
return fieldValue;
1818
1828
}
1819
1829
1820
- QString QgsPostgresProvider::geomParam ( int offset ) const
1821
- {
1822
- // TODO: retrieve these at construction time
1823
- QString toponame;
1824
- long layer_id;
1825
1830
1826
- if ( mSpatialColType == sctTopoGeometry )
1831
+ /* private */
1832
+ bool QgsPostgresProvider::getTopoLayerInfo ( )
1833
+ {
1834
+ QString sql = QString ( " SELECT t.name, l.layer_id "
1835
+ " FROM topology.layer l, topology.topology t "
1836
+ " WHERE l.topology_id = t.id AND l.schema_name=%1 "
1837
+ " AND l.table_name=%2 AND l.feature_column=%3" )
1838
+ .arg ( quotedValue ( mSchemaName ) )
1839
+ .arg ( quotedValue ( mTableName ) )
1840
+ .arg ( quotedValue ( mGeometryColumn ) );
1841
+ QgsPostgresResult result = mConnectionRO ->PQexec ( sql );
1842
+ if ( result.PQresultStatus () != PGRES_TUPLES_OK )
1827
1843
{
1828
- QString sql = QString ( " SELECT t.name, l.layer_id FROM topology.layer l, topology.topology t "
1829
- " WHERE l.topology_id = t.id AND l.schema_name=%1 "
1830
- " AND l.table_name=%2 AND l.feature_column=%3" )
1831
- .arg ( quotedValue ( mSchemaName ) )
1832
- .arg ( quotedValue ( mTableName ) )
1833
- .arg ( quotedValue ( mGeometryColumn ) );
1834
- QgsPostgresResult result = mConnectionRO ->PQexec ( sql );
1835
- if ( result.PQresultStatus () != PGRES_TUPLES_OK )
1836
- {
1837
- throw PGException ( result ); // we should probably not do this
1838
- }
1839
- if ( result.PQntuples () < 1 )
1840
- {
1841
- QgsMessageLog::logMessage ( tr ( " Could not find topology of layer %1.%2.%3" )
1842
- .arg ( quotedValue ( mSchemaName ) )
1843
- .arg ( quotedValue ( mTableName ) )
1844
- .arg ( quotedValue ( mGeometryColumn ) ),
1845
- tr ( " PostGIS" ) );
1846
- }
1847
- toponame = result.PQgetvalue ( 0 , 0 );
1848
- layer_id = result.PQgetvalue ( 0 , 1 ).toLong ();
1849
-
1844
+ throw PGException ( result ); // we should probably not do this
1845
+ }
1846
+ if ( result.PQntuples () < 1 )
1847
+ {
1848
+ QgsMessageLog::logMessage ( tr ( " Could not find topology of layer %1.%2.%3" )
1849
+ .arg ( quotedValue ( mSchemaName ) )
1850
+ .arg ( quotedValue ( mTableName ) )
1851
+ .arg ( quotedValue ( mGeometryColumn ) ),
1852
+ tr ( " PostGIS" ) );
1853
+ return false ;
1850
1854
}
1855
+ mTopoLayerInfo .topologyName = result.PQgetvalue ( 0 , 0 );
1856
+ mTopoLayerInfo .layerId = result.PQgetvalue ( 0 , 1 ).toLong ();
1857
+ return true ;
1858
+ }
1851
1859
1860
+ QString QgsPostgresProvider::geomParam ( int offset ) const
1861
+ {
1852
1862
QString geometry;
1853
1863
1854
1864
bool forceMulti = false ;
@@ -1903,8 +1913,8 @@ QString QgsPostgresProvider::geomParam( int offset ) const
1903
1913
if ( mSpatialColType == sctTopoGeometry )
1904
1914
{
1905
1915
geometry += QString ( " ,%1,%2)" )
1906
- .arg ( quotedValue ( toponame ) )
1907
- .arg ( layer_id );
1916
+ .arg ( quotedValue ( mTopoLayerInfo . topologyName ) )
1917
+ .arg ( mTopoLayerInfo . layerId );
1908
1918
}
1909
1919
1910
1920
return geometry;
@@ -2457,14 +2467,10 @@ bool QgsPostgresProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
2457
2467
// Later, we'll replace the old TopoGeometry with the new one,
2458
2468
// to avoid orphans and retain higher level in an eventual
2459
2469
// hierarchical definition
2460
- update = QString ( " WITH tg AS ( SELECT t.name, "
2461
- " toTopoGeom(%3, t.name, layer_id(o.%2)) as tg"
2462
- " FROM %1 o, topology.topology t WHERE t.id = topology_id(o.%2)"
2463
- " AND %4 ) SELECT layer_id(tg.tg), id(tg.tg), tg.name FROM tg" )
2464
- .arg ( mQuery )
2465
- .arg ( quotedIdentifier ( mGeometryColumn ) )
2470
+ update = QString ( " SELECT id(%1) FROM %2 o WHERE %3" )
2466
2471
.arg ( geomParam ( 1 ) )
2467
- .arg ( pkParamWhereClause ( 2 , " o" ) );
2472
+ .arg ( mQuery )
2473
+ .arg ( pkParamWhereClause ( 2 ) );
2468
2474
2469
2475
QString getid = QString ( " SELECT id(%1) FROM %2 WHERE %3" )
2470
2476
.arg ( quotedIdentifier ( mGeometryColumn ) )
@@ -2563,17 +2569,17 @@ bool QgsPostgresProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
2563
2569
2564
2570
if ( mSpatialColType == sctTopoGeometry )
2565
2571
{
2566
- long layer_id = result.PQgetvalue ( 0 , 0 ).toLong (); // new layer_id == old layer_id
2567
- long new_tg_id = result.PQgetvalue ( 0 , 1 ).toLong (); // new topogeo_id
2568
- QString toponame = result.PQgetvalue ( 0 , 2 ); // new topology name == old topology name
2572
+ long new_tg_id = result.PQgetvalue ( 0 , 0 ).toLong (); // new topogeo_id
2569
2573
2570
2574
// Replace old TopoGeom with new TopoGeom, so that
2571
2575
// any hierarchically defined TopoGeom will still have its
2572
2576
// definition and we'll leave no orphans
2577
+ // TODO: move this logic into a method using mTopoLayerInfo and
2578
+ // taking a topogeo_id (to be reused for deleteFeatures)
2573
2579
QString replace = QString ( " DELETE FROM %1.relation WHERE "
2574
2580
" layer_id = %2 AND topogeo_id = %3" )
2575
- .arg ( quotedIdentifier ( toponame ) )
2576
- .arg ( layer_id )
2581
+ .arg ( quotedIdentifier ( mTopoLayerInfo . topologyName ) )
2582
+ .arg ( mTopoLayerInfo . layerId )
2577
2583
.arg ( old_tg_id );
2578
2584
result = mConnectionRW ->PQexec ( replace );
2579
2585
if ( result.PQresultStatus () != PGRES_COMMAND_OK )
@@ -2585,9 +2591,9 @@ bool QgsPostgresProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
2585
2591
// TODO: use prepared query here
2586
2592
replace = QString ( " UPDATE %1.relation SET topogeo_id = %2 "
2587
2593
" WHERE layer_id = %3 AND topogeo_id = %4" )
2588
- .arg ( quotedIdentifier ( toponame ) )
2594
+ .arg ( quotedIdentifier ( mTopoLayerInfo . topologyName ) )
2589
2595
.arg ( old_tg_id )
2590
- .arg ( layer_id )
2596
+ .arg ( mTopoLayerInfo . layerId )
2591
2597
.arg ( new_tg_id );
2592
2598
QgsDebugMsg ( " relation swap: " + replace );
2593
2599
result = mConnectionRW ->PQexec ( replace );
0 commit comments