Skip to content

Commit 0174afd

Browse files
author
mhugent
committedJul 6, 2007
Fix to make deletion of postgres columns working again
git-svn-id: http://svn.osgeo.org/qgis/trunk@7063 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 108ddab commit 0174afd

File tree

7 files changed

+30
-19
lines changed

7 files changed

+30
-19
lines changed
 

‎src/app/qgsattributetable.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,7 @@ void QgsAttributeTable::deleteAttribute(const QString& name)
356356
}
357357
else
358358
{
359-
QgsDebugMsg("QgsAttributeTable: deleteAttribute " + name);
360-
361-
// TODO: [MD] mDeletedAttributes.insert(name);
359+
mDeletedAttributes.insert(name);
362360
removeAttrColumn(name);
363361
}
364362
mEdited=true;

‎src/app/qgsattributetable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class QgsAttributeTable:public Q3Table
130130
and the value the attribute type*/
131131
QgsNewAttributesMap mAddedAttributes;
132132
/**Set containing the attribute names of deleted attributes*/
133-
QgsFeatureIds mDeletedAttributes;
133+
QgsDeletedAttributesSet mDeletedAttributes;
134134
/**Nested map containing the changed attribute values. The int is the feature id,
135135
the first QString the attribute name and the second QString the new value*/
136136
QgsChangedAttributesMap mChangedValues;

‎src/core/qgsvectordataprovider.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ bool QgsVectorDataProvider::addAttributes(const QgsNewAttributesMap & attributes
8282
return false;
8383
}
8484

85-
bool QgsVectorDataProvider::deleteAttributes(const QgsAttributeIds & attributes)
85+
bool QgsVectorDataProvider::deleteAttributes(const QgsAttributeIds& attributes)
8686
{
8787
return false;
8888
}
@@ -220,15 +220,13 @@ QString QgsVectorDataProvider::capabilitiesString() const
220220
int QgsVectorDataProvider::indexFromFieldName(const QString& fieldName) const
221221
{
222222
const QgsFieldMap& theFields = fields();
223-
int counter = 0;
224223

225224
for (QgsFieldMap::const_iterator it = theFields.begin(); it != theFields.end(); ++it)
226225
{
227226
if(it->name() == fieldName)
228227
{
229-
return counter;
228+
return it.key();
230229
}
231-
++counter;
232230
}
233231
return -1;
234232
}

‎src/core/qgsvectordataprovider.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
190190

191191
/**
192192
* Deletes existing attributes
193-
* @param attributes a set containing indexes of attributes
193+
* @param attributes a set containing names of attributes
194194
* @return true in case of success and false in case of failure
195195
*/
196-
virtual bool deleteAttributes(const QgsAttributeIds & attributes);
196+
virtual bool deleteAttributes(const QgsAttributeIds& attributes);
197197

198198
/**
199199
* Changes attribute values of existing features.

‎src/core/qgsvectorlayer.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,27 +2878,36 @@ void QgsVectorLayer::setCoordinateSystem()
28782878

28792879
}
28802880

2881-
bool QgsVectorLayer::commitAttributeChanges(const QgsAttributeIds& deleted,
2881+
bool QgsVectorLayer::commitAttributeChanges(const QgsDeletedAttributesSet& deleted,
28822882
const QgsNewAttributesMap& added,
28832883
const QgsChangedAttributesMap& changed)
28842884
{
28852885
bool returnvalue=true;
28862886

28872887
if(mDataProvider)
28882888
{
2889+
2890+
QgsAttributeIds attIds;
2891+
QgsDeletedAttributesSet::const_iterator att_it = deleted.constBegin();
2892+
2893+
for(; att_it != deleted.constEnd(); ++att_it)
2894+
{
2895+
attIds.insert(mDataProvider->indexFromFieldName(*att_it));
2896+
}
2897+
28892898
if(mDataProvider->capabilities()&QgsVectorDataProvider::DeleteAttributes)
28902899
{
28912900
//delete attributes in all not commited features
28922901
for (QgsFeatureList::iterator iter = mAddedFeatures.begin(); iter != mAddedFeatures.end(); ++iter)
28932902
{
2894-
for (QgsAttributeIds::const_iterator it = deleted.begin(); it != deleted.end(); ++it)
2903+
for (QgsAttributeIds::const_iterator it = attIds.begin(); it != attIds.end(); ++it)
28952904
{
28962905
(*iter).deleteAttribute(*it);
28972906
}
28982907
}
28992908

29002909
//and then in the provider
2901-
if(!mDataProvider->deleteAttributes(deleted))
2910+
if(!mDataProvider->deleteAttributes(attIds))
29022911
{
29032912
returnvalue=false;
29042913
}

‎src/core/qgsvectorlayer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ typedef QSet<int> QgsAttributeIds;
5656
// key = attribute name, value = attribute type
5757
typedef QMap<QString, QString> QgsNewAttributesMap;
5858

59+
typedef QSet<QString> QgsDeletedAttributesSet;
60+
5961
typedef QMap<int, QgsField> QgsFieldMap;
6062

6163

@@ -314,7 +316,7 @@ existing rings, 5 no feature found where ring can be inserted*/
314316
of attribute values to change
315317
316318
*/
317-
bool commitAttributeChanges(const QgsAttributeIds& deleted,
319+
bool commitAttributeChanges(const QgsDeletedAttributesSet& deleted,
318320
const QgsNewAttributesMap& added,
319321
const QgsChangedAttributesMap& changed);
320322

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,17 +1768,21 @@ bool QgsPostgresProvider::addAttributes(const QgsNewAttributesMap & name)
17681768
return returnvalue;
17691769
}
17701770

1771-
bool QgsPostgresProvider::deleteAttributes(const QgsAttributeIds& name)
1771+
bool QgsPostgresProvider::deleteAttributes(const QgsAttributeIds& ids)
17721772
{
17731773
bool returnvalue=true;
17741774
PQexec(connection,"BEGIN");
1775-
for(QgsAttributeIds::const_iterator iter=name.begin();iter!=name.end();++iter)
1775+
1776+
for(QgsAttributeIds::const_iterator iter=ids.begin();iter != ids.end();++iter)
17761777
{
1777-
QString column = attributeFields[*iter].name();
1778+
QgsFieldMap::const_iterator field_it = attributeFields.find(*iter);
1779+
if(field_it == attributeFields.constEnd())
1780+
{
1781+
continue;
1782+
}
1783+
QString column = field_it->name();
17781784
QString sql="ALTER TABLE "+mSchemaTableName+" DROP COLUMN "+column;
17791785

1780-
QgsDebugMsg(sql);
1781-
17821786
//send sql statement and do error handling
17831787
PGresult* result=PQexec(connection, (const char *)(sql.utf8()));
17841788
if(result==0)

0 commit comments

Comments
 (0)
Please sign in to comment.