-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
port PostgreSQL execute and load SQL algorithm to C++ #64362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nyalldawson
merged 2 commits into
qgis:master
from
alexbruy:processing-execute-load-sql-cpp
Jan 8, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 0 additions & 153 deletions
153
python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
125 changes: 125 additions & 0 deletions
125
src/analysis/processing/qgsalgorithmexecuteandloadpostgisquery.cpp
This file contains hidden or 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,125 @@ | ||
| /*************************************************************************** | ||
| qgsalgorithmexecuteandloadpostgisquery.cpp | ||
| --------------------- | ||
| begin : December 2025 | ||
| copyright : (C) 2025 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 "qgsalgorithmexecuteandloadpostgisquery.h" | ||
|
|
||
| #include "qgsabstractdatabaseproviderconnection.h" | ||
| #include "qgsprovidermetadata.h" | ||
| #include "qgsproviderregistry.h" | ||
| #include "qgsvectorlayer.h" | ||
|
|
||
| ///@cond PRIVATE | ||
|
|
||
| QString QgsExecuteAndLoadPostgisQueryAlgorithm::name() const | ||
| { | ||
| return u"postgisexecuteandloadsql"_s; | ||
| } | ||
|
|
||
| QString QgsExecuteAndLoadPostgisQueryAlgorithm::displayName() const | ||
| { | ||
| return QObject::tr( "PostgreSQL execute and load SQL" ); | ||
| } | ||
|
|
||
| QStringList QgsExecuteAndLoadPostgisQueryAlgorithm::tags() const | ||
| { | ||
| return QObject::tr( "database,sql,postgresql,postgis,execute,load,layer,table" ).split( ',' ); | ||
| } | ||
|
|
||
| QString QgsExecuteAndLoadPostgisQueryAlgorithm::group() const | ||
| { | ||
| return QObject::tr( "Database" ); | ||
| } | ||
|
|
||
| QString QgsExecuteAndLoadPostgisQueryAlgorithm::groupId() const | ||
| { | ||
| return u"database"_s; | ||
| } | ||
|
|
||
| QString QgsExecuteAndLoadPostgisQueryAlgorithm::shortHelpString() const | ||
| { | ||
| return QObject::tr( "This algorithm performs a SQL database query on a PostgreSQL database connected to QGIS and loads the query results as a new layer." ); | ||
| } | ||
|
|
||
| QString QgsExecuteAndLoadPostgisQueryAlgorithm::shortDescription() const | ||
| { | ||
| return QObject::tr( "Executes a SQL command on a PostgreSQL database and loads the result as a layer." ); | ||
| } | ||
|
|
||
| Qgis::ProcessingAlgorithmFlags QgsExecuteAndLoadPostgisQueryAlgorithm::flags() const | ||
| { | ||
| return QgsProcessingAlgorithm::flags() | Qgis::ProcessingAlgorithmFlag::NotAvailableInStandaloneTool | Qgis::ProcessingAlgorithmFlag::RequiresProject; | ||
| } | ||
|
|
||
| QgsExecuteAndLoadPostgisQueryAlgorithm *QgsExecuteAndLoadPostgisQueryAlgorithm::createInstance() const | ||
| { | ||
| return new QgsExecuteAndLoadPostgisQueryAlgorithm(); | ||
| } | ||
|
|
||
| void QgsExecuteAndLoadPostgisQueryAlgorithm::initAlgorithm( const QVariantMap & ) | ||
| { | ||
| addParameter( new QgsProcessingParameterProviderConnection( u"DATABASE"_s, QObject::tr( "Database (connection name)" ), u"postgres"_s ) ); | ||
| addParameter( new QgsProcessingParameterString( u"SQL"_s, QObject::tr( "SQL query" ), QVariant(), true ) ); | ||
| addParameter( new QgsProcessingParameterString( u"ID_FIELD"_s, QObject::tr( "Unique ID field name" ), u"id"_s ) ); | ||
| addParameter( new QgsProcessingParameterString( u"GEOMETRY_FIELD"_s, QObject::tr( "Geometry field name" ), u"geom"_s, false, true ) ); | ||
| addOutput( new QgsProcessingOutputVectorLayer( u"OUTPUT"_s, QObject::tr( "Output layer" ) ) ); | ||
| } | ||
|
|
||
| QVariantMap QgsExecuteAndLoadPostgisQueryAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | ||
| { | ||
| Q_UNUSED( feedback ); | ||
|
|
||
| const QString connName = parameterAsConnectionName( parameters, u"DATABASE"_s, context ); | ||
|
|
||
| std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn; | ||
| try | ||
| { | ||
| QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( u"postgres"_s ); | ||
| 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, u"SQL"_s, context ).replace( '\n', ' ' ); | ||
| if ( sql.endsWith( ';' ) ) | ||
| { | ||
| sql.chop( 1 ); | ||
| } | ||
| const QString idField = parameterAsString( parameters, u"ID_FIELD"_s, context ); | ||
| const QString geomField = parameterAsString( parameters, u"GEOMETRY_FIELD"_s, context ); | ||
|
|
||
| QgsDataSourceUri uri( conn->uri() ); | ||
| uri.setDataSource( "", u"(%1)"_s.arg( sql ), geomField, "", idField ); | ||
|
|
||
| auto layer = std::make_unique<QgsVectorLayer>( uri.uri(), u"layername"_s, u"postgres"_s ); | ||
| if ( !layer->isValid() ) | ||
| { | ||
| throw QgsProcessingException( QObject::tr( "This layer is invalid! Please check the PostGIS log for error messages." ) ); | ||
| } | ||
|
|
||
| const QString layerId = layer->id(); | ||
| const QgsProcessingContext::LayerDetails details( u"SQL layer"_s, context.project(), u"OUTPUT"_s, QgsProcessingUtils::LayerHint::Vector ); | ||
| context.addLayerToLoadOnCompletion( layerId, details ); | ||
| context.temporaryLayerStore()->addMapLayer( layer.release() ); | ||
|
|
||
| QVariantMap outputs; | ||
| outputs.insert( u"OUTPUT"_s, layerId ); | ||
| return outputs; | ||
| } | ||
|
|
||
| ///@endcond | ||
52 changes: 52 additions & 0 deletions
52
src/analysis/processing/qgsalgorithmexecuteandloadpostgisquery.h
This file contains hidden or 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,52 @@ | ||
| /*************************************************************************** | ||
| qgsalgorithmexecuteandloadpostgisquery.h | ||
| ------------------------------ | ||
| begin : December 2025 | ||
| copyright : (C) 2025 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 QGSALGORITHMEXECUTEANDLOADPOSTGISQUERY_H | ||
| #define QGSALGORITHMEXECUTEANDLOADPOSTGISQUERY_H | ||
|
|
||
| #define SIP_NO_FILE | ||
|
|
||
| #include "qgsprocessingalgorithm.h" | ||
|
|
||
| ///@cond PRIVATE | ||
|
|
||
| /** | ||
| * Native execute and load PostGIS query algorithm. | ||
| */ | ||
| class QgsExecuteAndLoadPostgisQueryAlgorithm : public QgsProcessingAlgorithm | ||
| { | ||
| public: | ||
| QgsExecuteAndLoadPostgisQueryAlgorithm() = 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; | ||
| QString shortDescription() const override; | ||
| Qgis::ProcessingAlgorithmFlags flags() const override; | ||
| QgsExecuteAndLoadPostgisQueryAlgorithm *createInstance() const override SIP_FACTORY; | ||
|
|
||
| protected: | ||
| QVariantMap processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
| }; | ||
|
|
||
| ///@endcond PRIVATE | ||
|
|
||
|
|
||
| #endif // QGSALGORITHMEXECUTEANDLOADPOSTGISQUERY_H |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.