Skip to content
Permalink
Browse files

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

  • Loading branch information
alexbruy committed May 11, 2020
1 parent 072d27d commit e91b6c615e62e29ef3d629055de7c12703db56ab

This file was deleted.

@@ -64,7 +64,6 @@
from .PointsToPaths import PointsToPaths
from .PolarPlot import PolarPlot
from .Polygonize import Polygonize
from .PostGISExecuteSQL import PostGISExecuteSQL
from .PostGISExecuteAndLoadSQL import PostGISExecuteAndLoadSQL
from .RandomExtractWithinSubsets import RandomExtractWithinSubsets
from .RandomPointsAlongLines import RandomPointsAlongLines
@@ -144,7 +143,6 @@ def getAlgs(self):
PointsToPaths(),
PolarPlot(),
Polygonize(),
PostGISExecuteSQL(),
PostGISExecuteAndLoadSQL(),
RandomExtractWithinSubsets(),
RandomPointsAlongLines(),
@@ -50,6 +50,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmdrape.cpp
processing/qgsalgorithmdropgeometry.cpp
processing/qgsalgorithmdropmzvalues.cpp
processing/qgsalgorithmexecutepostgisquery.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 "qgsalgorithmexecutepostgisquery.h"
#include "qgsproviderregistry.h"
#include "qgsprovidermetadata.h"
#include "qgsabstractdatabaseproviderconnection.h"

///@cond PRIVATE

QString QgsExecutePostgisQueryAlgorithm::name() const
{
return QStringLiteral( "postgisexecutesql" );
}

QString QgsExecutePostgisQueryAlgorithm::displayName() const
{
return QObject::tr( "PostgreSQL execute SQL" );
}

QStringList QgsExecutePostgisQueryAlgorithm::tags() const
{
return QObject::tr( "database,sql,postgresql,postgis,execute" ).split( ',' );
}

QString QgsExecutePostgisQueryAlgorithm::group() const
{
return QObject::tr( "Database" );
}

QString QgsExecutePostgisQueryAlgorithm::groupId() const
{
return QStringLiteral( "database" );
}

QString QgsExecutePostgisQueryAlgorithm::shortHelpString() const
{
return QObject::tr( "Executes a SQL command on a PostgreSQL database." );
}

QgsExecutePostgisQueryAlgorithm *QgsExecutePostgisQueryAlgorithm::createInstance() const
{
return new QgsExecutePostgisQueryAlgorithm();
}

void QgsExecutePostgisQueryAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterProviderConnection( QStringLiteral( "DATABASE" ), QObject::tr( "Database (connection name)" ), QStringLiteral( "postgres" ) ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "SQL" ), QObject::tr( "SQL query" ), QVariant(), true ) );
}

QVariantMap QgsExecutePostgisQueryAlgorithm::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( "postgres" ) ) );
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 @@
/***************************************************************************
qgsalgorithmexecutepostgisquery.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 QGSALGORITHMEXECUTEPOSTGISQUERY_H
#define QGSALGORITHMEXECUTEPOSTGISQUERY_H

#define SIP_NO_FILE

#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"

///@cond PRIVATE

/**
* Native execute PostGIS query algorithm.
*/
class QgsExecutePostgisQueryAlgorithm : public QgsProcessingAlgorithm
{

public:

QgsExecutePostgisQueryAlgorithm() = 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;
QgsExecutePostgisQueryAlgorithm *createInstance() const override SIP_FACTORY;

protected:

QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};

///@endcond PRIVATE

#endif // QGSALGORITHMEXECUTEPOSTGISQUERY_H
@@ -45,6 +45,7 @@
#include "qgsalgorithmdrape.h"
#include "qgsalgorithmdropgeometry.h"
#include "qgsalgorithmdropmzvalues.h"
#include "qgsalgorithmexecutepostgisquery.h"
#include "qgsalgorithmexplode.h"
#include "qgsalgorithmexplodehstore.h"
#include "qgsalgorithmextendlines.h"
@@ -243,6 +244,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsDrapeToZAlgorithm() );
addAlgorithm( new QgsDropGeometryAlgorithm() );
addAlgorithm( new QgsDropMZValuesAlgorithm() );
addAlgorithm( new QgsExecutePostgisQueryAlgorithm() );
addAlgorithm( new QgsExplodeAlgorithm() );
addAlgorithm( new QgsExplodeHstoreAlgorithm() );
addAlgorithm( new QgsExtendLinesAlgorithm() );

0 comments on commit e91b6c6

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