enh1936_qgsspatialiteprovider_subsetString.diff

support for subset strings in SpatiaLite layers - Mathias Walker, 2009-09-18 07:35 AM

Download (4.57 KB)

View differences:

src/providers/spatialite/qgsspatialiteprovider.cpp (Arbeitskopie)
47 47
  QgsDataSourceURI mUri = QgsDataSourceURI( uri );
48 48

  
49 49
  // parsing members from the uri structure
50
  mSqlitePath = mUri.database();
50 51
  mTableName = mUri.table();
51 52
  geometryColumn = mUri.geometryColumn();
53
  mSubsetString = mUri.sql();
52 54

  
53
  // extracting the DB path
54
  int idx = uri.indexOf( "dbname='" );
55
  if ( idx >= 0 )
56
    mSqlitePath = uri.mid( idx + 8 );
57
  else
58
    mSqlitePath = uri;
59
  idx = mSqlitePath.indexOf( "' table=" );
60
  if ( idx > 0 )
61
    mSqlitePath.truncate( idx );
62

  
63 55
  // trying to open the SQLite DB
64 56
  spatialite_init( 0 );
65 57
  valid = true;
......
472 464
  return true;
473 465
}
474 466

  
467
QString QgsSpatiaLiteProvider::subsetString()
468
{
469
  return mSubsetString;
470
}
471

  
472
void QgsSpatiaLiteProvider::setSubsetString( QString theSQL )
473
{
474
  mSubsetString = theSQL;
475

  
476
  // update URI
477
  QgsDataSourceURI uri = QgsDataSourceURI(dataSourceUri());
478
  uri.setSql( theSQL );
479
  setDataSourceUri( uri.uri() );
480

  
481
  // update feature count and extents
482
  getTableSummary();
483
}
484

  
475 485
void QgsSpatiaLiteProvider::select( QgsAttributeList fetchAttributes, QgsRectangle rect, bool fetchGeometry, bool useIntersect )
476 486
{
477 487
// preparing the SQL statement
......
553 563
  if ( !whereClause.isEmpty() )
554 564
    sql += whereClause;
555 565

  
566
  if ( !mSubsetString.isEmpty() )
567
  {
568
    if ( !whereClause.isEmpty() )
569
    {
570
      sql += " AND ";
571
    }
572
    else
573
    {
574
      sql += " WHERE ";
575
    }
576
    sql += "( " + mSubsetString + ")";
577
  }
578

  
556 579
  mFetchGeom = fetchGeometry;
557 580
  mAttributesToFetch = fetchAttributes;
558 581
  strcpy( xSql, sql.toUtf8().constData() );
......
662 685

  
663 686
  QString sql = QString( "SELECT Min(%1) FROM %2" ).arg( fld.name() ).arg( quotedValue( mTableName ) );
664 687

  
688
  if ( !mSubsetString.isEmpty() )
689
  {
690
    sql += " WHERE ( " + mSubsetString + ")";
691
  }
692

  
665 693
  strcpy( xSql, sql.toUtf8().constData() );
666 694
  ret = sqlite3_get_table( sqliteHandle, xSql, &results, &rows, &columns, &errMsg );
667 695
  if ( ret != SQLITE_OK )
......
717 745

  
718 746
  QString sql = QString( "SELECT Max(%1) FROM %2" ).arg( fld.name() ).arg( quotedValue( mTableName ) );
719 747

  
748
  if ( !mSubsetString.isEmpty() )
749
  {
750
    sql += " WHERE ( " + mSubsetString + ")";
751
  }
752

  
720 753
  strcpy( xSql, sql.toUtf8().constData() );
721 754
  ret = sqlite3_get_table( sqliteHandle, xSql, &results, &rows, &columns, &errMsg );
722 755
  if ( ret != SQLITE_OK )
......
771 804

  
772 805
  sql = QString( "SELECT DISTINCT %1 FROM %2 ORDER BY %1" ).arg( fld.name() ).arg( quotedValue( mTableName ) );
773 806

  
807
  if ( !mSubsetString.isEmpty() )
808
  {
809
    sql += " WHERE ( " + mSubsetString + ")";
810
  }
811

  
774 812
  // SQLite prepared statement
775 813
  strcpy( xSql, sql.toUtf8().constData() );
776 814
  if ( sqlite3_prepare_v2( sqliteHandle, xSql, strlen( xSql ), &stmt, NULL ) != SQLITE_OK )
......
1550 1588
  QString sql = QString( "SELECT Min(MbrMinX(%1)), Min(MbrMinY(%1)), "
1551 1589
                         "Max(MbrMaxX(%1)), Max(MbrMaxY(%1)), Count(*) " "FROM %2" ).arg( geometryColumn ).arg( quotedValue( mTableName ) );
1552 1590

  
1591
  if ( !mSubsetString.isEmpty() )
1592
  {
1593
    sql += " WHERE ( " + mSubsetString + ")";
1594
  }
1595

  
1553 1596
  strcpy( xSql, sql.toUtf8().constData() );
1554 1597
  ret = sqlite3_get_table( sqliteHandle, xSql, &results, &rows, &columns, &errMsg );
1555 1598
  if ( ret != SQLITE_OK )
src/providers/spatialite/qgsspatialiteprovider.h (Arbeitskopie)
74 74
      */
75 75
    virtual bool featureAtId( int featureId,
76 76
                              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

  
77 84
    /** Select features based on a bounding rectangle. Features can be retrieved with calls to nextFeature.
78 85
     *  @param fetchAttributes list of attributes which should be fetched
79 86
     *  @param rect spatial filter
......
286 293
     */
287 294
    sqlite3_stmt *sqliteStatement;
288 295
    /**
296
     * String used to define a subset of the layer
297
     */
298
    QString mSubsetString;
299
    /**
289 300
       * Spatial reference id of the layer
290 301
       */
291 302
    int mSrid;