[QGIS Commit] r11731 - trunk/qgis/src/providers/spatialite

svn_qgis at osgeo.org svn_qgis at osgeo.org
Tue Sep 29 03:24:28 EDT 2009


Author: jef
Date: 2009-09-29 03:24:26 -0400 (Tue, 29 Sep 2009)
New Revision: 11731

Modified:
   trunk/qgis/src/providers/spatialite/qgsspatialiteprovider.cpp
   trunk/qgis/src/providers/spatialite/qgsspatialiteprovider.h
Log:
apply #1936

Modified: trunk/qgis/src/providers/spatialite/qgsspatialiteprovider.cpp
===================================================================
--- trunk/qgis/src/providers/spatialite/qgsspatialiteprovider.cpp	2009-09-28 21:46:04 UTC (rev 11730)
+++ trunk/qgis/src/providers/spatialite/qgsspatialiteprovider.cpp	2009-09-29 07:24:26 UTC (rev 11731)
@@ -44,12 +44,13 @@
 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 );
@@ -464,6 +465,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
@@ -545,6 +564,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() );
@@ -654,6 +686,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 )
@@ -709,6 +746,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 )
@@ -763,6 +805,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 )
@@ -1542,6 +1589,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 )

Modified: trunk/qgis/src/providers/spatialite/qgsspatialiteprovider.h
===================================================================
--- trunk/qgis/src/providers/spatialite/qgsspatialiteprovider.h	2009-09-28 21:46:04 UTC (rev 11730)
+++ trunk/qgis/src/providers/spatialite/qgsspatialiteprovider.h	2009-09-29 07:24:26 UTC (rev 11731)
@@ -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;



More information about the QGIS-commit mailing list