QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
26
27QString QgsExecuteSpatialiteQueryAlgorithm::name() const
28{
29 return QStringLiteral( "spatialiteexecutesql" );
30}
31
32QString QgsExecuteSpatialiteQueryAlgorithm::displayName() const
33{
34 return QObject::tr( "SpatiaLite execute SQL" );
35}
36
37QStringList QgsExecuteSpatialiteQueryAlgorithm::tags() const
38{
39 return QObject::tr( "database,sql,spatialite,execute" ).split( ',' );
40}
41
42QString QgsExecuteSpatialiteQueryAlgorithm::group() const
43{
44 return QObject::tr( "Database" );
45}
46
47QString QgsExecuteSpatialiteQueryAlgorithm::groupId() const
48{
49 return QStringLiteral( "database" );
50}
51
52QString QgsExecuteSpatialiteQueryAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm executes a SQL command on a SpatiaLite database. The database is determined by an input layer or file." );
55}
56
57QString QgsExecuteSpatialiteQueryAlgorithm::shortDescription() const
58{
59 return QObject::tr( "Executes a SQL command on a SpatiaLite database determined by an input layer or file." );
60}
61
62QgsExecuteSpatialiteQueryAlgorithm *QgsExecuteSpatialiteQueryAlgorithm::createInstance() const
63{
64 return new QgsExecuteSpatialiteQueryAlgorithm();
65}
66
67void QgsExecuteSpatialiteQueryAlgorithm::initAlgorithm( const QVariantMap & )
68{
69 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "DATABASE" ), QObject::tr( "Database layer (or file)" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
70 addParameter( new QgsProcessingParameterString( QStringLiteral( "SQL" ), QObject::tr( "SQL query" ), QVariant(), true ) );
71}
72
73QVariantMap QgsExecuteSpatialiteQueryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
74{
75 Q_UNUSED( feedback );
76 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "DATABASE" ), context );
77 QString databaseUri = layer->dataProvider()->dataSourceUri();
78 QgsDataSourceUri uri( databaseUri );
79 if ( uri.database().isEmpty() )
80 {
81 if ( databaseUri.contains( QStringLiteral( "|layername" ), Qt::CaseInsensitive ) )
82 databaseUri = databaseUri.left( databaseUri.indexOf( QLatin1String( "|layername" ) ) );
83 else if ( databaseUri.contains( QStringLiteral( "|layerid" ), Qt::CaseInsensitive ) )
84 databaseUri = databaseUri.left( databaseUri.indexOf( QLatin1String( "|layerid" ) ) );
85
86 uri = QgsDataSourceUri( QStringLiteral( "dbname='%1'" ).arg( databaseUri ) );
87 }
88
89 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
90 try
91 {
92 QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "spatialite" ) );
93 conn.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( uri.uri(), QVariantMap() ) ) );
94 }
96 {
97 throw QgsProcessingException( QObject::tr( "Could not connect to %1" ).arg( uri.uri() ) );
98 }
99
100 const QString sql = parameterAsString( parameters, QStringLiteral( "SQL" ), context ).replace( '\n', ' ' );
101 try
102 {
103 conn->executeSql( sql );
104 }
106 {
107 throw QgsProcessingException( QObject::tr( "Error executing SQL:\n%1" ).arg( ex.what() ) );
108 }
109
110 return QVariantMap();
111}
112
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3539
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.