Skip to content
Permalink
Browse files

[mssql] Implement native uniqueStringsMatching method, should speed u…

…p provider queries
  • Loading branch information
nyalldawson committed Oct 5, 2018
1 parent 4986d43 commit 17b80c149246cced1749c2563f92927329defbdb
Showing with 53 additions and 0 deletions.
  1. +50 −0 src/providers/mssql/qgsmssqlprovider.cpp
  2. +3 −0 src/providers/mssql/qgsmssqlprovider.h
@@ -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
@@ -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;

0 comments on commit 17b80c1

Please sign in to comment.
You can’t perform that action at this time.