Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSSQL provider: Implement minimumValue, maximumValue and uniqueValues #159

Merged
merged 1 commit into from
Jun 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 127 additions & 1 deletion src/providers/mssql/qgsmssqlprovider.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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