Skip to content

Commit

Permalink
Merge pull request #159 from szekerest/master
Browse files Browse the repository at this point in the history
MSSQL provider: Implement minimumValue, maximumValue and uniqueValues
  • Loading branch information
NathanW2 committed Jun 3, 2012
2 parents 8ad85ed + 2519df8 commit dab4758
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 1 deletion.
128 changes: 127 additions & 1 deletion src/providers/mssql/qgsmssqlprovider.cpp
Expand Up @@ -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;
Expand All @@ -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 )
{
Expand Down
30 changes: 30 additions & 0 deletions src/providers/mssql/qgsmssqlprovider.h
Expand Up @@ -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
Expand Down

0 comments on commit dab4758

Please sign in to comment.