You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// NOTE: We are creating a new TopoGeometry objects with the new shape.
2403
-
// TODO: _replace_ the initial TopoGeometry instead, so that it keeps
2404
-
// the same identifier and thus still partecipates in any
2405
-
// hierarchical definition. Also creating a new object results
2406
-
// in orphaned topogeometries!
2403
+
// We will create a new TopoGeometry object with the new shape.
2404
+
// Later, we'll replace the old TopoGeometry with the new one,
2405
+
// to avoid orphans and retain higher level in an eventual
2406
+
// hierarchical definition
2407
2407
update = QString( "UPDATE %1 SET %2=toTopoGeom(%3,t.name,layer_id(%2))"
2408
2408
" FROM topology.topology t WHERE t.id = topology_id(%2)"
2409
-
" AND %4" )
2409
+
" AND %4 RETURNING layer_id(%2), id(%2), t.name" )
2410
2410
.arg( mQuery )
2411
2411
.arg( quotedIdentifier( mGeometryColumn ) )
2412
2412
.arg( geomParam( 1 ) )
2413
2413
.arg( pkParamWhereClause( 2 ) );
2414
+
2415
+
QString getid = QString( "SELECT id(%1) FROM %2 WHERE %3" )
2416
+
.arg( quotedIdentifier( mGeometryColumn ) )
2417
+
.arg( mQuery )
2418
+
.arg( pkParamWhereClause( 1 ) );
2419
+
2420
+
QgsDebugMsg( "getting old topogeometry id: " + getid );
2421
+
2422
+
result = mConnectionRO->PQprepare( "getid", getid, 1, NULL );
2423
+
if ( result.PQresultStatus() != PGRES_COMMAND_OK ) {
2424
+
QgsDebugMsg( QString("Exception thrown due to PQprepare of this query returning != PGRES_COMMAND_OK (%1 != expected %2): %3").arg(result.PQresultStatus()).arg(PGRES_COMMAND_OK).arg(getid) );
2425
+
throwPGException( result );
2426
+
}
2427
+
2428
+
QString replace = QString( "UPDATE %1 SET %2="
2429
+
"( topology_id(%2),layer_id(%2),$1,type(%2) )"
2430
+
"WHERE %3" )
2431
+
.arg( mQuery )
2432
+
.arg( quotedIdentifier( mGeometryColumn ) )
2433
+
.arg( pkParamWhereClause( 2 ) );
2434
+
result = mConnectionRW->PQprepare( "replacetopogeom", replace, 2, NULL );
2435
+
if ( result.PQresultStatus() != PGRES_COMMAND_OK ) {
2436
+
QgsDebugMsg( QString("Exception thrown due to PQprepare of this query returning != PGRES_COMMAND_OK (%1 != expected %2): %3").arg(result.PQresultStatus()).arg(PGRES_COMMAND_OK).arg(replace) );
QgsPostgresResult result = mConnectionRW->PQprepare( "updatefeatures", update, 2, NULL );
2427
-
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
2452
+
result = mConnectionRW->PQprepare( "updatefeatures", update, 2, NULL );
2453
+
if ( result.PQresultStatus() != PGRES_COMMAND_OK ) {
2454
+
QgsDebugMsg( QString("Exception thrown due to PQprepare of this query returning != PGRES_COMMAND_OK (%1 != expected %2): %3").arg(result.PQresultStatus()).arg(PGRES_COMMAND_OK).arg(update) );
2428
2455
throwPGException( result );
2456
+
}
2429
2457
2430
2458
for ( QgsGeometryMap::iterator iter = geometry_map.begin();
QgsDebugMsg( QString("Exception thrown due to PQexecPrepared of 'getid' returning != PGRES_COMMAND_OK (%1 != expected %2)").arg(result.PQresultStatus()).arg(expected_status) );
2450
2497
throwPGException( result );
2498
+
}
2499
+
2500
+
if ( mSpatialColType == sctTopoGeometry )
2501
+
{
2502
+
long layer_id = result.PQgetvalue( 0, 0 ).toLong(); // new layer_id == old layer_id
2503
+
long new_tg_id = result.PQgetvalue( 0, 1 ).toLong(); // new topogeo_id
2504
+
QString toponame = result.PQgetvalue( 0, 2 ); // new topology name == old topology name
2505
+
2506
+
// Replace old TopoGeom with new TopoGeom, so that
2507
+
// any hierarchically defined TopoGeom will still have its
2508
+
// definition and we'll leave no orphans
2509
+
QString replace = QString( "DELETE FROM %1.relation WHERE "
2510
+
"layer_id = %2 AND topogeo_id = %3" )
2511
+
.arg( quotedIdentifier(toponame) )
2512
+
.arg( layer_id )
2513
+
.arg( old_tg_id );
2514
+
result = mConnectionRW->PQexec( replace );
2515
+
if ( result.PQresultStatus() != PGRES_COMMAND_OK ) {
2516
+
QgsDebugMsg( QString("Exception thrown due to PQexec of this query returning != PGRES_COMMAND_OK (%1 != expected %2): %3").arg(result.PQresultStatus()).arg(PGRES_COMMAND_OK).arg(replace) );
2517
+
throwPGException( result );
2518
+
}
2519
+
replace = QString( "UPDATE %1.relation SET topogeo_id = %2 "
2520
+
"WHERE layer_id = %3 AND topogeo_id = %4" )
2521
+
.arg( quotedIdentifier(toponame) )
2522
+
.arg( old_tg_id )
2523
+
.arg( layer_id )
2524
+
.arg( new_tg_id );
2525
+
QgsDebugMsg( "relation swap: " + replace );
2526
+
result = mConnectionRW->PQexec( replace );
2527
+
if ( result.PQresultStatus() != PGRES_COMMAND_OK ) {
2528
+
QgsDebugMsg( QString("Exception thrown due to PQexec of this query returning != PGRES_COMMAND_OK (%1 != expected %2): %3").arg(result.PQresultStatus()).arg(PGRES_COMMAND_OK).arg(replace) );
2529
+
throwPGException( result );
2530
+
}
2531
+
// Change the ID of the TopoGeometry object put in the table again
2532
+
params = QStringList();
2533
+
params << QString::number( old_tg_id );
2534
+
appendPkParams( iter.key(), params );
2535
+
QgsDebugMsg( "Replacing topogeom reference to use id " + QString::number(old_tg_id) );
2536
+
result = mConnectionRW->PQexecPrepared( "replacetopogeom", params );
2537
+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
2538
+
{
2539
+
QgsDebugMsg( QString("Exception thrown due to PQexecPrepared of 'replacetopogeom' returning != PGRES_COMMAND_OK (%1 != expected %2)").arg(result.PQresultStatus()).arg(PGRES_COMMAND_OK) );
0 commit comments