Fix to make deletion of postgres columns working again

git-svn-id: http://svn.osgeo.org/qgis/trunk@7063 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
mhugent 2007-07-06 13:39:41 +00:00
parent 108ddab1bd
commit 0174afd26a
7 changed files with 30 additions and 19 deletions

View File

@ -356,9 +356,7 @@ void QgsAttributeTable::deleteAttribute(const QString& name)
}
else
{
QgsDebugMsg("QgsAttributeTable: deleteAttribute " + name);
// TODO: [MD] mDeletedAttributes.insert(name);
mDeletedAttributes.insert(name);
removeAttrColumn(name);
}
mEdited=true;

View File

@ -130,7 +130,7 @@ class QgsAttributeTable:public Q3Table
and the value the attribute type*/
QgsNewAttributesMap mAddedAttributes;
/**Set containing the attribute names of deleted attributes*/
QgsFeatureIds mDeletedAttributes;
QgsDeletedAttributesSet mDeletedAttributes;
/**Nested map containing the changed attribute values. The int is the feature id,
the first QString the attribute name and the second QString the new value*/
QgsChangedAttributesMap mChangedValues;

View File

@ -82,7 +82,7 @@ bool QgsVectorDataProvider::addAttributes(const QgsNewAttributesMap & attributes
return false;
}
bool QgsVectorDataProvider::deleteAttributes(const QgsAttributeIds & attributes)
bool QgsVectorDataProvider::deleteAttributes(const QgsAttributeIds& attributes)
{
return false;
}
@ -220,15 +220,13 @@ QString QgsVectorDataProvider::capabilitiesString() const
int QgsVectorDataProvider::indexFromFieldName(const QString& fieldName) const
{
const QgsFieldMap& theFields = fields();
int counter = 0;
for (QgsFieldMap::const_iterator it = theFields.begin(); it != theFields.end(); ++it)
{
if(it->name() == fieldName)
{
return counter;
return it.key();
}
++counter;
}
return -1;
}

View File

@ -190,10 +190,10 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
/**
* Deletes existing attributes
* @param attributes a set containing indexes of attributes
* @param attributes a set containing names of attributes
* @return true in case of success and false in case of failure
*/
virtual bool deleteAttributes(const QgsAttributeIds & attributes);
virtual bool deleteAttributes(const QgsAttributeIds& attributes);
/**
* Changes attribute values of existing features.

View File

@ -2878,7 +2878,7 @@ void QgsVectorLayer::setCoordinateSystem()
}
bool QgsVectorLayer::commitAttributeChanges(const QgsAttributeIds& deleted,
bool QgsVectorLayer::commitAttributeChanges(const QgsDeletedAttributesSet& deleted,
const QgsNewAttributesMap& added,
const QgsChangedAttributesMap& changed)
{
@ -2886,19 +2886,28 @@ bool QgsVectorLayer::commitAttributeChanges(const QgsAttributeIds& deleted,
if(mDataProvider)
{
QgsAttributeIds attIds;
QgsDeletedAttributesSet::const_iterator att_it = deleted.constBegin();
for(; att_it != deleted.constEnd(); ++att_it)
{
attIds.insert(mDataProvider->indexFromFieldName(*att_it));
}
if(mDataProvider->capabilities()&QgsVectorDataProvider::DeleteAttributes)
{
//delete attributes in all not commited features
for (QgsFeatureList::iterator iter = mAddedFeatures.begin(); iter != mAddedFeatures.end(); ++iter)
{
for (QgsAttributeIds::const_iterator it = deleted.begin(); it != deleted.end(); ++it)
for (QgsAttributeIds::const_iterator it = attIds.begin(); it != attIds.end(); ++it)
{
(*iter).deleteAttribute(*it);
}
}
//and then in the provider
if(!mDataProvider->deleteAttributes(deleted))
if(!mDataProvider->deleteAttributes(attIds))
{
returnvalue=false;
}

View File

@ -56,6 +56,8 @@ typedef QSet<int> QgsAttributeIds;
// key = attribute name, value = attribute type
typedef QMap<QString, QString> QgsNewAttributesMap;
typedef QSet<QString> QgsDeletedAttributesSet;
typedef QMap<int, QgsField> QgsFieldMap;
@ -314,7 +316,7 @@ existing rings, 5 no feature found where ring can be inserted*/
of attribute values to change
*/
bool commitAttributeChanges(const QgsAttributeIds& deleted,
bool commitAttributeChanges(const QgsDeletedAttributesSet& deleted,
const QgsNewAttributesMap& added,
const QgsChangedAttributesMap& changed);

View File

@ -1768,17 +1768,21 @@ bool QgsPostgresProvider::addAttributes(const QgsNewAttributesMap & name)
return returnvalue;
}
bool QgsPostgresProvider::deleteAttributes(const QgsAttributeIds& name)
bool QgsPostgresProvider::deleteAttributes(const QgsAttributeIds& ids)
{
bool returnvalue=true;
PQexec(connection,"BEGIN");
for(QgsAttributeIds::const_iterator iter=name.begin();iter!=name.end();++iter)
for(QgsAttributeIds::const_iterator iter=ids.begin();iter != ids.end();++iter)
{
QString column = attributeFields[*iter].name();
QgsFieldMap::const_iterator field_it = attributeFields.find(*iter);
if(field_it == attributeFields.constEnd())
{
continue;
}
QString column = field_it->name();
QString sql="ALTER TABLE "+mSchemaTableName+" DROP COLUMN "+column;
QgsDebugMsg(sql);
//send sql statement and do error handling
PGresult* result=PQexec(connection, (const char *)(sql.utf8()));
if(result==0)