QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgsalgorithmexecutespatialitequery.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#include "qgsproviderregistry.h"
20#include "qgsprovidermetadata.h"
22#include "qgsvectorlayer.h"
23
25
26QString QgsExecuteSpatialiteQueryAlgorithm::name() const
27{
28 return QStringLiteral( "spatialiteexecutesql" );
29}
30
31QString QgsExecuteSpatialiteQueryAlgorithm::displayName() const
32{
33 return QObject::tr( "SpatiaLite execute SQL" );
34}
35
36QStringList QgsExecuteSpatialiteQueryAlgorithm::tags() const
37{
38 return QObject::tr( "database,sql,spatialite,execute" ).split( ',' );
39}
40
41QString QgsExecuteSpatialiteQueryAlgorithm::group() const
42{
43 return QObject::tr( "Database" );
44}
45
46QString QgsExecuteSpatialiteQueryAlgorithm::groupId() const
47{
48 return QStringLiteral( "database" );
49}
50
51QString QgsExecuteSpatialiteQueryAlgorithm::shortHelpString() const
52{
53 return QObject::tr( "Executes a SQL command on a SpatiaLite database." );
54}
55
56QgsExecuteSpatialiteQueryAlgorithm *QgsExecuteSpatialiteQueryAlgorithm::createInstance() const
57{
58 return new QgsExecuteSpatialiteQueryAlgorithm();
59}
60
61void QgsExecuteSpatialiteQueryAlgorithm::initAlgorithm( const QVariantMap & )
62{
63 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "DATABASE" ), QObject::tr( "Database (connection name)" ), QList< int >() << static_cast< int >( Qgis::ProcessingSourceType::Vector ) ) );
64 addParameter( new QgsProcessingParameterString( QStringLiteral( "SQL" ), QObject::tr( "SQL query" ), QVariant(), true ) );
65}
66
67QVariantMap QgsExecuteSpatialiteQueryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
68{
69 //Q_UNUSED( feedback );
70 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "DATABASE" ), context );
71 QString databaseUri = layer->dataProvider()->dataSourceUri();
72 QgsDataSourceUri uri( databaseUri );
73 if ( uri.database().isEmpty() )
74 {
75 if ( databaseUri.contains( QStringLiteral( "|layername" ), Qt::CaseInsensitive ) )
76 databaseUri = databaseUri.left( databaseUri.indexOf( QLatin1String( "|layername" ) ) );
77 else if ( databaseUri.contains( QStringLiteral( "|layerid" ), Qt::CaseInsensitive ) )
78 databaseUri = databaseUri.left( databaseUri.indexOf( QLatin1String( "|layerid" ) ) );
79
80 uri = QgsDataSourceUri( QStringLiteral( "dbname='%1'" ).arg( databaseUri ) );
81 }
82
83 feedback->pushInfo( databaseUri );
84 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
85 try
86 {
87 QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "spatialite" ) );
88 conn.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( uri.uri(), QVariantMap() ) ) );
89 }
91 {
92 throw QgsProcessingException( QObject::tr( "Could not connect to %1" ).arg( uri.uri() ) );
93 }
94
95 const QString sql = parameterAsString( parameters, QStringLiteral( "SQL" ), context ).replace( '\n', ' ' );
96 try
97 {
98 conn->executeSql( sql );
99 }
101 {
102 throw QgsProcessingException( QObject::tr( "Error executing SQL:\n%1" ).arg( ex.what() ) );
103 }
104
105 return QVariantMap();
106}
107
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
virtual QString dataSourceUri(bool expandAuthConfig=false) const
Gets the data source specification.
Class for storing the component parts of a RDBMS 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.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the 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 data sets.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.