Skip to content

Commit 126f8ca

Browse files
author
Daniel Townsend
committed
fleshed out objects query
1 parent 3b05421 commit 126f8ca

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

aragorm/query/__init__.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ class Select(
2727
OutputMixin,
2828
WhereMixin,
2929
):
30-
# columns_str => columns: t.List[str]
31-
def __init__(self, table: 'Table', columns_str: str) -> None:
32-
self.columns_str = columns_str
30+
def __init__(self, table: 'Table', column_names: t.List[str]) -> None:
31+
self.column_names = column_names
3332
super().__init__(table=table, base='')
3433

3534
def __str__(self):
35+
if len(self.column_names) == 0:
36+
columns_str = '*'
37+
else:
38+
# TODO - make sure the columns passed in are valid
39+
columns_str = ', '.join(self.column_names)
40+
3641
select = 'SELECT DISTINCT' if self.distinct else 'SELECT'
37-
query = f'{select} {self.columns_str} FROM "{self.table.Meta.tablename}"'
42+
query = f'{select} {columns_str} FROM "{self.table.Meta.tablename}"'
3843

3944
if self._where:
4045
query += f' WHERE {self._where.__str__()}'
@@ -48,8 +53,6 @@ def __str__(self):
4853
return query
4954

5055

51-
# TODO try and share as much between Select and Objects as possible ...
52-
#
5356
class Objects(
5457
Query,
5558
LimitMixin,
@@ -59,17 +62,23 @@ class Objects(
5962
"""
6063
Almost identical to select, except you have to select all fields, and
6164
table instances are returned, rather than just data.
62-
63-
Inherits almost everything except OutputMixin, Distinct, Count,
6465
"""
6566

66-
def __init__(self, table: 'Table') -> None:
67-
# TODO - remove base altogether
68-
self._output = Output(as_objects=True)
69-
super().__init__(table=table, base='')
70-
7167
def __str__(self):
72-
pass
68+
"""
69+
Need to do this without repeating select ...
70+
"""
71+
select = Select(
72+
table=self.table,
73+
column_names=[]
74+
)
75+
76+
for attr in ('_limit', '_where', 'order_by'):
77+
setattr(select, attr, getattr(self, attr))
78+
79+
select._output = Output(as_objects=True)
80+
81+
return select.__str__()
7382

7483

7584
class Insert(Query, AddMixin):

aragorm/table.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,9 @@ def select(cls, *column_names: str) -> Select:
153153
inspect that the column exists, and what we're trying to map it
154154
to doesn't already exist.
155155
"""
156-
if len(column_names) == 0:
157-
columns_str = '*'
158-
else:
159-
# TODO - make sure the columns passed in are valid
160-
columns_str = ', '.join(column_names)
161-
162156
return Select(
163157
table=cls,
164-
columns_str=columns_str
158+
column_names=column_names
165159
)
166160

167161
@classmethod
@@ -214,7 +208,8 @@ def update(cls, **columns) -> Update:
214208
"""
215209
columns_str = ', '.join([
216210
f'{column} = {getattr(cls, column).format_value(value)}' for (
217-
column, value) in columns.items()])
211+
column, value) in columns.items()
212+
])
218213

219214
return Update(
220215
table=cls,

0 commit comments

Comments
 (0)