Skip to content

Commit 83d0e35

Browse files
committed
joins working
1 parent 562a4d7 commit 83d0e35

File tree

2 files changed

+59
-49
lines changed

2 files changed

+59
-49
lines changed

aragorm/query/query_types.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,33 @@ def __str__(self):
3636
columns_str = '*'
3737
else:
3838
# TODO - make sure the columns passed in are valid
39-
columns_str = ', '.join(self.column_names)
39+
columns_str = ', '.join([
40+
i.replace('.', '$.') for i in self.column_names
41+
])
4042

4143
select = 'SELECT DISTINCT' if self.distinct else 'SELECT'
4244
query = f'{select} {columns_str} FROM "{self.table.Meta.tablename}"'
4345

4446
#######################################################################
4547

4648
# JOIN
49+
joins = []
4750
for column_name in self.column_names:
4851
if '.' in column_name:
4952
local_name, _ = column_name.split('.')
5053
table_name = self.table.get_column_by_name(
5154
local_name
5255
).references.Meta.tablename
56+
57+
alias = f'{local_name}$'
58+
59+
if alias in joins:
60+
continue
61+
5362
query += (
54-
f' JOIN {table_name} ON {local_name} = {table_name}.id'
63+
f' JOIN {table_name} {alias} ON {local_name} = {alias}.id'
5564
)
65+
joins.append(alias)
5666

5767
#######################################################################
5868

tests/table/test_join.py

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33
from ..example_project.tables import Pokemon, Stadium, Match
44

55

6-
class TestCreateJoin(TestCase):
6+
# class TestCreateJoin():
77

8-
def test_create_join(self):
8+
# def test_create_join(self):
99

10-
# Pokemon.create().run_sync()
11-
# Stadium.create().run_sync()
12-
Match.create().run_sync()
10+
# Pokemon.create().run_sync()
11+
# Stadium.create().run_sync()
12+
# Match.create().run_sync()
1313

14-
Match.delete().run_sync()
15-
Pokemon.delete().run_sync()
16-
Stadium.delete().run_sync()
14+
# Match.drop().run_sync()
15+
# Pokemon.drop().run_sync()
16+
# Stadium.drop().run_sync()
1717

1818

19-
# TODO - PUT BACK
20-
class _TestJoin(TestCase):
19+
class TestJoin(TestCase):
2120
"""
2221
Test instantiating Table instances
2322
"""
@@ -28,41 +27,42 @@ def setUp(self):
2827
Match.create().run_sync()
2928

3029
def tearDown(self):
31-
Match.delete().run_sync()
32-
Pokemon.delete().run_sync()
33-
Stadium.delete().run_sync()
34-
35-
def _test_join(self):
36-
"""
37-
Need a good example ...
38-
"""
39-
try:
40-
pikachu = Pokemon(name="pikachu")
41-
pikachu.save().run_sync()
42-
43-
bulbasaur = Pokemon(name="bulbasaur")
44-
bulbasaur.save().run_sync()
45-
46-
stadium = Stadium(name="fairy garden")
47-
48-
Match(
49-
pokemon1=pikachu,
50-
pokemon2=bulbasaur,
51-
stadium=stadium
52-
).save().run_sync()
53-
54-
response = Match.select(
55-
'pokemon1.name',
56-
'pokemon2.name',
57-
'stadium.name'
58-
).run_sync()
59-
except Exception:
60-
pass
61-
62-
def test_ref(self):
63-
"""
64-
Match.select().count().where(
65-
Match.ref('pokemon1.name') == 'pikachu'
30+
Match.drop().run_sync()
31+
Pokemon.drop().run_sync()
32+
Stadium.drop().run_sync()
33+
34+
def test_join(self):
35+
pikachu = Pokemon(name="pikachu", trainer="ash")
36+
pikachu.save().run_sync()
37+
38+
bulbasaur = Pokemon(name="bulbasaur")
39+
bulbasaur.save().run_sync()
40+
41+
stadium = Stadium(name="fairy garden")
42+
stadium.save().run_sync()
43+
44+
# TODO - make sure you can also do:
45+
# pokemon_1=pikachu
46+
save_query = Match(
47+
pokemon_1=pikachu.id,
48+
pokemon_2=bulbasaur.id,
49+
stadium=stadium.id
50+
).save()
51+
save_query.run_sync()
52+
53+
select_query = Match.select(
54+
'pokemon_1.name',
55+
'pokemon_2.name',
56+
'stadium.name',
57+
'pokemon_1.trainer'
6658
)
67-
"""
68-
pass
59+
response = select_query.run_sync()
60+
import ipdb; ipdb.set_trace()
61+
62+
# def _test_ref(self):
63+
# """
64+
# Match.select().count().where(
65+
# Match.ref('pokemon1.name') == 'pikachu'
66+
# )
67+
# """
68+
# pass

0 commit comments

Comments
 (0)