Project

General

Profile

« Previous | Next » 

Revision 2394

sql.py: mk_flatten_mapping(): Take as_items param to return a list of dict items instead of a dict. Sort preserve cols before other cols. flatten(): Turn on as_items so that cols list is sorted in input order, with preserve cols first. This ensures that if a pkey is provided in preserve, it will be the first col in the generated table.

View differences:

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

  
617
def mk_flatten_mapping(db, into, cols, preserve=[]):
617
def mk_flatten_mapping(db, into, cols, preserve=[], as_items=False):
618 618
    '''Creates a mapping from original column names (which may have collisions)
619 619
    to names that will be distinct among the given tables.
620 620
    This is meant to be used for several tables that are being joined together.
621 621
    @param into The table for the new columns
622
    @param preserve [sql_gen.Col...] Columns not to rename. The tables of the
623
        provided Col objects will be changed to into, so make copies of
624
        them if you want to keep the original tables.
622
    @param preserve [sql_gen.Col...] Columns not to rename. Note that these
623
        columns will be included in the mapping even if they are not in cols.
624
        The tables of the provided Col objects will be changed to into, so make
625
        copies of them if you want to keep the original tables.
626
    @param as_items Whether to return a list of dict items instead of a dict
625 627
    @return dict(orig_col=new_col, ...)
626 628
        * orig_col: sql_gen.Col(orig_col_name, orig_table)
627 629
        * new_col: sql_gen.Col(orig_col_name, into)
628 630
        * All mappings use the into table so its name can easily be
629 631
          changed for all columns at once
630 632
    '''
631
    flatten_mapping = {}
632
    for col in cols: flatten_mapping[col] = sql_gen.Col(str(col), into)
633
    items = []
633 634
    for col in preserve:
634 635
        orig_col = copy.copy(col)
635 636
        col.table = into
636
        flatten_mapping[orig_col] = col
637
    return flatten_mapping
637
        items.append((orig_col, col))
638
    preserve = set(preserve)
639
    for col in cols:
640
        if col not in preserve: items.append((col, sql_gen.Col(str(col), into)))
641
    
642
    if not as_items: items = dict(items)
643
    return items
638 644

  
639 645
def flatten(db, into, joins, cols, limit=None, start=None, **kw_args):
640 646
    '''For params, see mk_flatten_mapping()
641 647
    @return See return value of mk_flatten_mapping()
642 648
    '''
643
    mapping = mk_flatten_mapping(db, into, cols, **kw_args)
644
    cols = [sql_gen.NamedCol(new.name, old) for old, new in mapping.iteritems()]
649
    items = mk_flatten_mapping(db, into, cols, as_items=True, **kw_args)
650
    cols = [sql_gen.NamedCol(new.name, old) for old, new in items]
645 651
    run_query_into(db, *mk_select(db, joins, cols, limit=limit, start=start),
646 652
        into=into)
647
    return mapping
653
    return dict(items)
648 654

  
649 655
def pkey(db, table, recover=None):
650 656
    '''Assumed to be first column in table'''

Also available in: Unified diff