Revision 2151
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
326 | 326 |
|
327 | 327 |
##### Basic queries |
328 | 328 |
|
329 |
def run_query_into(db, query, params, into=None, *args, **kw_args): |
|
329 |
def run_query_into(db, query, params, into_ref=None, *args, **kw_args):
|
|
330 | 330 |
'''Outputs a query to a temp table. |
331 | 331 |
For params, see run_query(). |
332 | 332 |
''' |
333 |
if into == None: return run_query(db, query, params, *args, **kw_args) |
|
333 |
if into_ref == None: return run_query(db, query, params, *args, **kw_args)
|
|
334 | 334 |
else: # place rows in temp table |
335 |
check_name(into) |
|
336 |
return run_query(db, 'CREATE TEMP TABLE '+into+' AS '+query, params,
|
|
337 |
*args, **kw_args) # CREATE TABLE sets rowcount to # rows in query
|
|
335 |
check_name(into_ref[0])
|
|
336 |
return run_query(db, 'CREATE TEMP TABLE '+into_ref[0]+' AS '+query,
|
|
337 |
params, *args, **kw_args) # CREATE TABLE AS sets rowcount to # rows
|
|
338 | 338 |
|
339 | 339 |
order_by_pkey = object() # tells mk_select() to order by the pkey |
340 | 340 |
|
... | ... | |
477 | 477 |
'''For params, see mk_insert_select() and run_query_into() |
478 | 478 |
@param into Name of temp table to place RETURNING values in |
479 | 479 |
''' |
480 |
into = kw_args.pop('into', None)
|
|
481 |
if into != None: kw_args['embeddable'] = True |
|
480 |
into_ref = kw_args.pop('into_ref', None)
|
|
481 |
if into_ref != None: kw_args['embeddable'] = True
|
|
482 | 482 |
recover = kw_args.pop('recover', None) |
483 | 483 |
cacheable = kw_args.pop('cacheable', True) |
484 | 484 |
|
485 | 485 |
query, params = mk_insert_select(db, *args, **kw_args) |
486 |
return run_query_into(db, query, params, into, recover, cacheable) |
|
486 |
return run_query_into(db, query, params, into_ref, recover, cacheable)
|
|
487 | 487 |
|
488 | 488 |
default = object() # tells insert() to use the default value for a column |
489 | 489 |
|
... | ... | |
676 | 676 |
'''Inserts and capture output pkeys.''' |
677 | 677 |
cur = insert_select(db, out_table, mapping.keys(), |
678 | 678 |
*mk_select_(mapping.values()), returning=out_pkey, |
679 |
into=out_pkeys, recover=True, table_is_esc=table_is_esc)
|
|
679 |
into_ref=[out_pkeys], recover=True, table_is_esc=table_is_esc)
|
|
680 | 680 |
if row_ct_ref != None and cur.rowcount >= 0: |
681 | 681 |
row_ct_ref[0] += cur.rowcount |
682 | 682 |
add_row_num(db, out_pkeys) # for joining it with in_pkeys |
683 | 683 |
|
684 | 684 |
# Get input pkeys corresponding to rows in insert |
685 | 685 |
in_pkeys = temp_prefix+'_in_pkeys' |
686 |
run_query_into(db, *mk_select_([in_pkey]), into=in_pkeys)
|
|
686 |
run_query_into(db, *mk_select_([in_pkey]), into_ref=[in_pkeys])
|
|
687 | 687 |
add_row_num(db, in_pkeys) # for joining it with out_pkeys |
688 | 688 |
|
689 | 689 |
# Join together out_pkeys and in_pkeys |
690 | 690 |
run_query_into(db, *mk_select(db, |
691 | 691 |
[in_pkeys, (out_pkeys, {row_num_col: join_using})], |
692 |
pkeys_cols, start=0), into=pkeys)
|
|
692 |
pkeys_cols, start=0), into=[pkeys])
|
|
693 | 693 |
|
694 | 694 |
# Do inserts and selects |
695 | 695 |
try: insert_() |
... | ... | |
697 | 697 |
join_cols = util.dict_subset_right_join(mapping, e.cols) |
698 | 698 |
joins = in_joins + [(out_table, join_cols)] |
699 | 699 |
run_query_into(db, *mk_select(db, joins, pkeys_cols, |
700 |
table_is_esc=table_is_esc), into=pkeys, recover=True)
|
|
700 |
table_is_esc=table_is_esc), into=[pkeys], recover=True)
|
|
701 | 701 |
|
702 | 702 |
return (pkeys, out_pkey) |
703 | 703 |
|
Also available in: Unified diff
sql.py: run_query_into(): Made into param a reference so that the function can change it, and renamed it to into_ref