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 
18 #include "qgsalgorithmtruncatetable.h"
19 #include "qgsvectorlayer.h"
20 
21 ///@cond PRIVATE
22 
name() const23 QString QgsTruncateTableAlgorithm::name() const
24 {
25   return QStringLiteral( "truncatetable" );
26 }
27 
displayName() const28 QString QgsTruncateTableAlgorithm::displayName() const
29 {
30   return QObject::tr( "Truncate table" );
31 }
32 
tags() const33 QStringList QgsTruncateTableAlgorithm::tags() const
34 {
35   return QObject::tr( "empty,delete,layer,clear,features" ).split( ',' );
36 }
37 
group() const38 QString QgsTruncateTableAlgorithm::group() const
39 {
40   return QObject::tr( "Vector general" );
41 }
42 
groupId() const43 QString QgsTruncateTableAlgorithm::groupId() const
44 {
45   return QStringLiteral( "vectorgeneral" );
46 }
47 
shortHelpString() const48 QString 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 
flags() const55 QgsProcessingAlgorithm::Flags QgsTruncateTableAlgorithm::flags() const
56 {
57   return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagNoThreading;
58 }
59 
createInstance() const60 QgsTruncateTableAlgorithm *QgsTruncateTableAlgorithm::createInstance() const
61 {
62   return new QgsTruncateTableAlgorithm();
63 }
64 
initAlgorithm(const QVariantMap &)65 void 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 
processAlgorithm(const QVariantMap & parameters,QgsProcessingContext & context,QgsProcessingFeedback *)71 QVariantMap 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 
88 ///@endcond
89