Project

General

Profile

« Previous | Next » 

Revision 2201

sql.py: put_table(): Factored out code that inserts into pkeys table into run_query_into_pkeys() helper function

View differences:

lib/sql.py
778 778
    out_pkey = pkey(db, out_table, recover=True, table_is_esc=table_is_esc)
779 779
    pkeys_cols = [in_pkey, out_pkey]
780 780
    
781
    pkeys_table_exists_ref = [False]
782
    def run_query_into_pkeys(query, params):
783
        if pkeys_table_exists_ref[0]:
784
            insert_select(db, pkeys_ref[0], pkeys_cols, query, params)
785
        else:
786
            run_query_into(db, query, params, into_ref=pkeys_ref)
787
            pkeys_table_exists_ref[0] = True
788
    
781 789
    def mk_select_(cols):
782 790
        return mk_select(db, insert_joins, cols, limit=limit, start=start,
783 791
            table_is_esc=table_is_esc)
784 792
    
785 793
    out_pkeys_ref = ['out_pkeys_'+temp_suffix]
786
    def insert_(pkeys_table_exists=False):
794
    def insert_():
787 795
        '''Inserts and capture output pkeys.'''
788 796
        cur = insert_select(db, out_table, mapping.keys(),
789 797
            *mk_select_(mapping.values()), returning=out_pkey,
......
798 806
        add_row_num(db, in_pkeys_ref[0]) # for joining it with output pkeys
799 807
        
800 808
        # Join together output and input pkeys
801
        select_query = mk_select(db, [in_pkeys_ref[0],
802
            (out_pkeys_ref[0], {row_num_col: join_using})], pkeys_cols, start=0)
803
        if pkeys_table_exists:
804
            insert_select(db, pkeys_ref[0], pkeys_cols, *select_query)
805
        else: run_query_into(db, *select_query, into_ref=pkeys_ref)
809
        run_query_into_pkeys(*mk_select(db, [in_pkeys_ref[0], (out_pkeys_ref[0],
810
            {row_num_col: join_using})], pkeys_cols, start=0))
806 811
    
807 812
    # Do inserts and selects
808 813
    try: insert_()
......
811 816
        select_joins = insert_joins + [(out_table, join_cols)]
812 817
        
813 818
        # Get pkeys of already existing rows
814
        run_query_into(db, *mk_select(db, select_joins, pkeys_cols,
815
            order_by=None, start=0, table_is_esc=table_is_esc),
816
            into_ref=pkeys_ref, recover=True)
819
        run_query_into_pkeys(*mk_select(db, select_joins, pkeys_cols,
820
            order_by=None, start=0, table_is_esc=table_is_esc))
817 821
        
818 822
        # Save existing pkeys in temp table for joining on
819 823
        existing_pkeys_ref = ['existing_pkeys_'+temp_suffix]

Also available in: Unified diff