Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[processing] port SpatiaLite Execute SQL algorithm to C++
- Loading branch information
Showing
6 changed files
with
153 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 0 additions & 88 deletions
88
python/plugins/processing/algs/qgis/SpatialiteExecuteSQL.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/analysis/processing/qgsalgorithmexecutespatialitequery.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/*************************************************************************** | ||
qgsalgorithmexecutepostgisquery.cpp | ||
--------------------- | ||
begin : May 2020 | ||
copyright : (C) 2020 by Alexander Bruy | ||
email : alexander dot bruy at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsalgorithmexecutespatialitequery.h" | ||
#include "qgsproviderregistry.h" | ||
#include "qgsprovidermetadata.h" | ||
#include "qgsabstractdatabaseproviderconnection.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsExecuteSpatialiteQueryAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "spatialiteexecutesql" ); | ||
} | ||
|
||
QString QgsExecuteSpatialiteQueryAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "SpatiaLite execute SQL" ); | ||
} | ||
|
||
QStringList QgsExecuteSpatialiteQueryAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "database,sql,spatialite,execute" ).split( ',' ); | ||
} | ||
|
||
QString QgsExecuteSpatialiteQueryAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Database" ); | ||
} | ||
|
||
QString QgsExecuteSpatialiteQueryAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "database" ); | ||
} | ||
|
||
QString QgsExecuteSpatialiteQueryAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "Executes a SQL command on a SpatiaLite database." ); | ||
} | ||
|
||
QgsExecuteSpatialiteQueryAlgorithm *QgsExecuteSpatialiteQueryAlgorithm::createInstance() const | ||
{ | ||
return new QgsExecuteSpatialiteQueryAlgorithm(); | ||
} | ||
|
||
void QgsExecuteSpatialiteQueryAlgorithm::initAlgorithm( const QVariantMap & ) | ||
{ | ||
addParameter( new QgsProcessingParameterProviderConnection( QStringLiteral( "DATABASE" ), QObject::tr( "Database (connection name)" ), QStringLiteral( "spatialite" ) ) ); | ||
addParameter( new QgsProcessingParameterString( QStringLiteral( "SQL" ), QObject::tr( "SQL query" ), QVariant(), true ) ); | ||
} | ||
|
||
QVariantMap QgsExecuteSpatialiteQueryAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | ||
{ | ||
Q_UNUSED( feedback ); | ||
|
||
QString connName = parameterAsConnectionName( parameters, QStringLiteral( "DATABASE" ), context ); | ||
|
||
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn; | ||
try | ||
{ | ||
std::unique_ptr<QgsProviderMetadata> md( QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "spatialite" ) ) ); | ||
conn.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connName ) ) ); | ||
} | ||
catch ( QgsProviderConnectionException & ) | ||
{ | ||
throw QgsProcessingException( QObject::tr( "Could not retrieve connection details for %1" ).arg( connName ) ); | ||
} | ||
|
||
QString sql = parameterAsString( parameters, QStringLiteral( "SQL" ), context ).replace( '\n', ' ' ); | ||
try | ||
{ | ||
conn->executeSql( sql ); | ||
} | ||
catch ( QgsProviderConnectionException &ex ) | ||
{ | ||
throw QgsProcessingException( QObject::tr( "Error executing SQL:\n%1" ).arg( ex.what() ) ); | ||
} | ||
|
||
return QVariantMap(); | ||
} | ||
|
||
///@endcond |
54 changes: 54 additions & 0 deletions
54
src/analysis/processing/qgsalgorithmexecutespatialitequery.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*************************************************************************** | ||
qgsalgorithmexecutespatialitequery.h | ||
------------------------------ | ||
begin : May 2020 | ||
copyright : (C) 2020 by Alexander Bruy | ||
email : alexander dot bruy at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSALGORITHMEXECUTESPATIALITEQUERY_H | ||
#define QGSALGORITHMEXECUTESPATIALITEQUERY_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis_sip.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native execute PostGIS query algorithm. | ||
*/ | ||
class QgsExecuteSpatialiteQueryAlgorithm : public QgsProcessingAlgorithm | ||
{ | ||
|
||
public: | ||
|
||
QgsExecuteSpatialiteQueryAlgorithm() = default; | ||
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override; | ||
QString name() const override; | ||
QString displayName() const override; | ||
QStringList tags() const override; | ||
QString group() const override; | ||
QString groupId() const override; | ||
QString shortHelpString() const override; | ||
QgsExecuteSpatialiteQueryAlgorithm *createInstance() const override SIP_FACTORY; | ||
|
||
protected: | ||
|
||
QVariantMap processAlgorithm( const QVariantMap ¶meters, | ||
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMEXECUTESPATIALITEQUERY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters