Revision 2393
Added by Aaron Marcuse-Kubitza about 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, into, tables, preserve=[]):
|
|
617 |
def mk_flatten_mapping(db, into, cols, 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. |
... | ... | |
629 | 629 |
changed for all columns at once |
630 | 630 |
''' |
631 | 631 |
flatten_mapping = {} |
632 |
for table in tables: |
|
633 |
for col in table_cols(db, table): |
|
634 |
col = sql_gen.as_Col(col, table) |
|
635 |
flatten_mapping[col] = sql_gen.Col(str(col), into) |
|
632 |
for col in cols: flatten_mapping[col] = sql_gen.Col(str(col), into) |
|
636 | 633 |
for col in preserve: |
637 | 634 |
orig_col = copy.copy(col) |
638 | 635 |
col.table = into |
639 | 636 |
flatten_mapping[orig_col] = col |
640 | 637 |
return flatten_mapping |
641 | 638 |
|
642 |
def flatten(db, into, joins, limit=None, start=None, **kw_args): |
|
639 |
def flatten(db, into, joins, cols, limit=None, start=None, **kw_args):
|
|
643 | 640 |
'''For params, see mk_flatten_mapping() |
644 | 641 |
@return See return value of mk_flatten_mapping() |
645 | 642 |
''' |
646 |
tables = joins[:1]+[v.table for v in joins[1:]] |
|
647 |
mapping = mk_flatten_mapping(db, into, tables, **kw_args) |
|
643 |
mapping = mk_flatten_mapping(db, into, cols, **kw_args) |
|
648 | 644 |
cols = [sql_gen.NamedCol(new.name, old) for old, new in mapping.iteritems()] |
649 | 645 |
run_query_into(db, *mk_select(db, joins, cols, limit=limit, start=start), |
650 | 646 |
into=into) |
Also available in: Unified diff
sql.py: mk_flatten_mapping(), flatten(): Take list of cols to select instead of using all cols in all tables to join