Skip to content
Permalink
Browse files
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
  • Loading branch information
jef committed Apr 2, 2008
1 parent 6326891 commit adaa577fd40714c750436d885ca8cc1047709318
Showing with 32 additions and 41 deletions.
  1. +32 −33 src/providers/postgres/qgspostgresprovider.cpp
  2. +0 −8 src/providers/postgres/qgspostgresprovider.h
@@ -564,7 +564,6 @@ void QgsPostgresProvider::select(QgsAttributeList fetchAttributes, QgsRect rect,
return;

mFetching = true;
mFirstFetch = true;
}

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

// Top up our queue if it is empty
if (mFeatureQueue.empty())
if( mFeatureQueue.empty() )
{
QString fetch = QString("fetch forward %1 from %2").arg(mFeatureQueueSize).arg(cursorName);
if(mFirstFetch)
if(PQsendQuery(connection, fetch.toUtf8()) == 0) //fetch features in asynchronously
{
if(PQsendQuery(connection, fetch.toUtf8()) == 0) //fetch features in asynchronously
{
qWarning("PQsendQuery failed (1)");
}
qWarning("PQsendQuery failed (1)");
}
mFirstFetch = false;
queryResult = PQgetResult(connection);
PQgetResult(connection); //just to get the 0 pointer...

int rows = PQntuples(queryResult);
if (rows == 0)
PGresult *queryResult;
while( (queryResult = PQgetResult(connection)) )
{
QgsDebugMsg("End of features");
PQclear(queryResult);
return false;
}

for (int row = 0; row < rows; row++)
{
mFeatureQueue.push(QgsFeature());
getFeature(queryResult, row, mFetchGeom, mFeatureQueue.back(), mAttributesToFetch);
} // for each row in queue
int rows = PQntuples(queryResult);
if (rows == 0)
continue;

PQclear(queryResult);
for (int row = 0; row < rows; row++)
{
mFeatureQueue.push(QgsFeature());
getFeature(queryResult, row, mFetchGeom, mFeatureQueue.back(), mAttributesToFetch);
} // for each row in queue

if(PQsendQuery(connection, fetch.toUtf8()) == 0) //already fetch the next couple of features asynchronously
{
qWarning("PQsendQuery failed (2)");
PQclear(queryResult);
}
} // if new queue is required
}

if( mFeatureQueue.empty() )
{
QgsDebugMsg("End of features");
return false;
}

// Now return the next feature from the queue
if(mFetchGeom)
{
@@ -1890,8 +1883,10 @@ bool QgsPostgresProvider::deleteFeatures(const QgsFeatureIds & id)
PQexecNR(connection,QString("BEGIN").toUtf8());

for(QgsFeatureIds::const_iterator it=id.begin();it!=id.end();++it) {
QString sql("DELETE FROM "+mSchemaTableName+" WHERE "+quotedIdentifier(primaryKey)+"="+QString::number(*it));

QString sql = QString("DELETE FROM %1 WHERE %2=%3")
.arg(mSchemaTableName)
.arg(quotedIdentifier(primaryKey))
.arg(*it);
QgsDebugMsg("delete sql: "+sql);

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

for(QgsNewAttributesMap::const_iterator iter=name.begin();iter!=name.end();++iter)
{
QString sql="ALTER TABLE "+mSchemaTableName+" ADD COLUMN "+quotedIdentifier(iter.key())+" " +iter.value();

QString sql = QString("ALTER TABLE %1 ADD COLUMN %2 %3")
.arg(mSchemaTableName)
.arg(quotedIdentifier(iter.key()))
.arg(iter.value());
QgsDebugMsg(sql);

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

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

//send sql statement and do error handling
PGresult* result=PQexec(connection, sql.toUtf8());
@@ -349,7 +349,6 @@ class QgsPostgresProvider:public QgsVectorDataProvider
void loadFields();

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


/**
* Pointer to the PostgreSQL query result object. If this pointer is 0,
* there is no current selection set. Any future getNextFeature requests
* will require execution of the select query to recreate the result set.
*/
PGresult *queryResult;
/**
* Flag indicating if the layer data source is a valid PostgreSQL layer
*/

0 comments on commit adaa577

Please sign in to comment.