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
75 "This algorithm exports a vector layer to a PostgreSQL "
76 "database, creating a new table.\n\n"
77 "Prior to this a connection between QGIS and the PostgreSQL "
78 "database has to be created (for example through the QGIS Browser panel)."
82QString QgsExportToPostgresqlAlgorithm::shortDescription()
const
84 return QObject::tr(
"Exports a vector layer to a PostgreSQL database." );
87QgsExportToPostgresqlAlgorithm *QgsExportToPostgresqlAlgorithm::createInstance()
const
89 return new QgsExportToPostgresqlAlgorithm();
94 mSource.reset( parameterAsSource( parameters, u
"INPUT"_s, context ) );
98 const QString connectionName = parameterAsConnectionName( parameters, u
"DATABASE"_s, context );
107 QgsProcessingException( QObject::tr(
"Could not retrieve connection details for %1" ).arg( connectionName ) );
110 mSchema = parameterAsSchema( parameters, u
"SCHEMA"_s, context );
111 mPrimaryKeyField = parameterAsString( parameters, u
"PRIMARY_KEY"_s, context );
112 mEncoding = parameterAsString( parameters, u
"ENCODING"_s, context );
113 mOverwrite = parameterAsBoolean( parameters, u
"OVERWRITE"_s, context );
115 mTable = parameterAsDatabaseTableName( parameters, u
"TABLENAME"_s, context ).trimmed();
116 if ( mTable.isEmpty() )
118 mTable = mSource->sourceName();
119 mTable = mTable.replace(
'.',
'_' );
121 mTable = mTable.replace(
' ', QString() ).right( 63 );
123 mGeomColumn = parameterAsString( parameters, u
"GEOMETRY_COLUMN"_s, context );
124 if ( mGeomColumn.isEmpty() )
125 mGeomColumn = u
"geom"_s;
129 mCreateIndex = parameterAsBoolean( parameters, u
"CREATEINDEX"_s, context );
132 mOptions[u
"overwrite"_s] =
true;
133 if ( parameterAsBoolean( parameters, u
"LOWERCASE_NAMES"_s, context ) )
135 mOptions[u
"lowercaseFieldNames"_s] =
true;
136 mGeomColumn = mGeomColumn.toLower();
138 if ( parameterAsBoolean( parameters, u
"DROP_STRING_LENGTH"_s, context ) )
139 mOptions[u
"dropStringConstraints"_s] =
true;
140 if ( parameterAsBoolean( parameters, u
"FORCE_SINGLEPART"_s, context ) )
141 mOptions[u
"forceSinglePartGeometryType"_s] =
true;
142 if ( !mEncoding.isEmpty() )
143 mOptions[u
"fileEncoding"_s] = mEncoding;
150 Q_UNUSED( parameters );
159 auto exporter = std::make_unique<QgsVectorLayerExporter>( uri.
uri(), mProviderName, mSource->fields(), mSource->wkbType(), mSource->sourceCrs(), mOverwrite, mOptions );
162 throw QgsProcessingException( QObject::tr(
"Error exporting to PostGIS\n%1" ).arg( exporter->errorMessage() ) );
166 const double progressStep = ( mSource->featureCount() ) ? 100.0 / mSource->featureCount() : 0.0;
181 exporter->flushBuffer();
184 throw QgsProcessingException( QObject::tr(
"Error exporting to PostGIS\n%1" ).arg( exporter->errorMessage() ) );
188 if ( !mGeomColumn.isEmpty() && mCreateIndex )
194 mConn->createSpatialIndex( mSchema, mTable, opt );
204 mConn->vacuum( mSchema, mTable );
208 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.