Skip to content

Commit 2519df8

Browse files
committed
MSSQL provider: Implement minimumValue, maximumValue and uniqueValues (fixes #5610)
1 parent bb3e2f0 commit 2519df8

File tree

2 files changed

+157
-1
lines changed

2 files changed

+157
-1
lines changed

src/providers/mssql/qgsmssqlprovider.cpp

+127-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,10 @@ void QgsMssqlProvider::select( QgsAttributeList fetchAttributes,
604604

605605
if ( !mSqlWhereClause.isEmpty() )
606606
{
607-
mStatement += " and (" + mSqlWhereClause + ")";
607+
if ( rect.isEmpty() )
608+
mStatement += " where (" + mSqlWhereClause + ")";
609+
else
610+
mStatement += " and (" + mSqlWhereClause + ")";
608611
}
609612

610613
mFetchGeom = fetchGeometry;
@@ -626,6 +629,129 @@ void QgsMssqlProvider::select( QgsAttributeList fetchAttributes,
626629
}
627630
}
628631

632+
// Returns the minimum value of an attribute
633+
QVariant QgsMssqlProvider::minimumValue( int index )
634+
{
635+
// get the field name
636+
QgsField fld = mAttributeFields[ index ];
637+
QString sql = QString( "select min([%1]) from " )
638+
.arg( fld.name() );
639+
640+
if ( !mSchemaName.isEmpty() )
641+
sql += "[" + mSchemaName + "].";
642+
643+
sql += "[" + mTableName + "]";
644+
645+
if ( !mSqlWhereClause.isEmpty() )
646+
{
647+
sql += QString( " where (%1)" ).arg( mSqlWhereClause );
648+
}
649+
650+
mQuery = QSqlQuery( mDatabase );
651+
mQuery.setForwardOnly( true );
652+
653+
if ( !mQuery.exec( sql ) )
654+
{
655+
QString msg = mQuery.lastError().text();
656+
QgsDebugMsg( msg );
657+
}
658+
659+
if ( mQuery.isActive() )
660+
{
661+
if ( mQuery.next() )
662+
{
663+
return mQuery.value( 0 );
664+
}
665+
}
666+
667+
return QVariant( QString::null );
668+
}
669+
670+
// Returns the maximum value of an attribute
671+
QVariant QgsMssqlProvider::maximumValue( int index )
672+
{
673+
// get the field name
674+
QgsField fld = mAttributeFields[ index ];
675+
QString sql = QString( "select max([%1]) from " )
676+
.arg( fld.name() );
677+
678+
if ( !mSchemaName.isEmpty() )
679+
sql += "[" + mSchemaName + "].";
680+
681+
sql += "[" + mTableName + "]";
682+
683+
if ( !mSqlWhereClause.isEmpty() )
684+
{
685+
sql += QString( " where (%1)" ).arg( mSqlWhereClause );
686+
}
687+
688+
mQuery = QSqlQuery( mDatabase );
689+
mQuery.setForwardOnly( true );
690+
691+
if ( !mQuery.exec( sql ) )
692+
{
693+
QString msg = mQuery.lastError().text();
694+
QgsDebugMsg( msg );
695+
}
696+
697+
if ( mQuery.isActive() )
698+
{
699+
if ( mQuery.next() )
700+
{
701+
return mQuery.value( 0 );
702+
}
703+
}
704+
705+
return QVariant( QString::null );
706+
}
707+
708+
// Returns the list of unique values of an attribute
709+
void QgsMssqlProvider::uniqueValues( int index, QList<QVariant> &uniqueValues, int limit )
710+
{
711+
uniqueValues.clear();
712+
713+
// get the field name
714+
QgsField fld = mAttributeFields[ index ];
715+
QString sql = QString( "select distinct ");
716+
717+
if (limit > 0)
718+
{
719+
sql += QString( " top %1 " ).arg( limit );
720+
}
721+
722+
sql += QString( "[%1] from " )
723+
.arg( fld.name() );
724+
725+
if ( !mSchemaName.isEmpty() )
726+
sql += "[" + mSchemaName + "].";
727+
728+
sql += "[" + mTableName + "]";
729+
730+
if ( !mSqlWhereClause.isEmpty() )
731+
{
732+
sql += QString( " where (%1)" ).arg( mSqlWhereClause );
733+
}
734+
735+
mQuery = QSqlQuery( mDatabase );
736+
mQuery.setForwardOnly( true );
737+
738+
if ( !mQuery.exec( sql ) )
739+
{
740+
QString msg = mQuery.lastError().text();
741+
QgsDebugMsg( msg );
742+
}
743+
744+
if ( mQuery.isActive() )
745+
{
746+
// read all features
747+
while ( mQuery.next() )
748+
{
749+
uniqueValues.append( mQuery.value( 0 ) );
750+
}
751+
}
752+
}
753+
754+
629755
// update the extent, feature count, wkb type and srid for this layer
630756
void QgsMssqlProvider::UpdateStatistics( bool estimate )
631757
{

src/providers/mssql/qgsmssqlprovider.h

+30
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,36 @@ class QgsMssqlProvider : public QgsVectorDataProvider
133133
bool fetchGeometry = true,
134134
bool useIntersect = false );
135135

136+
/**
137+
* Returns the minimum value of an attribute
138+
* @param index the index of the attribute
139+
*
140+
* Default implementation walks all numeric attributes and caches minimal
141+
* and maximal values. If provider has facilities to retrieve minimal
142+
* value directly, override this function.
143+
*/
144+
virtual QVariant minimumValue( int index );
145+
146+
/**
147+
* Returns the maximum value of an attribute
148+
* @param index the index of the attribute
149+
*
150+
* Default implementation walks all numeric attributes and caches minimal
151+
* and maximal values. If provider has facilities to retrieve maximal
152+
* value directly, override this function.
153+
*/
154+
virtual QVariant maximumValue( int index );
155+
156+
/**
157+
* Return unique values of an attribute
158+
* @param index the index of the attribute
159+
* @param uniqueValues values reference to the list to fill
160+
* @param limit maxmum number of the values to return (added in 1.4)
161+
*
162+
* Default implementation simply iterates the features
163+
*/
164+
virtual void uniqueValues( int index, QList<QVariant> &uniqueValues, int limit = -1 );
165+
136166
/**
137167
* Get the next feature resulting from a select operation.
138168
* @param feature feature which will receive data from the provider

0 commit comments

Comments
 (0)