Relation module has SQL-like update
git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@76 014f5005-505e-4b48-8d0a-63407b615a7c
This commit is contained in:
parent
1b09369198
commit
fd5e8dd276
@ -52,3 +52,4 @@
|
|||||||
- Converted sample's relations to csv
|
- Converted sample's relations to csv
|
||||||
- Deb postinstall generates optimized files, this will increase loading speed
|
- Deb postinstall generates optimized files, this will increase loading speed
|
||||||
- Relation module has SQL-like delete
|
- Relation module has SQL-like delete
|
||||||
|
- Relation module has SQL-like update
|
||||||
|
27
relation.py
27
relation.py
@ -418,6 +418,33 @@ class relation (object):
|
|||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
def update(self,expr,dic):
|
||||||
|
'''Update, expr must be a valid boolean expression, can contain field names,
|
||||||
|
constant, math operations and boolean ones.
|
||||||
|
This operation will change the relation itself instead of generating a new one,
|
||||||
|
updating all the touples that make expr true.
|
||||||
|
Dic must be a dictionary that has the form field name:value. Every kind of value
|
||||||
|
will be converted into a string'''
|
||||||
|
attributes={}
|
||||||
|
keys=dic.keys() #List of headers to modify
|
||||||
|
f_ids=self.header.getAttributesId(keys) #List of indexes corresponding to keys
|
||||||
|
|
||||||
|
#new_content=[] #New content of the relation
|
||||||
|
for i in self.content:
|
||||||
|
for j in range(len(self.header.attributes)):
|
||||||
|
#Giving to the field it's right format (hopefully)
|
||||||
|
if i[j].isdigit():
|
||||||
|
attributes[self.header.attributes[j]]=int(i[j])
|
||||||
|
elif rstring(i[j]).isFloat():
|
||||||
|
attributes[self.header.attributes[j]]=float(i[j])
|
||||||
|
elif isDate(i[j]):
|
||||||
|
attributes[self.header.attributes[j]]=rdate(i[j])
|
||||||
|
else:
|
||||||
|
attributes[self.header.attributes[j]]=i[j]
|
||||||
|
if eval(expr,attributes): #If expr is true, changing the touple
|
||||||
|
for k in range(len(keys)):
|
||||||
|
i[f_ids[k]]=str(dic[keys[k]])
|
||||||
|
|
||||||
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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user