Revision 2325
Added by Aaron Marcuse-Kubitza over 12 years ago
sql.py | ||
---|---|---|
735 | 735 |
in_tables0 = in_tables.pop(0) # first table is separate |
736 | 736 |
in_pkey = pkey(db, in_tables0, recover=True) |
737 | 737 |
in_pkey_col = sql_gen.as_Col(in_pkey, in_tables0) |
738 |
insert_joins = [in_tables0]+[sql_gen.Join(v, {in_pkey: sql_gen.join_using})
|
|
738 |
input_joins = [in_tables0]+[sql_gen.Join(v, {in_pkey: sql_gen.join_using})
|
|
739 | 739 |
for v in in_tables] |
740 | 740 |
|
741 | 741 |
out_pkey = pkey(db, out_table, recover=True) |
... | ... | |
754 | 754 |
|
755 | 755 |
conds = {} |
756 | 756 |
distinct_on = [] |
757 |
def mk_main_select(cols): |
|
758 |
return mk_select(db, insert_joins, cols, conds, distinct_on,
|
|
759 |
limit=limit, start=start, default_table=in_tables0)
|
|
757 |
def mk_main_select(joins, cols):
|
|
758 |
return mk_select(db, joins, cols, conds, distinct_on, limit=limit,
|
|
759 |
start=start, default_table=in_tables0) |
|
760 | 760 |
|
761 | 761 |
def log_exc(e): |
762 | 762 |
db.log_debug('Caught exception: '+exc.str_(e, first_line_only=True)) |
... | ... | |
765 | 765 |
|
766 | 766 |
# Do inserts and selects |
767 | 767 |
join_cols = {} |
768 |
filter_join_added = False |
|
769 | 768 |
out_pkeys_ref = [temp_prefix+'_out_pkeys'] |
770 | 769 |
in_pkeys_ref = [temp_prefix+'_in_pkeys'] |
771 | 770 |
while True: |
772 | 771 |
has_joins = join_cols != {} |
773 | 772 |
|
774 | 773 |
# Prepare to insert new rows |
774 |
insert_joins = input_joins[:] # don't modify original! |
|
775 |
insert_args = dict(recover=True) |
|
775 | 776 |
if has_joins: |
776 |
insert_args = dict() |
|
777 | 777 |
distinct_on = [v.to_Col() for v in join_cols.values()] |
778 |
if not filter_join_added: |
|
779 |
insert_joins.append(sql_gen.Join(out_table, join_cols, |
|
780 |
sql_gen.filter_out)) |
|
781 |
filter_join_added = True |
|
782 |
else: insert_args = dict(returning=out_pkey, into_ref=out_pkeys_ref) |
|
778 |
insert_joins.append(sql_gen.Join(out_table, join_cols, |
|
779 |
sql_gen.filter_out)) |
|
780 |
else: |
|
781 |
insert_args.update(dict(returning=out_pkey, into_ref=out_pkeys_ref)) |
|
783 | 782 |
|
784 | 783 |
db.log_debug('Inserting new rows') |
785 | 784 |
try: |
786 | 785 |
cur = insert_select(db, out_table, mapping.keys(), |
787 |
*mk_main_select(mapping.values()), recover=True, **insert_args)
|
|
786 |
*mk_main_select(insert_joins, mapping.values()), **insert_args)
|
|
788 | 787 |
except DuplicateKeyException, e: |
789 | 788 |
log_exc(e) |
790 | 789 |
|
... | ... | |
823 | 822 |
row_ct_ref[0] += cur.rowcount |
824 | 823 |
|
825 | 824 |
if has_joins: |
826 |
select_joins = insert_joins+[sql_gen.Join(out_table, join_cols)]
|
|
825 |
select_joins = input_joins+[sql_gen.Join(out_table, join_cols)]
|
|
827 | 826 |
db.log_debug('Getting output pkeys of existing/inserted rows') |
828 | 827 |
run_query_into_pkeys(*mk_select(db, select_joins, pkeys_cols, |
829 | 828 |
start=0)) |
... | ... | |
831 | 830 |
add_row_num(db, out_pkeys_ref[0]) # for joining with input pkeys |
832 | 831 |
|
833 | 832 |
db.log_debug('Getting input pkeys for rows in insert') |
834 |
run_query_into(db, *mk_main_select([in_pkey]), |
|
833 |
run_query_into(db, *mk_main_select(input_joins, [in_pkey]),
|
|
835 | 834 |
into_ref=in_pkeys_ref) |
836 | 835 |
add_row_num(db, in_pkeys_ref[0]) # for joining with output pkeys |
837 | 836 |
|
Also available in: Unified diff
sql.py: put_table(): Fixed bug where the filter_out join should only be used in the insert, not in the select of existing/inserted rows. insert_select() call: Fixed compatibility bug where old versions of Python did not support mixing keyword args and ** args.