Simplified update function

Now avoids duplicating code.
This commit is contained in:
Salvo 'LtWorf' Tomaselli 2015-11-17 23:50:58 +01:00
parent f0608c7212
commit 1e464d9d4c

View File

@ -65,11 +65,12 @@ class Relation (object):
iterator = ((self.insert(i) for i in reader)) iterator = ((self.insert(i) for i in reader))
deque(iterator, maxlen=0) deque(iterator, maxlen=0)
def _make_writable(self): def _make_writable(self, copy_content=True):
'''If this relation is marked as readonly, this '''If this relation is marked as readonly, this
method will copy the content to make it writable too''' method will copy the content to make it writable too'''
if self._readonly: if self._readonly:
if copy_content:
self.content = set(self.content) self.content = set(self.content)
self._readonly = False self._readonly = False
@ -409,28 +410,21 @@ class Relation (object):
Returns the number of affected rows. Returns the number of affected rows.
''' '''
self._make_writable() self._make_writable(copy_content=False)
affected = 0 affected = self.selection(expr)
attributes = {} not_affected = self.difference(affected)
keys = dic.keys() # List of headers to modify
f_ids = self.header.getAttributesId(keys)
# new_content=[] #New content of the relation new_values = tuple(zip(self.header.getAttributesId(dic.keys()), dic.values()))
for i in set(self.content):
for j, attr in enumerate(self.header):
attributes[attr] = i[j].autocast()
if eval(expr, attributes): # If expr is true, changing the tuple for i in set(affected.content):
affected += 1 i = list(i)
new_tuple = list(i)
# Deleting the tuple, instead of changing it, so other for column,value in new_values:
# relations can still point to the same list without i[column] = value
# being affected. not_affected.insert(i)
self.content.remove(i)
for k in range(len(keys)): self.content = not_affected.content
new_tuple[f_ids[k]] = rstring(dic[keys[k]]) return len(affected)
self.content.add(tuple(new_tuple))
return affected
def insert(self, values): def insert(self, values):
''' '''