27using namespace Qt::StringLiterals;
31QString QgsExecuteAndLoadPostgisQueryAlgorithm::name()
const
33 return u
"postgisexecuteandloadsql"_s;
36QString QgsExecuteAndLoadPostgisQueryAlgorithm::displayName()
const
38 return QObject::tr(
"PostgreSQL execute and load SQL" );
41QStringList QgsExecuteAndLoadPostgisQueryAlgorithm::tags()
const
43 return QObject::tr(
"database,sql,postgresql,postgis,execute,load,layer,table" ).split(
',' );
46QString QgsExecuteAndLoadPostgisQueryAlgorithm::group()
const
48 return QObject::tr(
"Database" );
51QString QgsExecuteAndLoadPostgisQueryAlgorithm::groupId()
const
56QString QgsExecuteAndLoadPostgisQueryAlgorithm::shortHelpString()
const
58 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." );
61QString QgsExecuteAndLoadPostgisQueryAlgorithm::shortDescription()
const
63 return QObject::tr(
"Executes a SQL command on a PostgreSQL database and loads the result as a layer." );
71QgsExecuteAndLoadPostgisQueryAlgorithm *QgsExecuteAndLoadPostgisQueryAlgorithm::createInstance()
const
73 return new QgsExecuteAndLoadPostgisQueryAlgorithm();
76void QgsExecuteAndLoadPostgisQueryAlgorithm::initAlgorithm(
const QVariantMap & )
89 const QString connName = parameterAsConnectionName( parameters, u
"DATABASE"_s, context );
91 std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
99 throw QgsProcessingException( QObject::tr(
"Could not retrieve connection details for %1" ).arg( connName ) );
102 QString sql = parameterAsString( parameters, u
"SQL"_s, context ).replace(
'\n',
' ' );
103 if ( sql.endsWith(
';' ) )
107 const QString idField = parameterAsString( parameters, u
"ID_FIELD"_s, context );
108 const QString geomField = parameterAsString( parameters, u
"GEOMETRY_FIELD"_s, context );
111 uri.setDataSource( QString(), u
"(%1)"_s.arg( sql ), geomField, QString(), idField );
113 auto layer = std::make_unique<QgsVectorLayer>( uri.uri(), u
"layername"_s, u
"postgres"_s );
114 if ( !layer->isValid() )
116 throw QgsProcessingException( QObject::tr(
"This layer is invalid! Please check the PostGIS log for error messages." ) );
119 const QString layerId = layer->id();
125 outputs.insert( u
"OUTPUT"_s, layerId );
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ NotAvailableInStandaloneTool
Algorithm should not be available from the standalone "qgis_process" tool. Used to flag algorithms wh...
@ RequiresProject
The algorithm requires that a valid QgsProject is available from the processing context in order to e...
Provides common functionality for database based connections.
Stores the component parts of a data source URI (e.g.
QgsMapLayer * addMapLayer(QgsMapLayer *layer, bool takeOwnership=true)
Add a layer to the store.
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Details for layers to load into projects.
Contains information about the context in which a processing algorithm is executed.
void addLayerToLoadOnCompletion(const QString &layer, const QgsProcessingContext::LayerDetails &details)
Adds a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A vector layer output for processing algorithms.
A data provider connection parameter for processing algorithms, allowing users to select from availab...
A string parameter for processing algorithms.
@ Vector
Vector layer type.
Custom exception class for provider connection related exceptions.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.