Skip to content

Commit 83f2b2d

Browse files
author
Sandro Santilli
committed
Add alias to pk query, fixing ambiguous "ctid" reference (#6620)
1 parent abb924a commit 83f2b2d

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -678,23 +678,26 @@ bool QgsPostgresProvider::nextFeature( QgsFeature& feature )
678678
return true;
679679
}
680680

681-
QString QgsPostgresProvider::pkParamWhereClause( int offset ) const
681+
QString QgsPostgresProvider::pkParamWhereClause( int offset, const char *alias ) const
682682
{
683683
QString whereClause;
684684

685+
QString aliased;
686+
if ( alias ) aliased = QString("%1.").arg( alias );
687+
685688
switch ( mPrimaryKeyType )
686689
{
687690
case pktTid:
688-
whereClause = QString( "ctid=$%1" ).arg( offset );
691+
whereClause = QString( "%2ctid=$%1" ).arg( offset ).arg( aliased );
689692
break;
690693

691694
case pktOid:
692-
whereClause = QString( "oid=$%1" ).arg( offset );
695+
whereClause = QString( "%2oid=$%1" ).arg( offset ).arg( aliased );
693696
break;
694697

695698
case pktInt:
696699
Q_ASSERT( mPrimaryKeyAttrs.size() == 1 );
697-
whereClause = QString( "%1=$%2" ).arg( quotedIdentifier( field( mPrimaryKeyAttrs[0] ).name() ) ).arg( offset );
700+
whereClause = QString( "%3%1=$%2" ).arg( quotedIdentifier( field( mPrimaryKeyAttrs[0] ).name() ) ).arg( offset ).arg( aliased );
698701
break;
699702

700703
case pktFidMap:
@@ -705,7 +708,7 @@ QString QgsPostgresProvider::pkParamWhereClause( int offset ) const
705708
int idx = mPrimaryKeyAttrs[i];
706709
const QgsField &fld = field( idx );
707710

708-
whereClause += delim + QString( "%1=$%2" ).arg( mConnectionRO->fieldExpression( fld ) ).arg( offset++ );
711+
whereClause += delim + QString( "%3%1=$%2" ).arg( mConnectionRO->fieldExpression( fld ) ).arg( offset++ ).arg( aliased );
709712
delim = " AND ";
710713
}
711714
}
@@ -2404,13 +2407,13 @@ bool QgsPostgresProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
24042407
// Later, we'll replace the old TopoGeometry with the new one,
24052408
// to avoid orphans and retain higher level in an eventual
24062409
// hierarchical definition
2407-
update = QString( "UPDATE %1 SET %2=toTopoGeom(%3,t.name,layer_id(%2))"
2410+
update = QString( "UPDATE %1 o SET %2 = toTopoGeom(%3, t.name, layer_id(%2))"
24082411
" FROM topology.topology t WHERE t.id = topology_id(%2)"
24092412
" AND %4 RETURNING layer_id(%2), id(%2), t.name" )
24102413
.arg( mQuery )
24112414
.arg( quotedIdentifier( mGeometryColumn ) )
24122415
.arg( geomParam( 1 ) )
2413-
.arg( pkParamWhereClause( 2 ) );
2416+
.arg( pkParamWhereClause( 2, "o" ) );
24142417

24152418
QString getid = QString( "SELECT id(%1) FROM %2 WHERE %3" )
24162419
.arg( quotedIdentifier( mGeometryColumn ) )
@@ -2433,6 +2436,7 @@ bool QgsPostgresProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
24332436
.arg( mQuery )
24342437
.arg( quotedIdentifier( mGeometryColumn ) )
24352438
.arg( pkParamWhereClause( 2 ) );
2439+
QgsDebugMsg( "TopoGeom swap: " + replace );
24362440
result = mConnectionRW->PQprepare( "replacetopogeom", replace, 2, NULL );
24372441
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
24382442
{
@@ -2546,13 +2550,15 @@ bool QgsPostgresProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
25462550
params << QString::number( old_tg_id );
25472551
appendPkParams( iter.key(), params );
25482552
QgsDebugMsg( "Replacing topogeom reference to use id " + QString::number( old_tg_id ) );
2553+
QgsDebugMsg( "Params are: " + params.join(","));
25492554
result = mConnectionRW->PQexecPrepared( "replacetopogeom", params );
25502555
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
25512556
{
25522557
QgsDebugMsg( QString( "Exception thrown due to PQexecPrepared of 'replacetopogeom' returning != PGRES_COMMAND_OK (%1 != expected %2)" )
25532558
.arg( result.PQresultStatus() ).arg( PGRES_COMMAND_OK ) );
25542559
throw PGException( result );
25552560
}
2561+
QgsDebugMsg( QString( "TopoGeom swap affected " + QString::number(result.PQntuples())) );
25562562
} // if TopoGeometry
25572563

25582564
} // for each feature

src/providers/postgres/qgspostgresprovider.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,11 @@ class QgsPostgresProvider : public QgsVectorDataProvider
335335
const QgsAttributeList &fetchAttributes );
336336

337337
QString geomParam( int offset ) const;
338-
QString pkParamWhereClause( int offset ) const;
338+
/** Get parametrized primary key clause
339+
* @param offset specifies offset to use for the pk value parameter
340+
* @param alias specifies an optional alias given to the subject table
341+
*/
342+
QString pkParamWhereClause( int offset, const char* alias=0 ) const;
339343
QString whereClause( QgsFeatureId featureId ) const;
340344

341345
bool hasSufficientPermsAndCapabilities();

0 commit comments

Comments
 (0)