Skip to content
Permalink
Browse files

Add alias to pk query, fixing ambiguous "ctid" reference (#6620)

  • Loading branch information
Sandro Santilli
Sandro Santilli committed Nov 5, 2012
1 parent abb924a commit 83f2b2db42871a63ae622aa6bbff71cf58e2ce5d
Showing with 18 additions and 8 deletions.
  1. +13 −7 src/providers/postgres/qgspostgresprovider.cpp
  2. +5 −1 src/providers/postgres/qgspostgresprovider.h
@@ -678,23 +678,26 @@ bool QgsPostgresProvider::nextFeature( QgsFeature& feature )
return true;
}

QString QgsPostgresProvider::pkParamWhereClause( int offset ) const
QString QgsPostgresProvider::pkParamWhereClause( int offset, const char *alias ) const
{
QString whereClause;

QString aliased;
if ( alias ) aliased = QString("%1.").arg( alias );

switch ( mPrimaryKeyType )
{
case pktTid:
whereClause = QString( "ctid=$%1" ).arg( offset );
whereClause = QString( "%2ctid=$%1" ).arg( offset ).arg( aliased );
break;

case pktOid:
whereClause = QString( "oid=$%1" ).arg( offset );
whereClause = QString( "%2oid=$%1" ).arg( offset ).arg( aliased );
break;

case pktInt:
Q_ASSERT( mPrimaryKeyAttrs.size() == 1 );
whereClause = QString( "%1=$%2" ).arg( quotedIdentifier( field( mPrimaryKeyAttrs[0] ).name() ) ).arg( offset );
whereClause = QString( "%3%1=$%2" ).arg( quotedIdentifier( field( mPrimaryKeyAttrs[0] ).name() ) ).arg( offset ).arg( aliased );
break;

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

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

QString getid = QString( "SELECT id(%1) FROM %2 WHERE %3" )
.arg( quotedIdentifier( mGeometryColumn ) )
@@ -2433,6 +2436,7 @@ bool QgsPostgresProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
.arg( mQuery )
.arg( quotedIdentifier( mGeometryColumn ) )
.arg( pkParamWhereClause( 2 ) );
QgsDebugMsg( "TopoGeom swap: " + replace );
result = mConnectionRW->PQprepare( "replacetopogeom", replace, 2, NULL );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
@@ -2546,13 +2550,15 @@ bool QgsPostgresProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
params << QString::number( old_tg_id );
appendPkParams( iter.key(), params );
QgsDebugMsg( "Replacing topogeom reference to use id " + QString::number( old_tg_id ) );
QgsDebugMsg( "Params are: " + params.join(","));
result = mConnectionRW->PQexecPrepared( "replacetopogeom", params );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QgsDebugMsg( QString( "Exception thrown due to PQexecPrepared of 'replacetopogeom' returning != PGRES_COMMAND_OK (%1 != expected %2)" )
.arg( result.PQresultStatus() ).arg( PGRES_COMMAND_OK ) );
throw PGException( result );
}
QgsDebugMsg( QString( "TopoGeom swap affected " + QString::number(result.PQntuples())) );
} // if TopoGeometry

} // for each feature
@@ -335,7 +335,11 @@ class QgsPostgresProvider : public QgsVectorDataProvider
const QgsAttributeList &fetchAttributes );

QString geomParam( int offset ) const;
QString pkParamWhereClause( int offset ) const;
/** Get parametrized primary key clause
* @param offset specifies offset to use for the pk value parameter
* @param alias specifies an optional alias given to the subject table
*/
QString pkParamWhereClause( int offset, const char* alias=0 ) const;
QString whereClause( QgsFeatureId featureId ) const;

bool hasSufficientPermsAndCapabilities();

0 comments on commit 83f2b2d

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