Skip to content
Permalink
Browse files
apply #1936
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11731 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Sep 29, 2009
1 parent fccd924 commit 774fafd55ac44d60e195a57f380d88c246b1199a
Showing with 67 additions and 4 deletions.
  1. +56 −4 src/providers/spatialite/qgsspatialiteprovider.cpp
  2. +11 −0 src/providers/spatialite/qgsspatialiteprovider.h
@@ -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 );
@@ -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
@@ -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() );
@@ -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 )
@@ -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 )
@@ -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 )
@@ -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 )
@@ -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
@@ -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
*/

0 comments on commit 774fafd

Please sign in to comment.