Skip to content

Commit

Permalink
Faster listing of tables for MS SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed Feb 18, 2014
1 parent dc50304 commit 517b859
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
49 changes: 19 additions & 30 deletions src/providers/mssql/qgsmssqlsourceselect.cpp
Expand Up @@ -487,6 +487,8 @@ void QgsMssqlSourceSelect::on_btnConnect_clicked()

bool allowGeometrylessTables = cbxAllowGeometrylessTables->isChecked();

bool estimateMetadata = settings.value( key + "/estimatedMetadata", true ).toBool();

mConnInfo = "dbname='" + database + "' host=" + host + " user='" + username + "' password='" + password + "'";
if ( !service.isEmpty() )
mConnInfo += " service='" + service + "'";
Expand Down Expand Up @@ -567,7 +569,7 @@ void QgsMssqlSourceSelect::on_btnConnect_clicked()
{
if ( type == "GEOMETRY" || type.isNull() || srid.isEmpty() )
{
addSearchGeometryColumn( connectionName, layer );
addSearchGeometryColumn( connectionName, layer, estimateMetadata );
type = "";
srid = "";
}
Expand Down Expand Up @@ -671,12 +673,12 @@ void QgsMssqlSourceSelect::setSql( const QModelIndex &index )
delete vlayer;
}

void QgsMssqlSourceSelect::addSearchGeometryColumn( QString connectionName, QgsMssqlLayerProperty layerProperty )
void QgsMssqlSourceSelect::addSearchGeometryColumn( QString connectionName, QgsMssqlLayerProperty layerProperty, bool estimateMetadata )
{
// store the column details and do the query in a thread
if ( !mColumnTypeThread )
{
mColumnTypeThread = new QgsMssqlGeomColumnTypeThread( connectionName, mUseEstimatedMetadata );
mColumnTypeThread = new QgsMssqlGeomColumnTypeThread( connectionName, estimateMetadata );

connect( mColumnTypeThread, SIGNAL( setLayerType( QgsMssqlLayerProperty ) ),
this, SLOT( setLayerType( QgsMssqlLayerProperty ) ) );
Expand Down Expand Up @@ -753,33 +755,20 @@ void QgsMssqlGeomColumnTypeThread::run()
if ( !mStopped )
{
QString table;
if ( mUseEstimatedMetadata )
{
table = QString( "(SELECT TOP %1 [%2] FROM [%3].[%4] WHERE [%2] IS NOT NULL%5) AS t" )
.arg( 100 )
.arg( layerProperty.geometryColName )
.arg( layerProperty.schemaName )
.arg( layerProperty.tableName )
.arg( layerProperty.sql.isEmpty() ? "" : QString( " AND (%1)" ).arg( layerProperty.sql ) );
}
else if ( !layerProperty.schemaName.isEmpty() )
{
table = QString( "[%1].[%2]%3" )
.arg( layerProperty.schemaName )
.arg( layerProperty.tableName )
.arg( layerProperty.sql.isEmpty() ? "" : QString( " WHERE %1" ).arg( layerProperty.sql ) );
}
else
{
table = QString( "[%1]%2" )
.arg( layerProperty.tableName )
.arg( layerProperty.sql.isEmpty() ? "" : QString( " WHERE %1" ).arg( layerProperty.sql ) );
}

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" )
.arg( layerProperty.geometryColName )
.arg( table );

table = QString( "%1[%2]" )
.arg( layerProperty.schemaName.isEmpty() ? "" : QString("[%1].").arg( layerProperty.schemaName ))
.arg( layerProperty.tableName );

QString query = QString("SELECT %3"
" UPPER([%1].STGeometryType()),"
" [%1].STSrid"
" FROM %2"
" WHERE [%1] IS NOT NULL %4"
" GROUP BY [%1].STGeometryType(), [%1].STSrid")
.arg( layerProperty.geometryColName )
.arg( table )
.arg( mUseEstimatedMetadata ? "TOP 1" : "" )
.arg( layerProperty.sql.isEmpty() ? "" : QString( " AND %1" ).arg( layerProperty.sql ) );

// issue the sql query
QSqlDatabase db = QSqlDatabase::database( mConnectionName );
Expand Down
2 changes: 1 addition & 1 deletion src/providers/mssql/qgsmssqlsourceselect.h
Expand Up @@ -158,7 +158,7 @@ class QgsMssqlSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
bool mEmbeddedMode;

// queue another query for the thread
void addSearchGeometryColumn( QString connectionName, QgsMssqlLayerProperty layerProperty );
void addSearchGeometryColumn(QString connectionName, QgsMssqlLayerProperty layerProperty , bool estimateMetadata);

// Set the position of the database connection list to the last
// used one.
Expand Down

0 comments on commit 517b859

Please sign in to comment.