Project

General

Profile

« Previous | Next » 

Revision 2701

sql_gen.py: Col: Support tracking the column(s) a column is derived from, so that error messages can be attributes to the proper input column(s)

View differences:

lib/sql_gen.py
5 5
import UserDict
6 6

  
7 7
import dicts
8
import iters
9
import lists
8 10
import objects
9 11
import strings
10 12
import util
......
125 127

  
126 128
##### Columns
127 129

  
130
src_self = object() # tells Col that it is its own source column
131

  
128 132
class Col(Code):
129
    def __init__(self, name, table=None):
133
    def __init__(self, name, table=None, srcs=()):
130 134
        '''
131 135
        @param table Table|None (for no table)
136
        @param srcs See self.set_srcs()
132 137
        '''
133 138
        if util.is_str(table): table = Table(table)
134 139
        assert table == None or isinstance(table, Table)
135 140
        
136 141
        self.name = name
137 142
        self.table = table
143
        self.set_srcs(srcs)
138 144
    
139 145
    def to_str(self, db):
140 146
        str_ = ''
......
143 149
        return str_
144 150
    
145 151
    def to_Col(self): return self
152
    
153
    def set_srcs(self, srcs):
154
        '''
155
        @param srcs (Col...)|src_self The column(s) this column is derived from
156
        '''
157
        if srcs == src_self: srcs = (self,)
158
        srcs = tuple(srcs) # make Col hashable
159
        self.srcs = srcs
160
    
161
    def _compare_on(self):
162
        compare_on = self.__dict__.copy()
163
        del compare_on['srcs'] # ignore
164
        return compare_on
146 165

  
147 166
def is_table_col(col): return col.table != None
148 167

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

  
149 170
def as_Col(col, table=None, name=None):
150 171
    '''
151 172
    @param name If not None, any non-Col input will be renamed using NamedCol.

Also available in: Unified diff