1 |
13
|
aaronmk
|
# XML-database conversion
|
2 |
|
|
|
3 |
16
|
aaronmk
|
import re
|
4 |
13
|
aaronmk
|
from xml.dom import Node
|
5 |
|
|
|
6 |
46
|
aaronmk
|
import sql
|
7 |
84
|
aaronmk
|
import strings
|
8 |
133
|
aaronmk
|
import util
|
9 |
46
|
aaronmk
|
import xml_dom
|
10 |
13
|
aaronmk
|
|
11 |
46
|
aaronmk
|
def name_of(node): return re.sub(r'^.*\.', r'', xml_dom.name_of(node))
|
12 |
16
|
aaronmk
|
|
13 |
13
|
aaronmk
|
ptr_suffix = '_id'
|
14 |
|
|
|
15 |
|
|
def is_ptr(node_name): return node_name.endswith(ptr_suffix)
|
16 |
|
|
|
17 |
|
|
def ptr_type(node_name):
|
18 |
|
|
assert is_ptr(node_name)
|
19 |
|
|
return node_name[:-len(ptr_suffix)]
|
20 |
|
|
|
21 |
|
|
def ptr_target(node):
|
22 |
|
|
assert is_ptr(name_of(node))
|
23 |
46
|
aaronmk
|
return xml_dom.first_elem(node)
|
24 |
13
|
aaronmk
|
|
25 |
|
|
def find_by_name(node, name):
|
26 |
46
|
aaronmk
|
for parent in xml_dom.NodeParentIter(node):
|
27 |
13
|
aaronmk
|
if name_of(parent) == name: return parent
|
28 |
|
|
else:
|
29 |
46
|
aaronmk
|
for child in xml_dom.NodeElemIter(parent):
|
30 |
16
|
aaronmk
|
child_name = name_of(child)
|
31 |
13
|
aaronmk
|
if is_ptr(child_name):
|
32 |
|
|
if ptr_type(child_name) == name: return ptr_target(child)
|
33 |
|
|
elif child_name == name: return child
|
34 |
|
|
return None
|
35 |
|
|
|
36 |
133
|
aaronmk
|
def get(db, node, pkeys=None):
|
37 |
126
|
aaronmk
|
if pkeys == None: pkeys = {}
|
38 |
|
|
def pkey(table): return sql.pkey(db, pkeys, table)
|
39 |
|
|
|
40 |
133
|
aaronmk
|
for child in xml_dom.NodeElemIter(node):
|
41 |
|
|
assert xml_dom.is_text(child)
|
42 |
|
|
|
43 |
|
|
return []
|
44 |
126
|
aaronmk
|
|
45 |
48
|
aaronmk
|
def put(db, node, store_ids=False, row_ct_ref=None, pkeys=None, parent_id=None):
|
46 |
40
|
aaronmk
|
# store_ids enables searching the tree for missing fields
|
47 |
|
|
if pkeys == None: pkeys = {}
|
48 |
126
|
aaronmk
|
def pkey(table): return sql.pkey(db, pkeys, table)
|
49 |
15
|
aaronmk
|
|
50 |
48
|
aaronmk
|
table = name_of(node)
|
51 |
|
|
pkey_ = pkey(table)
|
52 |
|
|
row = {}
|
53 |
|
|
children = []
|
54 |
13
|
aaronmk
|
|
55 |
48
|
aaronmk
|
# Divide children into fields and children with fkeys to parent
|
56 |
|
|
for child in xml_dom.NodeElemIter(node):
|
57 |
|
|
child_name = name_of(child)
|
58 |
84
|
aaronmk
|
if xml_dom.is_text(child):
|
59 |
|
|
row[child_name] = strings.to_unicode(xml_dom.value(child))
|
60 |
48
|
aaronmk
|
elif is_ptr(child_name): row[child_name] = put(db, ptr_target(child),
|
61 |
|
|
store_ids, row_ct_ref, pkeys)
|
62 |
|
|
else: children.append(child)
|
63 |
|
|
try: del row[pkey_]
|
64 |
|
|
except KeyError: pass
|
65 |
|
|
|
66 |
|
|
# Add fkey to parent
|
67 |
59
|
aaronmk
|
if parent_id != None:
|
68 |
|
|
parent_ptr = node.getAttribute('fkey')
|
69 |
|
|
if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
|
70 |
|
|
row[parent_ptr] = parent_id
|
71 |
48
|
aaronmk
|
|
72 |
|
|
# Insert node
|
73 |
70
|
aaronmk
|
for try_num in xrange(2):
|
74 |
48
|
aaronmk
|
try:
|
75 |
|
|
id_ = sql.get(db, table, row, pkey_, True, row_ct_ref)
|
76 |
|
|
if store_ids: xml_dom.set_id(node, id_)
|
77 |
|
|
break
|
78 |
|
|
except sql.NullValueException, ex:
|
79 |
|
|
if try_num > 0: raise # exception still raised after retry
|
80 |
73
|
aaronmk
|
if is_ptr(ex.col):
|
81 |
|
|
# Search for required column in ancestors and their children
|
82 |
|
|
target = find_by_name(node, ptr_type(ex.col))
|
83 |
|
|
if target == None: raise
|
84 |
|
|
row[ex.col] = xml_dom.get_id(target)
|
85 |
|
|
else: raise
|
86 |
48
|
aaronmk
|
|
87 |
|
|
# Insert children with fkeys to parent
|
88 |
|
|
for child in children: put(db, child, store_ids, row_ct_ref, pkeys, id_)
|
89 |
|
|
|
90 |
|
|
return id_
|
91 |
40
|
aaronmk
|
|
92 |
|
|
def xml2db(db, node, row_ct_ref=None):
|
93 |
48
|
aaronmk
|
iter_ = xml_dom.NodeElemIter(node)
|
94 |
133
|
aaronmk
|
util.skip(iter_, xml_dom.is_text) # skip metadata
|
95 |
48
|
aaronmk
|
for child in iter_: put(db, child, True, row_ct_ref)
|