Skip to content
Permalink
Browse files

Rename drop/add column to delete/add field

for consistency with othet Qgis classes
  • Loading branch information
elpaso committed Jul 11, 2020
1 parent 6590d69 commit c3c87ce4dc0138a70c73ffa44a5dea2d177dccf1
@@ -234,8 +234,8 @@ This information is calculated from the geometry columns types.
CreateSpatialIndex,
SpatialIndexExists,
DeleteSpatialIndex,
DropColumn,
AddColumn,
DeleteField,
AddField,
};

typedef QFlags<QgsAbstractDatabaseProviderConnection::Capability> Capabilities;
@@ -359,12 +359,12 @@ Raises a QgsProviderConnectionException if any errors are encountered.
:raises :: py:class:`QgsProviderConnectionException`
%End

virtual void dropColumn( const QString &fieldName, const QString &schema, const QString &tableName, bool force = false ) const throw( QgsProviderConnectionException );
virtual void deleteField( const QString &fieldName, const QString &schema, const QString &tableName, bool force = false ) const throw( QgsProviderConnectionException );
%Docstring
Drops the column with the specified name.
Deletes the field with the specified name.
Raises a QgsProviderConnectionException if any errors are encountered.

:param fieldName: name of the field to be dropped
:param fieldName: name of the field to be deleted
:param schema: name of the schema (schema is ignored if not supported by the backend).
:param tableName: name of the table
:param force: if ``True``, a DROP CASCADE will drop all related objects
@@ -376,12 +376,12 @@ Raises a QgsProviderConnectionException if any errors are encountered.
:raises :: py:class:`QgsProviderConnectionException`
%End

virtual void addColumn( const QgsField &field, const QString &schema, const QString &tableName ) const throw( QgsProviderConnectionException );
virtual void addField( const QgsField &field, const QString &schema, const QString &tableName ) const throw( QgsProviderConnectionException );
%Docstring
Adds a column
Adds a field
Raises a QgsProviderConnectionException if any errors are encountered.

:param field: specification of the new column
:param field: specification of the new field
:param schema: name of the schema (schema is ignored if not supported by the backend).
:param tableName: name of the table

@@ -712,7 +712,7 @@ void QgsFieldsItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *me
{
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( fieldsItem->connectionUri(), {} ) ) };
// Check if it is supported
if ( conn && conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::AddColumn ) )
if ( conn && conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::AddField ) )
{
QAction *addColumnAction = new QAction( tr( "New field…" ), menu );
connect( addColumnAction, &QAction::triggered, this, [ = ]
@@ -726,7 +726,7 @@ void QgsFieldsItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *me
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn2 { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( fieldsItem->connectionUri(), {} ) ) };
try
{
conn2->addColumn( dialog.field(), fieldsItem->schema(), fieldsItem->tableName() );
conn2->addField( dialog.field(), fieldsItem->schema(), fieldsItem->tableName() );
item->refresh();
}
catch ( const QgsProviderConnectionException &ex )
@@ -784,17 +784,17 @@ void QgsFieldItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
if ( md )
{
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( fieldsItem->connectionUri(), {} ) ) };
if ( conn && conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::DropColumn ) )
if ( conn && conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::DeleteField ) )
{
QAction *dropColumnAction = new QAction( tr( "Delete field…" ), menu );
connect( dropColumnAction, &QAction::triggered, this, [ = ]
QAction *deleteFieldAction = new QAction( tr( "Delete field…" ), menu );
connect( deleteFieldAction, &QAction::triggered, this, [ = ]
{
if ( QMessageBox::warning( menu, tr( "Delete field" ), tr( "Delete '%1' permanently (with CASCADE)?" ).arg( item->name() ), QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Ok )
{
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn2 { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( fieldsItem->connectionUri(), {} ) ) };
try
{
conn2->dropColumn( item->name(), fieldsItem->schema(), fieldsItem->tableName(), true );
conn2->deleteField( item->name(), fieldsItem->schema(), fieldsItem->tableName(), true );
fieldsItem->refresh();
}
catch ( const QgsProviderConnectionException &ex )
@@ -810,7 +810,7 @@ void QgsFieldItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
}
}
} );
menu->addAction( dropColumnAction );
menu->addAction( deleteFieldAction );
}
}
}
@@ -318,8 +318,8 @@ void QgsGeoPackageProviderConnection::setDefaultCapabilities()
Capability::CreateSpatialIndex,
Capability::SpatialIndexExists,
Capability::DeleteSpatialIndex,
Capability::DropColumn,
Capability::AddColumn
Capability::DeleteField,
Capability::AddField
};
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
mCapabilities |= Capability::DropRasterTable;
@@ -154,9 +154,9 @@ bool QgsAbstractDatabaseProviderConnection::spatialIndexExists( const QString &,
return false;
}

void QgsAbstractDatabaseProviderConnection::dropColumn( const QString &fieldName, const QString &schema, const QString &tableName, bool ) const
void QgsAbstractDatabaseProviderConnection::deleteField( const QString &fieldName, const QString &schema, const QString &tableName, bool ) const
{
checkCapability( Capability::DropColumn );
checkCapability( Capability::DeleteField );

QgsVectorLayer::LayerOptions options { false, false };
options.skipCrsValidation = true;
@@ -179,9 +179,9 @@ void QgsAbstractDatabaseProviderConnection::dropColumn( const QString &fieldName
}
}

