@@ -1306,6 +1306,8 @@ bool QgsOgrProvider::addFeatures( QgsFeatureList & flist )
1306
1306
1307
1307
setRelevantFields ( ogrLayer, true , attributeIndexes () );
1308
1308
1309
+ const bool inTransaction = startTransaction ();
1310
+
1309
1311
bool returnvalue = true ;
1310
1312
for ( QgsFeatureList::iterator it = flist.begin (); it != flist.end (); ++it )
1311
1313
{
@@ -1315,6 +1317,11 @@ bool QgsOgrProvider::addFeatures( QgsFeatureList & flist )
1315
1317
}
1316
1318
}
1317
1319
1320
+ if ( inTransaction )
1321
+ {
1322
+ commitTransaction ();
1323
+ }
1324
+
1318
1325
if ( !syncToDisc () )
1319
1326
{
1320
1327
returnvalue = false ;
@@ -1521,6 +1528,31 @@ bool QgsOgrProvider::renameAttributes( const QgsFieldNameMap& renamedAttributes
1521
1528
#endif
1522
1529
}
1523
1530
1531
+ bool QgsOgrProvider::startTransaction ()
1532
+ {
1533
+ bool inTransaction = false ;
1534
+ if ( OGR_L_TestCapability ( ogrLayer, OLCTransactions ) )
1535
+ {
1536
+ // A transaction might already be active, so be robust on failed
1537
+ // StartTransaction.
1538
+ CPLPushErrorHandler ( CPLQuietErrorHandler );
1539
+ inTransaction = ( OGR_L_StartTransaction ( ogrLayer ) == OGRERR_NONE );
1540
+ CPLPopErrorHandler ();
1541
+ }
1542
+ return inTransaction;
1543
+ }
1544
+
1545
+
1546
+ bool QgsOgrProvider::commitTransaction ()
1547
+ {
1548
+ if ( OGR_L_CommitTransaction ( ogrLayer ) != OGRERR_NONE )
1549
+ {
1550
+ pushError ( tr ( " OGR error committing transaction: %1" ).arg ( CPLGetLastErrorMsg () ) );
1551
+ return false ;
1552
+ }
1553
+ return true ;
1554
+ }
1555
+
1524
1556
1525
1557
bool QgsOgrProvider::changeAttributeValues ( const QgsChangedAttributesMap &attr_map )
1526
1558
{
@@ -1534,6 +1566,8 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
1534
1566
1535
1567
setRelevantFields ( ogrLayer, true , attributeIndexes () );
1536
1568
1569
+ const bool inTransaction = startTransaction ();
1570
+
1537
1571
for ( QgsChangedAttributesMap::const_iterator it = attr_map.begin (); it != attr_map.end (); ++it )
1538
1572
{
1539
1573
QgsFeatureId fid = it.key ();
@@ -1649,6 +1683,11 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
1649
1683
OGR_F_Destroy ( of );
1650
1684
}
1651
1685
1686
+ if ( inTransaction )
1687
+ {
1688
+ commitTransaction ();
1689
+ }
1690
+
1652
1691
if ( OGR_L_SyncToDisk ( ogrLayer ) != OGRERR_NONE )
1653
1692
{
1654
1693
pushError ( tr ( " OGR error syncing to disk: %1" ).arg ( CPLGetLastErrorMsg () ) );
@@ -1664,6 +1703,8 @@ bool QgsOgrProvider::changeGeometryValues( const QgsGeometryMap &geometry_map )
1664
1703
1665
1704
setRelevantFields ( ogrLayer, true , attributeIndexes () );
1666
1705
1706
+ const bool inTransaction = startTransaction ();
1707
+
1667
1708
for ( QgsGeometryMap::const_iterator it = geometry_map.constBegin (); it != geometry_map.constEnd (); ++it )
1668
1709
{
1669
1710
if ( FID_TO_NUMBER ( it.key () ) > std::numeric_limits<long >::max () )
@@ -1731,6 +1772,12 @@ bool QgsOgrProvider::changeGeometryValues( const QgsGeometryMap &geometry_map )
1731
1772
1732
1773
OGR_F_Destroy ( theOGRFeature );
1733
1774
}
1775
+
1776
+ if ( inTransaction )
1777
+ {
1778
+ commitTransaction ();
1779
+ }
1780
+
1734
1781
QgsOgrConnPool::instance ()->invalidateConnections ( dataSourceUri () );
1735
1782
return syncToDisc ();
1736
1783
}
@@ -1780,6 +1827,8 @@ bool QgsOgrProvider::deleteFeatures( const QgsFeatureIds & id )
1780
1827
if ( !doInitialActionsForEdition () )
1781
1828
return false ;
1782
1829
1830
+ const bool inTransaction = startTransaction ();
1831
+
1783
1832
bool returnvalue = true ;
1784
1833
for ( QgsFeatureIds::const_iterator it = id.begin (); it != id.end (); ++it )
1785
1834
{
@@ -1789,6 +1838,11 @@ bool QgsOgrProvider::deleteFeatures( const QgsFeatureIds & id )
1789
1838
}
1790
1839
}
1791
1840
1841
+ if ( inTransaction )
1842
+ {
1843
+ commitTransaction ();
1844
+ }
1845
+
1792
1846
if ( !syncToDisc () )
1793
1847
{
1794
1848
returnvalue = false ;
0 commit comments