Project

General

Profile

« Previous | Next » 

Revision 2305

sql.py: put_table(): If there are join_cols, don't get output pkeys of inserted rows and instead select all rows (existing and inserted) after the insert

View differences:

lib/sql.py
770 770
    while True:
771 771
        has_joins = join_cols != {}
772 772
        
773
        # Prepare to insert new rows
773 774
        if has_joins:
774
            select_joins = insert_joins+[sql_gen.Join(out_table, join_cols)]
775
            db.log_debug('Getting pkeys of already existing rows')
776
            run_query_into_pkeys(*mk_select(db, select_joins, pkeys_cols,
777
                start=0))
778
            
779
            # Prepare to insert new rows
780
            input_join_cols = join_cols.values()
781
            distinct_on = filter(util.is_str, input_join_cols)
775
            distinct_on = filter(util.is_str, join_cols.values())
782 776
            if not filter_join_added:
783
                insert_joins.append(sql_gen.Join(pkeys_ref[0],
784
                    {in_pkey: in_pkey}, sql_gen.filter_out))
777
                insert_joins.append(sql_gen.Join(out_table, join_cols,
778
                    sql_gen.filter_out))
785 779
                filter_join_added = True
780
            returning = None
781
            into_ref = None
782
        else:
783
            returning = out_pkey
784
            into_ref = out_pkeys_ref
786 785
        
786
        db.log_debug('Inserting new rows')
787 787
        try:
788
            db.log_debug('Inserting new rows')
789 788
            cur = insert_select(db, out_table, mapping.keys(),
790
                *mk_main_select(mapping.values()), returning=out_pkey,
791
                into_ref=out_pkeys_ref, recover=True)
789
                *mk_main_select(mapping.values()), returning=returning,
790
                into_ref=into_ref, recover=True)
792 791
        except DuplicateKeyException, e:
793 792
            old_join_cols = join_cols.copy()
794 793
            join_cols.update(util.dict_subset_right_join(mapping, e.cols))
......
820 819
                row_ct_ref[0] += cur.rowcount
821 820
                add_row_num(db, out_pkeys_ref[0]) # for joining with input pkeys
822 821
            
823
            db.log_debug('Getting input pkeys corresponding to rows in insert')
824
            run_query_into(db, *mk_main_select([in_pkey]),
825
                into_ref=in_pkeys_ref)
826
            add_row_num(db, in_pkeys_ref[0]) # for joining with output pkeys
822
            if has_joins:
823
                select_joins = insert_joins+[sql_gen.Join(out_table, join_cols)]
824
                db.log_debug('Getting output pkeys of existing/inserted rows')
825
                run_query_into_pkeys(*mk_select(db, select_joins, pkeys_cols,
826
                    start=0))
827
            else:
828
                db.log_debug('Getting input pkeys for rows in insert')
829
                run_query_into(db, *mk_main_select([in_pkey]),
830
                    into_ref=in_pkeys_ref)
831
                add_row_num(db, in_pkeys_ref[0]) # for joining with output pkeys
832
                
833
                db.log_debug('Joining together output and input pkeys')
834
                run_query_into_pkeys(*mk_select(db, [in_pkeys_ref[0],
835
                    sql_gen.Join(out_pkeys_ref[0],
836
                    {row_num_col: sql_gen.join_using})], pkeys, start=0))
827 837
            
828
            db.log_debug('Joining together output and input pkeys')
829
            run_query_into_pkeys(*mk_select(db, [in_pkeys_ref[0],
830
                sql_gen.Join(out_pkeys_ref[0],
831
                {row_num_col: sql_gen.join_using})], pkeys, start=0))
832
            
833 838
            break # insert successful
834 839
    
835 840
    return sql_gen.Col(out_pkey, pkeys_ref[0])

Also available in: Unified diff