QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsalgorithmtruncatetable.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmtruncatetable.cpp
3 ---------------------
4 begin : December 2019
5 copyright : (C) 2019 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgsvectorlayer.h"
20
22
23QString QgsTruncateTableAlgorithm::name() const
24{
25 return QStringLiteral( "truncatetable" );
26}
27
28QString QgsTruncateTableAlgorithm::displayName() const
29{
30 return QObject::tr( "Truncate table" );
31}
32
33QStringList QgsTruncateTableAlgorithm::tags() const
34{
35 return QObject::tr( "empty,delete,layer,clear,features" ).split( ',' );
36}
37
38QString QgsTruncateTableAlgorithm::group() const
39{
40 return QObject::tr( "Vector general" );
41}
42
43QString QgsTruncateTableAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeneral" );
46}
47
48QString QgsTruncateTableAlgorithm::shortHelpString() const
49{
50 return QObject::tr( "This algorithm truncates a layer, by deleting all features from within the layer." )
51 + QStringLiteral( "\n\n" )
52 + QObject::tr( "Warning — this algorithm modifies the layer in place, and deleted features cannot be restored!" );
53}
54
55QgsProcessingAlgorithm::Flags QgsTruncateTableAlgorithm::flags() const
56{
58}
59
60QgsTruncateTableAlgorithm *QgsTruncateTableAlgorithm::createInstance() const
61{
62 return new QgsTruncateTableAlgorithm();
63}
64
65void QgsTruncateTableAlgorithm::initAlgorithm( const QVariantMap & )
66{
67 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
68 addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Truncated layer" ) ) );
69}
70
71QVariantMap QgsTruncateTableAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
72{
73 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
74
75 if ( !layer )
76 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
77
78 if ( !layer->dataProvider()->truncate() )
79 {
80 throw QgsProcessingException( QObject::tr( "Could not truncate table." ) );
81 }
82
83 QVariantMap outputs;
84 outputs.insert( QStringLiteral( "OUTPUT" ), layer->id() );
85 return outputs;
86}
87
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
@ FlagNoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
Base class for providing feedback from a processing algorithm.
A vector layer output for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
virtual bool truncate()
Removes all features from the layer.
Represents a vector layer which manages a vector based data sets.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.