Revision 39
Added by Aaron Marcuse-Kubitza about 13 years ago
xml_db.py | ||
---|---|---|
41 | 41 |
for child in xml_util.NodeElemIter(node): |
42 | 42 |
if not xml_util.is_text(child): obj(child) # not XML metadata |
43 | 43 |
|
44 |
def obj(node): |
|
44 |
def obj(node, parent_id=None):
|
|
45 | 45 |
table = name_of(node) |
46 | 46 |
pkey_ = pkey(table) |
47 | 47 |
row = {} |
... | ... | |
51 | 51 |
for child in xml_util.NodeElemIter(node): |
52 | 52 |
child_name = name_of(child) |
53 | 53 |
if xml_util.is_text(child): row[child_name] = xml_util.value(child) |
54 |
elif is_ptr(child_name): |
|
55 |
child = ptr_target(child) |
|
56 |
obj(child) |
|
57 |
row[child_name] = xml_util.get_id(child) |
|
54 |
elif is_ptr(child_name): row[child_name] = obj(ptr_target(child)) |
|
58 | 55 |
else: children.append(child) |
59 | 56 |
try: del row[pkey_] |
60 | 57 |
except KeyError: pass |
61 | 58 |
|
62 | 59 |
# Add fkey to parent |
63 |
parent_id = xml_util.get_id(node.parentNode) |
|
64 |
if parent_id != '': row[pkey(name_of(node.parentNode))] = parent_id |
|
60 |
if parent_id != None: row[pkey(name_of(node.parentNode))] = parent_id |
|
65 | 61 |
|
66 | 62 |
# Insert node |
67 | 63 |
for try_num in range(2): |
68 | 64 |
try: |
69 |
xml_util.set_id(node, db_util.insert_or_get(db, table, row,
|
|
70 |
pkey_, row_ct_ref))
|
|
65 |
id_ = db_util.insert_or_get(db, table, row, pkey_, row_ct_ref)
|
|
66 |
xml_util.set_id(node, id_)
|
|
71 | 67 |
break |
72 | 68 |
except db_util.NullValueException, ex: |
73 | 69 |
if try_num > 0: raise # exception still raised after retry |
... | ... | |
77 | 73 |
row[ex.col] = xml_util.get_id(target) |
78 | 74 |
|
79 | 75 |
# Insert children with fkeys to parent |
80 |
for child in children: obj(child) |
|
76 |
for child in children: obj(child, id_) |
|
77 |
|
|
78 |
return id_ |
|
81 | 79 |
|
82 | 80 |
row_ct_ref = [0] |
83 | 81 |
main(node) |
Also available in: Unified diff
xml2db: Changed to return ID (pkey) of inserted record and use this returned value as parent_id instead of getting the parent_id from the parent XML node