@@ -528,90 +528,7 @@ QgsAbstractFeatureSource *QgsOgrProvider::featureSource() const
528
528
529
529
bool QgsOgrProvider::setSubsetString ( const QString &theSQL, bool updateFeatureCount )
530
530
{
531
- QgsCPLErrorHandler handler;
532
-
533
- if ( !mOgrOrigLayer )
534
- return false ;
535
-
536
- if ( theSQL == mSubsetString && mFeaturesCounted != QgsVectorDataProvider::Uncounted )
537
- return true ;
538
-
539
- if ( !theSQL.isEmpty () )
540
- {
541
- bool origFidAdded = false ;
542
- QMutex *mutex = nullptr ;
543
- OGRLayerH layer = mOgrOrigLayer ->getHandleAndMutex ( mutex );
544
- GDALDatasetH ds = mOgrOrigLayer ->getDatasetHandleAndMutex ( mutex );
545
- OGRLayerH subsetLayerH;
546
- {
547
- QMutexLocker locker ( mutex );
548
- subsetLayerH = QgsOgrProviderUtils::setSubsetString ( layer, ds, textEncoding (), theSQL, origFidAdded );
549
- }
550
- if ( !subsetLayerH )
551
- {
552
- pushError ( tr ( " OGR[%1] error %2: %3" ).arg ( CPLGetLastErrorType () ).arg ( CPLGetLastErrorNo () ).arg ( CPLGetLastErrorMsg () ) );
553
- return false ;
554
- }
555
- mOgrSqlLayer = QgsOgrProviderUtils::getSqlLayer ( mOgrOrigLayer .get (), subsetLayerH, theSQL );
556
- Q_ASSERT ( mOgrSqlLayer .get () );
557
- mOgrLayer = mOgrSqlLayer .get ();
558
- }
559
- else
560
- {
561
- mOgrSqlLayer .reset ();
562
- mOgrLayer = mOgrOrigLayer .get ();
563
- }
564
- mSubsetString = theSQL;
565
-
566
- QString uri = mFilePath ;
567
- if ( !mLayerName .isNull () )
568
- {
569
- uri += QStringLiteral ( " |layername=%1" ).arg ( mLayerName );
570
- }
571
- else if ( mLayerIndex >= 0 )
572
- {
573
- uri += QStringLiteral ( " |layerid=%1" ).arg ( mLayerIndex );
574
- }
575
-
576
- if ( !mSubsetString .isEmpty () )
577
- {
578
- uri += QStringLiteral ( " |subset=%1" ).arg ( mSubsetString );
579
- }
580
-
581
- if ( mOgrGeometryTypeFilter != wkbUnknown )
582
- {
583
- uri += QStringLiteral ( " |geometrytype=%1" ).arg ( ogrWkbGeometryTypeName ( mOgrGeometryTypeFilter ) );
584
- }
585
-
586
- if ( uri != dataSourceUri () )
587
- {
588
- QgsOgrConnPool::instance ()->unref ( QgsOgrProviderUtils::connectionPoolId ( dataSourceUri ( true ) ) );
589
- setDataSourceUri ( uri );
590
- QgsOgrConnPool::instance ()->ref ( QgsOgrProviderUtils::connectionPoolId ( dataSourceUri ( true ) ) );
591
- }
592
-
593
- mOgrLayer ->ResetReading ();
594
-
595
- // getting the total number of features in the layer
596
- // TODO: This can be expensive, do we really need it!
597
- if ( updateFeatureCount )
598
- {
599
- recalculateFeatureCount ();
600
- }
601
-
602
- // check the validity of the layer
603
- QgsDebugMsgLevel ( " checking validity" , 4 );
604
- loadFields ();
605
- QgsDebugMsgLevel ( " Done checking validity" , 4 );
606
-
607
- invalidateCachedExtent ( false );
608
-
609
- // Changing the filter may change capabilities
610
- computeCapabilities ();
611
-
612
- emit dataChanged ();
613
-
614
- return true ;
531
+ return _setSubsetString ( theSQL, updateFeatureCount, true );
615
532
}
616
533
617
534
QString QgsOgrProvider::subsetString () const
@@ -1763,6 +1680,96 @@ bool QgsOgrProvider::commitTransaction()
1763
1680
return true ;
1764
1681
}
1765
1682
1683
+ bool QgsOgrProvider::_setSubsetString ( const QString &theSQL, bool updateFeatureCount, bool updateCapabilities )
1684
+ {
1685
+ QgsCPLErrorHandler handler;
1686
+
1687
+ if ( !mOgrOrigLayer )
1688
+ return false ;
1689
+
1690
+ if ( theSQL == mSubsetString && mFeaturesCounted != QgsVectorDataProvider::Uncounted )
1691
+ return true ;
1692
+
1693
+ if ( !theSQL.isEmpty () )
1694
+ {
1695
+ bool origFidAdded = false ;
1696
+ QMutex *mutex = nullptr ;
1697
+ OGRLayerH layer = mOgrOrigLayer ->getHandleAndMutex ( mutex );
1698
+ GDALDatasetH ds = mOgrOrigLayer ->getDatasetHandleAndMutex ( mutex );
1699
+ OGRLayerH subsetLayerH;
1700
+ {
1701
+ QMutexLocker locker ( mutex );
1702
+ subsetLayerH = QgsOgrProviderUtils::setSubsetString ( layer, ds, textEncoding (), theSQL, origFidAdded );
1703
+ }
1704
+ if ( !subsetLayerH )
1705
+ {
1706
+ pushError ( tr ( " OGR[%1] error %2: %3" ).arg ( CPLGetLastErrorType () ).arg ( CPLGetLastErrorNo () ).arg ( CPLGetLastErrorMsg () ) );
1707
+ return false ;
1708
+ }
1709
+ mOgrSqlLayer = QgsOgrProviderUtils::getSqlLayer ( mOgrOrigLayer .get (), subsetLayerH, theSQL );
1710
+ Q_ASSERT ( mOgrSqlLayer .get () );
1711
+ mOgrLayer = mOgrSqlLayer .get ();
1712
+ }
1713
+ else
1714
+ {
1715
+ mOgrSqlLayer .reset ();
1716
+ mOgrLayer = mOgrOrigLayer .get ();
1717
+ }
1718
+ mSubsetString = theSQL;
1719
+
1720
+ QString uri = mFilePath ;
1721
+ if ( !mLayerName .isNull () )
1722
+ {
1723
+ uri += QStringLiteral ( " |layername=%1" ).arg ( mLayerName );
1724
+ }
1725
+ else if ( mLayerIndex >= 0 )
1726
+ {
1727
+ uri += QStringLiteral ( " |layerid=%1" ).arg ( mLayerIndex );
1728
+ }
1729
+
1730
+ if ( !mSubsetString .isEmpty () )
1731
+ {
1732
+ uri += QStringLiteral ( " |subset=%1" ).arg ( mSubsetString );
1733
+ }
1734
+
1735
+ if ( mOgrGeometryTypeFilter != wkbUnknown )
1736
+ {
1737
+ uri += QStringLiteral ( " |geometrytype=%1" ).arg ( ogrWkbGeometryTypeName ( mOgrGeometryTypeFilter ) );
1738
+ }
1739
+
1740
+ if ( uri != dataSourceUri () )
1741
+ {
1742
+ QgsOgrConnPool::instance ()->unref ( QgsOgrProviderUtils::connectionPoolId ( dataSourceUri ( true ) ) );
1743
+ setDataSourceUri ( uri );
1744
+ QgsOgrConnPool::instance ()->ref ( QgsOgrProviderUtils::connectionPoolId ( dataSourceUri ( true ) ) );
1745
+ }
1746
+
1747
+ mOgrLayer ->ResetReading ();
1748
+
1749
+ // getting the total number of features in the layer
1750
+ // TODO: This can be expensive, do we really need it!
1751
+ if ( updateFeatureCount )
1752
+ {
1753
+ recalculateFeatureCount ();
1754
+ }
1755
+
1756
+ // check the validity of the layer
1757
+ QgsDebugMsgLevel ( " checking validity" , 4 );
1758
+ loadFields ();
1759
+ QgsDebugMsgLevel ( " Done checking validity" , 4 );
1760
+
1761
+ invalidateCachedExtent ( false );
1762
+
1763
+ // Changing the filter may change capabilities
1764
+ if ( updateCapabilities )
1765
+ computeCapabilities ();
1766
+
1767
+ emit dataChanged ();
1768
+
1769
+ return true ;
1770
+
1771
+ }
1772
+
1766
1773
1767
1774
bool QgsOgrProvider::changeAttributeValues ( const QgsChangedAttributesMap &attr_map )
1768
1775
{
@@ -2186,10 +2193,19 @@ QgsVectorDataProvider::Capabilities QgsOgrProvider::capabilities() const
2186
2193
void QgsOgrProvider::computeCapabilities ()
2187
2194
{
2188
2195
QgsVectorDataProvider::Capabilities ability = nullptr ;
2196
+ bool updateModeActivated = false ;
2189
2197
2190
2198
// collect abilities reported by OGR
2191
2199
if ( mOgrLayer )
2192
2200
{
2201
+
2202
+ // We want the layer in rw mode or capabilities will be wrong
2203
+ // If mUpdateModeStackDepth > 0, it means that an updateMode is already active and that we have write access
2204
+ if ( mUpdateModeStackDepth == 0 )
2205
+ {
2206
+ updateModeActivated = _enterUpdateMode ( true );
2207
+ }
2208
+
2193
2209
// Whilst the OGR documentation (e.g. at
2194
2210
// http://www.gdal.org/ogr/classOGRLayer.html#a17) states "The capability
2195
2211
// codes that can be tested are represented as strings, but #defined
@@ -2318,6 +2334,9 @@ void QgsOgrProvider::computeCapabilities()
2318
2334
}
2319
2335
}
2320
2336
2337
+ if ( updateModeActivated )
2338
+ leaveUpdateMode ();
2339
+
2321
2340
mCapabilities = ability;
2322
2341
}
2323
2342
@@ -3969,7 +3988,8 @@ void QgsOgrProvider::open( OpenMode mode )
3969
3988
mSubsetString .clear ();
3970
3989
// Block signals to avoid endless recusion reloadData -> emit dataChanged -> reloadData
3971
3990
blockSignals ( true );
3972
- mValid = setSubsetString ( origSubsetString );
3991
+ // Do not update capabilities: it will be done later
3992
+ mValid = _setSubsetString ( origSubsetString, true , false );
3973
3993
blockSignals ( false );
3974
3994
if ( mValid )
3975
3995
{
@@ -4032,7 +4052,8 @@ void QgsOgrProvider::open( OpenMode mode )
4032
4052
{
4033
4053
int featuresCountedBackup = mFeaturesCounted ;
4034
4054
mFeaturesCounted = -1 ;
4035
- mValid = setSubsetString ( mSubsetString , false );
4055
+ // Do not update capabilities here
4056
+ mValid = _setSubsetString ( mSubsetString , false , false );
4036
4057
mFeaturesCounted = featuresCountedBackup;
4037
4058
}
4038
4059
}
0 commit comments