Skip to content

Commit fa696cd

Browse files
author
jef
committedMar 27, 2008
another postgres provider update:
- make declareCursor and getFeature private - there's no need to track column names in cursors (which contained a bug triggering #962 again and more) - introduce private method field(index) that verifies that the index in valid or throws an exception. - use that in public methods that get indexes from outside (minValue, maxValue, uniqueValues...) git-svn-id: http://svn.osgeo.org/qgis/trunk@8290 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 68a41f6 commit fa696cd

File tree

2 files changed

+254
-194
lines changed

2 files changed

+254
-194
lines changed
 

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 239 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -367,108 +367,121 @@ QString QgsPostgresProvider::storageType() const
367367
return "PostgreSQL database with PostGIS extension";
368368
}
369369

370-
void QgsPostgresProvider::declareCursor(const QString &cursorName,
370+
bool QgsPostgresProvider::declareCursor(const QString &cursorName,
371371
const QgsAttributeList &fetchAttributes,
372372
bool fetchGeometry,
373-
QString whereClause,
374-
QStringList &attributeNames)
373+
QString whereClause)
375374
{
376-
QString declare = QString("declare %1 binary cursor with hold for select %2")
377-
.arg(cursorName).arg(quotedIdentifier(primaryKey));
378-
379-
if(fetchGeometry)
375+
try
380376
{
381-
declare += QString(",asbinary(%1,'%2') as qgs_feature_geometry")
382-
.arg( quotedIdentifier(geometryColumn) )
383-
.arg( endianString() );
384-
}
377+
QString declare = QString("declare %1 binary cursor with hold for select %2")
378+
.arg(cursorName).arg(quotedIdentifier(primaryKey));
385379

386-
QgsFieldMap attributeMap = fields();
387-
for (QgsAttributeList::const_iterator it = fetchAttributes.constBegin(); it != fetchAttributes.constEnd(); ++it)
388-
{
389-
QgsFieldMap::const_iterator fieldIt = attributeMap.find(*it);
390-
if(fieldIt != attributeMap.end())
380+
if(fetchGeometry)
381+
{
382+
declare += QString(",asbinary(%1,'%2') as qgs_feature_geometry")
383+
.arg( quotedIdentifier(geometryColumn) )
384+
.arg( endianString() );
385+
}
386+
387+
for (QgsAttributeList::const_iterator it = fetchAttributes.constBegin(); it != fetchAttributes.constEnd(); ++it)
391388
{
392-
const QString &fieldname = fieldIt.value().name();
389+
const QgsField &fld = field(*it);
390+
391+
const QString &fieldname = fld.name();
392+
if( fieldname == primaryKey )
393+
continue;
393394

394-
if( fieldname != primaryKey )
395+
const QString &type = fld.typeName();
396+
if( type == "money" || type.startsWith("_") )
395397
{
396-
if( fieldIt.value().typeName() == "money" || fieldIt.value().typeName().startsWith("_") )
397-
{
398-
// money and arrays don't support cast to text, but return text
399-
// TODO: check other types
400-
declare += "," + quotedIdentifier( fieldname );
401-
}
402-
else if( fieldIt.value().typeName() == "bool" )
403-
{
404-
// bool doesn't support cast to text either and even doesn't return text.
405-
// (even text() doesn't work with binary cursors)
406-
declare += QString(",CASE WHEN %1 THEN 't' WHEN NOT %1 THEN 'f' ELSE NULL END AS %1")
407-
.arg( quotedIdentifier(fieldname) );
408-
}
409-
else
410-
{
411-
declare += "," + quotedIdentifier( fieldname ) + "::text";
412-
}
398+
// money and arrays don't support cast to text, but return text
399+
// TODO: check other types
400+
declare += "," + quotedIdentifier( fieldname );
401+
}
402+
else if( type == "bool" )
403+
{
404+
// bool doesn't support cast to text either and even doesn't return text.
405+
// (even text() doesn't work with binary cursors)
406+
declare += QString(",CASE WHEN %1 THEN 't' WHEN NOT %1 THEN 'f' ELSE NULL END AS %1")
407+
.arg( quotedIdentifier(fieldname) );
408+
}
409+
else
410+
{
411+
declare += "," + quotedIdentifier( fieldname ) + "::text";
413412
}
414-
415-
attributeNames << fieldname;
416413
}
417-
}
418414

419-
declare += " from " + mSchemaTableName;
420-
421-
if( !whereClause.isEmpty() )
422-
declare += QString(" where %1").arg(whereClause);
415+
declare += " from " + mSchemaTableName;
416+
417+
if( !whereClause.isEmpty() )
418+
declare += QString(" where %1").arg(whereClause);
423419

424-
QgsDebugMsg("Binary cursor: " + declare);
420+
QgsDebugMsg("Binary cursor: " + declare);
425421

426-
PQexecNR(connection, declare.toUtf8());
422+
return PQexecNR(connection, declare.toUtf8());
423+
}
424+
catch(PGFieldNotFound)
425+
{
426+
return false;
427+
}
427428
}
428429

