update and delete now return the number of affected rows
git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@77 014f5005-505e-4b48-8d0a-63407b615a7c
This commit is contained in:
parent
fd5e8dd276
commit
600e6ee1e0
13
relation.py
13
relation.py
@ -424,7 +424,9 @@ class relation (object):
|
|||||||
This operation will change the relation itself instead of generating a new one,
|
This operation will change the relation itself instead of generating a new one,
|
||||||
updating all the touples that make expr true.
|
updating all the touples that make expr true.
|
||||||
Dic must be a dictionary that has the form field name:value. Every kind of value
|
Dic must be a dictionary that has the form field name:value. Every kind of value
|
||||||
will be converted into a string'''
|
will be converted into a string.
|
||||||
|
Returns the number of affected rows.'''
|
||||||
|
affected=0
|
||||||
attributes={}
|
attributes={}
|
||||||
keys=dic.keys() #List of headers to modify
|
keys=dic.keys() #List of headers to modify
|
||||||
f_ids=self.header.getAttributesId(keys) #List of indexes corresponding to keys
|
f_ids=self.header.getAttributesId(keys) #List of indexes corresponding to keys
|
||||||
@ -442,15 +444,18 @@ class relation (object):
|
|||||||
else:
|
else:
|
||||||
attributes[self.header.attributes[j]]=i[j]
|
attributes[self.header.attributes[j]]=i[j]
|
||||||
if eval(expr,attributes): #If expr is true, changing the touple
|
if eval(expr,attributes): #If expr is true, changing the touple
|
||||||
|
affected+=1
|
||||||
for k in range(len(keys)):
|
for k in range(len(keys)):
|
||||||
i[f_ids[k]]=str(dic[keys[k]])
|
i[f_ids[k]]=str(dic[keys[k]])
|
||||||
|
return affected
|
||||||
def delete(self,expr):
|
def delete(self,expr):
|
||||||
'''Delete, expr must be a valid boolean expression, can contain field names,
|
'''Delete, expr must be a valid boolean expression, can contain field names,
|
||||||
constant, math operations and boolean ones.
|
constant, math operations and boolean ones.
|
||||||
This operation will change the relation itself instead of generating a new one,
|
This operation will change the relation itself instead of generating a new one,
|
||||||
deleting all the touples that make expr true.'''
|
deleting all the touples that make expr true.
|
||||||
|
Returns the number of affected rows.'''
|
||||||
attributes={}
|
attributes={}
|
||||||
|
affected=len(self.content)
|
||||||
new_content=[] #New content of the relation
|
new_content=[] #New content of the relation
|
||||||
for i in self.content:
|
for i in self.content:
|
||||||
for j in range(len(self.header.attributes)):
|
for j in range(len(self.header.attributes)):
|
||||||
@ -463,8 +468,10 @@ class relation (object):
|
|||||||
else:
|
else:
|
||||||
attributes[self.header.attributes[j]]=i[j]
|
attributes[self.header.attributes[j]]=i[j]
|
||||||
if not eval(expr,attributes):
|
if not eval(expr,attributes):
|
||||||
|
affected-=1
|
||||||
new_content.append(i)
|
new_content.append(i)
|
||||||
self.content=new_content
|
self.content=new_content
|
||||||
|
return affected
|
||||||
|
|
||||||
class header (object):
|
class header (object):
|
||||||
'''This class defines the header of a relation.
|
'''This class defines the header of a relation.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user