Project

General

Profile

« Previous | Next » 

Revision 2131

sql.py: put_table(): Fully support multiple in_tables, joined together using the main input table's pkey

View differences:

lib/sql.py
636 636
        if not create: raise
637 637
        return put(db, table, row, pkey, row_ct_ref) # insert new row
638 638

  
639
def put_table(db, out_table, in_tables, mapping, pkey, row_ct_ref=None,
639
def put_table(db, out_table, in_tables, mapping, pkey_, row_ct_ref=None,
640 640
    table_is_esc=False):
641 641
    '''Recovers from errors.
642 642
    Only works under PostgreSQL (uses INSERT RETURNING).
643
    @param in_tables The main input table to select from, followed by a list of
644
        tables to join with it using the main input table's pkey
643 645
    @return Name of the table where the pkeys (from INSERT RETURNING) are made
644 646
        available
645 647
    '''
646 648
    pkeys_table = clean_name(out_table)+'_pkeys'
649
    
650
    in_tables = in_tables[:] # don't modify input!
651
    in_tables0 = in_tables.pop(0) # first table is separate
652
    in_pkey = pkey(db, in_tables0, recover=True, table_is_esc=table_is_esc)
653
    joins = [in_tables0] + [(t, {in_pkey: join_using}) for t in in_tables]
654
    
647 655
    def insert_():
648 656
        return insert_select(db, out_table, mapping.keys(), *mk_select(db,
649
            in_tables[0], mapping.values(), table_is_esc=table_is_esc),
650
            returning=pkey, into=pkeys_table, recover=True,
657
            joins, mapping.values(), table_is_esc=table_is_esc),
658
            returning=pkey_, into=pkeys_table, recover=True,
651 659
            table_is_esc=table_is_esc)
652 660
    try:
653 661
        cur = with_parsed_errors(db, insert_)

Also available in: Unified diff