Revision 2392
Added by Aaron Marcuse-Kubitza over 12 years ago
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, tables, flat_table, preserve=[]):
|
|
617 |
def mk_flatten_mapping(db, into, tables, preserve=[]):
|
|
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 |
@param flat_table The table for the new columns
|
|
621 |
@param into The table for the new columns
|
|
622 | 622 |
@param preserve [sql_gen.Col...] Columns not to rename. The tables of the |
623 |
provided Col objects will be changed to flat_table, so make copies of
|
|
623 |
provided Col objects will be changed to into, so make copies of
|
|
624 | 624 |
them if you want to keep the original tables. |
625 | 625 |
@return dict(orig_col=new_col, ...) |
626 | 626 |
* orig_col: sql_gen.Col(orig_col_name, orig_table) |
627 |
* new_col: sql_gen.Col(orig_col_name, flat_table)
|
|
628 |
* All mappings use the flat_table table so its name can easily be
|
|
627 |
* new_col: sql_gen.Col(orig_col_name, into)
|
|
628 |
* All mappings use the into table so its name can easily be
|
|
629 | 629 |
changed for all columns at once |
630 | 630 |
''' |
631 | 631 |
flatten_mapping = {} |
632 | 632 |
for table in tables: |
633 | 633 |
for col in table_cols(db, table): |
634 | 634 |
col = sql_gen.as_Col(col, table) |
635 |
flatten_mapping[col] = sql_gen.Col(str(col), flat_table)
|
|
635 |
flatten_mapping[col] = sql_gen.Col(str(col), into)
|
|
636 | 636 |
for col in preserve: |
637 | 637 |
orig_col = copy.copy(col) |
638 |
col.table = flat_table
|
|
638 |
col.table = into
|
|
639 | 639 |
flatten_mapping[orig_col] = col |
640 | 640 |
return flatten_mapping |
641 | 641 |
|
642 |
def flatten(db, joins, flat_table, limit=None, start=None, **kw_args):
|
|
642 |
def flatten(db, into, joins, limit=None, start=None, **kw_args):
|
|
643 | 643 |
'''For params, see mk_flatten_mapping() |
644 | 644 |
@return See return value of mk_flatten_mapping() |
645 | 645 |
''' |
646 | 646 |
tables = joins[:1]+[v.table for v in joins[1:]] |
647 |
mapping = mk_flatten_mapping(db, tables, flat_table, **kw_args)
|
|
647 |
mapping = mk_flatten_mapping(db, into, tables, **kw_args)
|
|
648 | 648 |
cols = [sql_gen.NamedCol(new.name, old) for old, new in mapping.iteritems()] |
649 | 649 |
run_query_into(db, *mk_select(db, joins, cols, limit=limit, start=start), |
650 |
into=flat_table)
|
|
650 |
into=into)
|
|
651 | 651 |
return mapping |
652 | 652 |
|
653 | 653 |
def pkey(db, table, recover=None): |
Also available in: Unified diff
sql.py: mk_flatten_mapping(), flatten(): Renamed flat_table param to into to be consistent with run_query_into() and put it first because it is the output param