@@ -487,6 +487,8 @@ void QgsMssqlSourceSelect::on_btnConnect_clicked()
487487
488488 bool allowGeometrylessTables = cbxAllowGeometrylessTables->isChecked ();
489489
490+ bool estimateMetadata = settings.value ( key + " /estimatedMetadata" , true ).toBool ();
491+
490492 mConnInfo = " dbname='" + database + " ' host=" + host + " user='" + username + " ' password='" + password + " '" ;
491493 if ( !service.isEmpty () )
492494 mConnInfo += " service='" + service + " '" ;
@@ -567,7 +569,7 @@ void QgsMssqlSourceSelect::on_btnConnect_clicked()
567569 {
568570 if ( type == " GEOMETRY" || type.isNull () || srid.isEmpty () )
569571 {
570- addSearchGeometryColumn ( connectionName, layer );
572+ addSearchGeometryColumn ( connectionName, layer, estimateMetadata );
571573 type = " " ;
572574 srid = " " ;
573575 }
@@ -671,12 +673,12 @@ void QgsMssqlSourceSelect::setSql( const QModelIndex &index )
671673 delete vlayer;
672674}
673675
674- void QgsMssqlSourceSelect::addSearchGeometryColumn ( QString connectionName, QgsMssqlLayerProperty layerProperty )
676+ void QgsMssqlSourceSelect::addSearchGeometryColumn ( QString connectionName, QgsMssqlLayerProperty layerProperty, bool estimateMetadata )
675677{
676678 // store the column details and do the query in a thread
677679 if ( !mColumnTypeThread )
678680 {
679- mColumnTypeThread = new QgsMssqlGeomColumnTypeThread ( connectionName, mUseEstimatedMetadata );
681+ mColumnTypeThread = new QgsMssqlGeomColumnTypeThread ( connectionName, estimateMetadata );
680682
681683 connect ( mColumnTypeThread , SIGNAL ( setLayerType ( QgsMssqlLayerProperty ) ),
682684 this , SLOT ( setLayerType ( QgsMssqlLayerProperty ) ) );
@@ -753,33 +755,20 @@ void QgsMssqlGeomColumnTypeThread::run()
753755 if ( !mStopped )
754756 {
755757 QString table;
756- if ( mUseEstimatedMetadata )
757- {
758- table = QString ( " (SELECT TOP %1 [%2] FROM [%3].[%4] WHERE [%2] IS NOT NULL%5) AS t" )
759- .arg ( 100 )
760- .arg ( layerProperty.geometryColName )
761- .arg ( layerProperty.schemaName )
762- .arg ( layerProperty.tableName )
763- .arg ( layerProperty.sql .isEmpty () ? " " : QString ( " AND (%1)" ).arg ( layerProperty.sql ) );
764- }
765- else if ( !layerProperty.schemaName .isEmpty () )
766- {
767- table = QString ( " [%1].[%2]%3" )
768- .arg ( layerProperty.schemaName )
769- .arg ( layerProperty.tableName )
770- .arg ( layerProperty.sql .isEmpty () ? " " : QString ( " WHERE %1" ).arg ( layerProperty.sql ) );
771- }
772- else
773- {
774- table = QString ( " [%1]%2" )
775- .arg ( layerProperty.tableName )
776- .arg ( layerProperty.sql .isEmpty () ? " " : QString ( " WHERE %1" ).arg ( layerProperty.sql ) );
777- }
778-
779- QString query = QString ( " SELECT DISTINCT CASE WHEN [%1].STGeometryType() IN ('Point', 'MultiPoint') THEN 'POINT' WHEN [%1].STGeometryType() IN ('Linestring', 'MultiLinestring') THEN 'LINESTRING' WHEN [%1].STGeometryType() IN ('Polygon', 'MultiPolygon') THEN 'POLYGON' ELSE UPPER([%1].STGeometryType()) END, [%1].STSrid FROM %2" )
780- .arg ( layerProperty.geometryColName )
781- .arg ( table );
782-
758+ table = QString ( " %1[%2]" )
759+ .arg ( layerProperty.schemaName .isEmpty () ? " " : QString (" [%1]." ).arg ( layerProperty.schemaName ))
760+ .arg ( layerProperty.tableName );
761+
762+ QString query = QString (" SELECT %3"
763+ " UPPER([%1].STGeometryType()),"
764+ " [%1].STSrid"
765+ " FROM %2"
766+ " WHERE [%1] IS NOT NULL %4"
767+ " GROUP BY [%1].STGeometryType(), [%1].STSrid" )
768+ .arg ( layerProperty.geometryColName )
769+ .arg ( table )
770+ .arg ( mUseEstimatedMetadata ? " TOP 1" : " " )
771+ .arg ( layerProperty.sql .isEmpty () ? " " : QString ( " AND %1" ).arg ( layerProperty.sql ) );
783772
784773 // issue the sql query
785774 QSqlDatabase db = QSqlDatabase::database ( mConnectionName );
0 commit comments