Simplified update function
Now avoids duplicating code.
This commit is contained in:
parent
f0608c7212
commit
1e464d9d4c
@ -65,12 +65,13 @@ 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:
|
||||||
self.content = set(self.content)
|
if copy_content:
|
||||||
|
self.content = set(self.content)
|
||||||
self._readonly = False
|
self._readonly = False
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
@ -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):
|
||||||
'''
|
'''
|
||||||
|
Loading…
x
Reference in New Issue
Block a user