Project

General

Profile

« Previous | Next » 

Revision 3034

sql.py: mk_insert_select(): INSERT IGNORE: Switched to using FOR loop rather than cursors because cursors are only needed if you want to process multiple rows in the same EXCEPTION block (which you can't do because then all the previous inserts will be rolled back if one row is a duplicate key)

View differences:

lib/sql.py
668 668
        query = '''\
669 669
DECLARE
670 670
    row '''+table.to_str(db)+'''%ROWTYPE;
671
    cur CURSOR FOR
672
'''+select_query+'''
673
;
674 671
BEGIN
675
    OPEN cur;
676
    
677 672
    /* Need an EXCEPTION block for each individual row because "When an error is
678 673
    caught by an EXCEPTION clause, [...] all changes to persistent database
679 674
    state within the block are rolled back."
......
682 677
    (http://www.postgresql.org/docs/8.3/static/plpgsql-control-structures.html\
683 678
#PLPGSQL-ERROR-TRAPPING)
684 679
    */
685
    WHILE true LOOP
686
        FETCH FROM cur INTO \
687
'''+(', '.join((c.to_str(db) for c in row)))+''';
688
        EXIT WHEN NOT FOUND;
689
        
680
    FOR '''+(', '.join((c.to_str(db) for c in row)))+''' IN
681
'''+select_query+'''
682
    LOOP
690 683
        BEGIN
691 684
            RETURN QUERY
692 685
'''+mk_insert(sql_gen.Values(row).to_str(db))+'''
......
695 688
            WHEN unique_violation THEN NULL; -- continue to next row
696 689
        END;
697 690
    END LOOP;
698
    
699
    CLOSE cur;
700 691
END;\
701 692
'''
702 693
    else: query = mk_insert(select_query)

Also available in: Unified diff