Revision 2786
Added by Aaron Marcuse-Kubitza over 12 years ago
sql.py | ||
---|---|---|
467 | 467 |
version = int(version)+1 |
468 | 468 |
return sql_gen.add_suffix(name, '#'+str(version)) |
469 | 469 |
|
470 |
def run_query_into(db, query, params, into=None, add_indexes_=False, **kw_args): |
|
470 |
def run_query_into(db, query, params=None, into=None, add_indexes_=False, |
|
471 |
**kw_args): |
|
471 | 472 |
'''Outputs a query to a temp table. |
472 | 473 |
For params, see run_query(). |
473 | 474 |
''' |
... | ... | |
516 | 517 |
* compare_right_side: sql_gen.ValueCond|literal value |
517 | 518 |
@param distinct_on The columns to SELECT DISTINCT ON, or distinct_on_all to |
518 | 519 |
use all columns |
519 |
@return tuple(query, params)
|
|
520 |
@return query
|
|
520 | 521 |
''' |
521 | 522 |
# Parse tables param |
522 | 523 |
if not lists.is_seq(tables): tables = [tables] |
... | ... | |
583 | 584 |
if missing: warnings.warn(DbWarning( |
584 | 585 |
'SELECT statement missing a WHERE, LIMIT, or OFFSET clause: '+query)) |
585 | 586 |
|
586 |
return (query, [])
|
|
587 |
return query
|
|
587 | 588 |
|
588 | 589 |
def select(db, *args, **kw_args): |
589 | 590 |
'''For params, see mk_select() and run_query()''' |
... | ... | |
591 | 592 |
cacheable = kw_args.pop('cacheable', True) |
592 | 593 |
log_level = kw_args.pop('log_level', 2) |
593 | 594 |
|
594 |
query, params = mk_select(db, *args, **kw_args)
|
|
595 |
return run_query(db, query, params, recover, cacheable, log_level=log_level)
|
|
595 |
return run_query(db, mk_select(db, *args, **kw_args), [], recover,
|
|
596 |
cacheable, log_level=log_level)
|
|
596 | 597 |
|
597 | 598 |
def mk_insert_select(db, table, cols=None, select_query=None, params=None, |
598 | 599 |
returning=None, embeddable=False): |
... | ... | |
646 | 647 |
# Return query that uses function |
647 | 648 |
func_table = sql_gen.NamedTable('f', sql_gen.FunctionCall(function), |
648 | 649 |
[returning]) # AS clause requires function alias |
649 |
return mk_select(db, func_table, start=0, order_by=None)
|
|
650 |
return (mk_select(db, func_table, start=0, order_by=None), [])
|
|
650 | 651 |
|
651 | 652 |
return (query, params) |
652 | 653 |
|
... | ... | |
746 | 747 |
''' |
747 | 748 |
items = mk_flatten_mapping(db, into, cols, as_items=True, **kw_args) |
748 | 749 |
cols = [sql_gen.NamedCol(new.name, old) for old, new in items] |
749 |
run_query_into(db, *mk_select(db, joins, cols, limit=limit, start=start),
|
|
750 |
run_query_into(db, mk_select(db, joins, cols, limit=limit, start=start), |
|
750 | 751 |
into=into) |
751 | 752 |
return dict(items) |
752 | 753 |
|
... | ... | |
768 | 769 |
sql_gen.filter_out)] |
769 | 770 |
|
770 | 771 |
return mk_insert_select(db, errors_table, name_only_cols, |
771 |
*mk_select(db, joins, select_cols, order_by=None))[0]
|
|
772 |
mk_select(db, joins, select_cols, order_by=None))[0] |
|
772 | 773 |
|
773 | 774 |
def track_data_error(db, errors_table, *args, **kw_args): |
774 | 775 |
''' |
... | ... | |
837 | 838 |
##### Database structure queries |
838 | 839 |
|
839 | 840 |
def table_row_count(db, table, recover=None): |
840 |
return value(run_query(db, *mk_select(db, table, [sql_gen.row_count],
|
|
841 |
return value(run_query(db, mk_select(db, table, [sql_gen.row_count], |
|
841 | 842 |
order_by=None, start=0), recover=recover, log_level=3)) |
842 | 843 |
|
843 | 844 |
def table_cols(db, table, recover=None): |
... | ... | |
1191 | 1192 |
|
1192 | 1193 |
pkeys_table_exists_ref = [False] |
1193 | 1194 |
def insert_into_pkeys(joins, cols): |
1194 |
query, params = mk_select(db, joins, cols, order_by=None, start=0)
|
|
1195 |
query = mk_select(db, joins, cols, order_by=None, start=0) |
|
1195 | 1196 |
if pkeys_table_exists_ref[0]: |
1196 |
insert_select(db, into, pkeys_names, query, params)
|
|
1197 |
insert_select(db, into, pkeys_names, query) |
|
1197 | 1198 |
else: |
1198 |
run_query_into(db, query, params, into=into)
|
|
1199 |
run_query_into(db, query, into=into) |
|
1199 | 1200 |
pkeys_table_exists_ref[0] = True |
1200 | 1201 |
|
1201 | 1202 |
limit_ref = [None] |
... | ... | |
1248 | 1249 |
while True: |
1249 | 1250 |
if limit_ref[0] == 0: # special case |
1250 | 1251 |
log_debug('Creating an empty pkeys table') |
1251 |
cur = run_query_into(db, *mk_select(db, out_table, [out_pkey],
|
|
1252 |
cur = run_query_into(db, mk_select(db, out_table, [out_pkey], |
|
1252 | 1253 |
limit=limit_ref[0]), into=insert_out_pkeys) |
1253 | 1254 |
break # don't do main case |
1254 | 1255 |
|
... | ... | |
1263 | 1264 |
sql_gen.filter_out)) |
1264 | 1265 |
else: |
1265 | 1266 |
insert_args.update(dict(returning=out_pkey, into=insert_out_pkeys)) |
1266 |
main_select = mk_main_select(insert_joins, mapping.values())[0]
|
|
1267 |
main_select = mk_main_select(insert_joins, mapping.values()) |
|
1267 | 1268 |
|
1268 | 1269 |
log_debug('Trying to insert new rows') |
1269 | 1270 |
try: |
... | ... | |
1325 | 1326 |
add_row_num(db, insert_out_pkeys) # for joining with input pkeys |
1326 | 1327 |
|
1327 | 1328 |
log_debug('Getting input table pkeys of inserted rows') |
1328 |
run_query_into(db, *mk_main_select(input_joins, [in_pkey]),
|
|
1329 |
run_query_into(db, mk_main_select(input_joins, [in_pkey]), |
|
1329 | 1330 |
into=insert_in_pkeys) |
1330 | 1331 |
add_row_num(db, insert_in_pkeys) # for joining with output pkeys |
1331 | 1332 |
|
Also available in: Unified diff
sql.py: mk_select(): Return just the query instead of the query plus empty params