natural join and example to test it
git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@8 014f5005-505e-4b48-8d0a-63407b615a7c
This commit is contained in:
parent
953ebc3d1e
commit
350bd00967
40
relation.py
40
relation.py
@ -198,6 +198,46 @@ class relation (object):
|
|||||||
if e not in newt.content:
|
if e not in newt.content:
|
||||||
newt.content.append(list(e))
|
newt.content.append(list(e))
|
||||||
return newt
|
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):
|
def __str__(self):
|
||||||
'''Returns a string representation of the relation, can be printed with
|
'''Returns a string representation of the relation, can be printed with
|
||||||
|
17
samples/skills.tlb
Normal file
17
samples/skills.tlb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user