Skip to content
Permalink
Browse files

Fixes #39620 : Saving processing results in a database

  • Loading branch information
troopa81 authored and nyalldawson committed Feb 12, 2021
1 parent 4137287 commit f5d915460c76a192d33b76a7210ff2112fbe2af9
Showing with 19 additions and 2 deletions.
  1. +4 −2 src/core/processing/qgsprocessingutils.cpp
  2. +15 −0 tests/src/analysis/testqgsprocessing.cpp
@@ -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 );
}
@@ -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()

0 comments on commit f5d9154

Please sign in to comment.