@@ -44,12 +44,13 @@ QMap < QString, QgsSpatiaLiteProvider::SqliteHandles * >QgsSpatiaLiteProvider::S
44
44
QgsSpatiaLiteProvider::QgsSpatiaLiteProvider ( QString const &uri ): QgsVectorDataProvider( uri ),
45
45
geomType( QGis::WKBUnknown ), sqliteHandle( NULL ), sqliteStatement( NULL ), mSrid( -1 ), spatialIndexRTree( false ), spatialIndexMbrCache( false )
46
46
{
47
- QgsDataSourceURI mUri = QgsDataSourceURI ( uri );
47
+ QgsDataSourceURI anUri = QgsDataSourceURI ( uri );
48
48
49
49
// 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 ();
53
54
54
55
// trying to open the SQLite DB
55
56
spatialite_init ( 0 );
@@ -464,6 +465,24 @@ bool QgsSpatiaLiteProvider::nextFeature( QgsFeature & feature )
464
465
return true ;
465
466
}
466
467
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
+
467
486
void QgsSpatiaLiteProvider::select ( QgsAttributeList fetchAttributes, QgsRectangle rect, bool fetchGeometry, bool useIntersect )
468
487
{
469
488
// preparing the SQL statement
@@ -545,6 +564,19 @@ void QgsSpatiaLiteProvider::select( QgsAttributeList fetchAttributes, QgsRectang
545
564
if ( !whereClause.isEmpty () )
546
565
sql += whereClause;
547
566
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
+
548
580
mFetchGeom = fetchGeometry;
549
581
mAttributesToFetch = fetchAttributes;
550
582
strcpy ( xSql, sql.toUtf8 ().constData () );
@@ -654,6 +686,11 @@ QVariant QgsSpatiaLiteProvider::minimumValue( int index )
654
686
655
687
QString sql = QString ( " SELECT Min(%1) FROM %2" ).arg ( fld.name () ).arg ( quotedValue ( mTableName ) );
656
688
689
+ if ( !mSubsetString .isEmpty () )
690
+ {
691
+ sql += " WHERE ( " + mSubsetString + " )" ;
692
+ }
693
+
657
694
strcpy ( xSql, sql.toUtf8 ().constData () );
658
695
ret = sqlite3_get_table ( sqliteHandle, xSql, &results, &rows, &columns, &errMsg );
659
696
if ( ret != SQLITE_OK )
@@ -709,6 +746,11 @@ QVariant QgsSpatiaLiteProvider::maximumValue( int index )
709
746
710
747
QString sql = QString ( " SELECT Max(%1) FROM %2" ).arg ( fld.name () ).arg ( quotedValue ( mTableName ) );
711
748
749
+ if ( !mSubsetString .isEmpty () )
750
+ {
751
+ sql += " WHERE ( " + mSubsetString + " )" ;
752
+ }
753
+
712
754
strcpy ( xSql, sql.toUtf8 ().constData () );
713
755
ret = sqlite3_get_table ( sqliteHandle, xSql, &results, &rows, &columns, &errMsg );
714
756
if ( ret != SQLITE_OK )
@@ -763,6 +805,11 @@ void QgsSpatiaLiteProvider::uniqueValues( int index, QList < QVariant > &uniqueV
763
805
764
806
sql = QString ( " SELECT DISTINCT %1 FROM %2 ORDER BY %1" ).arg ( fld.name () ).arg ( quotedValue ( mTableName ) );
765
807
808
+ if ( !mSubsetString .isEmpty () )
809
+ {
810
+ sql += " WHERE ( " + mSubsetString + " )" ;
811
+ }
812
+
766
813
// SQLite prepared statement
767
814
strcpy ( xSql, sql.toUtf8 ().constData () );
768
815
if ( sqlite3_prepare_v2 ( sqliteHandle, xSql, strlen ( xSql ), &stmt, NULL ) != SQLITE_OK )
@@ -1542,6 +1589,11 @@ bool QgsSpatiaLiteProvider::getTableSummary()
1542
1589
QString sql = QString ( " SELECT Min(MbrMinX(%1)), Min(MbrMinY(%1)), "
1543
1590
" Max(MbrMaxX(%1)), Max(MbrMaxY(%1)), Count(*) " " FROM %2" ).arg ( geometryColumn ).arg ( quotedValue ( mTableName ) );
1544
1591
1592
+ if ( !mSubsetString .isEmpty () )
1593
+ {
1594
+ sql += " WHERE ( " + mSubsetString + " )" ;
1595
+ }
1596
+
1545
1597
strcpy ( xSql, sql.toUtf8 ().constData () );
1546
1598
ret = sqlite3_get_table ( sqliteHandle, xSql, &results, &rows, &columns, &errMsg );
1547
1599
if ( ret != SQLITE_OK )
0 commit comments