@@ -307,9 +307,9 @@ QString QgsProcessingUtils::stringToPythonLiteral( const QString &string )
307
307
return s;
308
308
}
309
309
310
- void parseDestinationString ( QString &destination, QString &providerKey, QString &uri, QString &format, QMap<QString, QVariant> &options )
310
+ void QgsProcessingUtils:: parseDestinationString ( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString & format, QMap<QString, QVariant> &options, bool &useWriter )
311
311
{
312
- QRegularExpression splitRx ( QStringLiteral ( " ^(.{3,}):(.*)$" ) );
312
+ QRegularExpression splitRx ( QStringLiteral ( " ^(.{3,}? ):(.*)$" ) );
313
313
QRegularExpressionMatch match = splitRx.match ( destination );
314
314
if ( match.hasMatch () )
315
315
{
@@ -319,9 +319,25 @@ void parseDestinationString( QString &destination, QString &providerKey, QString
319
319
providerKey = QStringLiteral ( " postgres" );
320
320
}
321
321
uri = match.captured ( 2 );
322
+ if ( providerKey == QLatin1String ( " ogr" ) )
323
+ {
324
+ QgsDataSourceUri dsUri ( uri );
325
+ if ( !dsUri.database ().isEmpty () )
326
+ {
327
+ if ( !dsUri.table ().isEmpty () )
328
+ {
329
+ layerName = dsUri.table ();
330
+ options.insert ( " layerName" , layerName );
331
+ }
332
+ uri = dsUri.database ();
333
+ }
334
+ options.insert ( QStringLiteral ( " update" ), true );
335
+ }
336
+ useWriter = false ;
322
337
}
323
338
else
324
339
{
340
+ useWriter = true ;
325
341
providerKey = QStringLiteral ( " ogr" );
326
342
QRegularExpression splitRx ( QStringLiteral ( " ^(.*)\\ .(.*?)$" ) );
327
343
QRegularExpressionMatch match = splitRx.match ( destination );
@@ -356,12 +372,9 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
356
372
{
357
373
// memory provider cannot be used with QgsVectorLayerImport - so create layer manually
358
374
std::unique_ptr< QgsVectorLayer > layer ( QgsMemoryProviderUtils::createMemoryLayer ( destination, fields, geometryType, crs ) );
359
- if ( !layer )
360
- return nullptr ;
361
-
362
- if ( !layer->isValid () )
375
+ if ( !layer || !layer->isValid () )
363
376
{
364
- return nullptr ;
377
+ throw QgsProcessingException ( QObject::tr ( " Could not create memory layer " ) ) ;
365
378
}
366
379
367
380
// update destination to layer ID
@@ -377,30 +390,42 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
377
390
{
378
391
QString providerKey;
379
392
QString uri;
393
+ QString layerName;
380
394
QString format;
381
- parseDestinationString ( destination, providerKey, uri, format, options );
395
+ bool useWriter = false ;
396
+ parseDestinationString ( destination, providerKey, uri, layerName, format, options, useWriter );
382
397
383
- if ( providerKey == QLatin1String ( " ogr" ) )
398
+ if ( useWriter && providerKey == QLatin1String ( " ogr" ) )
384
399
{
385
400
// use QgsVectorFileWriter for OGR destinations instead of QgsVectorLayerImport, as that allows
386
401
// us to use any OGR format which supports feature addition
387
402
QString finalFileName;
388
- QgsVectorFileWriter * writer = new QgsVectorFileWriter ( destination, options.value ( QStringLiteral ( " fileEncoding" ) ).toString (), fields, geometryType, crs, format, QgsVectorFileWriter::defaultDatasetOptions ( format ),
403
+ std::unique_ptr< QgsVectorFileWriter > writer = qgis::make_unique< QgsVectorFileWriter > ( destination, options.value ( QStringLiteral ( " fileEncoding" ) ).toString (), fields, geometryType, crs, format, QgsVectorFileWriter::defaultDatasetOptions ( format ),
389
404
QgsVectorFileWriter::defaultLayerOptions ( format ), &finalFileName );
405
+
406
+ if ( writer->hasError () )
407
+ {
408
+ throw QgsProcessingException ( QObject::tr ( " Could not create layer %1: %2" ).arg ( destination, writer->errorMessage () ) );
409
+ }
390
410
destination = finalFileName;
391
- return writer;
411
+ return writer. release () ;
392
412
}
393
413
else
394
414
{
395
415
// create empty layer
396
- std::unique_ptr< QgsVectorLayerExporter > exporter ( new QgsVectorLayerExporter ( uri, providerKey, fields, geometryType, crs, false , options ) );
416
+ std::unique_ptr< QgsVectorLayerExporter > exporter ( new QgsVectorLayerExporter ( uri, providerKey, fields, geometryType, crs, true , options ) );
397
417
if ( exporter->errorCode () )
398
- return nullptr ;
418
+ {
419
+ throw QgsProcessingException ( QObject::tr ( " Could not create layer %1: %2" ).arg ( destination, exporter->errorMessage () ) );
420
+ }
399
421
400
422
// use destination string as layer name (eg "postgis:..." )
423
+ if ( !layerName.isEmpty () )
424
+ uri += QStringLiteral ( " |layername=%1" ).arg ( layerName );
401
425
std::unique_ptr< QgsVectorLayer > layer ( new QgsVectorLayer ( uri, destination, providerKey ) );
402
426
// update destination to layer ID
403
427
destination = layer->id ();
428
+
404
429
context.temporaryLayerStore ()->addMapLayer ( layer.release () );
405
430
return exporter.release ();
406
431
}
0 commit comments