Skip to content

Commit

Permalink
[mssql] Implement native uniqueStringsMatching method, should speed u…
Browse files Browse the repository at this point in the history
…p provider queries
  • Loading branch information
nyalldawson committed Oct 5, 2018
1 parent 4986d43 commit 17b80c1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/providers/mssql/qgsmssqlprovider.cpp
Expand Up @@ -690,6 +690,56 @@ QSet<QVariant> QgsMssqlProvider::uniqueValues( int index, int limit ) const
return uniqueValues;
}

QStringList QgsMssqlProvider::uniqueStringsMatching( int index, const QString &substring, int limit, QgsFeedback *feedback ) const
{
QStringList results;

if ( index < 0 || index >= mAttributeFields.count() )
{
return results;
}

// get the field name
QgsField fld = mAttributeFields.at( index );
QString sql = QStringLiteral( "select distinct " );

if ( limit > 0 )
{
sql += QStringLiteral( " top %1 " ).arg( limit );
}

sql += QStringLiteral( "[%1] from " )
.arg( fld.name() );

sql += QStringLiteral( "[%1].[%2] WHERE" ).arg( mSchemaName, mTableName );

if ( !mSqlWhereClause.isEmpty() )
{
sql += QStringLiteral( " (%1) AND" ).arg( mSqlWhereClause );
}

sql += QStringLiteral( " [%1] LIKE '%%2%'" ).arg( fld.name(), substring );

QSqlQuery query = createQuery();
query.setForwardOnly( true );

if ( !query.exec( sql ) )
{
QgsDebugMsg( query.lastError().text() );
}

if ( query.isActive() )
{
// read all features
while ( query.next() )
{
results << query.value( 0 ).toString();
if ( feedback && feedback->isCanceled() )
break;
}
}
return results;
}

// update the extent, wkb type and srid for this layer
void QgsMssqlProvider::UpdateStatistics( bool estimate ) const
Expand Down
3 changes: 3 additions & 0 deletions src/providers/mssql/qgsmssqlprovider.h
Expand Up @@ -67,6 +67,9 @@ class QgsMssqlProvider : public QgsVectorDataProvider
QVariant minimumValue( int index ) const override;
QVariant maximumValue( int index ) const override;
QSet<QVariant> uniqueValues( int index, int limit = -1 ) const override;
QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
QgsFeedback *feedback = nullptr ) const override;

QgsFeatureIterator getFeatures( const QgsFeatureRequest &request ) const override;

QgsWkbTypes::Type wkbType() const override;
Expand Down

0 comments on commit 17b80c1

Please sign in to comment.