429-
void QgsPostgresProvider::getFeature(PGresult *queryResult, int row, bool fetchGeometry,
430+
bool QgsPostgresProvider::getFeature(PGresult *queryResult, int row, bool fetchGeometry,
430431
QgsFeature &feature,
431-
const QStringList &attributeNames,
432432
const QgsAttributeList &fetchAttributes)
433433
{
434-
int oid = *(int *)PQgetvalue(queryResult, row, PQfnumber(queryResult,quotedIdentifier(primaryKey).toUtf8()));
435-
if (swapEndian)
436-
oid = ntohl(oid); // convert oid to opposite endian
434+
try
435+
{
436+
int oid = *(int *)PQgetvalue(queryResult, row, 0);
437+
if (swapEndian)
438+
oid = ntohl(oid); // convert oid to opposite endian
437439

438-
feature.setFeatureId(oid);
440+
feature.setFeatureId(oid);
439441

440-
// fetch attributes
441-
QgsAttributeList::const_iterator it = fetchAttributes.constBegin();
442-
for(QStringList::const_iterator namesIt = attributeNames.begin(); namesIt != attributeNames.end(); ++namesIt, ++it)
443-
{
444-
QString val;
442+
int col; // first attribute column
445443

446-
if( (*namesIt) == primaryKey)
444+
if (fetchGeometry)
447445
{
448-
val = QString::number(oid);
449-
}
450-
else
451-
{
452-
int fn = PQfnumber(queryResult,quotedIdentifier(*namesIt).toUtf8());
453-
454-
if( !PQgetisnull(queryResult, row, fn) )
446+
int returnedLength = PQgetlength(queryResult, row, 1);
447+
if(returnedLength > 0)
455448
{
456-
val = QString::fromUtf8(PQgetvalue(queryResult, row, fn));
449+
unsigned char *featureGeom = new unsigned char[returnedLength + 1];
450+
memset(featureGeom, '\0', returnedLength + 1);
451+
memcpy(featureGeom, PQgetvalue(queryResult, row, PQfnumber(queryResult,QString("qgs_feature_geometry").toUtf8())), returnedLength);
452+
feature.setGeometryAndOwnership(featureGeom, returnedLength + 1);
457453
}
458454
else
459455
{
460-
val = QString::null;
456+
feature.setGeometryAndOwnership(0, 0);
457+
QgsDebugMsg("Couldn't get the feature geometry in binary form");
461458
}
462-
}
463459

464-
if( val.isNull() )
465-
{
466-
feature.addAttribute(*it, val);
460+
col = 2;
467461
}
468462
else
469463
{
470-
switch (attributeFields[*it].type())
464+
col = 1;
465+
}
466+
467+
// iterate attributes
468+
for(QgsAttributeList::const_iterator it=fetchAttributes.constBegin(); it!=fetchAttributes.constEnd(); it++)
469+
{
470+
const QgsField &fld = field(*it);
471+
472+
if( fld.name() == primaryKey )
471473
{
474+
// primary key was already processed
475+
feature.addAttribute(*it, QString::number(oid) );
476+
continue;
477+
}
478+
479+
if( !PQgetisnull(queryResult, row, col) )
480+
{
481+
QString val = QString::fromUtf8(PQgetvalue(queryResult, row, col));
482+
483+
switch (fld.type())
484+
{
472485
case QVariant::LongLong:
473486
feature.addAttribute(*it, val.toLongLong());
474487
break;
@@ -483,26 +496,22 @@ void QgsPostgresProvider::getFeature(PGresult *queryResult, int row, bool fetchG
483496
break;
484497
default:
485498
assert(0 && "unsupported field type");
499+
}
486500
}
501+
else
502+
{
503+
feature.addAttribute(*it, QVariant(QString::null));
504+
}
505+
506+
col++;
487507
}
488-
}
489508

490-
if (fetchGeometry)
509+
return true;
510+
}
511+
catch(PGFieldNotFound)
491512
{
492-
int returnedLength = PQgetlength(queryResult, row, PQfnumber(queryResult,"qgs_feature_geometry"));
493-
if(returnedLength > 0)
494-
{
495-
unsigned char *featureGeom = new unsigned char[returnedLength + 1];
496-
memset(featureGeom, '\0', returnedLength + 1);
497-
memcpy(featureGeom, PQgetvalue(queryResult, row, PQfnumber(queryResult,QString("qgs_feature_geometry").toUtf8())), returnedLength);
498-
feature.setGeometryAndOwnership(featureGeom, returnedLength + 1);
499-
}
500-
else
501-
{
502-
feature.setGeometryAndOwnership(0, 0);
503-
QgsDebugMsg("Couldn't get the feature geometry in binary form");
504-
}
505-
}
513+
return false;
514+
}
506515
}
507516

508517
void QgsPostgresProvider::select(QgsAttributeList fetchAttributes, QgsRect rect, bool fetchGeometry, bool useIntersect)
@@ -552,8 +561,9 @@ void QgsPostgresProvider::select(QgsAttributeList fetchAttributes, QgsRect rect,
552561

553562
mFetchGeom = fetchGeometry;
554563
mAttributesToFetch = fetchAttributes;
555-
declareCursor( cursorName, fetchAttributes, fetchGeometry, whereClause, mFetchAttributeNames );
556-
564+
if( !declareCursor( cursorName, fetchAttributes, fetchGeometry, whereClause ) )
565+
return;
566+
557567
mFetching = true;
558568
mFirstFetch = true;
559569
}
@@ -594,7 +604,7 @@ bool QgsPostgresProvider::getNextFeature(QgsFeature& feature)
594604
for (int row = 0; row < rows; row++)
595605
{
596606
mFeatureQueue.push(QgsFeature());
597-
getFeature(queryResult, row, mFetchGeom, mFeatureQueue.back(), mFetchAttributeNames, mAttributesToFetch);
607+
getFeature(queryResult, row, mFetchGeom, mFeatureQueue.back(), mAttributesToFetch);
598608
} // for each row in queue
599609

600610
PQclear(queryResult);
@@ -625,9 +635,9 @@ bool QgsPostgresProvider::getNextFeature(QgsFeature& feature)
625635

626636
bool QgsPostgresProvider::getFeatureAtId(int featureId, QgsFeature& feature, bool fetchGeometry, QgsAttributeList fetchAttributes)
627637
{
628-
QStringList attributeNames;
629638
QString cursorName = QString("qgisfid%1").arg(providerId);
630-
declareCursor( cursorName, fetchAttributes, fetchGeometry, QString("%2=%3").arg(quotedIdentifier(primaryKey)).arg(featureId), attributeNames );
639+
if( !declareCursor( cursorName, fetchAttributes, fetchGeometry, QString("%2=%3").arg(quotedIdentifier(primaryKey)).arg(featureId) ) )
640+
return false;
631641

632642
PGresult *queryResult = PQexec(connection, QString("fetch forward 1 from %1").arg(cursorName).toUtf8());
633643
if(queryResult==0)
@@ -646,12 +656,13 @@ bool QgsPostgresProvider::getFeatureAtId(int featureId, QgsFeature& feature, boo
646656
QgsDebugMsg( QString("found %1 features instead of just one.").arg(rows) );
647657
}
648658

649-
getFeature(queryResult, 0, fetchGeometry, feature, attributeNames, fetchAttributes);
659+
bool gotit = getFeature(queryResult, 0, fetchGeometry, feature, fetchAttributes);
650660

651661
PQclear(queryResult);
652662

653663
PQexecNR(connection, QString("CLOSE %1").arg(cursorName).toUtf8());
654-
return true;
664+
665+
return gotit;
655666
}
656667

657668

@@ -690,6 +701,18 @@ long QgsPostgresProvider::featureCount() const
690701
return numberFeatures;
691702
}
692703

