26
26
#include " qgssettings.h"
27
27
#include " qgsexception.h"
28
28
#include " qgswkbtypes.h"
29
+ #include " qgsogrtransaction.h"
29
30
30
31
#include < QTextCodec>
31
32
#include < QFile>
@@ -42,38 +43,51 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrFeatureSource *source, bool
42
43
: QgsAbstractFeatureIteratorFromSource<QgsOgrFeatureSource>( source, ownSource, request )
43
44
, mFilterFids( mRequest .filterFids() )
44
45
, mFilterFidsIt( mFilterFids .constBegin() )
46
+ , mSharedDS( source->mSharedDS )
45
47
{
46
- // QgsDebugMsg( "Feature iterator of " + mSource->mLayerName + ": acquiring connection");
47
- mConn = QgsOgrConnPool::instance ()->acquireConnection ( QgsOgrProviderUtils::connectionPoolId ( mSource ->mDataSource ) );
48
- if ( !mConn ->ds )
48
+ if ( mSharedDS )
49
49
{
50
- return ;
51
- }
52
-
53
- if ( mSource ->mLayerName .isNull () )
54
- {
55
- mOgrLayer = GDALDatasetGetLayer ( mConn ->ds , mSource ->mLayerIndex );
50
+ mOgrLayer = mSharedDS ->createSQLResultLayer ( mSource ->mEncoding , mSource ->mLayerName , mSource ->mLayerIndex );
51
+ if ( !mOgrLayer )
52
+ {
53
+ return ;
54
+ }
56
55
}
57
56
else
58
57
{
59
- mOgrLayer = GDALDatasetGetLayerByName ( mConn -> ds , mSource ->mLayerName . toUtf8 (). constData () );
60
- }
61
- if ( !mOgrLayer )
62
- {
63
- return ;
64
- }
58
+ // QgsDebugMsg( "Feature iterator of " + mSource->mLayerName + ": acquiring connection" );
59
+ mConn = QgsOgrConnPool::instance ()-> acquireConnection ( QgsOgrProviderUtils::connectionPoolId ( mSource -> mDataSource ) );
60
+ if ( !mConn -> ds )
61
+ {
62
+ return ;
63
+ }
65
64
66
- if ( !mSource ->mSubsetString .isEmpty () )
67
- {
68
- mOgrOrigLayer = mOgrLayer ;
69
- mOgrLayerWithFid = QgsOgrProviderUtils::setSubsetString ( mOgrLayer , mConn ->ds , mSource ->mEncoding , QString (), true , &mOrigFidAdded );
70
- mOgrLayer = QgsOgrProviderUtils::setSubsetString ( mOgrLayer , mConn ->ds , mSource ->mEncoding , mSource ->mSubsetString , true , &mOrigFidAdded );
65
+ if ( mSource ->mLayerName .isNull () )
66
+ {
67
+ mOgrLayer = GDALDatasetGetLayer ( mConn ->ds , mSource ->mLayerIndex );
68
+ }
69
+ else
70
+ {
71
+ mOgrLayer = GDALDatasetGetLayerByName ( mConn ->ds , mSource ->mLayerName .toUtf8 ().constData () );
72
+ }
71
73
if ( !mOgrLayer )
72
74
{
73
- close ();
74
75
return ;
75
76
}
77
+
78
+ if ( !mSource ->mSubsetString .isEmpty () )
79
+ {
80
+ mOgrOrigLayer = mOgrLayer ;
81
+ mOgrLayerWithFid = QgsOgrProviderUtils::setSubsetString ( mOgrLayer , mConn ->ds , mSource ->mEncoding , QString (), true , &mOrigFidAdded );
82
+ mOgrLayer = QgsOgrProviderUtils::setSubsetString ( mOgrLayer , mConn ->ds , mSource ->mEncoding , mSource ->mSubsetString , true , &mOrigFidAdded );
83
+ if ( !mOgrLayer )
84
+ {
85
+ close ();
86
+ return ;
87
+ }
88
+ }
76
89
}
90
+ QMutexLocker locker ( mSharedDS ? &mSharedDS ->mutex () : nullptr );
77
91
78
92
if ( mRequest .destinationCrs ().isValid () && mRequest .destinationCrs () != mSource ->mCrs )
79
93
{
@@ -237,6 +251,8 @@ bool QgsOgrFeatureIterator::fetchFeatureWithId( QgsFeatureId id, QgsFeature &fea
237
251
238
252
bool QgsOgrFeatureIterator::fetchFeature ( QgsFeature &feature )
239
253
{
254
+ QMutexLocker locker ( mSharedDS ? &mSharedDS ->mutex () : nullptr );
255
+
240
256
feature.setValid ( false );
241
257
242
258
if ( mClosed || !mOgrLayer )
@@ -286,6 +302,8 @@ bool QgsOgrFeatureIterator::fetchFeature( QgsFeature &feature )
286
302
287
303
bool QgsOgrFeatureIterator::rewind ()
288
304
{
305
+ QMutexLocker locker ( mSharedDS ? &mSharedDS ->mutex () : nullptr );
306
+
289
307
if ( mClosed || !mOgrLayer )
290
308
return false ;
291
309
@@ -299,6 +317,20 @@ bool QgsOgrFeatureIterator::rewind()
299
317
300
318
bool QgsOgrFeatureIterator::close ()
301
319
{
320
+ if ( mSharedDS )
321
+ {
322
+ iteratorClosed ();
323
+
324
+ if ( mOgrLayer )
325
+ {
326
+ mSharedDS ->releaseResultSet ( mOgrLayer );
327
+ mOgrLayer = nullptr ;
328
+ }
329
+ mSharedDS .reset ();
330
+ mClosed = true ;
331
+ return true ;
332
+ }
333
+
302
334
if ( !mConn )
303
335
return false ;
304
336
@@ -444,7 +476,12 @@ QgsOgrFeatureSource::QgsOgrFeatureSource( const QgsOgrProvider *p )
444
476
, mDriverName( p->mGDALDriverName )
445
477
, mCrs( p->crs () )
446
478
, mWkbType( p->wkbType () )
479
+ , mSharedDS( nullptr )
447
480
{
481
+ if ( p->mTransaction )
482
+ {
483
+ mSharedDS = p->mTransaction ->sharedDS ();
484
+ }
448
485
for ( int i = ( p->mFirstFieldIsFid ) ? 1 : 0 ; i < mFields .size (); i++ )
449
486
mFieldsWithoutFid .append ( mFields .at ( i ) );
450
487
QgsOgrConnPool::instance ()->ref ( QgsOgrProviderUtils::connectionPoolId ( mDataSource ) );
0 commit comments