QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
26 QString QgsExecuteSpatialiteQueryAlgorithm::name() const
27 {
28  return QStringLiteral( "spatialiteexecutesql" );
29 }
30 
31 QString QgsExecuteSpatialiteQueryAlgorithm::displayName() const
32 {
33  return QObject::tr( "SpatiaLite execute SQL" );
34 }
35 
36 QStringList QgsExecuteSpatialiteQueryAlgorithm::tags() const
37 {
38  return QObject::tr( "database,sql,spatialite,execute" ).split( ',' );
39 }
40 
41 QString QgsExecuteSpatialiteQueryAlgorithm::group() const
42 {
43  return QObject::tr( "Database" );
44 }
45 
46 QString QgsExecuteSpatialiteQueryAlgorithm::groupId() const
47 {
48  return QStringLiteral( "database" );
49 }
50 
51 QString QgsExecuteSpatialiteQueryAlgorithm::shortHelpString() const
52 {
53  return QObject::tr( "Executes a SQL command on a SpatiaLite database." );
54 }
55 
56 QgsExecuteSpatialiteQueryAlgorithm *QgsExecuteSpatialiteQueryAlgorithm::createInstance() const
57 {
58  return new QgsExecuteSpatialiteQueryAlgorithm();
59 }
60 
61 void QgsExecuteSpatialiteQueryAlgorithm::initAlgorithm( const QVariantMap & )
62 {
63  addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "DATABASE" ), QObject::tr( "Database (connection name)" ), QList< int >() << QgsProcessing::TypeVector ) );
64  addParameter( new QgsProcessingParameterString( QStringLiteral( "SQL" ), QObject::tr( "SQL query" ), QVariant(), true ) );
65 }
66 
67 QVariantMap 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  }
100  catch ( QgsProviderConnectionException &ex )
101  {
102  throw QgsProcessingException( QObject::tr( "Error executing SQL:\n%1" ).arg( ex.what() ) );
103  }
104 
105  return QVariantMap();
106 }
107 
QgsDataSourceUri
Class for storing the component parts of a RDBMS data source URI (e.g. a Postgres data source).
Definition: qgsdatasourceuri.h:37
QgsVectorLayer::dataProvider
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Definition: qgsvectorlayer.cpp:676
QgsProviderMetadata::createConnection
virtual QgsAbstractProviderConnection * createConnection(const QString &uri, const QVariantMap &configuration) SIP_THROW(QgsProviderConnectionException)
Creates a new connection from uri and configuration, the newly created connection is not automaticall...
Definition: qgsprovidermetadata.cpp:316
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
qgsalgorithmexecutespatialitequery.h
QgsProcessingFeedback::pushInfo
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:77
QgsDataProvider::dataSourceUri
virtual QString dataSourceUri(bool expandAuthConfig=false) const
Gets the data source specification.
Definition: qgsdataprovider.h:166
qgsprovidermetadata.h
QgsProcessing::TypeVector
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:54
qgsproviderregistry.h
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsException::what
QString what() const
Definition: qgsexception.h:48
QgsProcessingParameterString
A string parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2647
QgsProviderRegistry::providerMetadata
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
Definition: qgsproviderregistry.cpp:873
QgsProcessingParameterVectorLayer
A vector layer (with or without geometry) parameter for processing algorithms. Consider using the mor...
Definition: qgsprocessingparameters.h:2827
QgsProviderConnectionException
Custom exception class for provider connection related exceptions.
Definition: qgsexception.h:100
QgsProviderMetadata
Holds data provider key, description, and associated shared library file or function pointer informat...
Definition: qgsprovidermetadata.h:177
qgsvectorlayer.h
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
qgsabstractdatabaseproviderconnection.h
QgsProviderRegistry::instance
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Definition: qgsproviderregistry.cpp:73
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsAbstractDatabaseProviderConnection
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Definition: qgsabstractdatabaseproviderconnection.h:44