Revision 2386
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
395 | 395 |
name = match.group(2) |
396 | 396 |
return 'v'+str(version)+'_'+name |
397 | 397 |
|
398 |
def run_query_into(db, query, params, into_ref=None, *args, **kw_args):
|
|
398 |
def run_query_into(db, query, params, into=None, *args, **kw_args): |
|
399 | 399 |
'''Outputs a query to a temp table. |
400 | 400 |
For params, see run_query(). |
401 | 401 |
''' |
402 |
if into_ref == None: return run_query(db, query, params, *args, **kw_args)
|
|
402 |
if into == 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)
|
|
404 |
assert isinstance(into, sql_gen.Table) |
|
405 | 405 |
|
406 | 406 |
kw_args['recover'] = True |
407 | 407 |
while True: |
408 | 408 |
try: |
409 | 409 |
create_query = 'CREATE' |
410 | 410 |
if not db.debug: create_query += ' TEMP' |
411 |
into = into_ref.to_str(db) |
|
412 |
create_query += ' TABLE '+into+' AS '+query |
|
411 |
create_query += ' TABLE '+into.to_str(db)+' AS '+query |
|
413 | 412 |
|
414 | 413 |
return run_query(db, create_query, params, *args, **kw_args) |
415 | 414 |
# CREATE TABLE AS sets rowcount to # rows in query |
416 | 415 |
except DuplicateTableException, e: |
417 |
into_ref.name = next_version(into_ref.name)
|
|
416 |
into.name = next_version(into.name)
|
|
418 | 417 |
# try again with next version of name |
419 | 418 |
|
420 | 419 |
order_by_pkey = object() # tells mk_select() to order by the pkey |
... | ... | |
563 | 562 |
|
564 | 563 |
def insert_select(db, *args, **kw_args): |
565 | 564 |
'''For params, see mk_insert_select() and run_query_into() |
566 |
@param into_ref List with name of temp table to place RETURNING values in |
|
565 |
@param into sql_gen.Table with suggested name of temp table to put RETURNING |
|
566 |
values in |
|
567 | 567 |
''' |
568 |
into_ref = kw_args.pop('into_ref', None)
|
|
569 |
if into_ref != None: kw_args['embeddable'] = True
|
|
568 |
into = kw_args.pop('into', None)
|
|
569 |
if into != None: kw_args['embeddable'] = True |
|
570 | 570 |
recover = kw_args.pop('recover', None) |
571 | 571 |
cacheable = kw_args.pop('cacheable', True) |
572 | 572 |
|
573 | 573 |
query, params = mk_insert_select(db, *args, **kw_args) |
574 |
return run_query_into(db, query, params, into_ref, recover=recover,
|
|
574 |
return run_query_into(db, query, params, into, recover=recover, |
|
575 | 575 |
cacheable=cacheable) |
576 | 576 |
|
577 | 577 |
default = object() # tells insert() to use the default value for a column |
... | ... | |
795 | 795 |
if pkeys_table_exists_ref[0]: |
796 | 796 |
insert_select(db, pkeys_ref, pkeys, query, params) |
797 | 797 |
else: |
798 |
run_query_into(db, query, params, into_ref=pkeys_ref)
|
|
798 |
run_query_into(db, query, params, into=pkeys_ref) |
|
799 | 799 |
pkeys_table_exists_ref[0] = True |
800 | 800 |
|
801 | 801 |
conds = set() |
... | ... | |
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_ref=out_pkeys_ref))
|
|
830 |
insert_args.update(dict(returning=out_pkey, into=out_pkeys_ref)) |
|
831 | 831 |
|
832 | 832 |
db.log_debug('Inserting new rows') |
833 | 833 |
try: |
... | ... | |
871 | 871 |
|
872 | 872 |
db.log_debug('Getting input pkeys for rows in insert') |
873 | 873 |
run_query_into(db, *mk_main_select(input_joins, [in_pkey]), |
874 |
into_ref=in_pkeys_ref)
|
|
874 |
into=in_pkeys_ref) |
|
875 | 875 |
add_row_num(db, in_pkeys_ref) # for joining with output pkeys |
876 | 876 |
|
877 | 877 |
db.log_debug('Joining together output and input pkeys') |
Also available in: Unified diff
sql.py: run_query_into(): Renamed into_ref param to into to reflect that it's now an object rather than an array-based reference