Project

General

Profile

« Previous | Next » 

Revision 2257

sql.py: put_table(): Moved getting pkeys of already existing rows from DuplicateKeyException to try clause, so that it always runs if there are join_cols. DuplicateKeyException: Add new duplicate key cols to join_cols instead of replacing join_cols so that multiple unique constraints being violated causes the union of their columns to be used for join_cols.

View differences:

lib/sql.py
814 814
        db.log_debug('Ignoring rows with '+in_col+' = '+value)
815 815
    
816 816
    # Do inserts and selects
817
    join_cols = {}
818
    filter_join_added = False
817 819
    out_pkeys_ref = [temp_prefix+'_out_pkeys']
818 820
    in_pkeys_ref = [temp_prefix+'_in_pkeys']
819 821
    while True:
820 822
        try:
823
            if join_cols != {}:
824
                select_joins = insert_joins + [(out_table, join_cols)]
825
                db.log_debug('Getting pkeys of already existing rows')
826
                run_query_into_pkeys(*mk_select(db, select_joins, pkeys_cols,
827
                    order_by=None, start=0, table_is_esc=table_is_esc))
828
                
829
                # Prepare to insert new rows
830
                input_join_cols = join_cols.values()
831
                distinct_on = filter(util.is_str, input_join_cols)
832
                if not filter_join_added:
833
                    insert_joins.append((pkeys_ref[0], {in_pkey: filter_out}))
834
                    filter_join_added = True
835
            
821 836
            db.log_debug('Inserting new rows')
822 837
            cur = insert_select(db, out_table, mapping.keys(),
823 838
                *mk_main_select(mapping.values()), returning=out_pkey,
......
837 852
            
838 853
            break # insert successful
839 854
        except DuplicateKeyException, e:
840
            join_cols = util.dict_subset_right_join(mapping, e.cols)
855
            join_cols.update(util.dict_subset_right_join(mapping, e.cols))
841 856
            db.log_debug('Ignoring existing rows, comparing on '+str(join_cols))
842
            select_joins = insert_joins + [(out_table, join_cols)]
843
            
844
            db.log_debug('Getting pkeys of already existing rows')
845
            run_query_into_pkeys(*mk_select(db, select_joins, pkeys_cols,
846
                order_by=None, start=0, table_is_esc=table_is_esc))
847
            
848
            db.log_debug('Saving existing pkeys in temp table for joining on')
849
            existing_pkeys_ref = [temp_prefix+'_existing_pkeys']
850
            run_query_into(db, *mk_select(db, pkeys_ref[0], [in_pkey],
851
                order_by=None, start=0, table_is_esc=True),
852
                into_ref=existing_pkeys_ref)
853
                # need table_is_esc=True to make table name case-insensitive
854
            
855
            # Insert new rows
856
            input_join_cols = join_cols.values()
857
            distinct_on = filter(util.is_str, input_join_cols)
858
            insert_joins.append((existing_pkeys_ref[0], {in_pkey: filter_out}))
859
            
860 857
            # rerun loop with additional constraints
861 858
        except NullValueException, e:
862 859
            out_col, = e.cols

Also available in: Unified diff