Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes #39620 : Saving processing results in a database
  • Loading branch information
troopa81 authored and nyalldawson committed Feb 12, 2021
1 parent 4137287 commit f5d9154
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -846,9 +846,11 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs

// use destination string as layer name (eg "postgis:..." )
if ( !layerName.isEmpty() )
{
uri += QStringLiteral( "|layername=%1" ).arg( layerName );
// update destination to generated URI
destination = uri;
// update destination to generated URI
destination = uri;
}

return new QgsProcessingFeatureSink( exporter.release(), destination, context, true );
}
Expand Down
15 changes: 15 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -2020,6 +2020,21 @@ void TestQgsProcessing::createFeatureSink()
QCOMPARE( layer->featureCount(), 2L );
QVERIFY( layer->getFeatures().nextFeature( f ) );
QCOMPARE( f.attribute( "my_field" ).toString(), QStringLiteral( "val2" ) );

// save to database
destination = "postgres://dbname='qgis_test' service='qgis_test' table=\"public\".\"test_feature_sink\" (geom)";
sink.reset( QgsProcessingUtils::createFeatureSink( destination, context, fields, QgsWkbTypes::Polygon, QgsCoordinateReferenceSystem::fromEpsgId( 3111 ) ) );
QVERIFY( sink.get() );
f = QgsFeature( fields );
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((0 0, 0 1, 1 1, 1 0, 0 0 ))" ) ) );
f.setAttributes( QgsAttributes() << "val" );
QVERIFY( sink->addFeature( f ) );
sink.reset( nullptr );
layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( destination, context, true ) );
QVERIFY( layer && layer->isValid() );
QCOMPARE( layer->wkbType(), QgsWkbTypes::Polygon );
QVERIFY( layer->getFeatures().nextFeature( f ) );
QCOMPARE( f.attribute( "my_field" ).toString(), QStringLiteral( "val" ) );
}

void TestQgsProcessing::source()
Expand Down

0 comments on commit f5d9154

Please sign in to comment.