QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
24 
25 QString QgsExecuteSpatialiteQueryAlgorithm::name() const
26 {
27  return QStringLiteral( "spatialiteexecutesql" );
28 }
29 
30 QString QgsExecuteSpatialiteQueryAlgorithm::displayName() const
31 {
32  return QObject::tr( "SpatiaLite execute SQL" );
33 }
34 
35 QStringList QgsExecuteSpatialiteQueryAlgorithm::tags() const
36 {
37  return QObject::tr( "database,sql,spatialite,execute" ).split( ',' );
38 }
39 
40 QString QgsExecuteSpatialiteQueryAlgorithm::group() const
41 {
42  return QObject::tr( "Database" );
43 }
44 
45 QString QgsExecuteSpatialiteQueryAlgorithm::groupId() const
46 {
47  return QStringLiteral( "database" );
48 }
49 
50 QString QgsExecuteSpatialiteQueryAlgorithm::shortHelpString() const
51 {
52  return QObject::tr( "Executes a SQL command on a SpatiaLite database." );
53 }
54 
55 QgsExecuteSpatialiteQueryAlgorithm *QgsExecuteSpatialiteQueryAlgorithm::createInstance() const
56 {
57  return new QgsExecuteSpatialiteQueryAlgorithm();
58 }
59 
60 void QgsExecuteSpatialiteQueryAlgorithm::initAlgorithm( const QVariantMap & )
61 {
62  addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "DATABASE" ), QObject::tr( "Database (connection name)" ), QList< int >() << QgsProcessing::TypeVector ) );
63  addParameter( new QgsProcessingParameterString( QStringLiteral( "SQL" ), QObject::tr( "SQL query" ), QVariant(), true ) );
64 }
65 
66 QVariantMap QgsExecuteSpatialiteQueryAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
67 {
68  //Q_UNUSED( feedback );
69  QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "DATABASE" ), context );
70  QString databaseUri = layer->dataProvider()->dataSourceUri();
71  QgsDataSourceUri uri( databaseUri );
72  if ( uri.database().isEmpty() )
73  {
74  if ( databaseUri.contains( QStringLiteral( "|layername" ), Qt::CaseInsensitive ) )
75  databaseUri = databaseUri.left( databaseUri.indexOf( QStringLiteral( "|layername" ) ) );
76  else if ( databaseUri.contains( QStringLiteral( "|layerid" ), Qt::CaseInsensitive ) )
77  databaseUri = databaseUri.left( databaseUri.indexOf( QStringLiteral( "|layerid" ) ) );
78 
79  uri = QgsDataSourceUri( QStringLiteral( "dbname='%1'" ).arg( databaseUri ) );
80  }
81 
82  feedback->pushInfo( databaseUri );
83  std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
84  try
85  {
86  QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "spatialite" ) );
87  conn.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( uri.uri(), QVariantMap() ) ) );
88  }
90  {
91  throw QgsProcessingException( QObject::tr( "Could not connect to %1" ).arg( uri.uri() ) );
92  }
93 
94  QString sql = parameterAsString( parameters, QStringLiteral( "SQL" ), context ).replace( '\n', ' ' );
95  try
96  {
97  conn->executeSql( sql );
98  }
99  catch ( QgsProviderConnectionException &ex )
100  {
101  throw QgsProcessingException( QObject::tr( "Error executing SQL:\n%1" ).arg( ex.what() ) );
102  }
103 
104  return QVariantMap();
105 }
106 
QgsDataSourceUri
Definition: qgsdatasourceuri.h:35
QgsVectorLayer::dataProvider
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Definition: qgsvectorlayer.cpp:627
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:241
QgsProcessingFeedback
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:48
QgsDataProvider::dataSourceUri
virtual QString dataSourceUri(bool expandAuthConfig=false) const
Gets the data source specification.
Definition: qgsdataprovider.h:142
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:53
qgsproviderregistry.h
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsException::what
QString what() const
Definition: qgsexception.h:48
QgsProcessingParameterString
Definition: qgsprocessingparameters.h:2202
QgsProviderRegistry::providerMetadata
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
Definition: qgsproviderregistry.cpp:722
QgsProcessingParameterVectorLayer
Definition: qgsprocessingparameters.h:2382
QgsProviderConnectionException
Definition: qgsexception.h:100
QgsProviderMetadata
Definition: qgsprovidermetadata.h:126
QgsVectorLayer
Definition: qgsvectorlayer.h:385
qgsabstractdatabaseproviderconnection.h
QgsProviderRegistry::instance
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Definition: qgsproviderregistry.cpp:48
QgsProcessingException
Definition: qgsexception.h:82
QgsAbstractDatabaseProviderConnection
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Definition: qgsabstractdatabaseproviderconnection.h:40