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() ) );