QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
src
analysis
processing
qgsalgorithmexecutepostgisquery.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgsalgorithmexecutepostgisquery.cpp
3
---------------------
4
begin : May 2020
5
copyright : (C) 2020 by Alexander Bruy
6
email : alexander dot bruy at gmail dot com
7
***************************************************************************/
8
9
/***************************************************************************
10
* *
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
15
* *
16
***************************************************************************/
17
18
#include "
qgsalgorithmexecutepostgisquery.h
"
19
#include "
qgsproviderregistry.h
"
20
#include "
qgsprovidermetadata.h
"
21
#include "
qgsabstractdatabaseproviderconnection.h
"
22
24
25
QString QgsExecutePostgisQueryAlgorithm::name()
const
26
{
27
return
QStringLiteral(
"postgisexecutesql"
);
28
}
29
30
QString QgsExecutePostgisQueryAlgorithm::displayName()
const
31
{
32
return
QObject::tr(
"PostgreSQL execute SQL"
);
33
}
34
35
QStringList QgsExecutePostgisQueryAlgorithm::tags()
const
36
{
37
return
QObject::tr(
"database,sql,postgresql,postgis,execute"
).split(
','
);
38
}
39
40
QString QgsExecutePostgisQueryAlgorithm::group()
const
41
{
42
return
QObject::tr(
"Database"
);
43
}
44
45
QString QgsExecutePostgisQueryAlgorithm::groupId()
const
46
{
47
return
QStringLiteral(
"database"
);
48
}
49
50
QString QgsExecutePostgisQueryAlgorithm::shortHelpString()
const
51
{
52
return
QObject::tr(
"Executes a SQL command on a PostgreSQL database."
);
53
}
54
55
QgsExecutePostgisQueryAlgorithm *QgsExecutePostgisQueryAlgorithm::createInstance()
const
56
{
57
return
new
QgsExecutePostgisQueryAlgorithm();
58
}
59
60
void
QgsExecutePostgisQueryAlgorithm::initAlgorithm(
const
QVariantMap & )
61
{
62
addParameter(
new
QgsProcessingParameterProviderConnection
( QStringLiteral(
"DATABASE"
), QObject::tr(
"Database (connection name)"
), QStringLiteral(
"postgres"
) ) );
63
addParameter(
new
QgsProcessingParameterString
( QStringLiteral(
"SQL"
), QObject::tr(
"SQL query"
), QVariant(),
true
) );
64
}
65
66
QVariantMap QgsExecutePostgisQueryAlgorithm::processAlgorithm(
const
QVariantMap ¶meters,
QgsProcessingContext
&context,
QgsProcessingFeedback
*feedback )
67
{
68
Q_UNUSED( feedback );
69
70
const
QString connName = parameterAsConnectionName( parameters, QStringLiteral(
"DATABASE"
), context );
71
72
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn;
73
try
74
{
75
QgsProviderMetadata
*md =
QgsProviderRegistry::instance
()->
providerMetadata
( QStringLiteral(
"postgres"
) );
76
conn.reset(
static_cast<
QgsAbstractDatabaseProviderConnection
*
>
( md->
createConnection
( connName ) ) );
77
}
78
catch
(
QgsProviderConnectionException
& )
79
{
80
throw
QgsProcessingException
( QObject::tr(
"Could not retrieve connection details for %1"
).arg( connName ) );
81
}
82
83
const
QString sql = parameterAsString( parameters, QStringLiteral(
"SQL"
), context ).replace(
'\n'
,
' '
);
84
try
85
{
86
conn->executeSql( sql );
87
}
88
catch
(
QgsProviderConnectionException
&ex )
89
{
90
throw
QgsProcessingException
( QObject::tr(
"Error executing SQL:\n%1"
).arg( ex.
what
() ) );
91
}
92
93
return
QVariantMap();
94
}
95
QgsProviderMetadata::createConnection
virtual QgsAbstractProviderConnection * createConnection(const QString &uri, const QVariantMap &configuration) SIP_THROW(QgsProviderConnectionException)
Creates a new connection from uri and configuration, the newly created connection is not automaticall...
Definition:
qgsprovidermetadata.cpp:316
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition:
qgsprocessingfeedback.h:37
qgsprovidermetadata.h
qgsproviderregistry.h
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition:
qgsprocessingcontext.h:46
qgsalgorithmexecutepostgisquery.h
QgsException::what
QString what() const
Definition:
qgsexception.h:48
QgsProcessingParameterString
A string parameter for processing algorithms.
Definition:
qgsprocessingparameters.h:2647
QgsProcessingParameterProviderConnection
A data provider connection parameter for processing algorithms, allowing users to select from availab...
Definition:
qgsprocessingparameters.h:4064
QgsProviderRegistry::providerMetadata
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
Definition:
qgsproviderregistry.cpp:873
QgsProviderConnectionException
Custom exception class for provider connection related exceptions.
Definition:
qgsexception.h:100
QgsProviderMetadata
Holds data provider key, description, and associated shared library file or function pointer informat...
Definition:
qgsprovidermetadata.h:177
qgsabstractdatabaseproviderconnection.h
QgsProviderRegistry::instance
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Definition:
qgsproviderregistry.cpp:73
QgsProcessingException
Custom exception class for processing related exceptions.
Definition:
qgsexception.h:82
QgsAbstractDatabaseProviderConnection
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Definition:
qgsabstractdatabaseproviderconnection.h:44
Generated on Sun Sep 11 2022 00:03:17 for QGIS API Documentation by
1.8.17