Skip to content

Commit adaa577

Browse files
author
jef
committed
postgres provider update
- handle PQsendQuery in one getNextFeature run (fixes nested iteration of cursors with connection pooling) - beautify DELETE and ALTER TABLE statements git-svn-id: http://svn.osgeo.org/qgis/trunk@8313 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 6326891 commit adaa577

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

src/providers/postgres/qgspostgresprovider.cpp

+32-33
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,6 @@ void QgsPostgresProvider::select(QgsAttributeList fetchAttributes, QgsRect rect,
564564
return;
565565

566566
mFetching = true;
567-
mFirstFetch = true;
568567
}
569568

570569
bool QgsPostgresProvider::getNextFeature(QgsFeature& feature)
@@ -577,43 +576,37 @@ bool QgsPostgresProvider::getNextFeature(QgsFeature& feature)
577576
return false;
578577
}
579578

580-
// Top up our queue if it is empty
581-
if (mFeatureQueue.empty())
579+
if( mFeatureQueue.empty() )
582580
{
583581
QString fetch = QString("fetch forward %1 from %2").arg(mFeatureQueueSize).arg(cursorName);
584-
if(mFirstFetch)
582+
if(PQsendQuery(connection, fetch.toUtf8()) == 0) //fetch features in asynchronously
585583
{
586-
if(PQsendQuery(connection, fetch.toUtf8()) == 0) //fetch features in asynchronously
587-
{
588-
qWarning("PQsendQuery failed (1)");
589-
}
584+
qWarning("PQsendQuery failed (1)");
590585
}
591-
mFirstFetch = false;
592-
queryResult = PQgetResult(connection);
593-
PQgetResult(connection); //just to get the 0 pointer...
594586

595-
int rows = PQntuples(queryResult);
596-
if (rows == 0)
587+
PGresult *queryResult;
588+
while( (queryResult = PQgetResult(connection)) )
597589
{
598-
QgsDebugMsg("End of features");
599-
PQclear(queryResult);
600-
return false;
601-
}
602-
603-
for (int row = 0; row < rows; row++)
604-
{
605-
mFeatureQueue.push(QgsFeature());
606-
getFeature(queryResult, row, mFetchGeom, mFeatureQueue.back(), mAttributesToFetch);
607-
} // for each row in queue
590+
int rows = PQntuples(queryResult);
591+
if (rows == 0)
592+
continue;
608593

609-
PQclear(queryResult);
594+
for (int row = 0; row < rows; row++)
595+
{
596+
mFeatureQueue.push(QgsFeature());
597+
getFeature(queryResult, row, mFetchGeom, mFeatureQueue.back(), mAttributesToFetch);
598+
} // for each row in queue
610599

611-
if(PQsendQuery(connection, fetch.toUtf8()) == 0) //already fetch the next couple of features asynchronously
612-
{
613-
qWarning("PQsendQuery failed (2)");
600+
PQclear(queryResult);
614601
}
615-
} // if new queue is required
602+
}
616603

604+
if( mFeatureQueue.empty() )
605+
{
606+
QgsDebugMsg("End of features");
607+
return false;
608+
}
609+
617610
// Now return the next feature from the queue
618611
if(mFetchGeom)
619612
{
@@ -1890,8 +1883,10 @@ bool QgsPostgresProvider::deleteFeatures(const QgsFeatureIds & id)
18901883
PQexecNR(connection,QString("BEGIN").toUtf8());
18911884

18921885
for(QgsFeatureIds::const_iterator it=id.begin();it!=id.end();++it) {
1893-
QString sql("DELETE FROM "+mSchemaTableName+" WHERE "+quotedIdentifier(primaryKey)+"="+QString::number(*it));
1894-
1886+
QString sql = QString("DELETE FROM %1 WHERE %2=%3")
1887+
.arg(mSchemaTableName)
1888+
.arg(quotedIdentifier(primaryKey))
1889+
.arg(*it);
18951890
QgsDebugMsg("delete sql: "+sql);
18961891

18971892
//send DELETE statement and do error handling
@@ -1920,8 +1915,10 @@ bool QgsPostgresProvider::addAttributes(const QgsNewAttributesMap & name)
19201915

19211916
for(QgsNewAttributesMap::const_iterator iter=name.begin();iter!=name.end();++iter)
19221917
{
1923-
QString sql="ALTER TABLE "+mSchemaTableName+" ADD COLUMN "+quotedIdentifier(iter.key())+" " +iter.value();
1924-
1918+
QString sql = QString("ALTER TABLE %1 ADD COLUMN %2 %3")
1919+
.arg(mSchemaTableName)
1920+
.arg(quotedIdentifier(iter.key()))
1921+
.arg(iter.value());
19251922
QgsDebugMsg(sql);
19261923

19271924
//send sql statement and do error handling
@@ -1956,7 +1953,9 @@ bool QgsPostgresProvider::deleteAttributes(const QgsAttributeIds& ids)
19561953
continue;
19571954

19581955
QString column = field_it->name();
1959-
QString sql="ALTER TABLE "+mSchemaTableName+" DROP COLUMN "+quotedIdentifier(column);
1956+
QString sql = QString("ALTER TABLE %1 DROP COLUMN %2")
1957+
.arg(mSchemaTableName)
1958+
.arg(quotedIdentifier(column));
19601959

19611960
//send sql statement and do error handling
19621961
PGresult* result=PQexec(connection, sql.toUtf8());

src/providers/postgres/qgspostgresprovider.h

-8
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ class QgsPostgresProvider:public QgsVectorDataProvider
349349
void loadFields();
350350

351351
bool mFetching; // true if a cursor was declared
352-
bool mFirstFetch; // true if fetch forward is called the first time after select
353352
std::vector < QgsFeature > features;
354353
QgsFieldMap attributeFields;
355354
QString mDataComment;
@@ -364,13 +363,6 @@ class QgsPostgresProvider:public QgsVectorDataProvider
364363
//! Child thread for calculating count.
365364
QgsPostgresCountThread mCountThread;
366365

367-
368-
/**
369-
* Pointer to the PostgreSQL query result object. If this pointer is 0,
370-
* there is no current selection set. Any future getNextFeature requests
371-
* will require execution of the select query to recreate the result set.
372-
*/
373-
PGresult *queryResult;
374366
/**
375367
* Flag indicating if the layer data source is a valid PostgreSQL layer
376368
*/

0 commit comments

Comments
 (0)