27QString QgsExportToPostgresqlAlgorithm::name()
const
29 return QStringLiteral(
"importintopostgis" );
32QString QgsExportToPostgresqlAlgorithm::displayName()
const
34 return QStringLiteral(
"Export to PostgreSQL" );
37QStringList QgsExportToPostgresqlAlgorithm::tags()
const
39 return QObject::tr(
"export,import,postgis,table,layer,into,copy" ).split(
',' );
42QString QgsExportToPostgresqlAlgorithm::group()
const
44 return QStringLiteral(
"Database" );
47QString QgsExportToPostgresqlAlgorithm::groupId()
const
49 return QStringLiteral(
"database" );
52void QgsExportToPostgresqlAlgorithm::initAlgorithm(
const QVariantMap & )
56 addParameter(
new QgsProcessingParameterDatabaseSchema( QStringLiteral(
"SCHEMA" ), QObject::tr(
"Schema (schema name)" ), QStringLiteral(
"DATABASE" ), QStringLiteral(
"public" ),
true ) );
57 addParameter(
new QgsProcessingParameterDatabaseTable( QStringLiteral(
"TABLENAME" ), QObject::tr(
"Table to export to (leave blank to use layer name)" ), QStringLiteral(
"DATABASE" ), QStringLiteral(
"SCHEMA" ), QVariant(),
true,
true ) );
59 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"GEOMETRY_COLUMN" ), QObject::tr(
"Geometry column" ), QStringLiteral(
"geom" ) ) );
60 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"ENCODING" ), QObject::tr(
"Encoding" ), QStringLiteral(
"UTF-8" ),
false,
true ) );
63 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"LOWERCASE_NAMES" ), QObject::tr(
"Convert field names to lowercase" ),
true ) );
64 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"DROP_STRING_LENGTH" ), QObject::tr(
"Drop length constraints on character fields" ),
false ) );
65 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"FORCE_SINGLEPART" ), QObject::tr(
"Create single-part geometries instead of multipart" ),
false ) );
68QString QgsExportToPostgresqlAlgorithm::shortHelpString()
const
70 return QObject::tr(
"This algorithm exports a vector layer to a PostgreSQL "
71 "database, creating a new table.\n\n"
72 "Prior to this a connection between QGIS and the PostgreSQL "
73 "database has to be created (for example through the QGIS Browser panel)."
77QString QgsExportToPostgresqlAlgorithm::shortDescription()
const
79 return QObject::tr(
"Exports a vector layer to a PostgreSQL database." );
82QgsExportToPostgresqlAlgorithm *QgsExportToPostgresqlAlgorithm::createInstance()
const
84 return new QgsExportToPostgresqlAlgorithm();
89 mSource.reset( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
93 const QString connectionName = parameterAsConnectionName( parameters, QStringLiteral(
"DATABASE" ), context );
102 QgsProcessingException( QObject::tr(
"Could not retrieve connection details for %1" ).arg( connectionName ) );
105 mSchema = parameterAsSchema( parameters, QStringLiteral(
"SCHEMA" ), context );
106 mPrimaryKeyField = parameterAsString( parameters, QStringLiteral(
"PRIMARY_KEY" ), context );
107 mEncoding = parameterAsString( parameters, QStringLiteral(
"ENCODING" ), context );
108 mOverwrite = parameterAsBoolean( parameters, QStringLiteral(
"OVERWRITE" ), context );
110 mTable = parameterAsDatabaseTableName( parameters, QStringLiteral(
"TABLENAME" ), context ).trimmed();
111 if ( mTable.isEmpty() )
113 mTable = mSource->sourceName();
114 mTable = mTable.replace(
'.',
'_' );
116 mTable = mTable.replace(
' ', QString() ).right( 63 );
118 mGeomColumn = parameterAsString( parameters, QStringLiteral(
"GEOMETRY_COLUMN" ), context );
119 if ( mGeomColumn.isEmpty() )
120 mGeomColumn = QStringLiteral(
"geom" );
124 mCreateIndex = parameterAsBoolean( parameters, QStringLiteral(
"CREATEINDEX" ), context );
127 mOptions[QStringLiteral(
"overwrite" )] =
true;
128 if ( parameterAsBoolean( parameters, QStringLiteral(
"LOWERCASE_NAMES" ), context ) )
130 mOptions[QStringLiteral(
"lowercaseFieldNames" )] =
true;
131 mGeomColumn = mGeomColumn.toLower();
133 if ( parameterAsBoolean( parameters, QStringLiteral(
"DROP_STRING_LENGTH" ), context ) )
134 mOptions[QStringLiteral(
"dropStringConstraints" )] =
true;
135 if ( parameterAsBoolean( parameters, QStringLiteral(
"FORCE_SINGLEPART" ), context ) )
136 mOptions[QStringLiteral(
"forceSinglePartGeometryType" )] =
true;
137 if ( !mEncoding.isEmpty() )
138 mOptions[QStringLiteral(
"fileEncoding" )] = mEncoding;
145 Q_UNUSED( parameters );
154 auto exporter = std::make_unique<QgsVectorLayerExporter>( uri.
uri(), mProviderName, mSource->fields(), mSource->wkbType(), mSource->sourceCrs(), mOverwrite, mOptions );
157 throw QgsProcessingException( QObject::tr(
"Error exporting to PostGIS\n%1" ).arg( exporter->errorMessage() ) );
161 const double progressStep = ( mSource->featureCount() ) ? 100.0 / mSource->featureCount() : 0.0;
176 exporter->flushBuffer();
179 throw QgsProcessingException( QObject::tr(
"Error exporting to PostGIS\n%1" ).arg( exporter->errorMessage() ) );
183 if ( !mGeomColumn.isEmpty() && mCreateIndex )
189 mConn->createSpatialIndex( mSchema, mTable, opt );
199 mConn->vacuum( mSchema, mTable );
203 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.