[QGIS Commit] r10657 - in trunk/qgis: python/core src/core src/providers/postgres

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sun Apr 26 18:49:38 EDT 2009


Author: jef
Date: 2009-04-26 18:49:37 -0400 (Sun, 26 Apr 2009)
New Revision: 10657

Modified:
   trunk/qgis/python/core/qgsdatasourceuri.sip
   trunk/qgis/src/core/qgsdatasourceuri.cpp
   trunk/qgis/src/core/qgsdatasourceuri.h
   trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
Log:
speed up loading of projects with (complex) views:
save key column of postgresql layers to projects file and try it first on
reload.



Modified: trunk/qgis/python/core/qgsdatasourceuri.sip
===================================================================
--- trunk/qgis/python/core/qgsdatasourceuri.sip	2009-04-26 22:40:49 UTC (rev 10656)
+++ trunk/qgis/python/core/qgsdatasourceuri.sip	2009-04-26 22:49:37 UTC (rev 10657)
@@ -43,7 +43,8 @@
   void setDataSource(const QString& aSchema,
                      const QString& aTable,
                      const QString& aGeometryColumn,
-                     const QString& aSql = QString());
+                     const QString& aSql = QString(),
+                     const QString& aKeyColumn = QString());
 
   /** Removes password from uris 
    * @note this method was added in QGIS 1.1
@@ -64,4 +65,8 @@
   SSLmode sslMode() const;
 
   void setSql(QString sql);
+
+  // added in 1.2
+  QString keyColumn() const;
+  void setKeyColumn(QString column);
 };

Modified: trunk/qgis/src/core/qgsdatasourceuri.cpp
===================================================================
--- trunk/qgis/src/core/qgsdatasourceuri.cpp	2009-04-26 22:40:49 UTC (rev 10656)
+++ trunk/qgis/src/core/qgsdatasourceuri.cpp	2009-04-26 22:49:37 UTC (rev 10657)
@@ -23,12 +23,12 @@
 #include <QStringList>
 #include <QRegExp>
 
-QgsDataSourceURI::QgsDataSourceURI() : mSSLmode( SSLprefer )
+QgsDataSourceURI::QgsDataSourceURI() : mSSLmode( SSLprefer ), mKeyColumn( "" )
 {
   // do nothing
 }
 
-QgsDataSourceURI::QgsDataSourceURI( QString uri ) : mSSLmode( SSLprefer )
+QgsDataSourceURI::QgsDataSourceURI( QString uri ) : mSSLmode( SSLprefer ), mKeyColumn( "" )
 {
   int i = 0;
   while ( i < uri.length() )
@@ -104,6 +104,10 @@
           i++;
         }
       }
+      else if ( pname == "key" )
+      {
+        mKeyColumn = pval;
+      }
       else if ( pname == "service" )
       {
         QgsDebugMsg( "service keyword ignored" );
@@ -257,6 +261,16 @@
   return mGeometryColumn;
 }
 
+QString QgsDataSourceURI::keyColumn() const
+{
+  return mKeyColumn;
+}
+
+void QgsDataSourceURI::setKeyColumn( QString column )
+{
+  mKeyColumn = column;
+}
+
 void QgsDataSourceURI::setSql( QString sql )
 {
   mSql = sql;
@@ -376,7 +390,8 @@
 QString QgsDataSourceURI::uri() const
 {
   return connectionInfo()
-         + QString( " table=%1 (%2) sql=%3" )
+         + QString( " key='%1' table=%2 (%3) sql=%4" )
+         .arg( keyColumn() )
          .arg( quotedTablename() )
          .arg( mGeometryColumn )
          .arg( mSql );
@@ -408,10 +423,12 @@
 void QgsDataSourceURI::setDataSource( const QString &schema,
                                       const QString &table,
                                       const QString &geometryColumn,
+                                      const QString &keyColumn,
                                       const QString &sql )
 {
   mSchema = schema;
   mTable = table;
   mGeometryColumn = geometryColumn;
+  mKeyColumn = keyColumn;
   mSql = sql;
 }

Modified: trunk/qgis/src/core/qgsdatasourceuri.h
===================================================================
--- trunk/qgis/src/core/qgsdatasourceuri.h	2009-04-26 22:40:49 UTC (rev 10656)
+++ trunk/qgis/src/core/qgsdatasourceuri.h	2009-04-26 22:49:37 UTC (rev 10657)
@@ -61,7 +61,8 @@
     void setDataSource( const QString& aSchema,
                         const QString& aTable,
                         const QString& aGeometryColumn,
-                        const QString& aSql = QString() );
+                        const QString& aSql = QString(),
+                        const QString& aKeyColumn = QString() );
 
     //! Removes password element from uris
     static QString removePassword( const QString& aUri );
@@ -82,6 +83,10 @@
     QString password() const;
     enum SSLmode sslMode() const;
 
+    // added in version 1.2
+    QString keyColumn() const;
+    void setKeyColumn( QString column );
+
   private:
     void skipBlanks( const QString &uri, int &i );
     QString getValue( const QString &uri, int &i );
@@ -108,6 +113,8 @@
     QString mPassword;
     //! ssl mode
     enum SSLmode mSSLmode;
+    //! key column
+    QString mKeyColumn;
 };
 
 #endif //QGSDATASOURCEURI_H

Modified: trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
===================================================================
--- trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2009-04-26 22:40:49 UTC (rev 10656)
+++ trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2009-04-26 22:49:37 UTC (rev 10657)
@@ -74,6 +74,7 @@
   mTableName = mUri.table();
   geometryColumn = mUri.geometryColumn();
   sqlWhereClause = mUri.sql();
+  primaryKey = mUri.keyColumn();
 
   // Keep a schema qualified table name for convenience later on.
   mSchemaTableName = mUri.quotedTablename();
@@ -483,7 +484,7 @@
 
       if ( block > 0xffff )
       {
-        qWarning( "block number %x exceed 16 bit", block );
+        QgsDebugMsg( QString( "block number %1 exceeds 16 bit" ).arg( block ) );
         return false;
       }
 
@@ -491,7 +492,7 @@
     }
     else
     {
-      qWarning( "expecting 6 bytes for tid (found %d bytes)", PQgetlength( queryResult, row, 0 ) );
+      QgsDebugMsg( QString( "expecting 6 bytes for tid (found %1 bytes)" ).arg( PQgetlength( queryResult, row, 0 ) ) );
       return false;
     }
 
@@ -623,7 +624,7 @@
     QString fetch = QString( "fetch forward %1 from %2" ).arg( mFeatureQueueSize ).arg( cursorName );
     if ( connectionRO->PQsendQuery( fetch ) == 0 ) // fetch features asynchronously
     {
-      qWarning( "PQsendQuery failed (1)" );
+      QgsDebugMsg( "PQsendQuery failed (1)" );
     }
 
     Result queryResult;
@@ -948,12 +949,12 @@
     Result tableType = connectionRO->PQexec( sql );
     QString type = QString::fromUtf8( PQgetvalue( tableType, 0, 0 ) );
 
-    primaryKey = "";
-
     if ( type == "r" ) // the relation is a table
     {
       QgsDebugMsg( "Relation is a table. Checking to see if it has an oid column." );
 
+      primaryKey = "";
+
       // If there is an oid on the table, use that instead,
       // otherwise give up
       sql = QString( "SELECT attname FROM pg_attribute WHERE attname='oid' AND attrelid=regclass(%1)" )
@@ -1008,15 +1009,40 @@
     }
     else if ( type == "v" ) // the relation is a view
     {
-      // 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 );
-      // From the view columns, choose one for which the underlying
-      // column is suitable for use as a key into the view.
-      primaryKey = chooseViewColumn( cols );
+      if ( !primaryKey.isEmpty() )
+      {
+        // check last used candidate
+        sql = QString( "select pg_type.typname from pg_attribute,pg_type where atttypid=pg_type.oid and attname=%1 and attrelid=regclass(%2)" )
+              .arg( quotedValue( primaryKey ) ).arg( quotedValue( mSchemaTableName ) );
+
+        QgsDebugMsg( "checking candidate: " + sql );
+
+        Result result = connectionRO->PQexec( sql );
+
+        if ( PQresultStatus( result ) != PGRES_TUPLES_OK ||
+             PQntuples( result ) != 1 ||
+             QString( PQgetvalue( result, 0, 0 ) ) != "int4" ||
+             !uniqueData( mSchemaName, mTableName, primaryKey ) )
+        {
+          primaryKey = "";
+        }
+      }
+
+      if ( primaryKey.isEmpty() )
+      {
+        // 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 );
+        // From the view columns, choose one for which the underlying
+        // column is suitable for use as a key into the view.
+        primaryKey = chooseViewColumn( cols );
+
+        mUri.setKeyColumn( primaryKey );
+        setDataSourceUri( mUri.uri() );
+      }
     }
     else
       QgsDebugMsg( "Unexpected relation type of '" + type + "'." );



More information about the QGIS-commit mailing list