Revision 2790
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
472 | 472 |
For params, see run_query(). |
473 | 473 |
''' |
474 | 474 |
if into == None: return run_query(db, query, **kw_args) |
475 |
else: # place rows in temp table |
|
476 |
assert isinstance(into, sql_gen.Table) |
|
475 |
|
|
476 |
assert isinstance(into, sql_gen.Table) |
|
477 |
|
|
478 |
kw_args['recover'] = True |
|
479 |
kw_args.setdefault('log_ignore_excs', (DuplicateTableException,)) |
|
480 |
|
|
481 |
temp = not db.autocommit # tables are permanent in autocommit mode |
|
482 |
# "temporary tables cannot specify a schema name", so remove schema |
|
483 |
if temp: into.schema = None |
|
484 |
|
|
485 |
# Create table |
|
486 |
while True: |
|
487 |
create_query = 'CREATE' |
|
488 |
if temp: create_query += ' TEMP' |
|
489 |
create_query += ' TABLE '+into.to_str(db)+' AS\n'+query |
|
477 | 490 |
|
478 |
kw_args['recover'] = True |
|
479 |
kw_args.setdefault('log_ignore_excs', (DuplicateTableException,)) |
|
480 |
|
|
481 |
temp = not db.autocommit # tables are permanent in autocommit mode |
|
482 |
# "temporary tables cannot specify a schema name", so remove schema |
|
483 |
if temp: into.schema = None |
|
484 |
|
|
485 |
# Create table |
|
486 |
while True: |
|
487 |
create_query = 'CREATE' |
|
488 |
if temp: create_query += ' TEMP' |
|
489 |
create_query += ' TABLE '+into.to_str(db)+' AS\n'+query |
|
490 |
|
|
491 |
try: |
|
492 |
cur = run_query(db, create_query, **kw_args) |
|
493 |
# CREATE TABLE AS sets rowcount to # rows in query |
|
494 |
break |
|
495 |
except DuplicateTableException, e: |
|
496 |
into.name = next_version(into.name) |
|
497 |
# try again with next version of name |
|
498 |
|
|
499 |
if add_indexes_: add_indexes(db, into) |
|
500 |
|
|
501 |
return cur |
|
491 |
try: |
|
492 |
cur = run_query(db, create_query, **kw_args) |
|
493 |
# CREATE TABLE AS sets rowcount to # rows in query |
|
494 |
break |
|
495 |
except DuplicateTableException, e: |
|
496 |
into.name = next_version(into.name) |
|
497 |
# try again with next version of name |
|
498 |
|
|
499 |
if add_indexes_: add_indexes(db, into) |
|
500 |
|
|
501 |
return cur |
|
502 | 502 |
|
503 | 503 |
order_by_pkey = object() # tells mk_select() to order by the pkey |
504 | 504 |
|
Also available in: Unified diff
sql.py: run_query_into(): Moved main case (into != None) outside of if statement because the special-case if statement contains `return`