26 QString QgsExportToPostgresqlAlgorithm::name()
const
28 return QStringLiteral(
"importintopostgis" );
31 QString QgsExportToPostgresqlAlgorithm::displayName()
const
33 return QStringLiteral(
"Export to PostgreSQL" );
36 QStringList QgsExportToPostgresqlAlgorithm::tags()
const
38 return QObject::tr(
"export,import,postgis,table,layer,into,copy" ).split(
',' );
41 QString QgsExportToPostgresqlAlgorithm::group()
const
43 return QStringLiteral(
"Database" );
46 QString QgsExportToPostgresqlAlgorithm::groupId()
const
48 return QStringLiteral(
"database" );
51 void QgsExportToPostgresqlAlgorithm::initAlgorithm(
const QVariantMap & )
55 addParameter(
new QgsProcessingParameterDatabaseSchema( QStringLiteral(
"SCHEMA" ), QObject::tr(
"Schema (schema name)" ), QStringLiteral(
"DATABASE" ), QStringLiteral(
"public" ),
true ) );
56 addParameter(
new QgsProcessingParameterDatabaseTable( QStringLiteral(
"TABLENAME" ), QObject::tr(
"Table to export to (leave blank to use layer name)" ), QStringLiteral(
"DATABASE" ), QStringLiteral(
"SCHEMA" ), QVariant(),
true,
true ) );
58 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"GEOMETRY_COLUMN" ), QObject::tr(
"Geometry column" ), QStringLiteral(
"geom" ) ) );
59 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"ENCODING" ), QObject::tr(
"Encoding" ), QStringLiteral(
"UTF-8" ),
false,
true ) );
62 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"LOWERCASE_NAMES" ), QObject::tr(
"Convert field names to lowercase" ),
true ) );
63 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"DROP_STRING_LENGTH" ), QObject::tr(
"Drop length constraints on character fields" ),
false ) );
64 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"FORCE_SINGLEPART" ), QObject::tr(
"Create single-part geometries instead of multipart" ),
false ) );
67 QString QgsExportToPostgresqlAlgorithm::shortHelpString()
const
69 return QObject::tr(
"This algorithm exports a vector layer to a PostgreSQL "
70 "database, creating a new table.\n\n"
71 "Prior to this a connection between QGIS and the PostgreSQL "
72 "database has to be created (for example through the QGIS Browser panel)."
76 QString QgsExportToPostgresqlAlgorithm::shortDescription()
const
78 return QObject::tr(
"Exports a vector layer to a PostgreSQL database" );
81 QgsExportToPostgresqlAlgorithm *QgsExportToPostgresqlAlgorithm::createInstance()
const
83 return new QgsExportToPostgresqlAlgorithm();
88 mSource.reset( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
92 const QString connectionName = parameterAsConnectionName( parameters, QStringLiteral(
"DATABASE" ), context );
101 QgsProcessingException( QObject::tr(
"Could not retrieve connection details for %1" ).arg( connectionName ) );
104 mSchema = parameterAsSchema( parameters, QStringLiteral(
"SCHEMA" ), context );
105 mPrimaryKeyField = parameterAsString( parameters, QStringLiteral(
"PRIMARY_KEY" ), context );
106 mEncoding = parameterAsString( parameters, QStringLiteral(
"ENCODING" ), context );
107 mOverwrite = parameterAsBoolean( parameters, QStringLiteral(
"OVERWRITE" ), context );
109 mTable = parameterAsDatabaseTableName( parameters, QStringLiteral(
"TABLENAME" ), context ).trimmed();
110 if ( mTable.isEmpty() )
112 mTable = mSource->sourceName();
113 mTable = mTable.replace(
'.',
'_' );
115 mTable = mTable.replace(
' ', QString() ).right( 63 );
117 mGeomColumn = parameterAsString( parameters, QStringLiteral(
"GEOMETRY_COLUMN" ), context );
118 if ( mGeomColumn.isEmpty() )
119 mGeomColumn = QStringLiteral(
"geom" );
123 mCreateIndex = parameterAsBoolean( parameters, QStringLiteral(
"CREATEINDEX" ), context );
126 mOptions[QStringLiteral(
"overwrite" )] =
true;
127 if ( parameterAsBoolean( parameters, QStringLiteral(
"LOWERCASE_NAMES" ), context ) )
129 mOptions[QStringLiteral(
"lowercaseFieldNames" )] =
true;
130 mGeomColumn = mGeomColumn.toLower();
132 if ( parameterAsBoolean( parameters, QStringLiteral(
"DROP_STRING_LENGTH" ), context ) )
133 mOptions[QStringLiteral(
"dropStringConstraints" )] =
true;
134 if ( parameterAsBoolean( parameters, QStringLiteral(
"FORCE_SINGLEPART" ), context ) )
135 mOptions[QStringLiteral(
"forceSinglePartGeometryType" )] =
true;
136 if ( !mEncoding.isEmpty() )
137 mOptions[QStringLiteral(
"fileEncoding" )] = mEncoding;
144 Q_UNUSED( parameters );
153 std::unique_ptr< QgsVectorLayerExporter > exporter = std::make_unique< QgsVectorLayerExporter >( uri.
uri(), mProviderName, mSource->fields(), mSource->wkbType(), mSource->sourceCrs(), mOverwrite, mOptions );
155 if ( exporter->errorCode() != Qgis::VectorExportResult::Success )
156 throw QgsProcessingException( QObject::tr(
"Error exporting to PostGIS\n%1" ).arg( exporter->errorMessage() ) );
160 const double progressStep = ( mSource->featureCount() ) ? 100.0 / mSource->featureCount() : 0.0;
175 exporter->flushBuffer();
177 if ( exporter->errorCode() != Qgis::VectorExportResult::Success )
178 throw QgsProcessingException( QObject::tr(
"Error exporting to PostGIS\n%1" ).arg( exporter->errorMessage() ) );
182 if ( !mGeomColumn.isEmpty() && mCreateIndex )
188 mConn->createSpatialIndex( mSchema, mTable, opt );
198 mConn->vacuum( mSchema, mTable );
202 feedback->
reportError( QObject::tr(
"Error vacuuming table:\n{0}" ).arg( ex.
what() ) );
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Class for storing the component parts of a RDBMS 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)
@ 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 SIP_HOLDGIL
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, allowing users to select from existing dat...
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.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
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.