From fd5e8dd276e86c5640ec9263f282afeebbff5b5a Mon Sep 17 00:00:00 2001 From: LtWorf Date: Thu, 25 Dec 2008 11:28:38 +0000 Subject: [PATCH] Relation module has SQL-like update git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@76 014f5005-505e-4b48-8d0a-63407b615a7c --- CHANGELOG | 1 + relation.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index e82223a..bbc7a24 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -52,3 +52,4 @@ - Converted sample's relations to csv - Deb postinstall generates optimized files, this will increase loading speed - Relation module has SQL-like delete +- Relation module has SQL-like update diff --git a/relation.py b/relation.py index 8224f2a..a3772a7 100644 --- a/relation.py +++ b/relation.py @@ -418,6 +418,33 @@ class relation (object): 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): '''Delete, expr must be a valid boolean expression, can contain field names, constant, math operations and boolean ones.