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