Skip to content

Commit 145a7a8

Browse files
author
mhugent
committed
Fix for problem where columns that are used as primary keys are not displayed
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7401 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 44da450 commit 145a7a8

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/providers/postgres/qgspostgresprovider.cpp

+28-10
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@ bool QgsPostgresProvider::getNextFeature(QgsFeature& feature)
418418
for (int row = 0; row < rows; row++)
419419
{
420420
int oid = *(int *)PQgetvalue(queryResult, row, PQfnumber(queryResult,"\""+primaryKey+"\""));
421-
// QgsDebugMsg("Primary key type is " + QString::number(primaryKeyType));
422421

423422
if (swapEndian)
424423
oid = ntohl(oid); // convert oid to opposite endian
@@ -432,11 +431,17 @@ bool QgsPostgresProvider::getNextFeature(QgsFeature& feature)
432431

433432
for(; name_it != mFetchAttributeNames.end(); ++name_it, ++index_it)
434433
{
435-
QString name = *name_it; //just for the debugger
436-
//int number = PQfnumber(queryResult,*name_it); //just for the debugger
437-
char* attribute = PQgetvalue(queryResult, row, PQfnumber(queryResult,*name_it));
434+
QString val;
435+
if( (*name_it) == primaryKey)
436+
{
437+
val = QString::number(oid);
438+
}
439+
else
440+
{
441+
char* attribute = PQgetvalue(queryResult, row, PQfnumber(queryResult,*name_it));
442+
val = QString::fromUtf8(attribute);
443+
}
438444

439-
QString val = QString::fromUtf8(attribute);
440445
switch (attributeFields[*index_it].type())
441446
{
442447
case QVariant::Int:
@@ -534,7 +539,10 @@ void QgsPostgresProvider::select(QgsAttributeList fetchAttributes,
534539
}
535540
for(std::list<QString>::const_iterator it = mFetchAttributeNames.begin(); it != mFetchAttributeNames.end(); ++it)
536541
{
537-
declare += "," + *it + "::text";
542+
if( (*it) != primaryKey) //no need to fetch primary key again
543+
{
544+
declare += "," + *it + "::text";
545+
}
538546
}
539547

540548
declare += " ";
@@ -625,7 +633,10 @@ bool QgsPostgresProvider::getFeatureAtId(int featureId,
625633
}
626634
for(namesIt = attributeNames.begin(); namesIt != attributeNames.end(); ++namesIt)
627635
{
628-
sql += "," + *namesIt + "::text";
636+
if( (*namesIt) != primaryKey) //no need to fetch primary key again
637+
{
638+
sql += "," + *namesIt + "::text";
639+
}
629640
}
630641

631642
sql += " " + QString("from %1").arg(mSchemaTableName);
@@ -660,10 +671,17 @@ bool QgsPostgresProvider::getFeatureAtId(int featureId,
660671

661672
for(namesIt = attributeNames.begin(); namesIt != attributeNames.end(); ++namesIt, ++it)
662673
{
663-
QString name = *namesIt; //just for the debugger
664-
char* attribute = PQgetvalue(res, 0, PQfnumber(res,*namesIt));
674+
QString val;
675+
if( (*namesIt) == primaryKey)
676+
{
677+
val = QString::number(oid);
678+
}
679+
else
680+
{
681+
char* attribute = PQgetvalue(res, 0, PQfnumber(res,*namesIt));
682+
val = QString::fromUtf8(attribute);
683+
}
665684

666-
QString val = QString::fromUtf8(attribute);
667685
switch (attributeFields[*it].type())
668686
{
669687
case QVariant::Int:

0 commit comments

Comments
 (0)