Project

General

Profile

« Previous | Next » 

Revision 2132

sql.py: put_table(): Get input pkeys corresponding to rows in insert and join together out_pkeys and in_pkeys into final pkeys table

View differences:

lib/sql.py
645 645
    @return Name of the table where the pkeys (from INSERT RETURNING) are made
646 646
        available
647 647
    '''
648
    pkeys_table = clean_name(out_table)+'_pkeys'
648
    out_table_clean = clean_name(out_table)
649
    pkeys = out_table_clean+'_pkeys'
649 650
    
651
    # Join together input tables
650 652
    in_tables = in_tables[:] # don't modify input!
651 653
    in_tables0 = in_tables.pop(0) # first table is separate
652 654
    in_pkey = pkey(db, in_tables0, recover=True, table_is_esc=table_is_esc)
653 655
    joins = [in_tables0] + [(t, {in_pkey: join_using}) for t in in_tables]
654 656
    
657
    def mk_select_(cols):
658
        return mk_select(db, joins, cols, table_is_esc=table_is_esc)
659
    
660
    out_pkeys = out_table_clean+'_out_pkeys'
655 661
    def insert_():
656
        return insert_select(db, out_table, mapping.keys(), *mk_select(db,
657
            joins, mapping.values(), table_is_esc=table_is_esc),
658
            returning=pkey_, into=pkeys_table, recover=True,
659
            table_is_esc=table_is_esc)
660
    try:
661
        cur = with_parsed_errors(db, insert_)
662
        cur = insert_select(db, out_table, mapping.keys(),
663
            *mk_select_(mapping.values()), returning=pkey_,
664
            into=out_pkeys, recover=True, table_is_esc=table_is_esc)
662 665
        if row_ct_ref != None and cur.rowcount >= 0:
663 666
            row_ct_ref[0] += cur.rowcount
667
        add_row_num(db, out_pkeys) # for joining it with in_pkeys
664 668
        
665
        # Add row_num to pkeys_table, so it can be joined with in_table's pkeys
666
        add_row_num(db, pkeys_table)
669
        # Get input pkeys corresponding to rows in insert
670
        in_pkeys = out_table_clean+'_in_pkeys'
671
        run_query_into(db, *mk_select_([in_pkey]), into=in_pkeys)
672
        add_row_num(db, in_pkeys) # for joining it with out_pkeys
667 673
        
668
        return pkeys_table
674
        # Join together out_pkeys and in_pkeys
675
        run_query_into(db, *mk_select(db,
676
            [in_pkeys, (out_pkeys, {row_num_col: join_using})],
677
            [in_pkey, pkey_]), into=pkeys)
678
    
679
    try:
680
        # Insert and capture output pkeys
681
        with_parsed_errors(db, insert_)
682
        
683
        return pkeys
669 684
    except DuplicateKeyException, e: raise
670 685

  
671 686
##### Data cleanup

Also available in: Unified diff