@@ -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- #
5356class 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
7584class Insert (Query , AddMixin ):
0 commit comments