mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-28 00:17:30 -05:00
[processing] port SpatiaLite Execute SQL algorithm to C++
This commit is contained in:
parent
e91b6c615e
commit
c0e8178366
@ -82,7 +82,6 @@ from .SelectByExpression import SelectByExpression
|
||||
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 @@ class QgisAlgorithmProvider(QgsProcessingProvider):
|
||||
SetRasterStyle(),
|
||||
SetVectorStyle(),
|
||||
SnapGeometriesToLayer(),
|
||||
SpatialiteExecuteSQL(),
|
||||
SpatialJoinSummary(),
|
||||
StatisticsByCategories(),
|
||||
TextToFloat(),
|
||||
|
@ -1,88 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
***************************************************************************
|
||||
SpatialiteExecuteSQL.py
|
||||
---------------------
|
||||
Date : October 2016
|
||||
Copyright : (C) 2016 by Mathieu Pellerin
|
||||
Email : nirvn dot asia 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. *
|
||||
* *
|
||||
***************************************************************************
|
||||
"""
|
||||
|
||||
__author__ = 'Mathieu Pellerin'
|
||||
__date__ = 'October 2016'
|
||||
__copyright__ = '(C) 2016, Mathieu Pellerin'
|
||||
|
||||
from qgis.core import (QgsDataSourceUri,
|
||||
QgsProcessing,
|
||||
QgsProcessingAlgorithm,
|
||||
QgsProcessingException,
|
||||
QgsProcessingParameterVectorLayer,
|
||||
QgsProcessingParameterString,
|
||||
QgsProviderRegistry,
|
||||
QgsProviderConnectionException)
|
||||
|
||||
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
||||
|
||||
|
||||
class SpatialiteExecuteSQL(QgisAlgorithm):
|
||||
DATABASE = 'DATABASE'
|
||||
SQL = 'SQL'
|
||||
|
||||
def group(self):
|
||||
return self.tr('Database')
|
||||
|
||||
def groupId(self):
|
||||
return 'database'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def initAlgorithm(self, config=None):
|
||||
self.addParameter(QgsProcessingParameterVectorLayer(self.DATABASE, self.tr('File Database'), types=[QgsProcessing.TypeVector], optional=False))
|
||||
self.addParameter(QgsProcessingParameterString(self.SQL, self.tr('SQL query'), multiLine=True))
|
||||
|
||||
def name(self):
|
||||
return 'spatialiteexecutesql'
|
||||
|
||||
def displayName(self):
|
||||
return self.tr('SpatiaLite execute SQL')
|
||||
|
||||
def shortDescription(self):
|
||||
return self.tr('Executes a SQL command on a SpatiaLite database')
|
||||
|
||||
def flags(self):
|
||||
return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
|
||||
|
||||
def processAlgorithm(self, parameters, context, feedback):
|
||||
database = self.parameterAsVectorLayer(parameters, self.DATABASE, context)
|
||||
databaseuri = database.dataProvider().dataSourceUri()
|
||||
uri = QgsDataSourceUri(databaseuri)
|
||||
if uri.database() == '':
|
||||
if '|layername' in databaseuri:
|
||||
databaseuri = databaseuri[:databaseuri.find('|layername')]
|
||||
elif '|layerid' in databaseuri:
|
||||
databaseuri = databaseuri[:databaseuri.find('|layerid')]
|
||||
uri = QgsDataSourceUri('dbname=\'%s\'' % (databaseuri))
|
||||
|
||||
try:
|
||||
md = QgsProviderRegistry.instance().providerMetadata('spatialite')
|
||||
conn = md.createConnection(uri.uri(), {})
|
||||
except QgsProviderConnectionException:
|
||||
raise QgsProcessingException(self.tr('Could not connect to {}').format(uri.uri()))
|
||||
|
||||
sql = self.parameterAsString(parameters, self.SQL, context).replace('\n', ' ')
|
||||
try:
|
||||
conn.executeSql(sql)
|
||||
except QgsProviderConnectionException as e:
|
||||
raise QgsProcessingException(self.tr('Error executing SQL:\n{0}').format(e))
|
||||
|
||||
return {}
|
@ -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 ¶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
src/analysis/processing/qgsalgorithmexecutespatialitequery.h
Normal file
54
src/analysis/processing/qgsalgorithmexecutespatialitequery.h
Normal file
@ -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
|
@ -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() );
|
||||
|
Loading…
x
Reference in New Issue
Block a user