Project

General

Profile

« Previous | Next » 

Revision 3015

sql.py: mk_insert_select(): INSERT IGNORE: Added duplicate key handling by using EXCEPTION block to catch unique_violation

View differences:

sql.py
665 665
;
666 666
BEGIN
667 667
    OPEN cur;
668
    
669
    /* Use extra loop inside EXCEPTION because the EXCEPTION block is expensive:
670
    "A block containing an EXCEPTION clause is significantly more expensive to
671
    enter and exit than a block without one."
672
    (http://www.postgresql.org/docs/8.3/static/plpgsql-control-structures.html\
673
#PLPGSQL-ERROR-TRAPPING)
674
    */
675
    <<outer>>
668 676
    WHILE true LOOP
669
        FETCH FROM cur INTO '''+(', '.join((c.to_str(db) for c in row)))+''';
670
        EXIT WHEN NOT FOUND;
671
        
677
        BEGIN
678
            WHILE true LOOP
679
                FETCH FROM cur INTO \
680
'''+(', '.join((c.to_str(db) for c in row)))+''';
681
                EXIT outer WHEN NOT FOUND;
682
                
672 683
'''+mk_insert(sql_gen.Values(row).to_str(db))+'''
673 684
;
685
            END LOOP;
686
        EXCEPTION
687
            WHEN unique_violation THEN NULL; -- outer loop continues to next row
688
        END;
674 689
    END LOOP;
690
    
675 691
    CLOSE cur;
676 692
END;\
677 693
'''

Also available in: Unified diff