Revision 446
Added by Aaron Marcuse-Kubitza over 12 years ago
db_xml.py | ||
---|---|---|
4 | 4 |
import traceback |
5 | 5 |
from xml.dom import Node |
6 | 6 |
|
7 |
import exc |
|
7 | 8 |
import sql |
8 | 9 |
import strings |
9 | 10 |
import util |
... | ... | |
61 | 62 |
if pkeys == None: pkeys = {} |
62 | 63 |
def pkey(table): return sql.pkey(db, pkeys, table) |
63 | 64 |
|
65 |
def on_error(e): |
|
66 |
exc.add_msg(e, 'node:\n'+str(node)) |
|
67 |
raise |
|
68 |
|
|
64 | 69 |
table = name_of(node) |
65 |
pkey_ = pkey(table) |
|
70 |
try: pkey_ = pkey(table) |
|
71 |
except sql.DatabaseErrors, e: on_error(e) |
|
66 | 72 |
row = {} |
67 | 73 |
children = [] |
68 | 74 |
|
... | ... | |
71 | 77 |
child_name = name_of(child) |
72 | 78 |
if xml_dom.is_text(child): |
73 | 79 |
row[child_name] = strings.to_unicode(xml_dom.value(child)) |
74 |
elif is_ptr(child_name): row[child_name] = put(db, ptr_target(child), |
|
75 |
store_ids, row_ct_ref, pkeys) |
|
80 |
elif is_ptr(child_name): |
|
81 |
row[child_name] = put(db, ptr_target(child), store_ids, |
|
82 |
row_ct_ref, pkeys) |
|
76 | 83 |
else: children.append(child) |
77 | 84 |
try: del row[pkey_] |
78 | 85 |
except KeyError: pass |
... | ... | |
84 | 91 |
row[parent_ptr] = parent_id |
85 | 92 |
|
86 | 93 |
# Insert node |
87 |
for try_num in xrange(2): |
|
88 |
try: |
|
89 |
id_ = sql.get(db, table, row, pkey_, True, row_ct_ref) |
|
90 |
if store_ids: xml_dom.set_id(node, id_) |
|
91 |
break |
|
92 |
except sql.NullValueException, e: |
|
93 |
if try_num > 0: raise # exception still raised after retry |
|
94 |
if store_ids and is_ptr(e.col): |
|
95 |
# Search for required column in ancestors and their children |
|
96 |
target = find_by_name(node, ptr_type_guess(e.col)) |
|
97 |
if target == None: raise |
|
98 |
row[e.col] = xml_dom.get_id(target) |
|
99 |
else: raise |
|
94 |
try: |
|
95 |
for try_num in xrange(2): |
|
96 |
try: |
|
97 |
id_ = sql.get(db, table, row, pkey_, True, row_ct_ref) |
|
98 |
if store_ids: xml_dom.set_id(node, id_) |
|
99 |
break |
|
100 |
except sql.NullValueException, e: |
|
101 |
if try_num > 0: raise # exception still raised after retry |
|
102 |
if store_ids and is_ptr(e.col): |
|
103 |
# Search for required column in ancestors and their children |
|
104 |
target = find_by_name(node, ptr_type_guess(e.col)) |
|
105 |
if target == None: raise |
|
106 |
row[e.col] = xml_dom.get_id(target) |
|
107 |
else: raise |
|
108 |
except sql.DatabaseErrors, e: on_error(e) |
|
100 | 109 |
|
101 | 110 |
# Insert children with fkeys to parent |
102 | 111 |
for child in children: put(db, child, store_ids, row_ct_ref, pkeys, id_) |
Also available in: Unified diff
db_xml.py: Add node to any database exceptions generated in put()