Revision 5239
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql_io.py | ||
---|---|---|
422 | 422 |
# Do after applying dicts.join() because that returns a plain dict. |
423 | 423 |
mapping = sql_gen.ColDict(db, out_table, mapping) |
424 | 424 |
|
425 |
# Resolve default value column |
|
426 |
if default != None: |
|
427 |
try: default = mapping[default] |
|
428 |
except KeyError: |
|
429 |
db.log_debug('Default value column ' |
|
430 |
+strings.as_tt(strings.repr_no_u(default)) |
|
431 |
+' does not exist in mapping, falling back to None', level=2.1) |
|
432 |
default = None |
|
433 |
|
|
434 |
# Save default values for all rows since in_table may have rows deleted |
|
425 |
# Save all rows since in_table may have rows deleted |
|
435 | 426 |
if is_literals: pass |
436 | 427 |
elif is_function: full_in_table = in_table |
437 | 428 |
else: |
438 | 429 |
full_in_table = sql_gen.suffixed_table(in_table, '_full') |
439 |
full_in_table_cols = [in_pkey_col] |
|
440 |
if default != None: |
|
441 |
full_in_table_cols.append(default) |
|
442 |
default = sql_gen.with_table(default, full_in_table) |
|
443 |
sql.run_query_into(db, sql.mk_select(db, in_table, full_in_table_cols, |
|
444 |
order_by=None), into=full_in_table, add_pkey_=True) |
|
430 |
sql.run_query_into(db, sql.mk_select(db, in_table, None, order_by=None), |
|
431 |
into=full_in_table, add_pkey_=True) |
|
445 | 432 |
|
446 | 433 |
pkeys_table_exists_ref = [False] |
447 | 434 |
def insert_into_pkeys(joins, cols=None, limit=None, **kw_args): |
... | ... | |
580 | 567 |
)) |
581 | 568 |
wrapper_table = sql_gen.FunctionCall(wrapper) |
582 | 569 |
|
570 |
missing_msg = None |
|
571 |
|
|
583 | 572 |
# Do inserts and selects |
584 | 573 |
while True: |
585 | 574 |
has_joins = join_cols != {} |
586 | 575 |
|
587 |
# Handle unrecoverable errors in a special case |
|
588 |
if limit_ref[0] == 0: |
|
589 |
if is_literals or default == None: |
|
590 |
default = sql_gen.remove_col_rename(default) |
|
591 |
log_debug('Returning default: ' |
|
592 |
+strings.as_tt(strings.urepr(default))) |
|
593 |
return default |
|
594 |
elif is_function: pass # empty pkeys table already created |
|
595 |
else: |
|
596 |
log_debug('Creating an empty output pkeys table') |
|
597 |
has_joins = False # use the no-joins case |
|
598 |
cur = sql.run_query_into(db, sql.mk_select(db, out_table, |
|
599 |
[out_pkey], order_by=None, limit=0), into=insert_out_pkeys) |
|
600 |
|
|
601 |
break # don't do main case |
|
576 |
if limit_ref[0] == 0: break # unrecoverable error, so don't do main case |
|
602 | 577 |
|
603 | 578 |
# Prepare to insert new rows |
604 | 579 |
if is_function: |
... | ... | |
674 | 649 |
except KeyError, e: |
675 | 650 |
try: in_col = mapping[out_col] = col_defaults[out_col] |
676 | 651 |
except KeyError: |
677 |
msg = 'Missing mapping for NOT NULL column '+out_col |
|
678 |
log_debug(msg) |
|
679 |
if default == None: warnings.warn(UserWarning(msg)) |
|
680 |
# not an error because sometimes the mappings include |
|
681 |
# extra tables which aren't used by the dataset |
|
652 |
missing_msg = 'Missing mapping for NOT NULL column '+out_col |
|
653 |
log_debug(missing_msg) |
|
682 | 654 |
remove_all_rows() |
683 | 655 |
else: ignore(in_col, None, e) |
684 | 656 |
except sql.CheckException, e: |
... | ... | |
700 | 672 |
remove_all_rows() |
701 | 673 |
# after exception handled, rerun loop with additional constraints |
702 | 674 |
|
675 |
# Resolve default value column |
|
676 |
if default != None: |
|
677 |
try: default = mapping[default] |
|
678 |
except KeyError: |
|
679 |
db.log_debug('Default value column ' |
|
680 |
+strings.as_tt(strings.repr_no_u(default)) |
|
681 |
+' does not exist in mapping, falling back to None', level=2.1) |
|
682 |
default = None |
|
683 |
|
|
684 |
if missing_msg != None and default == None: |
|
685 |
warnings.warn(UserWarning(missing_msg)) |
|
686 |
# not an error because sometimes the mappings include |
|
687 |
# extra tables which aren't used by the dataset |
|
688 |
|
|
689 |
# Handle unrecoverable errors |
|
690 |
if limit_ref[0] == 0: |
|
691 |
if is_literals or default == None: |
|
692 |
default = sql_gen.remove_col_rename(default) |
|
693 |
log_debug('Returning default: ' |
|
694 |
+strings.as_tt(strings.urepr(default))) |
|
695 |
return default |
|
696 |
elif is_function: pass # empty pkeys table already created |
|
697 |
else: |
|
698 |
log_debug('Creating an empty output pkeys table') |
|
699 |
has_joins = False # use the no-joins case |
|
700 |
cur = sql.run_query_into(db, sql.mk_select(db, out_table, |
|
701 |
[out_pkey], order_by=None, limit=0), into=insert_out_pkeys) |
|
702 |
|
|
703 | 703 |
if cur != None and row_ct_ref != None and cur.rowcount >= 0: |
704 | 704 |
row_ct_ref[0] += cur.rowcount |
705 | 705 |
|
Also available in: Unified diff
sql_io.py: put_table(): Resolve default value column after the main loop (inserts and selects), so that the default value column can refer to an output column that is not in the original mapping but is added to the mapping from a col_defaults entry. This requires deferring the "Missing mapping for NOT NULL column" warning until the default value column is resolved, and including all columns in the full_in_table since the default value input column is not yet known.