diff --git a/relation.py b/relation.py index 1c5c2af..dbf1eb3 100644 --- a/relation.py +++ b/relation.py @@ -198,6 +198,46 @@ class relation (object): if e not in newt.content: newt.content.append(list(e)) return newt + def join(self,other): + '''Natural join, using field's names''' + shared=[] + for i in self.header.fields: + if i in other.header.fields: + shared.append(i) + newt=relation() #Creates the new relation + + #Adds all the fields of the 1st relation + newt.header=header(list(self.header.fields)) + + #Adds all the fields of the 2nd, when non shared + for i in other.header.fields: + if i not in shared: + newt.header.fields.append(i) + #Shared ids of self + sid=self.header.getFieldsId(shared) + #Shared ids of the other relation + oid=other.header.getFieldsId(shared) + + #Non shared ids of the other relation + noid=[] + for i in range(len(other.header.fields)): + if i not in oid: + noid.append(i) + + for i in self.content: + for j in other.content: + match=True + for k in range(len(sid)): + match=match and ( i[sid[k]]== j[oid[k]]) + + if match: + item=list(i) + for l in noid: + item.append(j[l]) + + newt.content.append(item) + + return newt def __str__(self): '''Returns a string representation of the relation, can be printed with @@ -227,7 +267,7 @@ class relation (object): col+=1 return res - + class header (object): '''This class defines the header of a relation. It is used within relations to know if requested operations are accepted''' diff --git a/samples/skills.tlb b/samples/skills.tlb new file mode 100644 index 0000000..ca0d9e5 --- /dev/null +++ b/samples/skills.tlb @@ -0,0 +1,17 @@ +id skill +0 C +0 Python +1 Python +1 C++ +1 SystemAdmin +2 C +2 PHP +3 C++ +4 C++ +4 C +4 Pearl +5 Pearl +5 C +7 Python +7 C +7 PHP \ No newline at end of file