Skip to content
Permalink
Browse files

[processing] port SpatiaLite Execute SQL algorithm to C++

  • Loading branch information
alexbruy committed May 11, 2020
1 parent e91b6c6 commit c0e81783669fc6d67f22828423a179e2bc9b3982
@@ -82,7 +82,6 @@
from .SetRasterStyle import SetRasterStyle
from .SetVectorStyle import SetVectorStyle
from .SnapGeometries import SnapGeometriesToLayer
from .SpatialiteExecuteSQL import SpatialiteExecuteSQL
from .SpatialJoinSummary import SpatialJoinSummary
from .StatisticsByCategories import StatisticsByCategories
from .TextToFloat import TextToFloat
@@ -161,7 +160,6 @@ def getAlgs(self):
SetRasterStyle(),
SetVectorStyle(),
SnapGeometriesToLayer(),
SpatialiteExecuteSQL(),
SpatialJoinSummary(),
StatisticsByCategories(),
TextToFloat(),

This file was deleted.

@@ -51,6 +51,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmdropgeometry.cpp
processing/qgsalgorithmdropmzvalues.cpp
processing/qgsalgorithmexecutepostgisquery.cpp
processing/qgsalgorithmexecutespatialitequery.cpp
processing/qgsalgorithmexplode.cpp
processing/qgsalgorithmexplodehstore.cpp
processing/qgsalgorithmextendlines.cpp
@@ -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 &parameters, 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
@@ -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 &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};

///@endcond PRIVATE

#endif // QGSALGORITHMEXECUTESPATIALITEQUERY_H
@@ -46,6 +46,7 @@
#include "qgsalgorithmdropgeometry.h"
#include "qgsalgorithmdropmzvalues.h"
#include "qgsalgorithmexecutepostgisquery.h"
#include "qgsalgorithmexecutespatialitequery.h"
#include "qgsalgorithmexplode.h"
#include "qgsalgorithmexplodehstore.h"
#include "qgsalgorithmextendlines.h"
@@ -245,6 +246,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsDropGeometryAlgorithm() );
addAlgorithm( new QgsDropMZValuesAlgorithm() );
addAlgorithm( new QgsExecutePostgisQueryAlgorithm() );
addAlgorithm( new QgsExecuteSpatialiteQueryAlgorithm() );
addAlgorithm( new QgsExplodeAlgorithm() );
addAlgorithm( new QgsExplodeHstoreAlgorithm() );
addAlgorithm( new QgsExtendLinesAlgorithm() );

0 comments on commit c0e8178

Please sign in to comment.
You can’t perform that action at this time.