Skip to content

Commit

Permalink
Fix to make deletion of postgres columns working again
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7063 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Jul 6, 2007
1 parent a46ad0e commit 8aaef4e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 19 deletions.
4 changes: 1 addition & 3 deletions src/app/qgsattributetable.cpp
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsattributetable.h
Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/core/qgsvectordataprovider.cpp
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsvectordataprovider.h
Expand Up @@ -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.
Expand Down
15 changes: 12 additions & 3 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2878,27 +2878,36 @@ void QgsVectorLayer::setCoordinateSystem()

}

bool QgsVectorLayer::commitAttributeChanges(const QgsAttributeIds& deleted,
bool QgsVectorLayer::commitAttributeChanges(const QgsDeletedAttributesSet& deleted,
const QgsNewAttributesMap& added,
const QgsChangedAttributesMap& changed)
{
bool returnvalue=true;

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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsvectorlayer.h
Expand Up @@ -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;


Expand Down Expand Up @@ -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);

Expand Down
14 changes: 9 additions & 5 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -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)
Expand Down

0 comments on commit 8aaef4e

Please sign in to comment.