Index: src/providers/spatialite/qgsspatialiteprovider.cpp =================================================================== --- src/providers/spatialite/qgsspatialiteprovider.cpp (Revision 11600) +++ src/providers/spatialite/qgsspatialiteprovider.cpp (Arbeitskopie) @@ -47,19 +47,11 @@ QgsDataSourceURI mUri = QgsDataSourceURI( uri ); // parsing members from the uri structure + mSqlitePath = mUri.database(); mTableName = mUri.table(); geometryColumn = mUri.geometryColumn(); + mSubsetString = mUri.sql(); - // extracting the DB path - int idx = uri.indexOf( "dbname='" ); - if ( idx >= 0 ) - mSqlitePath = uri.mid( idx + 8 ); - else - mSqlitePath = uri; - idx = mSqlitePath.indexOf( "' table=" ); - if ( idx > 0 ) - mSqlitePath.truncate( idx ); - // trying to open the SQLite DB spatialite_init( 0 ); valid = true; @@ -472,6 +464,24 @@ return true; } +QString QgsSpatiaLiteProvider::subsetString() +{ + return mSubsetString; +} + +void QgsSpatiaLiteProvider::setSubsetString( QString theSQL ) +{ + mSubsetString = theSQL; + + // update URI + QgsDataSourceURI uri = QgsDataSourceURI(dataSourceUri()); + uri.setSql( theSQL ); + setDataSourceUri( uri.uri() ); + + // update feature count and extents + getTableSummary(); +} + void QgsSpatiaLiteProvider::select( QgsAttributeList fetchAttributes, QgsRectangle rect, bool fetchGeometry, bool useIntersect ) { // preparing the SQL statement @@ -553,6 +563,19 @@ if ( !whereClause.isEmpty() ) sql += whereClause; + if ( !mSubsetString.isEmpty() ) + { + if ( !whereClause.isEmpty() ) + { + sql += " AND "; + } + else + { + sql += " WHERE "; + } + sql += "( " + mSubsetString + ")"; + } + mFetchGeom = fetchGeometry; mAttributesToFetch = fetchAttributes; strcpy( xSql, sql.toUtf8().constData() ); @@ -662,6 +685,11 @@ QString sql = QString( "SELECT Min(%1) FROM %2" ).arg( fld.name() ).arg( quotedValue( mTableName ) ); + if ( !mSubsetString.isEmpty() ) + { + sql += " WHERE ( " + mSubsetString + ")"; + } + strcpy( xSql, sql.toUtf8().constData() ); ret = sqlite3_get_table( sqliteHandle, xSql, &results, &rows, &columns, &errMsg ); if ( ret != SQLITE_OK ) @@ -717,6 +745,11 @@ QString sql = QString( "SELECT Max(%1) FROM %2" ).arg( fld.name() ).arg( quotedValue( mTableName ) ); + if ( !mSubsetString.isEmpty() ) + { + sql += " WHERE ( " + mSubsetString + ")"; + } + strcpy( xSql, sql.toUtf8().constData() ); ret = sqlite3_get_table( sqliteHandle, xSql, &results, &rows, &columns, &errMsg ); if ( ret != SQLITE_OK ) @@ -771,6 +804,11 @@ sql = QString( "SELECT DISTINCT %1 FROM %2 ORDER BY %1" ).arg( fld.name() ).arg( quotedValue( mTableName ) ); + if ( !mSubsetString.isEmpty() ) + { + sql += " WHERE ( " + mSubsetString + ")"; + } + // SQLite prepared statement strcpy( xSql, sql.toUtf8().constData() ); if ( sqlite3_prepare_v2( sqliteHandle, xSql, strlen( xSql ), &stmt, NULL ) != SQLITE_OK ) @@ -1550,6 +1588,11 @@ QString sql = QString( "SELECT Min(MbrMinX(%1)), Min(MbrMinY(%1)), " "Max(MbrMaxX(%1)), Max(MbrMaxY(%1)), Count(*) " "FROM %2" ).arg( geometryColumn ).arg( quotedValue( mTableName ) ); + if ( !mSubsetString.isEmpty() ) + { + sql += " WHERE ( " + mSubsetString + ")"; + } + strcpy( xSql, sql.toUtf8().constData() ); ret = sqlite3_get_table( sqliteHandle, xSql, &results, &rows, &columns, &errMsg ); if ( ret != SQLITE_OK ) Index: src/providers/spatialite/qgsspatialiteprovider.h =================================================================== --- src/providers/spatialite/qgsspatialiteprovider.h (Revision 11600) +++ src/providers/spatialite/qgsspatialiteprovider.h (Arbeitskopie) @@ -74,6 +74,13 @@ */ virtual bool featureAtId( int featureId, QgsFeature & feature, bool fetchGeometry = true, QgsAttributeList fetchAttributes = QgsAttributeList() ); + + /** Accessor for sql where clause used to limit dataset */ + virtual QString subsetString(); + + /** mutator for sql where clause used to limit dataset size */ + virtual void setSubsetString( QString theSQL ); + /** Select features based on a bounding rectangle. Features can be retrieved with calls to nextFeature. * @param fetchAttributes list of attributes which should be fetched * @param rect spatial filter @@ -286,6 +293,10 @@ */ sqlite3_stmt *sqliteStatement; /** + * String used to define a subset of the layer + */ + QString mSubsetString; + /** * Spatial reference id of the layer */ int mSrid;