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:

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, 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
lib/db_xml.py
132 132
        put()'s node param, but with the input column name prefixed by "$" in
133 133
        place of the column value.
134 134
    @param commit Whether to commit after each query
135
    @return tuple(table, col) Where the pkeys (from INSERT RETURNING) are made
135
    @return (table, col) Where the pkeys (from INSERT RETURNING) are made
136 136
        available
137 137
    '''
138 138
    def esc_name(name): return sql.esc_name(db, name)
......
143 143
        return put_table(db, node, in_table, in_schema, commit, row_ct_ref)
144 144
    
145 145
    out_table = name_of(node)
146
    pkey_ = pkey(out_table)
147 146
    row = {}
148 147
    children = []
149 148
    in_tables = [qual_name(in_table)]
......
161 160
                row[child_name] = '$'+col
162 161
                in_tables.append(table)
163 162
            else: children.append(child)
164
    try: del row[pkey_]
163
    try: del row[pkey(out_table)]
165 164
    except KeyError: pass
166 165
    
167 166
    # Divide fields into input columns and literal values
......
171 170
        else: row[out_col] = (value, out_col) # value is literal value
172 171
    
173 172
    # Insert node
174
    pkeys_table = sql.put_table(db, esc_name(out_table), in_tables, row, pkey_,
173
    pkeys_loc = sql.put_table(db, esc_name(out_table), in_tables, row,
175 174
        row_ct_ref, table_is_esc=True)
176 175
    
177 176
    if commit: db.db.commit()
178
    return (pkeys_table, pkey_)
177
    return pkeys_loc

Also available in: Unified diff