704+
const QgsField &QgsPostgresProvider::field(int index) const
705+
{
706+
QgsFieldMap::const_iterator it = attributeFields.find(index);
707+
708+
if( it==attributeFields.constEnd() ) {
709+
QgsDebugMsg("Field " + QString::number(index) + " not found.");
710+
throw PGFieldNotFound();
711+
}
712+
713+
return it.value();
714+
}
715+
693716
/**
694717
* Return the number of fields
695718
*/
@@ -1577,83 +1600,103 @@ void QgsPostgresProvider::findColumns(tableCols& cols)
15771600
// Returns the minimum value of an attribute
15781601
QVariant QgsPostgresProvider::minValue(int index)
15791602
{
1580-
// get the field name
1581-
QgsField fld = attributeFields[index];
1582-
QString sql;
1583-
if(sqlWhereClause.isEmpty())
1603+
try
15841604
{
1585-
sql = QString("select min(%1) from %2")
1586-
.arg(quotedIdentifier(fld.name()))
1587-
.arg(mSchemaTableName);
1605+
// get the field name
1606+
const QgsField &fld = field(index);
1607+
QString sql;
1608+
if(sqlWhereClause.isEmpty())
1609+
{
1610+
sql = QString("select min(%1) from %2")
1611+
.arg(quotedIdentifier(fld.name()))
1612+
.arg(mSchemaTableName);
1613+
}
1614+
else
1615+
{
1616+
sql = QString("select min(%1) from %2 where %3")
1617+
.arg(quotedIdentifier(fld.name()))
1618+
.arg(mSchemaTableName)
1619+
.arg(sqlWhereClause);
1620+
}
1621+
PGresult *rmin = PQexec(connection, sql.toUtf8());
1622+
QString minValue = QString::fromUtf8(PQgetvalue(rmin,0,0));
1623+
PQclear(rmin);
1624+
return minValue.toDouble();
15881625
}
1589-
else
1626+
catch(PGFieldNotFound)
15901627
{
1591-
sql = QString("select min(%1) from %2 where %3")
1592-
.arg(quotedIdentifier(fld.name()))
1593-
.arg(mSchemaTableName)
1594-
.arg(sqlWhereClause);
1628+
return QVariant(QString::null);
15951629
}
1596-
PGresult *rmin = PQexec(connection, sql.toUtf8());
1597-
QString minValue = QString::fromUtf8(PQgetvalue(rmin,0,0));
1598-
PQclear(rmin);
1599-
return minValue.toDouble();
16001630
}
16011631

