27using namespace Qt::StringLiterals;
31QString QgsExportToPostgresqlAlgorithm::name()
const
33 return u
"importintopostgis"_s;
36QString QgsExportToPostgresqlAlgorithm::displayName()
const
38 return u
"Export to PostgreSQL"_s;
41QStringList QgsExportToPostgresqlAlgorithm::tags()
const
43 return QObject::tr(
"export,import,postgis,table,layer,into,copy" ).split(
',' );
46QString QgsExportToPostgresqlAlgorithm::group()
const
51QString QgsExportToPostgresqlAlgorithm::groupId()
const
56void QgsExportToPostgresqlAlgorithm::initAlgorithm(
const QVariantMap & )
61 addParameter(
new QgsProcessingParameterDatabaseTable( u
"TABLENAME"_s, QObject::tr(
"Table to export to (leave blank to use layer name)" ), u
"DATABASE"_s, u
"SCHEMA"_s, QVariant(),
true,
true ) );
68 addParameter(
new QgsProcessingParameterBoolean( u
"DROP_STRING_LENGTH"_s, QObject::tr(
"Drop length constraints on character fields" ),
false ) );
69 addParameter(
new QgsProcessingParameterBoolean( u
"FORCE_SINGLEPART"_s, QObject::tr(
"Create single-part geometries instead of multipart" ),
false ) );
72QString QgsExportToPostgresqlAlgorithm::shortHelpString()
const
74 return QObject::tr(
"This algorithm exports a vector layer to a PostgreSQL "
75 "database, creating a new table.\n\n"
76 "Prior to this a connection between QGIS and the PostgreSQL "
77 "database has to be created (for example through the QGIS Browser panel)."
81QString QgsExportToPostgresqlAlgorithm::shortDescription()
const
83 return QObject::tr(
"Exports a vector layer to a PostgreSQL database." );
86QgsExportToPostgresqlAlgorithm *QgsExportToPostgresqlAlgorithm::createInstance()
const
88 return new QgsExportToPostgresqlAlgorithm();
93 mSource.reset( parameterAsSource( parameters, u
"INPUT"_s, context ) );
97 const QString connectionName = parameterAsConnectionName( parameters, u
"DATABASE"_s, context );
106 QgsProcessingException( QObject::tr(
"Could not retrieve connection details for %1" ).arg( connectionName ) );
109 mSchema = parameterAsSchema( parameters, u
"SCHEMA"_s, context );
110 mPrimaryKeyField = parameterAsString( parameters, u
"PRIMARY_KEY"_s, context );
111 mEncoding = parameterAsString( parameters, u
"ENCODING"_s, context );
112 mOverwrite = parameterAsBoolean( parameters, u
"OVERWRITE"_s, context );
114 mTable = parameterAsDatabaseTableName( parameters, u
"TABLENAME"_s, context ).trimmed();
115 if ( mTable.isEmpty() )
117 mTable = mSource->sourceName();
118 mTable = mTable.replace(
'.',
'_' );
120 mTable = mTable.replace(
' ', QString() ).right( 63 );
122 mGeomColumn = parameterAsString( parameters, u
"GEOMETRY_COLUMN"_s, context );
123 if ( mGeomColumn.isEmpty() )
124 mGeomColumn = u
"geom"_s;
128 mCreateIndex = parameterAsBoolean( parameters, u
"CREATEINDEX"_s, context );
131 mOptions[u
"overwrite"_s] =
true;
132 if ( parameterAsBoolean( parameters, u
"LOWERCASE_NAMES"_s, context ) )
134 mOptions[u
"lowercaseFieldNames"_s] =
true;
135 mGeomColumn = mGeomColumn.toLower();
137 if ( parameterAsBoolean( parameters, u
"DROP_STRING_LENGTH"_s, context ) )
138 mOptions[u
"dropStringConstraints"_s] =
true;
139 if ( parameterAsBoolean( parameters, u
"FORCE_SINGLEPART"_s, context ) )
140 mOptions[u
"forceSinglePartGeometryType"_s] =
true;
141 if ( !mEncoding.isEmpty() )
142 mOptions[u
"fileEncoding"_s] = mEncoding;
149 Q_UNUSED( parameters );
158 auto exporter = std::make_unique<QgsVectorLayerExporter>( uri.
uri(), mProviderName, mSource->fields(), mSource->wkbType(), mSource->sourceCrs(), mOverwrite, mOptions );
161 throw QgsProcessingException( QObject::tr(
"Error exporting to PostGIS\n%1" ).arg( exporter->errorMessage() ) );
165 const double progressStep = ( mSource->featureCount() ) ? 100.0 / mSource->featureCount() : 0.0;
180 exporter->flushBuffer();
183 throw QgsProcessingException( QObject::tr(
"Error exporting to PostGIS\n%1" ).arg( exporter->errorMessage() ) );
187 if ( !mGeomColumn.isEmpty() && mCreateIndex )
193 mConn->createSpatialIndex( mSchema, mTable, opt );
203 mConn->vacuum( mSchema, mTable );
207 feedback->
reportError( QObject::tr(
"Error vacuuming table:\n{0}" ).arg( ex.
what() ) );
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ Success
No errors were encountered.
Provides common functionality for database based connections.
Stores the component parts of a data source URI (e.g.
void setSchema(const QString &schema)
Sets the scheme for the URI.
void setTable(const QString &table)
Sets table to table.
void setGeometryColumn(const QString &geometryColumn)
Sets geometry column name to geometryColumn.
QString uri(bool expandAuthConfig=true) const
Returns the complete URI as a string.
void setKeyColumn(const QString &column)
Sets the name of the (primary) key column.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A boolean parameter for processing algorithms.
A database schema parameter for processing algorithms, allowing users to select from existing schemas...
A database table name parameter for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A data provider connection parameter for processing algorithms, allowing users to select from availab...
A string parameter for processing algorithms.
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.
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
The SpatialIndexOptions contains extra options relating to spatial index creation.
QString geometryColumnName
Specifies the name of the geometry column to create the index for.