QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmexecutepostgisquery.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmexecutepostgisquery.cpp
3 ---------------------
4 begin : May 2020
5 copyright : (C) 2020 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
21#include "qgsprovidermetadata.h"
22#include "qgsproviderregistry.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29
30QString QgsExecutePostgisQueryAlgorithm::name() const
31{
32 return u"postgisexecutesql"_s;
33}
34
35QString QgsExecutePostgisQueryAlgorithm::displayName() const
36{
37 return QObject::tr( "PostgreSQL execute SQL" );
38}
39
40QStringList QgsExecutePostgisQueryAlgorithm::tags() const
41{
42 return QObject::tr( "database,sql,postgresql,postgis,execute" ).split( ',' );
43}
44
45QString QgsExecutePostgisQueryAlgorithm::group() const
46{
47 return QObject::tr( "Database" );
48}
49
50QString QgsExecutePostgisQueryAlgorithm::groupId() const
51{
52 return u"database"_s;
53}
54
55QString QgsExecutePostgisQueryAlgorithm::shortHelpString() const
56{
57 return QObject::tr( "This algorithm executes a SQL command on a PostgreSQL database." );
58}
59
60QString QgsExecutePostgisQueryAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Executes a SQL command on a PostgreSQL database." );
63}
64
65QgsExecutePostgisQueryAlgorithm *QgsExecutePostgisQueryAlgorithm::createInstance() const
66{
67 return new QgsExecutePostgisQueryAlgorithm();
68}
69
70void QgsExecutePostgisQueryAlgorithm::initAlgorithm( const QVariantMap & )
71{
72 addParameter( new QgsProcessingParameterProviderConnection( u"DATABASE"_s, QObject::tr( "Database (connection name)" ), u"postgres"_s ) );
73 addParameter( new QgsProcessingParameterString( u"SQL"_s, QObject::tr( "SQL query" ), QVariant(), true ) );
74}
75
76QVariantMap QgsExecutePostgisQueryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
77{
78 Q_UNUSED( feedback );
79
80 const QString connName = parameterAsConnectionName( parameters, u"DATABASE"_s, context );
81
82 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
83 try
84 {
86 conn.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connName ) ) );
87 }
89 {
90 throw QgsProcessingException( QObject::tr( "Could not retrieve connection details for %1" ).arg( connName ) );
91 }
92
93 const QString sql = parameterAsString( parameters, u"SQL"_s, context ).replace( '\n', ' ' );
94 try
95 {
96 conn->executeSql( sql );
97 }
99 {
100 throw QgsProcessingException( QObject::tr( "Error executing SQL:\n%1" ).arg( ex.what() ) );
101 }
102
103 return QVariantMap();
104}
105
Provides common functionality for database based connections.
QString what() const
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A data provider connection parameter for processing algorithms, allowing users to select from availab...
A string parameter for processing algorithms.
Custom exception class for provider connection related exceptions.
Holds data provider key, description, and associated shared library file or function pointer informat...
virtual QgsAbstractProviderConnection * createConnection(const QString &uri, const QVariantMap &configuration)
Creates a new connection from uri and configuration, the newly created connection is not automaticall...
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.