16021632
// Returns the list of unique values of an attribute
16031633
void QgsPostgresProvider::getUniqueValues(int index, QStringList &uniqueValues)
16041634
{
1605-
// get the field name
1606-
QgsField fld = attributeFields[index];
1607-
QString sql;
1608-
if(sqlWhereClause.isEmpty())
1609-
{
1610-
sql = QString("select distinct %1 from %2 order by %1")
1611-
.arg(quotedIdentifier(fld.name()))
1612-
.arg(mSchemaTableName);
1613-
}
1614-
else
1615-
{
1616-
sql = QString("select distinct %1 from %2 where %3 order by %1")
1617-
.arg(quotedIdentifier(fld.name()))
1618-
.arg(mSchemaTableName)
1619-
.arg(sqlWhereClause);
1620-
}
1621-
16221635
uniqueValues.clear();
16231636

1624-
PGresult *res= PQexec(connection, sql.toUtf8());
1625-
if (PQresultStatus(res) == PGRES_TUPLES_OK)
1637+
try
16261638
{
1627-
for(int i=0; i<PQntuples(res); i++)
1628-
uniqueValues.append( QString::fromUtf8(PQgetvalue(res,i,0)) );
1639+
// get the field name
1640+
const QgsField &fld = field(index);
1641+
QString sql;
1642+
if(sqlWhereClause.isEmpty())
1643+
{
1644+
sql = QString("select distinct %1 from %2 order by %1")
1645+
.arg(quotedIdentifier(fld.name()))
1646+
.arg(mSchemaTableName);
1647+
}
1648+
else
1649+
{
1650+
sql = QString("select distinct %1 from %2 where %3 order by %1")
1651+
.arg(quotedIdentifier(fld.name()))
1652+
.arg(mSchemaTableName)
1653+
.arg(sqlWhereClause);
1654+
}
1655+
1656+
PGresult *res= PQexec(connection, sql.toUtf8());
1657+
if (PQresultStatus(res) == PGRES_TUPLES_OK)
1658+
{
1659+
for(int i=0; i<PQntuples(res); i++)
1660+
uniqueValues.append( QString::fromUtf8(PQgetvalue(res,i,0)) );
1661+
}
1662+
PQclear(res);
1663+
}
1664+
catch(PGFieldNotFound)
1665+
{
16291666
}
1630-
PQclear(res);
16311667
}
16321668

