Revision 2385
Added by Aaron Marcuse-Kubitza over 12 years ago
sql.py | ||
---|---|---|
401 | 401 |
''' |
402 | 402 |
if into_ref == None: return run_query(db, query, params, *args, **kw_args) |
403 | 403 |
else: # place rows in temp table |
404 |
assert isinstance(into_ref, sql_gen.Table) |
|
405 |
|
|
404 | 406 |
kw_args['recover'] = True |
405 | 407 |
while True: |
406 | 408 |
try: |
407 | 409 |
create_query = 'CREATE' |
408 | 410 |
if not db.debug: create_query += ' TEMP' |
409 |
into = sql_gen.as_Table(into_ref[0]).to_str(db)
|
|
411 |
into = into_ref.to_str(db)
|
|
410 | 412 |
create_query += ' TABLE '+into+' AS '+query |
411 | 413 |
|
412 | 414 |
return run_query(db, create_query, params, *args, **kw_args) |
413 | 415 |
# CREATE TABLE AS sets rowcount to # rows in query |
414 | 416 |
except DuplicateTableException, e: |
415 |
into_ref[0] = next_version(into_ref[0])
|
|
417 |
into_ref.name = next_version(into_ref.name)
|
|
416 | 418 |
# try again with next version of name |
417 | 419 |
|
418 | 420 |
order_by_pkey = object() # tells mk_select() to order by the pkey |
... | ... | |
772 | 774 |
assert isinstance(in_table_col, sql_gen.Col) |
773 | 775 |
|
774 | 776 |
temp_prefix = out_table.name |
775 |
pkeys_ref = [temp_prefix+'_pkeys']
|
|
777 |
pkeys_ref = sql_gen.Table(temp_prefix+'_pkeys')
|
|
776 | 778 |
|
777 | 779 |
# Create input joins from list of input tables |
778 | 780 |
in_tables_ = in_tables[:] # don't modify input! |
... | ... | |
791 | 793 |
pkeys_table_exists_ref = [False] |
792 | 794 |
def run_query_into_pkeys(query, params): |
793 | 795 |
if pkeys_table_exists_ref[0]: |
794 |
insert_select(db, pkeys_ref[0], pkeys, query, params)
|
|
796 |
insert_select(db, pkeys_ref, pkeys, query, params) |
|
795 | 797 |
else: |
796 | 798 |
run_query_into(db, query, params, into_ref=pkeys_ref) |
797 | 799 |
pkeys_table_exists_ref[0] = True |
... | ... | |
812 | 814 |
|
813 | 815 |
# Do inserts and selects |
814 | 816 |
join_cols = {} |
815 |
out_pkeys_ref = [temp_prefix+'_out_pkeys']
|
|
816 |
in_pkeys_ref = [temp_prefix+'_in_pkeys']
|
|
817 |
out_pkeys_ref = sql_gen.Table(temp_prefix+'_out_pkeys')
|
|
818 |
in_pkeys_ref = sql_gen.Table(temp_prefix+'_in_pkeys')
|
|
817 | 819 |
while True: |
818 | 820 |
has_joins = join_cols != {} |
819 | 821 |
|
... | ... | |
865 | 867 |
run_query_into_pkeys(*mk_select(db, select_joins, pkeys_cols, |
866 | 868 |
start=0)) |
867 | 869 |
else: |
868 |
add_row_num(db, out_pkeys_ref[0]) # for joining with input pkeys
|
|
870 |
add_row_num(db, out_pkeys_ref) # for joining with input pkeys |
|
869 | 871 |
|
870 | 872 |
db.log_debug('Getting input pkeys for rows in insert') |
871 | 873 |
run_query_into(db, *mk_main_select(input_joins, [in_pkey]), |
872 | 874 |
into_ref=in_pkeys_ref) |
873 |
add_row_num(db, in_pkeys_ref[0]) # for joining with output pkeys
|
|
875 |
add_row_num(db, in_pkeys_ref) # for joining with output pkeys |
|
874 | 876 |
|
875 | 877 |
db.log_debug('Joining together output and input pkeys') |
876 |
pkey_joins = [in_pkeys_ref[0], sql_gen.Join(out_pkeys_ref[0],
|
|
878 |
pkey_joins = [in_pkeys_ref, sql_gen.Join(out_pkeys_ref,
|
|
877 | 879 |
{row_num_col: sql_gen.join_same_not_null})] |
878 | 880 |
run_query_into_pkeys(*mk_select(db, pkey_joins, pkeys, start=0)) |
879 | 881 |
|
880 | 882 |
db.log_debug("Setting missing rows' pkeys to NULL") |
881 |
missing_rows_joins = input_joins+[sql_gen.Join(pkeys_ref[0],
|
|
883 |
missing_rows_joins = input_joins+[sql_gen.Join(pkeys_ref, |
|
882 | 884 |
{in_pkey: sql_gen.join_same_not_null}, sql_gen.filter_out)] |
883 | 885 |
# must use join_same_not_null or query will take forever |
884 | 886 |
run_query_into_pkeys(*mk_select(db, missing_rows_joins, |
885 | 887 |
[in_pkey_col, sql_gen.NamedCol(out_pkey, None)], start=0)) |
886 | 888 |
|
887 |
return sql_gen.Col(out_pkey, pkeys_ref[0])
|
|
889 |
return sql_gen.Col(out_pkey, pkeys_ref) |
|
888 | 890 |
|
889 | 891 |
##### Data cleanup |
890 | 892 |
|
Also available in: Unified diff
sql.py: run_query_into(): Made into_ref a sql_gen.Table instead of an array containing a table name to improve flexibility and clarity