Project

General

Profile

« Previous | Next » 

Revision 2711

sql_gen.py: Moved srcs-related functionality from Col to new superclass Derived

View differences:

lib/sql_gen.py
87 87

  
88 88
def is_null(value): return isinstance(value, Literal) and value.value == None
89 89

  
90
##### Derived elements
91

  
92
src_self = object() # tells Col that it is its own source column
93

  
94
class Derived(Code):
95
    def __init__(self, srcs):
96
        '''
97
        @param srcs See self.set_srcs()
98
        '''
99
        self.set_srcs(srcs)
100
    
101
    def set_srcs(self, srcs):
102
        '''
103
        @param srcs (self_type...)|src_self The element(s) this is derived from
104
        '''
105
        if srcs == src_self: srcs = (self,)
106
        srcs = tuple(srcs) # make Col hashable
107
        self.srcs = srcs
108
    
109
    def _compare_on(self):
110
        compare_on = self.__dict__.copy()
111
        del compare_on['srcs'] # ignore
112
        return compare_on
113

  
114
def cols_srcs(cols): return lists.uniqify(iters.flatten((v.srcs for v in cols)))
115

  
90 116
##### Tables
91 117

  
92 118
class Table(Code):
......
129 155

  
130 156
##### Columns
131 157

  
132
src_self = object() # tells Col that it is its own source column
133

  
134
class Col(Code):
158
class Col(Derived):
135 159
    def __init__(self, name, table=None, srcs=()):
136 160
        '''
137 161
        @param table Table|None (for no table)
138
        @param srcs See self.set_srcs()
162
        @param srcs (Col...)|src_self See Derived.set_srcs()
139 163
        '''
164
        Derived.__init__(self, srcs)
165
        
140 166
        if util.is_str(table): table = Table(table)
141 167
        assert table == None or isinstance(table, Table)
142 168
        
143 169
        self.name = name
144 170
        self.table = table
145
        self.set_srcs(srcs)
146 171
    
147 172
    def to_str(self, db):
148 173
        str_ = ''
......
151 176
        return str_
152 177
    
153 178
    def to_Col(self): return self
154
    
155
    def set_srcs(self, srcs):
156
        '''
157
        @param srcs (Col...)|src_self The column(s) this column is derived from
158
        '''
159
        if srcs == src_self: srcs = (self,)
160
        srcs = tuple(srcs) # make Col hashable
161
        self.srcs = srcs
162
    
163
    def _compare_on(self):
164
        compare_on = self.__dict__.copy()
165
        del compare_on['srcs'] # ignore
166
        return compare_on
167 179

  
168 180
def is_table_col(col): return col.table != None
169 181

  
170
def cols_srcs(cols): return lists.uniqify(iters.flatten((v.srcs for v in cols)))
171

  
172 182
def as_Col(col, table=None, name=None):
173 183
    '''
174 184
    @param name If not None, any non-Col input will be renamed using NamedCol.

Also available in: Unified diff