16331669
// Returns the maximum value of an attribute
16341670

16351671
QVariant QgsPostgresProvider::maxValue(int index)
16361672
{
1637-
// get the field name
1638-
QgsField fld = attributeFields[index];
1639-
QString sql;
1640-
if(sqlWhereClause.isEmpty())
1673+
try
16411674
{
1642-
sql = QString("select max(%1) from %2")
1643-
.arg(quotedIdentifier(fld.name()))
1644-
.arg(mSchemaTableName);
1675+
// get the field name
1676+
const QgsField &fld = field(index);
1677+
QString sql;
1678+
if(sqlWhereClause.isEmpty())
1679+
{
1680+
sql = QString("select max(%1) from %2")
1681+
.arg(quotedIdentifier(fld.name()))
1682+
.arg(mSchemaTableName);
1683+
}
1684+
else
1685+
{
1686+
sql = QString("select max(%1) from %2 where %3")
1687+
.arg(quotedIdentifier(fld.name()))
1688+
.arg(mSchemaTableName)
1689+
.arg(sqlWhereClause);
1690+
}
1691+
PGresult *rmax = PQexec(connection, sql.toUtf8());
1692+
QString maxValue = QString::fromUtf8(PQgetvalue(rmax,0,0));
1693+
PQclear(rmax);
1694+
return maxValue.toDouble();
16451695
}
1646-
else
1696+
catch(PGFieldNotFound)
16471697
{
1648-
sql = QString("select max(%1) from %2 where %3")
1649-
.arg(quotedIdentifier(fld.name()))
1650-
.arg(mSchemaTableName)
1651-
.arg(sqlWhereClause);
1652-
}
1653-
PGresult *rmax = PQexec(connection, sql.toUtf8());
1654-
QString maxValue = QString::fromUtf8(PQgetvalue(rmax,0,0));
1655-
PQclear(rmax);
1656-
return maxValue.toDouble();
1698+
return QVariant(QString::null);
1699+
}
16571700
}
16581701

16591702

