QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsalgorithmexecutespatialitequeryregistered.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmexecutespatialitequeryregistered.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
25
26QString QgsExecuteRegisteredSpatialiteQueryAlgorithm::name() const
27{
28 return QStringLiteral( "spatialiteexecutesqlregistered" );
29}
30
31QString QgsExecuteRegisteredSpatialiteQueryAlgorithm::displayName() const
32{
33 return QObject::tr( "SpatiaLite execute SQL (registered DB)" );
34}
35
36QStringList QgsExecuteRegisteredSpatialiteQueryAlgorithm::tags() const
37{
38 return QObject::tr( "database,sql,spatialite,execute" ).split( ',' );
39}
40
41QString QgsExecuteRegisteredSpatialiteQueryAlgorithm::group() const
42{
43 return QObject::tr( "Database" );
44}
45
46QString QgsExecuteRegisteredSpatialiteQueryAlgorithm::groupId() const
47{
48 return QStringLiteral( "database" );
49}
50
51QString QgsExecuteRegisteredSpatialiteQueryAlgorithm::shortHelpString() const
52{
53 return QObject::tr( "This algorithm executes a SQL command on a SpatiaLite database." );
54}
55
56QString QgsExecuteRegisteredSpatialiteQueryAlgorithm::shortDescription() const
57{
58 return QObject::tr( "Executes a SQL command on a SpatiaLite database." );
59}
60
61QgsExecuteRegisteredSpatialiteQueryAlgorithm *QgsExecuteRegisteredSpatialiteQueryAlgorithm::createInstance() const
62{
63 return new QgsExecuteRegisteredSpatialiteQueryAlgorithm();
64}
65
66void QgsExecuteRegisteredSpatialiteQueryAlgorithm::initAlgorithm( const QVariantMap & )
67{
68 addParameter( new QgsProcessingParameterProviderConnection( QStringLiteral( "DATABASE" ), QObject::tr( "Database (connection name)" ), QStringLiteral( "spatialite" ) ) );
69 addParameter( new QgsProcessingParameterString( QStringLiteral( "SQL" ), QObject::tr( "SQL query" ), QVariant(), true ) );
70}
71
72QVariantMap QgsExecuteRegisteredSpatialiteQueryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
73{
74 Q_UNUSED( feedback );
75
76 const QString connName = parameterAsConnectionName( parameters, QStringLiteral( "DATABASE" ), context );
77
78 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
79 try
80 {
81 QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "spatialite" ) );
82 conn.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connName ) ) );
83 }
85 {
86 throw QgsProcessingException( QObject::tr( "Could not retrieve connection details for %1" ).arg( connName ) );
87 }
88
89 const QString sql = parameterAsString( parameters, QStringLiteral( "SQL" ), context ).replace( '\n', ' ' );
90 try
91 {
92 conn->executeSql( sql );
93 }
95 {
96 throw QgsProcessingException( QObject::tr( "Error executing SQL:\n%1" ).arg( ex.what() ) );
97 }
98
99 return QVariantMap();
100}
101
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.