Project

General

Profile

« Previous | Next » 

Revision 3000

sql_gen.py: Store index cols in the table instead of in the column, because columns are often recreated from the table and name but tables are generally not copied

View differences:

lib/sql_gen.py
144 144
        self.name = name
145 145
        self.schema = schema
146 146
        self.is_temp = is_temp
147
        self.index_cols = {}
147 148
    
148 149
    def to_str(self, db):
149 150
        str_ = ''
......
152 153
        return str_
153 154
    
154 155
    def to_Table(self): return self
156
    
157
    def _compare_on(self):
158
        compare_on = Derived._compare_on(self)
159
        del compare_on['index_cols'] # ignore
160
        return compare_on
155 161

  
156 162
def is_underlying_table(table):
157 163
    return isinstance(table, Table) and table.to_Table() is table
......
208 214
        
209 215
        self.name = name
210 216
        self.table = table
211
        self.index_col = None
212 217
    
213 218
    def to_str(self, db, for_str=False):
214 219
        str_ = db.esc_name(self.name)
......
225 230

  
226 231
def is_table_col(col): return isinstance(col, Col) and col.table != None
227 232

  
228
def is_indexed_col(col): return isinstance(col, Col) and col.index_col != None
233
def index_col(col):
234
    if not is_table_col(col): return None
235
    return col.table.index_cols.get(col.name, None)
229 236

  
230 237
def is_temp_col(col): return col.table != None and col.table.is_temp
231 238

  
lib/sql.py
1005 1005
        +col.to_str(db)+' SET NOT NULL', cacheable=True)
1006 1006

  
1007 1007
def add_index_col(db, col, suffix, expr, nullable=True):
1008
    if col.index_col != None: return # already has index col
1008
    if sql_gen.index_col(col) != None: return # already has index col
1009 1009
    
1010 1010
    new_col = sql_gen.suffixed_col(col, suffix)
1011 1011
    new_typed_col = sql_gen.TypedCol(new_col.name, db.col_info(col).type)
......
1015 1015
    if not nullable: add_not_null(db, new_col)
1016 1016
    add_index(db, new_col)
1017 1017
    
1018
    col.index_col = new_col
1018
    col.table.index_cols[col.name] = new_col
1019 1019

  
1020 1020
def ensure_not_null(db, col):
1021 1021
    '''For params, see sql_gen.ensure_not_null()'''
......
1024 1024
    # If nullable column in a temp table, add separate column instead
1025 1025
    if sql_gen.is_temp_col(col) and isinstance(expr, sql_gen.EnsureNotNull):
1026 1026
        add_index_col(db, col, '::NOT NULL', expr, nullable=False)
1027
        expr = col.index_col
1027
        expr = sql_gen.index_col(col)
1028 1028
    
1029 1029
    return expr
1030 1030

  

Also available in: Unified diff