@@ -41,6 +41,10 @@ email : sherman at mrcc.com
4141#include < pg_config.h>
4242#endif
4343
44+ // Note: Because the the geometry type select SQL is also in the qgspostgresprovider
45+ // code this parameter is duplicated there.
46+ static const int sGeomTypeSelectLimit = 100 ;
47+
4448QgsPgSourceSelect::QgsPgSourceSelect ( QWidget *parent, Qt::WFlags fl )
4549 : QDialog( parent, fl ), mColumnTypeThread( NULL ), pd( 0 )
4650{
@@ -142,6 +146,7 @@ void QgsPgSourceSelect::on_btnDelete_clicked()
142146 settings.remove ( key + " /sslmode" );
143147 settings.remove ( key + " /publicOnly" );
144148 settings.remove ( key + " /geometryColumnsOnly" );
149+ settings.remove ( key + " /estimatedMetadata" );
145150 settings.remove ( key + " /saveUsername" );
146151 settings.remove ( key + " /savePassword" );
147152 settings.remove ( key + " /save" );
@@ -352,6 +357,11 @@ QString QgsPgSourceSelect::layerURI( const QModelIndex &index )
352357 uri += QString ( " key=\" %1\" " ).arg ( pkColumnName );
353358 }
354359
360+ if ( mUseEstimatedMetadata )
361+ {
362+ uri += QString ( " estimatedmetadata=true" );
363+ }
364+
355365 uri += QString ( " table=\" %1\" .\" %2\" (%3) sql=%4" )
356366 .arg ( schemaName ).arg ( tableName )
357367 .arg ( geomColumnName )
@@ -419,7 +429,7 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
419429
420430 bool searchPublicOnly = settings.value ( key + " /publicOnly" ).toBool ();
421431 bool searchGeometryColumnsOnly = settings.value ( key + " /geometryColumnsOnly" ).toBool ();
422-
432+ mUseEstimatedMetadata = settings. value ( key + " /estimatedMetadata " ). toBool ();
423433 // Need to escape the password to allow for single quotes and backslashes
424434
425435 QgsDebugMsg ( " Connection info: " + uri.connectionInfo () );
@@ -562,7 +572,7 @@ void QgsPgSourceSelect::addSearchGeometryColumn( const QString &schema, const QS
562572 if ( mColumnTypeThread == NULL )
563573 {
564574 mColumnTypeThread = new QgsGeomColumnTypeThread ();
565- mColumnTypeThread ->setConnInfo ( m_privConnInfo );
575+ mColumnTypeThread ->setConnInfo ( m_privConnInfo, mUseEstimatedMetadata );
566576 }
567577 mColumnTypeThread ->addGeometryColumn ( schema, table, column );
568578}
@@ -794,9 +804,10 @@ void QgsPgSourceSelect::setSearchExpression( const QString& regexp )
794804{
795805}
796806
797- void QgsGeomColumnTypeThread::setConnInfo ( QString s )
807+ void QgsGeomColumnTypeThread::setConnInfo ( QString conninfo, bool useEstimatedMetadata )
798808{
799- mConnInfo = s;
809+ mConnInfo = conninfo;
810+ mUseEstimatedMetadata = useEstimatedMetadata;
800811}
801812
802813void QgsGeomColumnTypeThread::addGeometryColumn ( QString schema, QString table, QString column )
@@ -828,8 +839,19 @@ void QgsGeomColumnTypeThread::getLayerTypes()
828839 " when geometrytype(%1) IN ('LINESTRING','MULTILINESTRING') THEN 'LINESTRING'"
829840 " when geometrytype(%1) IN ('POLYGON','MULTIPOLYGON') THEN 'POLYGON'"
830841 " end "
831- " from "
832- " \" %2\" .\" %3\" " ).arg ( " \" " + columns[i] + " \" " ).arg ( schemas[i] ).arg ( tables[i] );
842+ " from " ).arg ( " \" " + columns[i] + " \" " );
843+ if ( mUseEstimatedMetadata )
844+ {
845+ query += QString ( " (select %1 from %2 where %1 is not null limit %3) as t" )
846+ .arg ( " \" " + columns[i] + " \" " )
847+ .arg ( " \" " + schemas[i] + " \" .\" " + tables[i] + " \" " )
848+ .arg ( sGeomTypeSelectLimit );
849+ }
850+ else
851+ {
852+ query += " \" " + schemas[i] + " \" .\" " + tables[i] + " \" " ;
853+ }
854+
833855 PGresult* gresult = PQexec ( pd, query.toUtf8 () );
834856 QString type;
835857 if ( PQresultStatus ( gresult ) == PGRES_TUPLES_OK )
0 commit comments