Project

General

Profile

« Previous | Next » 

Revision 3450

sql.py: mk_insert_select(): embeddable: Use new sql_gen.RowExcIgnore

View differences:

lib/sql.py
787 787
        
788 788
        embeddable = True # must use function
789 789
        
790
        if cols == None:
791
            row = [sql_gen.Col(sql_gen.all_cols, 'row')]
792
            row_vars = [sql_gen.Table('row')]
793
        else:
794
            row_vars = row = [sql_gen.Col(c.name, 'row') for c in cols]
790
        if cols == None: row = [sql_gen.Col(sql_gen.all_cols, 'row')]
791
        else: row = [sql_gen.Col(c.name, 'row') for c in cols]
795 792
        
796
        query = sql_gen.CustomCode('''\
797
DECLARE
798
    row '''+table.to_str(db)+'''%ROWTYPE;
799
BEGIN
800
    /* Need an EXCEPTION block for each individual row because "When an error is
801
    caught by an EXCEPTION clause, [...] all changes to persistent database
802
    state within the block are rolled back."
803
    This is unfortunate because "A block containing an EXCEPTION clause is
804
    significantly more expensive to enter and exit than a block without one."
805
    (http://www.postgresql.org/docs/8.3/static/plpgsql-control-structures.html\
806
#PLPGSQL-ERROR-TRAPPING)
807
    */
808
    FOR '''+(', '.join((v.to_str(db) for v in row_vars)))+''' IN
809
'''+select_query+'''
810
    LOOP
811
        BEGIN
812
            RETURN QUERY
813
'''+mk_insert(sql_gen.Values(row).to_str(db))+'''
814
;
815
        EXCEPTION
816
            WHEN unique_violation THEN NULL; -- continue to next row
817
        END;
818
    END LOOP;
819
END;\
820
''')
821
        query.lang = 'plpgsql'
793
        query = sql_gen.RowExcIgnore(table.to_str(db)+'%ROWTYPE', select_query,
794
            mk_insert(sql_gen.Values(row).to_str(db)), cols)
822 795
    else: query = mk_insert(select_query)
823 796
    
824 797
    if embeddable:

Also available in: Unified diff