Revision 2303
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
768 | 768 |
out_pkeys_ref = [temp_prefix+'_out_pkeys'] |
769 | 769 |
in_pkeys_ref = [temp_prefix+'_in_pkeys'] |
770 | 770 |
while True: |
771 |
has_joins = join_cols != {} |
|
772 |
|
|
773 |
if has_joins: |
|
774 |
select_joins = insert_joins+[sql_gen.Join(out_table, join_cols)] |
|
775 |
db.log_debug('Getting pkeys of already existing rows') |
|
776 |
run_query_into_pkeys(*mk_select(db, select_joins, pkeys_cols, |
|
777 |
start=0)) |
|
778 |
|
|
779 |
# Prepare to insert new rows |
|
780 |
input_join_cols = join_cols.values() |
|
781 |
distinct_on = filter(util.is_str, input_join_cols) |
|
782 |
if not filter_join_added: |
|
783 |
insert_joins.append(sql_gen.Join(pkeys_ref[0], |
|
784 |
{in_pkey: in_pkey}, sql_gen.filter_out)) |
|
785 |
filter_join_added = True |
|
786 |
|
|
771 | 787 |
try: |
772 |
if join_cols != {}: |
|
773 |
select_joins = insert_joins+[sql_gen.Join(out_table, join_cols)] |
|
774 |
db.log_debug('Getting pkeys of already existing rows') |
|
775 |
run_query_into_pkeys(*mk_select(db, select_joins, pkeys_cols, |
|
776 |
start=0)) |
|
777 |
|
|
778 |
# Prepare to insert new rows |
|
779 |
input_join_cols = join_cols.values() |
|
780 |
distinct_on = filter(util.is_str, input_join_cols) |
|
781 |
if not filter_join_added: |
|
782 |
insert_joins.append(sql_gen.Join(pkeys_ref[0], |
|
783 |
{in_pkey: in_pkey}, sql_gen.filter_out)) |
|
784 |
filter_join_added = True |
|
785 |
|
|
786 | 788 |
db.log_debug('Inserting new rows') |
787 | 789 |
cur = insert_select(db, out_table, mapping.keys(), |
788 | 790 |
*mk_main_select(mapping.values()), returning=out_pkey, |
789 | 791 |
into_ref=out_pkeys_ref, recover=True) |
790 |
if row_ct_ref != None and cur.rowcount >= 0: |
|
791 |
row_ct_ref[0] += cur.rowcount |
|
792 |
add_row_num(db, out_pkeys_ref[0]) # for joining with input pkeys |
|
793 |
|
|
794 |
db.log_debug('Getting input pkeys corresponding to rows in insert') |
|
795 |
run_query_into(db, *mk_main_select([in_pkey]), |
|
796 |
into_ref=in_pkeys_ref) |
|
797 |
add_row_num(db, in_pkeys_ref[0]) # for joining with output pkeys |
|
798 |
|
|
799 |
db.log_debug('Joining together output and input pkeys') |
|
800 |
run_query_into_pkeys(*mk_select(db, [in_pkeys_ref[0], |
|
801 |
sql_gen.Join(out_pkeys_ref[0], |
|
802 |
{row_num_col: sql_gen.join_using})], pkeys, start=0)) |
|
803 |
|
|
804 |
break # insert successful |
|
805 | 792 |
except DuplicateKeyException, e: |
806 | 793 |
old_join_cols = join_cols.copy() |
807 | 794 |
join_cols.update(util.dict_subset_right_join(mapping, e.cols)) |
... | ... | |
828 | 815 |
log_ignore(in_col, value) |
829 | 816 |
conds[in_col] = sql_gen.CompareCond(value, '!=') |
830 | 817 |
# rerun loop with additional constraints |
818 |
else: |
|
819 |
if row_ct_ref != None and cur.rowcount >= 0: |
|
820 |
row_ct_ref[0] += cur.rowcount |
|
821 |
add_row_num(db, out_pkeys_ref[0]) # for joining with input pkeys |
|
822 |
|
|
823 |
db.log_debug('Getting input pkeys corresponding to rows in insert') |
|
824 |
run_query_into(db, *mk_main_select([in_pkey]), |
|
825 |
into_ref=in_pkeys_ref) |
|
826 |
add_row_num(db, in_pkeys_ref[0]) # for joining with output pkeys |
|
827 |
|
|
828 |
db.log_debug('Joining together output and input pkeys') |
|
829 |
run_query_into_pkeys(*mk_select(db, [in_pkeys_ref[0], |
|
830 |
sql_gen.Join(out_pkeys_ref[0], |
|
831 |
{row_num_col: sql_gen.join_using})], pkeys, start=0)) |
|
832 |
|
|
833 |
break # insert successful |
|
831 | 834 |
|
832 | 835 |
return sql_gen.Col(out_pkey, pkeys_ref[0]) |
833 | 836 |
|
Also available in: Unified diff
sql.py: put_table(): Moved things outside of the try clause which should not produce the exceptions