1 |
13
|
aaronmk
|
# XML-database conversion
|
2 |
|
|
|
3 |
16
|
aaronmk
|
import re
|
4 |
294
|
aaronmk
|
import traceback
|
5 |
13
|
aaronmk
|
from xml.dom import Node
|
6 |
|
|
|
7 |
446
|
aaronmk
|
import exc
|
8 |
46
|
aaronmk
|
import sql
|
9 |
84
|
aaronmk
|
import strings
|
10 |
133
|
aaronmk
|
import util
|
11 |
46
|
aaronmk
|
import xml_dom
|
12 |
13
|
aaronmk
|
|
13 |
139
|
aaronmk
|
def name_of(node): return re.sub(r'^.*\.', r'', node.tagName)
|
14 |
16
|
aaronmk
|
|
15 |
13
|
aaronmk
|
ptr_suffix = '_id'
|
16 |
|
|
|
17 |
138
|
aaronmk
|
def is_ptr(node_name): return node_name.lower().endswith(ptr_suffix)
|
18 |
13
|
aaronmk
|
|
19 |
172
|
aaronmk
|
def ptr_type_guess(node_name):
|
20 |
13
|
aaronmk
|
assert is_ptr(node_name)
|
21 |
|
|
return node_name[:-len(ptr_suffix)]
|
22 |
|
|
|
23 |
|
|
def ptr_target(node):
|
24 |
|
|
assert is_ptr(name_of(node))
|
25 |
46
|
aaronmk
|
return xml_dom.first_elem(node)
|
26 |
13
|
aaronmk
|
|
27 |
|
|
def find_by_name(node, name):
|
28 |
46
|
aaronmk
|
for parent in xml_dom.NodeParentIter(node):
|
29 |
13
|
aaronmk
|
if name_of(parent) == name: return parent
|
30 |
|
|
else:
|
31 |
46
|
aaronmk
|
for child in xml_dom.NodeElemIter(parent):
|
32 |
16
|
aaronmk
|
child_name = name_of(child)
|
33 |
13
|
aaronmk
|
if is_ptr(child_name):
|
34 |
172
|
aaronmk
|
target = ptr_target(child)
|
35 |
|
|
if target.tagName == name: return target
|
36 |
13
|
aaronmk
|
elif child_name == name: return child
|
37 |
|
|
return None
|
38 |
|
|
|
39 |
146
|
aaronmk
|
def get(db, node, pkeys=None, limit=None):
|
40 |
126
|
aaronmk
|
if pkeys == None: pkeys = {}
|
41 |
|
|
def pkey(table): return sql.pkey(db, pkeys, table)
|
42 |
|
|
|
43 |
141
|
aaronmk
|
node = node.firstChild
|
44 |
135
|
aaronmk
|
table = name_of(node)
|
45 |
|
|
pkey_ = pkey(table)
|
46 |
|
|
|
47 |
|
|
fields = []
|
48 |
|
|
conds = {}
|
49 |
133
|
aaronmk
|
for child in xml_dom.NodeElemIter(node):
|
50 |
135
|
aaronmk
|
child_name = name_of(child)
|
51 |
|
|
if xml_dom.is_empty(child): fields.append(child_name)
|
52 |
|
|
elif xml_dom.is_text(child): conds[child_name] = xml_dom.value(child)
|
53 |
|
|
else: raise Exception('Joins not supported yet')
|
54 |
|
|
id_ = xml_dom.get_id(node)
|
55 |
|
|
if id_ != '': conds[pkey(table)] = id_ # replace any existing pkey value
|
56 |
|
|
if fields == []: fields.append(pkey_)
|
57 |
133
|
aaronmk
|
|
58 |
146
|
aaronmk
|
return sql.select(db, table, fields, conds, limit)
|
59 |
126
|
aaronmk
|
|
60 |
48
|
aaronmk
|
def put(db, node, store_ids=False, row_ct_ref=None, pkeys=None, parent_id=None):
|
61 |
138
|
aaronmk
|
'''store_ids enables searching the tree for missing fields'''
|
62 |
40
|
aaronmk
|
if pkeys == None: pkeys = {}
|
63 |
126
|
aaronmk
|
def pkey(table): return sql.pkey(db, pkeys, table)
|
64 |
15
|
aaronmk
|
|
65 |
446
|
aaronmk
|
def on_error(e):
|
66 |
|
|
exc.add_msg(e, 'node:\n'+str(node))
|
67 |
|
|
raise
|
68 |
|
|
|
69 |
48
|
aaronmk
|
table = name_of(node)
|
70 |
446
|
aaronmk
|
try: pkey_ = pkey(table)
|
71 |
|
|
except sql.DatabaseErrors, e: on_error(e)
|
72 |
48
|
aaronmk
|
row = {}
|
73 |
|
|
children = []
|
74 |
13
|
aaronmk
|
|
75 |
48
|
aaronmk
|
# Divide children into fields and children with fkeys to parent
|
76 |
|
|
for child in xml_dom.NodeElemIter(node):
|
77 |
|
|
child_name = name_of(child)
|
78 |
84
|
aaronmk
|
if xml_dom.is_text(child):
|
79 |
|
|
row[child_name] = strings.to_unicode(xml_dom.value(child))
|
80 |
451
|
aaronmk
|
elif not xml_dom.has_elems(child): pass # ignore empty field
|
81 |
446
|
aaronmk
|
elif is_ptr(child_name):
|
82 |
|
|
row[child_name] = put(db, ptr_target(child), store_ids,
|
83 |
|
|
row_ct_ref, pkeys)
|
84 |
48
|
aaronmk
|
else: children.append(child)
|
85 |
|
|
try: del row[pkey_]
|
86 |
|
|
except KeyError: pass
|
87 |
|
|
|
88 |
|
|
# Add fkey to parent
|
89 |
59
|
aaronmk
|
if parent_id != None:
|
90 |
|
|
parent_ptr = node.getAttribute('fkey')
|
91 |
|
|
if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
|
92 |
|
|
row[parent_ptr] = parent_id
|
93 |
48
|
aaronmk
|
|
94 |
|
|
# Insert node
|
95 |
446
|
aaronmk
|
try:
|
96 |
|
|
for try_num in xrange(2):
|
97 |
|
|
try:
|
98 |
|
|
id_ = sql.get(db, table, row, pkey_, True, row_ct_ref)
|
99 |
|
|
if store_ids: xml_dom.set_id(node, id_)
|
100 |
|
|
break
|
101 |
|
|
except sql.NullValueException, e:
|
102 |
|
|
if try_num > 0: raise # exception still raised after retry
|
103 |
|
|
if store_ids and is_ptr(e.col):
|
104 |
|
|
# Search for required column in ancestors and their children
|
105 |
|
|
target = find_by_name(node, ptr_type_guess(e.col))
|
106 |
|
|
if target == None: raise
|
107 |
|
|
row[e.col] = xml_dom.get_id(target)
|
108 |
|
|
else: raise
|
109 |
|
|
except sql.DatabaseErrors, e: on_error(e)
|
110 |
48
|
aaronmk
|
|
111 |
|
|
# Insert children with fkeys to parent
|
112 |
|
|
for child in children: put(db, child, store_ids, row_ct_ref, pkeys, id_)
|
113 |
|
|
|
114 |
|
|
return id_
|