Skip to content

Commit 17b80c1

Browse files
committedOct 5, 2018
[mssql] Implement native uniqueStringsMatching method, should speed up provider queries
1 parent 4986d43 commit 17b80c1

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
 

‎src/providers/mssql/qgsmssqlprovider.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,56 @@ QSet<QVariant> QgsMssqlProvider::uniqueValues( int index, int limit ) const
690690
return uniqueValues;
691691
}
692692

693+
QStringList QgsMssqlProvider::uniqueStringsMatching( int index, const QString &substring, int limit, QgsFeedback *feedback ) const
694+
{
695+
QStringList results;
696+
697+
if ( index < 0 || index >= mAttributeFields.count() )
698+
{
699+
return results;
700+
}
701+
702+
// get the field name
703+
QgsField fld = mAttributeFields.at( index );
704+
QString sql = QStringLiteral( "select distinct " );
705+
706+
if ( limit > 0 )
707+
{
708+
sql += QStringLiteral( " top %1 " ).arg( limit );
709+
}
710+
711+
sql += QStringLiteral( "[%1] from " )
712+
.arg( fld.name() );
713+
714+
sql += QStringLiteral( "[%1].[%2] WHERE" ).arg( mSchemaName, mTableName );
715+
716+
if ( !mSqlWhereClause.isEmpty() )
717+
{
718+
sql += QStringLiteral( " (%1) AND" ).arg( mSqlWhereClause );
719+
}
720+
721+
sql += QStringLiteral( " [%1] LIKE '%%2%'" ).arg( fld.name(), substring );
722+
723+
QSqlQuery query = createQuery();
724+
query.setForwardOnly( true );
725+
726+
if ( !query.exec( sql ) )
727+
{
728+
QgsDebugMsg( query.lastError().text() );
729+
}
730+
731+
if ( query.isActive() )
732+
{
733+
// read all features
734+
while ( query.next() )
735+
{
736+
results << query.value( 0 ).toString();
737+
if ( feedback && feedback->isCanceled() )
738+
break;
739+
}
740+
}
741+
return results;
742+
}
693743

694744
// update the extent, wkb type and srid for this layer
695745
void QgsMssqlProvider::UpdateStatistics( bool estimate ) const

‎src/providers/mssql/qgsmssqlprovider.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class QgsMssqlProvider : public QgsVectorDataProvider
6767
QVariant minimumValue( int index ) const override;
6868
QVariant maximumValue( int index ) const override;
6969
QSet<QVariant> uniqueValues( int index, int limit = -1 ) const override;
70+
QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
71+
QgsFeedback *feedback = nullptr ) const override;
72+
7073
QgsFeatureIterator getFeatures( const QgsFeatureRequest &request ) const override;
7174

7275
QgsWkbTypes::Type wkbType() const override;

0 commit comments

Comments
 (0)