@@ -1679,33 +1722,39 @@ bool QgsPostgresProvider::isValid(){
16791722

16801723
QVariant QgsPostgresProvider::getDefaultValue(int fieldId)
16811724
{
1682-
// Get the default column value from the Postgres information
1683-
// schema. If there is no default we return an empty string.
1684-
1685-
// Maintaining a cache of the results of this query would be quite
1686-
// simple and if this query is called lots, could save some time.
1725+
try
1726+
{
1727+
// Get the default column value from the Postgres information
1728+
// schema. If there is no default we return an empty string.
16871729

1688-
QString fieldName = attributeFields[fieldId].name();
1730+
// Maintaining a cache of the results of this query would be quite
1731+
// simple and if this query is called lots, could save some time.
1732+
QString fieldName = field(fieldId).name();
16891733

1690-
QString sql("SELECT column_default FROM"
1734+
QString sql("SELECT column_default FROM"
16911735
" information_schema.columns WHERE"
16921736
" column_default IS NOT NULL"
16931737
" AND table_schema = " + quotedValue(mSchemaName) +
16941738
" AND table_name = " + quotedValue(mTableName) +
16951739
" AND column_name = " + quotedValue(fieldName) );
16961740

1697-
QVariant defaultValue(QString::null);
1741+
QVariant defaultValue(QString::null);
16981742

1699-
PGresult* result = PQexec(connection, sql.toUtf8());
1743+
PGresult* result = PQexec(connection, sql.toUtf8());
17001744

1701-
if (PQntuples(result)==1 && !PQgetisnull(result, 0, 0) )
1702-
defaultValue = QString::fromUtf8(PQgetvalue(result, 0, 0));
1745+
if (PQntuples(result)==1 && !PQgetisnull(result, 0, 0) )
1746+
defaultValue = QString::fromUtf8(PQgetvalue(result, 0, 0));
17031747

1704-
// QgsDebugMsg( QString("defaultValue for %1 is NULL: %2").arg(fieldId).arg( defaultValue.isNull() ) );
1748+
// QgsDebugMsg( QString("defaultValue for %1 is NULL: %2").arg(fieldId).arg( defaultValue.isNull() ) );
17051749

1706-
PQclear(result);
1750+
PQclear(result);
17071751

1708-
return defaultValue;
1752+
return defaultValue;
1753+
}
1754+
catch(PGFieldNotFound)
1755+
{
1756+
return QVariant(QString::null);
1757+
}
17091758
}
17101759

17111760
/**
@@ -2035,20 +2084,27 @@ bool QgsPostgresProvider::changeAttributeValues(const QgsChangedAttributesMap &
20352084
// cycle through the changed attributes of the feature
20362085
for(QgsAttributeMap::const_iterator siter = attrs.begin(); siter != attrs.end(); ++siter)
20372086
{
2038-
QString fieldName = attributeFields[siter.key()].name();
2039-
2040-
QString sql = QString("UPDATE %1 SET %2=%3 WHERE %4=%5")
2041-
.arg( mSchemaTableName )
2042-
.arg( quotedIdentifier(fieldName) )
2043-
.arg( quotedValue( siter->toString() ) )
2044-
.arg( quotedIdentifier(primaryKey) )
2045-
.arg( fid );
2046-
QgsDebugMsg(sql);
2047-
2048-
PGresult* result=PQexec(connection, sql.toUtf8());
2049-
if( result==0 || PQresultStatus(result)==PGRES_FATAL_ERROR )
2050-
throw PGException(result);
2051-
PQclear(result);
2087+
try {
2088+
QString fieldName = field(siter.key()).name();
2089+
2090+
QString sql = QString("UPDATE %1 SET %2=%3 WHERE %4=%5")
2091+
.arg( mSchemaTableName )
2092+
.arg( quotedIdentifier(fieldName) )
2093+
.arg( quotedValue( siter->toString() ) )
2094+
.arg( quotedIdentifier(primaryKey) )
2095+
.arg( fid );
2096+
QgsDebugMsg(sql);
2097+
2098+
PGresult* result=PQexec(connection, sql.toUtf8());
2099+
if( result==0 || PQresultStatus(result)==PGRES_FATAL_ERROR )
2100+
throw PGException(result);
2101+
2102+
PQclear(result);
2103+
}
2104+
catch(PGFieldNotFound)
2105+
{
2106+
// Field was missing - shouldn't happen
2107+
}
20522108
}
20532109
}
20542110

@@ -2149,7 +2205,7 @@ bool QgsPostgresProvider::changeGeometryValues(QgsGeometryMap & geometry_map)
21492205
QgsAttributeList QgsPostgresProvider::allAttributesList()
21502206
{
21512207
QgsAttributeList attributes;
2152-
for(QgsFieldMap::iterator it = attributeFields.begin(); it != attributeFields.end(); ++it)
2208+
for(QgsFieldMap::const_iterator it = attributeFields.constBegin(); it != attributeFields.constEnd(); ++it)
21532209
{
21542210
attributes.push_back(it.key());
21552211
}
@@ -2607,21 +2663,25 @@ QString QgsPostgresProvider::quotedValue( QString value ) const
26072663
return value.prepend("'").append("'");
26082664
}
26092665

2610-
void QgsPostgresProvider::PQexecNR(PGconn *conn, const char *query)
2666+
bool QgsPostgresProvider::PQexecNR(PGconn *conn, const char *query)
26112667
{
26122668
PGresult *res = PQexec(conn, query);
26132669
if(res)
26142670
{
2671+
int errorStatus = PQresultStatus(res);
26152672
QgsDebugMsgLevel( QString("Query: %1 returned %2 [%3]")
26162673
.arg(query)
2617-
.arg(PQresStatus(PQresultStatus(res)))
2674+
.arg(errorStatus)
26182675
.arg(PQresultErrorMessage(res)), 3 );
26192676
PQclear(res);
2677+
2678+
return errorStatus==PGRES_COMMAND_OK;
26202679
}
26212680
else
26222681
{
26232682
QgsDebugMsgLevel( QString("Query: %1 returned no result buffer").arg(query), 3 );
26242683
}
2684+
return false;
26252685
}
26262686

26272687
void QgsPostgresProvider::showMessageBox(const QString& title, const QString& text)

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,6 @@ class QgsPostgresProvider:public QgsVectorDataProvider
110110
bool fetchGeometry = true,
111111
QgsAttributeList fetchAttributes = QgsAttributeList());
112112

113-
void declareCursor(const QString &cursorName,
114-
const QgsAttributeList &fetchAttributes,
115-
bool fetchGeometry,
116-
QString whereClause,
117-
QStringList &attributeNames);
118-
119-
void getFeature(PGresult *queryResult, int row, bool fetchGeometry,
120-
QgsFeature &feature,
121-
const QStringList &attributeNames,
122-
const QgsAttributeList &fetchAttributes);
123-
124113
/** Get the feature type. This corresponds to
125114
* WKBPoint,
126115
* WKBLineString,
@@ -336,6 +325,17 @@ class QgsPostgresProvider:public QgsVectorDataProvider
336325
private:
337326
int providerId; // id to append to provider specific identified (like cursors)
338327

328+
bool declareCursor(const QString &cursorName,
329+
const QgsAttributeList &fetchAttributes,
330+
bool fetchGeometry,
331+
QString whereClause);
332+
333+
bool getFeature(PGresult *queryResult, int row, bool fetchGeometry,
334+
QgsFeature &feature,
335+
const QgsAttributeList &fetchAttributes);
336+
337+
const QgsField &field(int index) const;
338+
339339
/** Double quote a PostgreSQL identifier for placement in a SQL string.
340340
*/
341341
QString quotedIdentifier( QString ident ) const;
@@ -453,9 +453,6 @@ class QgsPostgresProvider:public QgsVectorDataProvider
453453
*/
454454
bool swapEndian;
455455

456-
/**Stores the names of the attributes to fetch*/
457-
QStringList mFetchAttributeNames;
458-
459456
bool deduceEndian();
460457
bool getGeometryDetails();
461458

@@ -483,6 +480,9 @@ class QgsPostgresProvider:public QgsVectorDataProvider
483480
QString column_type;
484481
};
485482

483+
struct PGFieldNotFound {
484+
};
485+
486486
struct PGException {
487487
PGException(PGresult *r) : result(r)
488488
{
@@ -603,7 +603,7 @@ class QgsPostgresProvider:public QgsVectorDataProvider
603603
QByteArray paramValue(QString fieldvalue, const QString &defaultValue) const;
604604

605605
// run a query and free result buffer
606-
static void PQexecNR(PGconn *conn, const char *query);
606+
static bool PQexecNR(PGconn *conn, const char *query);
607607

608608
struct Conn
609609
{

0 commit comments

Comments
 (0)
Please sign in to comment.