From 8d0830fd6bfad6d9fe361fd6242dd26b3ac24d1a Mon Sep 17 00:00:00 2001 From: LtWorf Date: Tue, 10 Feb 2009 12:11:32 +0000 Subject: [PATCH] added insert method, can be used to add one touple per time into a relation and does integrity check (just prevents inserting duplicated entries) git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@80 014f5005-505e-4b48-8d0a-63407b615a7c --- relation.py | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/relation.py b/relation.py index 9239dc1..4000ed0 100644 --- a/relation.py +++ b/relation.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # Relational # Copyright (C) 2008 Salvo "LtWorf" Tomaselli # @@ -449,30 +450,25 @@ class relation (object): i[f_ids[k]]=str(dic[keys[k]]) return affected def insert(self,values): - '''Inserts the values in the relation. If values[0] is not a list | tuple, - values will be inserted as a tuple. Otherwise values will be considered as a - table containing many tuples that will be inserted. + '''Inserts a touple in the relation. This function will not insert duplicate tuples. All the values will be converted in string. Will return the number of inserted rows.''' - affected=0 - l=len(self.header.attributes) - if str(values[0].__class__) == "" or str(values[0].__class__) == "": - for i in values: - if l==len(i) and i not in self.content: - affected+=1 - t=[] - for q in i: - t.append(str(i)) - self.content.append(t) + + #Returns if touple doesn't fit the number of attributes + if len(self.header.attributes) != len(values): + return 0 + + #Creating list containing only strings + t=[] + for i in values: + t.append(str(i)) + + if t not in self.content: + self.content.append(t) + return 1 else: - if len(values)== l and values not in self.content: - t=[] - for q in values: - t.append(str(q)) - self.content.append(t) - return 1 - return affected + return 0 def delete(self,expr): '''Delete, expr must be a valid boolean expression, can contain field names,