Skip to content

Commit 4b87fbf

Browse files
author
jef
committedSep 29, 2009
apply #1936
git-svn-id: http://svn.osgeo.org/qgis/trunk@11731 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent d1eb98a commit 4b87fbf

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed
 

‎src/providers/spatialite/qgsspatialiteprovider.cpp

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ QMap < QString, QgsSpatiaLiteProvider::SqliteHandles * >QgsSpatiaLiteProvider::S
4444
QgsSpatiaLiteProvider::QgsSpatiaLiteProvider( QString const &uri ): QgsVectorDataProvider( uri ),
4545
geomType( QGis::WKBUnknown ), sqliteHandle( NULL ), sqliteStatement( NULL ), mSrid( -1 ), spatialIndexRTree( false ), spatialIndexMbrCache( false )
4646
{
47-
QgsDataSourceURI mUri = QgsDataSourceURI( uri );
47+
QgsDataSourceURI anUri = QgsDataSourceURI( uri );
4848

4949
// parsing members from the uri structure
50-
mTableName = mUri.table();
51-
geometryColumn = mUri.geometryColumn();
52-
mSqlitePath = mUri.database();
50+
mTableName = anUri.table();
51+
geometryColumn = anUri.geometryColumn();
52+
mSqlitePath = anUri.database();
53+
mSubsetString = anUri.sql();
5354

5455
// trying to open the SQLite DB
5556
spatialite_init( 0 );
@@ -464,6 +465,24 @@ bool QgsSpatiaLiteProvider::nextFeature( QgsFeature & feature )
464465
return true;
465466
}
466467

468+
QString QgsSpatiaLiteProvider::subsetString()
469+
{
470+
return mSubsetString;
471+
}
472+
473+
void QgsSpatiaLiteProvider::setSubsetString( QString theSQL )
474+
{
475+
mSubsetString = theSQL;
476+
477+
// update URI
478+
QgsDataSourceURI uri = QgsDataSourceURI(dataSourceUri());
479+
uri.setSql( theSQL );
480+
setDataSourceUri( uri.uri() );
481+
482+
// update feature count and extents
483+
getTableSummary();
484+
}
485+
467486
void QgsSpatiaLiteProvider::select( QgsAttributeList fetchAttributes, QgsRectangle rect, bool fetchGeometry, bool useIntersect )
468487
{
469488
// preparing the SQL statement
@@ -545,6 +564,19 @@ void QgsSpatiaLiteProvider::select( QgsAttributeList fetchAttributes, QgsRectang
545564
if ( !whereClause.isEmpty() )
546565
sql += whereClause;
547566

567+
if ( !mSubsetString.isEmpty() )
568+
{
569+
if ( !whereClause.isEmpty() )
570+
{
571+
sql += " AND ";
572+
}
573+
else
574+
{
575+
sql += " WHERE ";
576+
}
577+
sql += "( " + mSubsetString + ")";
578+
}
579+
548580
mFetchGeom = fetchGeometry;
549581
mAttributesToFetch = fetchAttributes;
550582
strcpy( xSql, sql.toUtf8().constData() );
@@ -654,6 +686,11 @@ QVariant QgsSpatiaLiteProvider::minimumValue( int index )
654686

655687
QString sql = QString( "SELECT Min(%1) FROM %2" ).arg( fld.name() ).arg( quotedValue( mTableName ) );
656688

689+
if ( !mSubsetString.isEmpty() )
690+
{
691+
sql += " WHERE ( " + mSubsetString + ")";
692+
}
693+
657694
strcpy( xSql, sql.toUtf8().constData() );
658695
ret = sqlite3_get_table( sqliteHandle, xSql, &results, &rows, &columns, &errMsg );
659696
if ( ret != SQLITE_OK )
@@ -709,6 +746,11 @@ QVariant QgsSpatiaLiteProvider::maximumValue( int index )
709746

710747
QString sql = QString( "SELECT Max(%1) FROM %2" ).arg( fld.name() ).arg( quotedValue( mTableName ) );
711748

749+
if ( !mSubsetString.isEmpty() )
750+
{
751+
sql += " WHERE ( " + mSubsetString + ")";
752+
}
753+
712754
strcpy( xSql, sql.toUtf8().constData() );
713755
ret = sqlite3_get_table( sqliteHandle, xSql, &results, &rows, &columns, &errMsg );
714756
if ( ret != SQLITE_OK )
@@ -763,6 +805,11 @@ void QgsSpatiaLiteProvider::uniqueValues( int index, QList < QVariant > &uniqueV
763805

764806
sql = QString( "SELECT DISTINCT %1 FROM %2 ORDER BY %1" ).arg( fld.name() ).arg( quotedValue( mTableName ) );
765807

808+
if ( !mSubsetString.isEmpty() )
809+
{
810+
sql += " WHERE ( " + mSubsetString + ")";
811+
}
812+
766813
// SQLite prepared statement
767814
strcpy( xSql, sql.toUtf8().constData() );
768815
if ( sqlite3_prepare_v2( sqliteHandle, xSql, strlen( xSql ), &stmt, NULL ) != SQLITE_OK )
@@ -1542,6 +1589,11 @@ bool QgsSpatiaLiteProvider::getTableSummary()
15421589
QString sql = QString( "SELECT Min(MbrMinX(%1)), Min(MbrMinY(%1)), "
15431590
"Max(MbrMaxX(%1)), Max(MbrMaxY(%1)), Count(*) " "FROM %2" ).arg( geometryColumn ).arg( quotedValue( mTableName ) );
15441591

1592+
if ( !mSubsetString.isEmpty() )
1593+
{
1594+
sql += " WHERE ( " + mSubsetString + ")";
1595+
}
1596+
15451597
strcpy( xSql, sql.toUtf8().constData() );
15461598
ret = sqlite3_get_table( sqliteHandle, xSql, &results, &rows, &columns, &errMsg );
15471599
if ( ret != SQLITE_OK )

‎src/providers/spatialite/qgsspatialiteprovider.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
7474
*/
7575
virtual bool featureAtId( int featureId,
7676
QgsFeature & feature, bool fetchGeometry = true, QgsAttributeList fetchAttributes = QgsAttributeList() );
77+
78+
/** Accessor for sql where clause used to limit dataset */
79+
virtual QString subsetString();
80+
81+
/** mutator for sql where clause used to limit dataset size */
82+
virtual void setSubsetString( QString theSQL );
83+
7784
/** Select features based on a bounding rectangle. Features can be retrieved with calls to nextFeature.
7885
* @param fetchAttributes list of attributes which should be fetched
7986
* @param rect spatial filter
@@ -285,6 +292,10 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
285292
* SQLite statement handle
286293
*/
287294
sqlite3_stmt *sqliteStatement;
295+
/**
296+
* String used to define a subset of the layer
297+
*/
298+
QString mSubsetString;
288299
/**
289300
* Spatial reference id of the layer
290301
*/

0 commit comments

Comments
 (0)
Please sign in to comment.