Skip to content

Commit 517b859

Browse files
committed
Faster listing of tables for MS SQL
1 parent dc50304 commit 517b859

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

src/providers/mssql/qgsmssqlsourceselect.cpp

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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 );

src/providers/mssql/qgsmssqlsourceselect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class QgsMssqlSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
158158
bool mEmbeddedMode;
159159

160160
// queue another query for the thread
161-
void addSearchGeometryColumn( QString connectionName, QgsMssqlLayerProperty layerProperty );
161+
void addSearchGeometryColumn(QString connectionName, QgsMssqlLayerProperty layerProperty , bool estimateMetadata);
162162

163163
// Set the position of the database connection list to the last
164164
// used one.

0 commit comments

Comments
 (0)