Skip to content
Permalink
Browse files

MSSQL provider: Implement minimumValue, maximumValue and uniqueValues (

…fixes #5610)
  • Loading branch information
szekerest committed Jun 3, 2012
1 parent bb3e2f0 commit 2519df85ffdc0917e105846e1b6bb4006408bca7
Showing with 157 additions and 1 deletion.
  1. +127 −1 src/providers/mssql/qgsmssqlprovider.cpp
  2. +30 −0 src/providers/mssql/qgsmssqlprovider.h
@@ -604,7 +604,10 @@ void QgsMssqlProvider::select( QgsAttributeList fetchAttributes,

if ( !mSqlWhereClause.isEmpty() )
{
mStatement += " and (" + mSqlWhereClause + ")";
if ( rect.isEmpty() )
mStatement += " where (" + mSqlWhereClause + ")";
else
mStatement += " and (" + mSqlWhereClause + ")";
}

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

// Returns the minimum value of an attribute
QVariant QgsMssqlProvider::minimumValue( int index )
{
// get the field name
QgsField fld = mAttributeFields[ index ];
QString sql = QString( "select min([%1]) from " )
.arg( fld.name() );

if ( !mSchemaName.isEmpty() )
sql += "[" + mSchemaName + "].";

sql += "[" + mTableName + "]";

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

mQuery = QSqlQuery( mDatabase );
mQuery.setForwardOnly( true );

if ( !mQuery.exec( sql ) )
{
QString msg = mQuery.lastError().text();
QgsDebugMsg( msg );
}

if ( mQuery.isActive() )
{
if ( mQuery.next() )
{
return mQuery.value( 0 );
}
}

return QVariant( QString::null );
}

// Returns the maximum value of an attribute
QVariant QgsMssqlProvider::maximumValue( int index )
{
// get the field name
QgsField fld = mAttributeFields[ index ];
QString sql = QString( "select max([%1]) from " )
.arg( fld.name() );

if ( !mSchemaName.isEmpty() )
sql += "[" + mSchemaName + "].";

sql += "[" + mTableName + "]";

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

mQuery = QSqlQuery( mDatabase );
mQuery.setForwardOnly( true );

if ( !mQuery.exec( sql ) )
{
QString msg = mQuery.lastError().text();
QgsDebugMsg( msg );
}

if ( mQuery.isActive() )
{
if ( mQuery.next() )
{
return mQuery.value( 0 );
}
}

return QVariant( QString::null );
}

// Returns the list of unique values of an attribute
void QgsMssqlProvider::uniqueValues( int index, QList<QVariant> &uniqueValues, int limit )
{
uniqueValues.clear();

// get the field name
QgsField fld = mAttributeFields[ index ];
QString sql = QString( "select distinct ");

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

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

if ( !mSchemaName.isEmpty() )
sql += "[" + mSchemaName + "].";

sql += "[" + mTableName + "]";

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

mQuery = QSqlQuery( mDatabase );
mQuery.setForwardOnly( true );

if ( !mQuery.exec( sql ) )
{
QString msg = mQuery.lastError().text();
QgsDebugMsg( msg );
}

if ( mQuery.isActive() )
{
// read all features
while ( mQuery.next() )
{
uniqueValues.append( mQuery.value( 0 ) );
}
}
}


// update the extent, feature count, wkb type and srid for this layer
void QgsMssqlProvider::UpdateStatistics( bool estimate )
{
@@ -133,6 +133,36 @@ class QgsMssqlProvider : public QgsVectorDataProvider
bool fetchGeometry = true,
bool useIntersect = false );

/**
* Returns the minimum value of an attribute
* @param index the index of the attribute
*
* Default implementation walks all numeric attributes and caches minimal
* and maximal values. If provider has facilities to retrieve minimal
* value directly, override this function.
*/
virtual QVariant minimumValue( int index );

/**
* Returns the maximum value of an attribute
* @param index the index of the attribute
*
* Default implementation walks all numeric attributes and caches minimal
* and maximal values. If provider has facilities to retrieve maximal
* value directly, override this function.
*/
virtual QVariant maximumValue( int index );

/**
* Return unique values of an attribute
* @param index the index of the attribute
* @param uniqueValues values reference to the list to fill
* @param limit maxmum number of the values to return (added in 1.4)
*
* Default implementation simply iterates the features
*/
virtual void uniqueValues( int index, QList<QVariant> &uniqueValues, int limit = -1 );

/**
* Get the next feature resulting from a select operation.
* @param feature feature which will receive data from the provider

0 comments on commit 2519df8

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