@@ -503,89 +503,8 @@ QgsAbstractFeatureSource* QgsOgrProvider::featureSource() const
503
503
504
504
bool QgsOgrProvider::setSubsetString ( const QString& theSQL, bool updateFeatureCount )
505
505
{
506
- QgsCPLErrorHandler handler;
507
-
508
- if ( !ogrDataSource )
509
- return false ;
510
-
511
- if ( theSQL == mSubsetString && mFeaturesCounted >= 0 )
512
- return true ;
513
-
514
- OGRLayerH prevLayer = ogrLayer;
515
- QString prevSubsetString = mSubsetString ;
516
- mSubsetString = theSQL;
517
-
518
- if ( !mSubsetString .isEmpty () )
519
- {
520
-
521
- ogrLayer = setSubsetString ( ogrOrigLayer, ogrDataSource );
522
- if ( !ogrLayer )
523
- {
524
- pushError ( tr ( " OGR[%1] error %2: %3" ).arg ( CPLGetLastErrorType () ).arg ( CPLGetLastErrorNo () ).arg ( CPLGetLastErrorMsg () ) );
525
- ogrLayer = prevLayer;
526
- mSubsetString = prevSubsetString;
527
- return false ;
528
- }
529
- }
530
- else
531
- {
532
- ogrLayer = ogrOrigLayer;
533
- }
534
-
535
- if ( prevLayer != ogrOrigLayer )
536
- {
537
- OGR_DS_ReleaseResultSet ( ogrDataSource, prevLayer );
538
- }
539
-
540
- QString uri = mFilePath ;
541
- if ( !mLayerName .isNull () )
542
- {
543
- uri += QString ( " |layername=%1" ).arg ( mLayerName );
544
- }
545
- else if ( mLayerIndex >= 0 )
546
- {
547
- uri += QString ( " |layerid=%1" ).arg ( mLayerIndex );
548
- }
549
-
550
- if ( !mSubsetString .isEmpty () )
551
- {
552
- uri += QString ( " |subset=%1" ).arg ( mSubsetString );
553
- }
554
-
555
- if ( mOgrGeometryTypeFilter != wkbUnknown )
556
- {
557
- uri += QString ( " |geometrytype=%1" ).arg ( ogrWkbGeometryTypeName ( mOgrGeometryTypeFilter ) );
558
- }
559
-
560
- if ( uri != dataSourceUri () )
561
- {
562
- QgsOgrConnPool::instance ()->unref ( dataSourceUri () );
563
- setDataSourceUri ( uri );
564
- QgsOgrConnPool::instance ()->ref ( dataSourceUri () );
565
- }
566
-
567
- OGR_L_ResetReading ( ogrLayer );
568
-
569
- // getting the total number of features in the layer
570
- // TODO: This can be expensive, do we really need it!
571
- if ( updateFeatureCount )
572
- {
573
- recalculateFeatureCount ();
574
- }
575
-
576
- // check the validity of the layer
577
- QgsDebugMsg ( " checking validity" );
578
- loadFields ();
579
- QgsDebugMsg ( " Done checking validity" );
580
-
581
- invalidateCachedExtent ( false );
582
-
583
- // Changing the filter may change capabilities
584
- computeCapabilities ();
585
-
586
- emit dataChanged ();
587
-
588
- return true ;
506
+ // Always update capabilities
507
+ return _setSubsetString ( theSQL, updateFeatureCount, true );
589
508
}
590
509
591
510
QString QgsOgrProvider::subsetString ()
@@ -1640,6 +1559,94 @@ bool QgsOgrProvider::commitTransaction()
1640
1559
return true ;
1641
1560
}
1642
1561
1562
+ bool QgsOgrProvider::_setSubsetString ( const QString &theSQL, bool updateFeatureCount, bool updateCapabilities )
1563
+ {
1564
+ QgsCPLErrorHandler handler;
1565
+
1566
+ if ( !ogrDataSource )
1567
+ return false ;
1568
+
1569
+ if ( theSQL == mSubsetString && mFeaturesCounted >= 0 )
1570
+ return true ;
1571
+
1572
+ OGRLayerH prevLayer = ogrLayer;
1573
+ QString prevSubsetString = mSubsetString ;
1574
+ mSubsetString = theSQL;
1575
+
1576
+ if ( !mSubsetString .isEmpty () )
1577
+ {
1578
+
1579
+ ogrLayer = setSubsetString ( ogrOrigLayer, ogrDataSource );
1580
+ if ( !ogrLayer )
1581
+ {
1582
+ pushError ( tr ( " OGR[%1] error %2: %3" ).arg ( CPLGetLastErrorType () ).arg ( CPLGetLastErrorNo () ).arg ( CPLGetLastErrorMsg () ) );
1583
+ ogrLayer = prevLayer;
1584
+ mSubsetString = prevSubsetString;
1585
+ return false ;
1586
+ }
1587
+ }
1588
+ else
1589
+ {
1590
+ ogrLayer = ogrOrigLayer;
1591
+ }
1592
+
1593
+ if ( prevLayer != ogrOrigLayer )
1594
+ {
1595
+ OGR_DS_ReleaseResultSet ( ogrDataSource, prevLayer );
1596
+ }
1597
+
1598
+ QString uri = mFilePath ;
1599
+ if ( !mLayerName .isNull () )
1600
+ {
1601
+ uri += QString ( " |layername=%1" ).arg ( mLayerName );
1602
+ }
1603
+ else if ( mLayerIndex >= 0 )
1604
+ {
1605
+ uri += QString ( " |layerid=%1" ).arg ( mLayerIndex );
1606
+ }
1607
+
1608
+ if ( !mSubsetString .isEmpty () )
1609
+ {
1610
+ uri += QString ( " |subset=%1" ).arg ( mSubsetString );
1611
+ }
1612
+
1613
+ if ( mOgrGeometryTypeFilter != wkbUnknown )
1614
+ {
1615
+ uri += QString ( " |geometrytype=%1" ).arg ( ogrWkbGeometryTypeName ( mOgrGeometryTypeFilter ) );
1616
+ }
1617
+
1618
+ if ( uri != dataSourceUri () )
1619
+ {
1620
+ QgsOgrConnPool::instance ()->unref ( dataSourceUri () );
1621
+ setDataSourceUri ( uri );
1622
+ QgsOgrConnPool::instance ()->ref ( dataSourceUri () );
1623
+ }
1624
+
1625
+ OGR_L_ResetReading ( ogrLayer );
1626
+
1627
+ // getting the total number of features in the layer
1628
+ // TODO: This can be expensive, do we really need it!
1629
+ if ( updateFeatureCount )
1630
+ {
1631
+ recalculateFeatureCount ();
1632
+ }
1633
+
1634
+ // check the validity of the layer
1635
+ QgsDebugMsg ( " checking validity" );
1636
+ loadFields ();
1637
+ QgsDebugMsg ( " Done checking validity" );
1638
+
1639
+ invalidateCachedExtent ( false );
1640
+
1641
+ // Changing the filter may change capabilities
1642
+ if ( updateCapabilities )
1643
+ computeCapabilities ();
1644
+
1645
+ emit dataChanged ();
1646
+
1647
+ return true ;
1648
+ }
1649
+
1643
1650
1644
1651
bool QgsOgrProvider::changeAttributeValues ( const QgsChangedAttributesMap &attr_map )
1645
1652
{
@@ -2008,10 +2015,18 @@ int QgsOgrProvider::capabilities() const
2008
2015
void QgsOgrProvider::computeCapabilities ()
2009
2016
{
2010
2017
int ability = 0 ;
2018
+ bool updateModeActivated = false ;
2011
2019
2012
2020
// collect abilities reported by OGR
2013
2021
if ( ogrLayer )
2014
2022
{
2023
+ // We want the layer in rw mode or capabilities will be wrong
2024
+ // If mUpdateModeStackDepth > 0, it means that an updateMode is already active and that we have write access
2025
+ if ( mUpdateModeStackDepth == 0 )
2026
+ {
2027
+ updateModeActivated = enterUpdateMode ( );
2028
+ }
2029
+
2015
2030
// Whilst the OGR documentation (e.g. at
2016
2031
// http://www.gdal.org/ogr/classOGRLayer.html#a17) states "The capability
2017
2032
// codes that can be tested are represented as strings, but #defined
@@ -2138,6 +2153,10 @@ void QgsOgrProvider::computeCapabilities()
2138
2153
}
2139
2154
}
2140
2155
2156
+ if ( updateModeActivated )
2157
+ leaveUpdateMode ();
2158
+
2159
+
2141
2160
mCapabilities = ability;
2142
2161
}
2143
2162
@@ -3620,7 +3639,8 @@ void QgsOgrProvider::open( OpenMode mode )
3620
3639
mSubsetString = " " ;
3621
3640
// Block signals to avoid endless recusion reloadData -> emit dataChanged -> reloadData
3622
3641
blockSignals ( true );
3623
- mValid = setSubsetString ( origSubsetString );
3642
+ // Do not update capabilities: it will be done later
3643
+ mValid = _setSubsetString ( origSubsetString, true , false );
3624
3644
blockSignals ( false );
3625
3645
if ( mValid )
3626
3646
{
@@ -3696,7 +3716,8 @@ void QgsOgrProvider::open( OpenMode mode )
3696
3716
{
3697
3717
int featuresCountedBackup = mFeaturesCounted ;
3698
3718
mFeaturesCounted = -1 ;
3699
- mValid = setSubsetString ( mSubsetString , false );
3719
+ // Do not update capabilities here
3720
+ mValid = _setSubsetString ( mSubsetString , false , false );
3700
3721
mFeaturesCounted = featuresCountedBackup;
3701
3722
}
3702
3723
}
0 commit comments