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

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Dec 14 08:02:08 EST 2009


Author: jef
Date: 2009-12-14 08:02:07 -0500 (Mon, 14 Dec 2009)
New Revision: 12450

Modified:
   trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
   trunk/qgis/src/providers/postgres/qgspostgresprovider.h
Log:
better fix for slowdown of r12418

Modified: trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
===================================================================
--- trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2009-12-14 10:51:02 UTC (rev 12449)
+++ trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2009-12-14 13:02:07 UTC (rev 12450)
@@ -55,7 +55,8 @@
     : QgsVectorDataProvider( uri ),
     mFetching( false ),
     geomType( QGis::WKBUnknown ),
-    mFeatureQueueSize( 200 )
+    mFeatureQueueSize( 200 ),
+    mPrimaryKeyDefault( QString::null )
 {
   // assume this is a valid layer until we determine otherwise
   valid = true;
@@ -1064,13 +1065,13 @@
       }
       else
       {
-        primaryKeyDefault = defaultValue( primaryKey ).toString();
-        if ( primaryKeyDefault.isNull() )
+        mPrimaryKeyDefault = defaultValue( primaryKey ).toString();
+        if ( mPrimaryKeyDefault.isNull() )
         {
-          primaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
-                              .arg( quotedIdentifier( primaryKey ) )
-                              .arg( quotedIdentifier( mSchemaName ) )
-                              .arg( quotedIdentifier( mTableName ) );
+          mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
+                               .arg( quotedIdentifier( primaryKey ) )
+                               .arg( quotedIdentifier( mSchemaName ) )
+                               .arg( quotedIdentifier( mTableName ) );
         }
       }
     }
@@ -1093,6 +1094,8 @@
           type = PQgetvalue( result, 0, 0 );
         }
 
+        // mPrimaryKeyDefault stays null and is retrieved later on demand
+
         if (( type != "int4" && type != "oid" ) ||
             !uniqueData( mSchemaName, mTableName, primaryKey ) )
         {
@@ -1100,33 +1103,10 @@
         }
       }
 
-      // Have a poke around the view to see if any of the columns
-      // could be used as the primary key.
-      tableCols cols;
-
-      // Given a schema.view, populate the cols variable with the
-      // schema.table.column's that underly the view columns.
-      findColumns( cols );
-
       if ( primaryKey.isEmpty() )
       {
-        // From the view columns, choose one for which the underlying
-        // column is suitable for use as a key into the view.
-        primaryKey = chooseViewColumn( cols );
+        parseView();
       }
-
-      tableCols::const_iterator it = cols.find( primaryKey );
-      if ( it != cols.end() )
-      {
-        primaryKeyDefault = defaultValue( it->second.column, it->second.relation, it->second.schema ).toString();
-        if ( primaryKeyDefault.isNull() )
-        {
-          primaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
-                              .arg( quotedIdentifier( it->second.column ) )
-                              .arg( quotedIdentifier( it->second.schema ) )
-                              .arg( quotedIdentifier( it->second.relation ) );
-        }
-      }
     }
     else
       QgsDebugMsg( "Unexpected relation type of '" + type + "'." );
@@ -1227,13 +1207,13 @@
     }
     else
     {
-      primaryKeyDefault = defaultValue( primaryKey ).toString();
-      if ( primaryKeyDefault.isNull() )
+      mPrimaryKeyDefault = defaultValue( primaryKey ).toString();
+      if ( mPrimaryKeyDefault.isNull() )
       {
-        primaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
-                            .arg( quotedIdentifier( primaryKey ) )
-                            .arg( quotedIdentifier( mSchemaName ) )
-                            .arg( quotedIdentifier( mTableName ) );
+        mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
+                             .arg( quotedIdentifier( primaryKey ) )
+                             .arg( quotedIdentifier( mSchemaName ) )
+                             .arg( quotedIdentifier( mTableName ) );
       }
     }
   }
@@ -1250,11 +1230,58 @@
   return primaryKey;
 }
 
+void QgsPostgresProvider::parseView()
+{
+  // Have a poke around the view to see if any of the columns
+  // could be used as the primary key.
+  tableCols cols;
+
+  // Given a schema.view, populate the cols variable with the
+  // schema.table.column's that underly the view columns.
+  findColumns( cols );
+
+  // pick the primary key, if we don't have one yet
+  if ( primaryKey.isEmpty() )
+  {
+    // From the view columns, choose one for which the underlying
+    // column is suitable for use as a key into the view.
+    primaryKey = chooseViewColumn( cols );
+  }
+
+  tableCols::const_iterator it = cols.find( primaryKey );
+  if ( it != cols.end() )
+  {
+    mPrimaryKeyDefault = defaultValue( it->second.column, it->second.relation, it->second.schema ).toString();
+    if ( mPrimaryKeyDefault.isNull() )
+    {
+      mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
+                           .arg( quotedIdentifier( it->second.column ) )
+                           .arg( quotedIdentifier( it->second.schema ) )
+                           .arg( quotedIdentifier( it->second.relation ) );
+    }
+  }
+  else
+  {
+    mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
+                         .arg( quotedIdentifier( primaryKey ) )
+                         .arg( quotedIdentifier( mSchemaName ) )
+                         .arg( quotedIdentifier( mTableName ) );
+  }
+}
+
+QString QgsPostgresProvider::primaryKeyDefault()
+{
+  if ( mPrimaryKeyDefault.isNull() )
+    parseView();
+
+  return mPrimaryKeyDefault;
+}
+
 // Given the table and column that each column in the view refers to,
 // choose one. Prefers column with an index on them, but will
 // otherwise choose something suitable.
 
-QString QgsPostgresProvider::chooseViewColumn( const tableCols& cols )
+QString QgsPostgresProvider::chooseViewColumn( const tableCols &cols )
 {
   // For each relation name and column name need to see if it
   // has unique constraints on it, or is a primary key (if not,
@@ -2206,7 +2233,7 @@
 
       if ( primaryKeyType != "tid" )
       {
-        int id = paramValue( primaryKeyDefault, primaryKeyDefault ).toInt();
+        int id = paramValue( primaryKeyDefault(), primaryKeyDefault() ).toInt();
         params << QString::number( id );
         newIds << id;
       }

Modified: trunk/qgis/src/providers/postgres/qgspostgresprovider.h
===================================================================
--- trunk/qgis/src/providers/postgres/qgspostgresprovider.h	2009-12-14 10:51:02 UTC (rev 12449)
+++ trunk/qgis/src/providers/postgres/qgspostgresprovider.h	2009-12-14 13:02:07 UTC (rev 12450)
@@ -409,10 +409,6 @@
      */
     QString primaryKey;
     /**
-     * Default value for primary key
-     */
-    QString primaryKeyDefault;
-    /**
      * Data type for the primary key
      */
     QString primaryKeyType;
@@ -699,6 +695,14 @@
     void disconnectDb();
 
     static int providerIds;
+
+    QString primaryKeyDefault();
+    void parseView();
+
+    /**
+     * Default value for primary key
+     */
+    QString mPrimaryKeyDefault;
 };
 
 #endif



More information about the QGIS-commit mailing list