Project

General

Profile

« Previous | Next » 

Revision 2133

sql.py: put_table(): Return the column where the pkeys are made available (the out_pkey) instead of taking it as an argument

View differences:

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, 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 643
    @param in_tables The main input table to select from, followed by a list of
644 644
        tables to join with it using the main input table's pkey
645
    @return Name of the table where the pkeys (from INSERT RETURNING) are made
645
    @return (table, col) Where the pkeys (from INSERT RETURNING) are made
646 646
        available
647 647
    '''
648 648
    out_table_clean = clean_name(out_table)
649 649
    pkeys = out_table_clean+'_pkeys'
650 650
    
651
    out_pkey = pkey(db, out_table, recover=True, table_is_esc=table_is_esc)
652
    
651 653
    # Join together input tables
652 654
    in_tables = in_tables[:] # don't modify input!
653 655
    in_tables0 = in_tables.pop(0) # first table is separate
......
660 662
    out_pkeys = out_table_clean+'_out_pkeys'
661 663
    def insert_():
662 664
        cur = insert_select(db, out_table, mapping.keys(),
663
            *mk_select_(mapping.values()), returning=pkey_,
665
            *mk_select_(mapping.values()), returning=out_pkey,
664 666
            into=out_pkeys, recover=True, table_is_esc=table_is_esc)
665 667
        if row_ct_ref != None and cur.rowcount >= 0:
666 668
            row_ct_ref[0] += cur.rowcount
......
674 676
        # Join together out_pkeys and in_pkeys
675 677
        run_query_into(db, *mk_select(db,
676 678
            [in_pkeys, (out_pkeys, {row_num_col: join_using})],
677
            [in_pkey, pkey_]), into=pkeys)
679
            [in_pkey, out_pkey]), into=pkeys)
678 680
    
679 681
    try:
680 682
        # Insert and capture output pkeys
681 683
        with_parsed_errors(db, insert_)
682 684
        
683
        return pkeys
685
        return (pkeys, out_pkey)
684 686
    except DuplicateKeyException, e: raise
685 687

  
686 688
##### Data cleanup

Also available in: Unified diff