Project

General

Profile

« Previous | Next » 

Revision 4484

sql_io.py: put_table(): Creating the into table: Fixed bug where in_pkey and out_pkey names would collide if the output and input pkeys have the same name (as is the case for SALVIAS.projects). This entails changing out_pkey to new into_out_pkey wherever the into table's out_pkey is created or referenced.

View differences:

lib/sql_io.py
315 315
    if not is_literals:
316 316
        into = sql_gen.as_Table(into_table_name(out_table, in_tables0, mapping,
317 317
            is_function))
318
        # Ensure into's out_pkey is different from in_pkey by prepending table
319
        into_out_pkey = strings.ustr(out_pkey_col)
318 320
        
319 321
        # Set column sources
320 322
        in_cols = filter(sql_gen.is_table_col, mapping.values())
......
355 357
        sql.run_query_into(db, sql.mk_select(db, in_table, full_in_table_cols,
356 358
            order_by=None), into=full_in_table, add_pkey_=True)
357 359
    
358
    if not is_literals:
359
        pkeys_names = [in_pkey, out_pkey]
360
        pkeys_cols = [in_pkey_col, out_pkey_col]
361
    
362 360
    pkeys_table_exists_ref = [False]
363 361
    def insert_into_pkeys(joins, cols=None, limit=None, **kw_args):
364 362
        query = sql.mk_select(db, joins, cols, order_by=None, limit=limit)
365 363
        if pkeys_table_exists_ref[0]:
366
            sql.insert_select(db, into, pkeys_names, query, **kw_args)
364
            sql.insert_select(db, into, [in_pkey, into_out_pkey], query,
365
                **kw_args)
367 366
        else:
368 367
            sql.run_query_into(db, query, into=into, add_pkey_=True, **kw_args)
369 368
            pkeys_table_exists_ref[0] = True
......
454 453
    
455 454
    def mk_func_call():
456 455
        args = dict(((k.name, v) for k, v in mapping.iteritems()))
457
        func_call = sql_gen.NamedCol(out_pkey,
458
            sql_gen.FunctionCall(out_table, **args))
459
        return func_call, args
456
        return sql_gen.FunctionCall(out_table, **args), args
460 457
        
461 458
    if is_function and not is_literals:
462 459
        log_debug('Defining wrapper function')
463 460
        
464 461
        func_call, args = mk_func_call()
462
        func_call = sql_gen.NamedCol(into_out_pkey, func_call)
465 463
        
466 464
        # Create empty pkeys table so its row type can be used
467 465
        insert_into_pkeys(input_joins, [in_pkey_col, func_call], limit=0,
468 466
            recover=True)
469
        result_type = db.col_info(sql_gen.Col(out_pkey, into)).type
467
        result_type = db.col_info(sql_gen.Col(into_out_pkey, into)).type
470 468
        
471 469
        ## Create error handling wrapper function
472 470
        
......
623 621
    elif has_joins:
624 622
        select_joins = input_joins+[sql_gen.Join(out_table, join_cols)]
625 623
        log_debug('Getting output table pkeys of existing/inserted rows')
626
        insert_into_pkeys(select_joins, pkeys_cols)
624
        insert_into_pkeys(select_joins, [in_pkey_col,
625
            sql_gen.NamedCol(into_out_pkey, out_pkey_col)])
627 626
    else:
628 627
        sql.add_row_num(db, insert_out_pkeys) # for joining with input pkeys
629 628
        
......
641 640
        log_debug('Combining output and input pkeys in inserted order')
642 641
        pkey_joins = [insert_in_pkeys, sql_gen.Join(insert_out_pkeys,
643 642
            {sql.row_num_col: sql_gen.join_same_not_null})]
644
        insert_into_pkeys(pkey_joins, [sql_gen.Col(in_pkey, insert_in_pkeys),
645
            sql_gen.Col(out_pkey, insert_out_pkeys)])
643
        in_col = sql_gen.Col(in_pkey, insert_in_pkeys)
644
        out_col = sql_gen.NamedCol(into_out_pkey,
645
            sql_gen.Col(out_pkey, insert_out_pkeys))
646
        insert_into_pkeys(pkey_joins, [in_col, out_col])
646 647
        
647 648
        sql.empty_temp(db, [insert_out_pkeys, insert_in_pkeys])
648 649
    
......
654 655
            # must use join_same_not_null or query will take forever
655 656
        insert_into_pkeys(missing_rows_joins,
656 657
            [sql_gen.Col(in_pkey, full_in_table),
657
            sql_gen.NamedCol(out_pkey, default)])
658
            sql_gen.NamedCol(into_out_pkey, default)])
658 659
    # otherwise, there is already an entry for every row
659 660
    
660 661
    assert (sql.table_row_count(db, into)
......
664 665
    
665 666
    srcs = []
666 667
    if is_function: srcs = sql_gen.cols_srcs(in_cols)
667
    return sql_gen.Col(out_pkey, into, srcs)
668
    return sql_gen.Col(into_out_pkey, into, srcs)

Also available in: Unified diff