Revision 2774
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
465 | 465 |
version = int(version)+1 |
466 | 466 |
return sql_gen.add_suffix(name, '#'+str(version)) |
467 | 467 |
|
468 |
def run_query_into(db, query, params, into=None, *args, **kw_args):
|
|
468 |
def run_query_into(db, query, params, into=None, add_indexes_=False, **kw_args):
|
|
469 | 469 |
'''Outputs a query to a temp table. |
470 | 470 |
For params, see run_query(). |
471 | 471 |
''' |
472 |
if into == None: return run_query(db, query, params, *args, **kw_args)
|
|
472 |
if into == None: return run_query(db, query, params, **kw_args) |
|
473 | 473 |
else: # place rows in temp table |
474 | 474 |
assert isinstance(into, sql_gen.Table) |
475 | 475 |
|
... | ... | |
480 | 480 |
# "temporary tables cannot specify a schema name", so remove schema |
481 | 481 |
if temp: into.schema = None |
482 | 482 |
|
483 |
# Create table |
|
483 | 484 |
while True: |
485 |
create_query = 'CREATE' |
|
486 |
if temp: create_query += ' TEMP' |
|
487 |
create_query += ' TABLE '+into.to_str(db)+' AS\n'+query |
|
488 |
|
|
484 | 489 |
try: |
485 |
create_query = 'CREATE' |
|
486 |
if temp: create_query += ' TEMP' |
|
487 |
create_query += ' TABLE '+into.to_str(db)+' AS\n'+query |
|
488 |
|
|
489 |
return run_query(db, create_query, params, *args, **kw_args) |
|
490 |
cur = run_query(db, create_query, params, **kw_args) |
|
490 | 491 |
# CREATE TABLE AS sets rowcount to # rows in query |
492 |
break |
|
491 | 493 |
except DuplicateTableException, e: |
492 | 494 |
into.name = next_version(into.name) |
493 | 495 |
# try again with next version of name |
496 |
|
|
497 |
if add_indexes_: add_indexes(db, into) |
|
498 |
|
|
499 |
return cur |
|
494 | 500 |
|
495 | 501 |
order_by_pkey = object() # tells mk_select() to order by the pkey |
496 | 502 |
|
Also available in: Unified diff
sql.py: run_query_into(): Added add_indexes_ param which causes the function to add indexes on the created table