QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmexecutespatialitequery.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmexecutespatialitequery.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#include "qgsvectorlayer.h"
24
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
30
31QString QgsExecuteSpatialiteQueryAlgorithm::name() const
32{
33 return u"spatialiteexecutesql"_s;
34}
35
36QString QgsExecuteSpatialiteQueryAlgorithm::displayName() const
37{
38 return QObject::tr( "SpatiaLite execute SQL" );
39}
40
41QStringList QgsExecuteSpatialiteQueryAlgorithm::tags() const
42{
43 return QObject::tr( "database,sql,spatialite,execute" ).split( ',' );
44}
45
46QString QgsExecuteSpatialiteQueryAlgorithm::group() const
47{
48 return QObject::tr( "Database" );
49}
50
51QString QgsExecuteSpatialiteQueryAlgorithm::groupId() const
52{
53 return u"database"_s;
54}
55
56QString QgsExecuteSpatialiteQueryAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm executes a SQL command on a SpatiaLite database. The database is determined by an input layer or file." );
59}
60
61QString QgsExecuteSpatialiteQueryAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Executes a SQL command on a SpatiaLite database determined by an input layer or file." );
64}
65
66QgsExecuteSpatialiteQueryAlgorithm *QgsExecuteSpatialiteQueryAlgorithm::createInstance() const
67{
68 return new QgsExecuteSpatialiteQueryAlgorithm();
69}
70
71void QgsExecuteSpatialiteQueryAlgorithm::initAlgorithm( const QVariantMap & )
72{
73 addParameter( new QgsProcessingParameterVectorLayer( u"DATABASE"_s, QObject::tr( "Database layer (or file)" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
74 addParameter( new QgsProcessingParameterString( u"SQL"_s, QObject::tr( "SQL query" ), QVariant(), true ) );
75}
76
77QVariantMap QgsExecuteSpatialiteQueryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
78{
79 Q_UNUSED( feedback );
80 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, u"DATABASE"_s, context );
81 QString databaseUri = layer->dataProvider()->dataSourceUri();
82 QgsDataSourceUri uri( databaseUri );
83 if ( uri.database().isEmpty() )
84 {
85 if ( databaseUri.contains( u"|layername"_s, Qt::CaseInsensitive ) )
86 databaseUri = databaseUri.left( databaseUri.indexOf( "|layername"_L1 ) );
87 else if ( databaseUri.contains( u"|layerid"_s, Qt::CaseInsensitive ) )
88 databaseUri = databaseUri.left( databaseUri.indexOf( "|layerid"_L1 ) );
89
90 uri = QgsDataSourceUri( u"dbname='%1'"_s.arg( databaseUri ) );
91 }
92
93 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
94 try
95 {
97 conn.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( uri.uri(), QVariantMap() ) ) );
98 }
100 {
101 throw QgsProcessingException( QObject::tr( "Could not connect to %1" ).arg( uri.uri() ) );
102 }
103
104 const QString sql = parameterAsString( parameters, u"SQL"_s, context ).replace( '\n', ' ' );
105 try
106 {
107 conn->executeSql( sql );
108 }
110 {
111 throw QgsProcessingException( QObject::tr( "Error executing SQL:\n%1" ).arg( ex.what() ) );
112 }
113
114 return QVariantMap();
115}
116
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3610
Provides common functionality for database based connections.
virtual QString dataSourceUri(bool expandAuthConfig=false) const
Gets the data source specification.
Stores the component parts of a data source URI (e.g.
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 string parameter for processing algorithms.
A vector layer (with or without geometry) 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.
Represents a vector layer which manages a vector based dataset.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.