void QgsAbstractDatabaseProviderConnection::addColumn( const QgsField &field, const QString &schema, const QString &tableName ) const
void QgsAbstractDatabaseProviderConnection::addField( const QgsField &field, const QString &schema, const QString &tableName ) const
{
checkCapability( Capability::AddColumn );
checkCapability( Capability::AddField );

QgsVectorLayer::LayerOptions options { false, false };
options.skipCrsValidation = true;
@@ -293,8 +293,8 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
CreateSpatialIndex = 1 << 16, //!< The connection can create spatial indices
SpatialIndexExists = 1 << 17, //!< The connection can determine if a spatial index exists
DeleteSpatialIndex = 1 << 18, //!< The connection can delete spatial indices for tables
DropColumn = 1 << 19, //!< Can delete an existing column
AddColumn = 1 << 20, //!< Can add a new column
DeleteField = 1 << 19, //!< Can delete an existing field/column
AddField = 1 << 20, //!< Can add a new field/column
};

Q_ENUM( Capability )
@@ -395,27 +395,27 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
virtual void dropSchema( const QString &name, bool force = false ) const SIP_THROW( QgsProviderConnectionException );

/**
* Drops the column with the specified name.
* Deletes the field with the specified name.
* Raises a QgsProviderConnectionException if any errors are encountered.
* \param fieldName name of the field to be dropped
* \param fieldName name of the field to be deleted
* \param schema name of the schema (schema is ignored if not supported by the backend).
* \param tableName name of the table
* \param force if TRUE, a DROP CASCADE will drop all related objects
* \note it is responsibility of the caller to handle open layers and registry entries.
* \throws QgsProviderConnectionException
*/
virtual void dropColumn( const QString &fieldName, const QString &schema, const QString &tableName, bool force = false ) const SIP_THROW( QgsProviderConnectionException );
virtual void deleteField( const QString &fieldName, const QString &schema, const QString &tableName, bool force = false ) const SIP_THROW( QgsProviderConnectionException );

/**
* Adds a column
* Adds a field
* Raises a QgsProviderConnectionException if any errors are encountered.
* \param field specification of the new column
* \param field specification of the new field
* \param schema name of the schema (schema is ignored if not supported by the backend).
* \param tableName name of the table
* \note it is responsibility of the caller to handle open layers and registry entries.
* \throws QgsProviderConnectionException
*/
virtual void addColumn( const QgsField &field, const QString &schema, const QString &tableName ) const SIP_THROW( QgsProviderConnectionException );
virtual void addField( const QgsField &field, const QString &schema, const QString &tableName ) const SIP_THROW( QgsProviderConnectionException );


/**
@@ -84,8 +84,8 @@ void QgsMssqlProviderConnection::setDefaultCapabilities()
Capability::Schemas,
Capability::Spatial,
Capability::TableExists,
Capability::DropColumn,
Capability::AddColumn
Capability::DeleteField,
Capability::AddField
};
}

@@ -68,8 +68,8 @@ void QgsPostgresProviderConnection::setDefaultCapabilities()
Capability::CreateSpatialIndex,
Capability::SpatialIndexExists,
Capability::DeleteSpatialIndex,
Capability::DropColumn,
Capability::AddColumn
Capability::DeleteField,
Capability::AddField
};
}

@@ -362,8 +362,8 @@ void QgsSpatiaLiteProviderConnection::setDefaultCapabilities()
Capability::ExecuteSql,
Capability::CreateSpatialIndex,
Capability::SpatialIndexExists,
Capability::DropColumn,
Capability::AddColumn
Capability::DeleteField,
Capability::AddField
};
}

@@ -446,7 +446,7 @@ QString QgsSpatiaLiteProviderConnection::pathFromUri() const
}


void QgsSpatiaLiteProviderConnection::dropColumn( const QString &fieldName, const QString &, const QString &tableName, bool ) const
void QgsSpatiaLiteProviderConnection::deleteField( const QString &fieldName, const QString &, const QString &tableName, bool ) const
{
QgsVectorLayer::LayerOptions options { false, false };
options.skipCrsValidation = true;
@@ -45,7 +45,7 @@ class QgsSpatiaLiteProviderConnection : public QgsAbstractDatabaseProviderConnec
QList<QgsAbstractDatabaseProviderConnection::TableProperty> tables( const QString &schema = QString(),
const TableFlags &flags = nullptr ) const override;
QIcon icon() const override;
void dropColumn( const QString &fieldName, const QString &schema, const QString &tableName, bool force ) const override;
void deleteField( const QString &fieldName, const QString &schema, const QString &tableName, bool force ) const override;

private:

@@ -329,14 +329,14 @@ def _test_operations(self, md, conn):
for f in ['string_t', 'long_t', 'double_t', 'integer_t', 'date_t', 'datetime_t', 'time_t']:
self.assertTrue(f in fields.names())

if capabilities & QgsAbstractDatabaseProviderConnection.AddColumn:
if capabilities & QgsAbstractDatabaseProviderConnection.AddField:
field = QgsField('short_lived_field', QVariant.Int, 'integer')
conn.addColumn(field, 'myNewSchema', 'myNewTable')
conn.addField(field, 'myNewSchema', 'myNewTable')
fields = conn.fields('myNewSchema', 'myNewTable')
self.assertTrue('short_lived_field' in fields.names())

if capabilities & QgsAbstractDatabaseProviderConnection.DropColumn:
conn.dropColumn('short_lived_field', 'myNewSchema', 'myNewTable')
if capabilities & QgsAbstractDatabaseProviderConnection.DeleteField:
conn.deleteField('short_lived_field', 'myNewSchema', 'myNewTable')
fields = conn.fields('myNewSchema', 'myNewTable')
self.assertFalse('short_lived_field' in fields.names())

0 comments on commit c3c87ce

Please sign in to comment.
You can’t perform that action at this time.