Revision 2387
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
774 | 774 |
assert isinstance(in_table_col, sql_gen.Col) |
775 | 775 |
|
776 | 776 |
temp_prefix = out_table.name |
777 |
pkeys_ref = sql_gen.Table(temp_prefix+'_pkeys')
|
|
777 |
pkeys = sql_gen.Table(temp_prefix+'_pkeys') |
|
778 | 778 |
|
779 | 779 |
# Create input joins from list of input tables |
780 | 780 |
in_tables_ = in_tables[:] # don't modify input! |
... | ... | |
787 | 787 |
out_pkey = pkey(db, out_table, recover=True) |
788 | 788 |
out_pkey_col = sql_gen.as_Col(out_pkey, out_table) |
789 | 789 |
|
790 |
pkeys = [in_pkey, out_pkey] |
|
790 |
pkeys_names = [in_pkey, out_pkey]
|
|
791 | 791 |
pkeys_cols = [in_pkey_col, out_pkey_col] |
792 | 792 |
|
793 | 793 |
pkeys_table_exists_ref = [False] |
794 | 794 |
def run_query_into_pkeys(query, params): |
795 | 795 |
if pkeys_table_exists_ref[0]: |
796 |
insert_select(db, pkeys_ref, pkeys, query, params)
|
|
796 |
insert_select(db, pkeys, pkeys_names, query, params)
|
|
797 | 797 |
else: |
798 |
run_query_into(db, query, params, into=pkeys_ref)
|
|
798 |
run_query_into(db, query, params, into=pkeys) |
|
799 | 799 |
pkeys_table_exists_ref[0] = True |
800 | 800 |
|
801 | 801 |
conds = set() |
... | ... | |
814 | 814 |
|
815 | 815 |
# Do inserts and selects |
816 | 816 |
join_cols = {} |
817 |
out_pkeys_ref = sql_gen.Table(temp_prefix+'_out_pkeys')
|
|
818 |
in_pkeys_ref = sql_gen.Table(temp_prefix+'_in_pkeys')
|
|
817 |
out_pkeys = sql_gen.Table(temp_prefix+'_out_pkeys') |
|
818 |
in_pkeys = sql_gen.Table(temp_prefix+'_in_pkeys') |
|
819 | 819 |
while True: |
820 | 820 |
has_joins = join_cols != {} |
821 | 821 |
|
... | ... | |
827 | 827 |
insert_joins.append(sql_gen.Join(out_table, join_cols, |
828 | 828 |
sql_gen.filter_out)) |
829 | 829 |
else: |
830 |
insert_args.update(dict(returning=out_pkey, into=out_pkeys_ref))
|
|
830 |
insert_args.update(dict(returning=out_pkey, into=out_pkeys)) |
|
831 | 831 |
|
832 | 832 |
db.log_debug('Inserting new rows') |
833 | 833 |
try: |
... | ... | |
864 | 864 |
if has_joins: |
865 | 865 |
select_joins = input_joins+[sql_gen.Join(out_table, join_cols)] |
866 | 866 |
db.log_debug('Getting output pkeys of existing/inserted rows') |
867 |
run_query_into_pkeys(*mk_select(db, select_joins, pkeys_cols, |
|
868 |
start=0)) |
|
867 |
run_query_into_pkeys(*mk_select(db, select_joins, pkeys_cols, start=0)) |
|
869 | 868 |
else: |
870 |
add_row_num(db, out_pkeys_ref) # for joining with input pkeys
|
|
869 |
add_row_num(db, out_pkeys) # for joining with input pkeys |
|
871 | 870 |
|
872 | 871 |
db.log_debug('Getting input pkeys for rows in insert') |
873 | 872 |
run_query_into(db, *mk_main_select(input_joins, [in_pkey]), |
874 |
into=in_pkeys_ref)
|
|
875 |
add_row_num(db, in_pkeys_ref) # for joining with output pkeys
|
|
873 |
into=in_pkeys) |
|
874 |
add_row_num(db, in_pkeys) # for joining with output pkeys |
|
876 | 875 |
|
877 | 876 |
db.log_debug('Joining together output and input pkeys') |
878 |
pkey_joins = [in_pkeys_ref, sql_gen.Join(out_pkeys_ref,
|
|
877 |
pkey_joins = [in_pkeys, sql_gen.Join(out_pkeys,
|
|
879 | 878 |
{row_num_col: sql_gen.join_same_not_null})] |
880 |
run_query_into_pkeys(*mk_select(db, pkey_joins, pkeys, start=0)) |
|
879 |
run_query_into_pkeys(*mk_select(db, pkey_joins, pkeys_names, start=0))
|
|
881 | 880 |
|
882 | 881 |
db.log_debug("Setting missing rows' pkeys to NULL") |
883 |
missing_rows_joins = input_joins+[sql_gen.Join(pkeys_ref,
|
|
882 |
missing_rows_joins = input_joins+[sql_gen.Join(pkeys, |
|
884 | 883 |
{in_pkey: sql_gen.join_same_not_null}, sql_gen.filter_out)] |
885 | 884 |
# must use join_same_not_null or query will take forever |
886 | 885 |
run_query_into_pkeys(*mk_select(db, missing_rows_joins, |
887 | 886 |
[in_pkey_col, sql_gen.NamedCol(out_pkey, None)], start=0)) |
888 | 887 |
|
889 |
return sql_gen.Col(out_pkey, pkeys_ref)
|
|
888 |
return sql_gen.Col(out_pkey, pkeys) |
|
890 | 889 |
|
891 | 890 |
##### Data cleanup |
892 | 891 |
|
Also available in: Unified diff
sql.py: put_table(): Renamed *pkeys_ref to *pkeys to reflect that they are now objects rather than an array-based references