Skip to content

Commit

Permalink
apply #1936
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@11731 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Sep 29, 2009
1 parent d1eb98a commit 4b87fbf
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
60 changes: 56 additions & 4 deletions src/providers/spatialite/qgsspatialiteprovider.cpp
Expand Up @@ -44,12 +44,13 @@ QMap < QString, QgsSpatiaLiteProvider::SqliteHandles * >QgsSpatiaLiteProvider::S
QgsSpatiaLiteProvider::QgsSpatiaLiteProvider( QString const &uri ): QgsVectorDataProvider( uri ),
geomType( QGis::WKBUnknown ), sqliteHandle( NULL ), sqliteStatement( NULL ), mSrid( -1 ), spatialIndexRTree( false ), spatialIndexMbrCache( false )
{
QgsDataSourceURI mUri = QgsDataSourceURI( uri );
QgsDataSourceURI anUri = QgsDataSourceURI( uri );

// parsing members from the uri structure
mTableName = mUri.table();
geometryColumn = mUri.geometryColumn();
mSqlitePath = mUri.database();
mTableName = anUri.table();
geometryColumn = anUri.geometryColumn();
mSqlitePath = anUri.database();
mSubsetString = anUri.sql();

// trying to open the SQLite DB
spatialite_init( 0 );
Expand Down Expand Up @@ -464,6 +465,24 @@ bool QgsSpatiaLiteProvider::nextFeature( QgsFeature & feature )
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
Expand Down Expand Up @@ -545,6 +564,19 @@ void QgsSpatiaLiteProvider::select( QgsAttributeList fetchAttributes, QgsRectang
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() );
Expand Down Expand Up @@ -654,6 +686,11 @@ QVariant QgsSpatiaLiteProvider::minimumValue( int index )

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 )
Expand Down Expand Up @@ -709,6 +746,11 @@ QVariant QgsSpatiaLiteProvider::maximumValue( int index )

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 )
Expand Down Expand Up @@ -763,6 +805,11 @@ void QgsSpatiaLiteProvider::uniqueValues( int index, QList < QVariant > &uniqueV

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 )
Expand Down Expand Up @@ -1542,6 +1589,11 @@ bool QgsSpatiaLiteProvider::getTableSummary()
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 )
Expand Down
11 changes: 11 additions & 0 deletions src/providers/spatialite/qgsspatialiteprovider.h
Expand Up @@ -74,6 +74,13 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
*/
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
Expand Down Expand Up @@ -285,6 +292,10 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
* SQLite statement handle
*/
sqlite3_stmt *sqliteStatement;
/**
* String used to define a subset of the layer
*/
QString mSubsetString;
/**
* Spatial reference id of the layer
*/
Expand Down

0 comments on commit 4b87fbf

Please sign in to comment.