Project

General

Profile

« Previous | Next » 

Revision 5726

sql_io.py: put_table(): is_function: Moved definition of wrapper function inside try block of main loop because the creation of the empty pkeys table (whose row type is needed for the wrapper function) can itself produce MissingCastExceptions, which must be thrown inside the loop in order to be handled properly

View differences:

sql_io.py
536 536
    def mk_func_call():
537 537
        args = dict(((k.name, v) for k, v in mapping.iteritems()))
538 538
        return sql_gen.FunctionCall(out_table, **args), args
539
        
540
    if is_function and not is_literals:
541
        log_debug('Defining wrapper function')
542
        
543
        func_call, args = mk_func_call()
544
        func_call = sql_gen.NamedCol(into_out_pkey, func_call)
545
        
546
        # Create empty pkeys table so its row type can be used
547
        insert_into_pkeys(sql.mk_select(db, input_joins,
548
            [in_pkey_col, func_call], limit=0), recover=True)
549
        result_type = db.col_info(sql_gen.Col(into_out_pkey, into)).type
550
        
551
        ## Create error handling wrapper function
552
        
553
        wrapper = db.TempFunction(sql_gen.concat(into.name, '_wrap'))
554
        
555
        select_cols = [in_pkey_col]+args.values()
556
        row_var = copy.copy(sql_gen.row_var)
557
        row_var.set_srcs([in_table])
558
        in_pkey_var = sql_gen.Col(in_pkey, row_var)
559
        
560
        args = dict(((k, sql_gen.with_table(v, row_var))
561
            for k, v in args.iteritems()))
562
        func_call = sql_gen.FunctionCall(out_table, **args)
563
        
564
        def mk_return(result):
565
            return sql_gen.ReturnQuery(sql.mk_select(db,
566
                fields=[in_pkey_var, result], explain=False))
567
        exc_handler = func_wrapper_exception_handler(db,
568
            mk_return(sql_gen.Cast(result_type, None)), args.values(),
569
            errors_table_)
570
        
571
        sql.define_func(db, sql_gen.FunctionDef(wrapper, sql_gen.SetOf(into),
572
            sql_gen.RowExcIgnore(sql_gen.RowType(in_table),
573
                sql.mk_select(db, input_joins), mk_return(func_call),
574
                exc_handler=exc_handler)
575
            ))
576
        wrapper_table = sql_gen.FunctionCall(wrapper)
577 539
    
578 540
    missing_msg = None
579 541
    
......
585 547
        
586 548
        # Prepare to insert new rows
587 549
        if is_function:
588
            log_debug('Calling function on input rows')
589
            if is_literals: func_call, args = mk_func_call()
550
            if is_literals:
551
                log_debug('Calling function')
552
                func_call, args = mk_func_call()
590 553
        else:
591 554
            log_debug('Trying to insert new rows')
592 555
            insert_args = dict(recover=True, cacheable=False)
......
606 569
                    cur = sql.select(db, fields=[func_call], recover=True,
607 570
                        cacheable=True)
608 571
                else:
572
                    log_debug('Defining wrapper function')
573
                    
574
                    func_call, args = mk_func_call()
575
                    func_call = sql_gen.NamedCol(into_out_pkey, func_call)
576
                    
577
                    # Create empty pkeys table so its row type can be used
578
                    insert_into_pkeys(sql.mk_select(db, input_joins,
579
                        [in_pkey_col, func_call], limit=0), recover=True)
580
                    result_type = db.col_info(sql_gen.Col(into_out_pkey,
581
                        into)).type
582
                    
583
                    ## Create error handling wrapper function
584
                    
585
                    wrapper = db.TempFunction(sql_gen.concat(into.name,
586
                        '_wrap'))
587
                    
588
                    select_cols = [in_pkey_col]+args.values()
589
                    row_var = copy.copy(sql_gen.row_var)
590
                    row_var.set_srcs([in_table])
591
                    in_pkey_var = sql_gen.Col(in_pkey, row_var)
592
                    
593
                    args = dict(((k, sql_gen.with_table(v, row_var))
594
                        for k, v in args.iteritems()))
595
                    func_call = sql_gen.FunctionCall(out_table, **args)
596
                    
597
                    def mk_return(result):
598
                        return sql_gen.ReturnQuery(sql.mk_select(db,
599
                            fields=[in_pkey_var, result], explain=False))
600
                    exc_handler = func_wrapper_exception_handler(db,
601
                        mk_return(sql_gen.Cast(result_type, None)),
602
                        args.values(), errors_table_)
603
                    
604
                    sql.define_func(db, sql_gen.FunctionDef(wrapper,
605
                        sql_gen.SetOf(into),
606
                        sql_gen.RowExcIgnore(sql_gen.RowType(in_table),
607
                            sql.mk_select(db, input_joins),
608
                            mk_return(func_call), exc_handler=exc_handler)
609
                        ))
610
                    wrapper_table = sql_gen.FunctionCall(wrapper)
611
                    
612
                    log_debug('Calling function')
609 613
                    insert_into_pkeys(sql.mk_select(db, wrapper_table,
610 614
                        order_by=None), recover=True)
611 615
            else:

Also available in: Unified diff