[QGIS Commit] r8313 - trunk/qgis/src/providers/postgres

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Apr 2 18:35:34 EDT 2008


Author: jef
Date: 2008-04-02 18:35:33 -0400 (Wed, 02 Apr 2008)
New Revision: 8313

Modified:
   trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
   trunk/qgis/src/providers/postgres/qgspostgresprovider.h
Log:
postgres provider update
- handle PQsendQuery in one getNextFeature run (fixes nested iteration of
  cursors with connection pooling)
- beautify DELETE and ALTER TABLE statements


Modified: trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
===================================================================
--- trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2008-04-02 18:31:09 UTC (rev 8312)
+++ trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2008-04-02 22:35:33 UTC (rev 8313)
@@ -564,7 +564,6 @@
     return;
   
   mFetching = true;
-  mFirstFetch = true;
 }
 
 bool QgsPostgresProvider::getNextFeature(QgsFeature& feature)
@@ -577,43 +576,37 @@
     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;
-    }
+      int rows = PQntuples(queryResult);
+      if (rows == 0)
+        continue;
 
-    for (int row = 0; row < rows; row++)
-    {
-      mFeatureQueue.push(QgsFeature());
-      getFeature(queryResult, row, mFetchGeom, mFeatureQueue.back(), mAttributesToFetch);
-    } // for each row in queue
+      for (int row = 0; row < rows; row++)
+      {
+        mFeatureQueue.push(QgsFeature());
+        getFeature(queryResult, row, mFetchGeom, mFeatureQueue.back(), mAttributesToFetch);
+      } // for each row in queue
 
-    PQclear(queryResult);
-
-    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 @@
     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 @@
 
     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 @@
         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());

Modified: trunk/qgis/src/providers/postgres/qgspostgresprovider.h
===================================================================
--- trunk/qgis/src/providers/postgres/qgspostgresprovider.h	2008-04-02 18:31:09 UTC (rev 8312)
+++ trunk/qgis/src/providers/postgres/qgspostgresprovider.h	2008-04-02 22:35:33 UTC (rev 8313)
@@ -349,7 +349,6 @@
     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,14 +363,7 @@
     //! 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
      */
     bool valid;



More information about the QGIS-commit mailing list