Project

General

Profile

« Previous | Next » 

Revision 2383

sql.py: Added mk_flatten_mapping()

View differences:

lib/sql.py
612 612
    return list(col_names(select(db, table, limit=0, order_by=None,
613 613
        recover=recover)))
614 614

  
615
def mk_flatten_mapping(db, tables, flat_table):
616
    '''Creates a mapping from original column names (which may have collisions)
617
    to names that will be distinct among the given tables.
618
    This is meant to be used for several tables that are being joined together.
619
    @param flat_table The table for the new columns
620
    @return dict(orig_col=new_col, ...)
621
        * orig_col: sql_gen.Col(orig_col_name, orig_table)
622
        * new_col: sql_gen.Col(orig_col_name, flat_table)
623
        * All mappings use the flat_table table so its name can easily be
624
          changed for all columns at once
625
    '''
626
    flatten_mapping = {}
627
    for table in tables:
628
        for col in table_cols(db, table):
629
            col = sql_gen.as_Col(col, table)
630
            flatten_mapping[col] = sql_gen.Col(str(col), flat_table)
631
    return flatten_mapping
632

  
615 633
def pkey(db, table, recover=None):
616 634
    '''Assumed to be first column in table'''
617 635
    return table_cols(db, table, recover)[0]

Also available in: Unified diff