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 |
2113
|
aaronmk
|
import xml_func
|
13 |
13
|
aaronmk
|
|
14 |
139
|
aaronmk
|
def name_of(node): return re.sub(r'^.*\.', r'', node.tagName)
|
15 |
16
|
aaronmk
|
|
16 |
13
|
aaronmk
|
ptr_suffix = '_id'
|
17 |
|
|
|
18 |
138
|
aaronmk
|
def is_ptr(node_name): return node_name.lower().endswith(ptr_suffix)
|
19 |
13
|
aaronmk
|
|
20 |
172
|
aaronmk
|
def ptr_type_guess(node_name):
|
21 |
13
|
aaronmk
|
assert is_ptr(node_name)
|
22 |
|
|
return node_name[:-len(ptr_suffix)]
|
23 |
|
|
|
24 |
|
|
def ptr_target(node):
|
25 |
|
|
assert is_ptr(name_of(node))
|
26 |
2113
|
aaronmk
|
return xml_dom.value_node(node)
|
27 |
13
|
aaronmk
|
|
28 |
|
|
def find_by_name(node, name):
|
29 |
46
|
aaronmk
|
for parent in xml_dom.NodeParentIter(node):
|
30 |
13
|
aaronmk
|
if name_of(parent) == name: return parent
|
31 |
|
|
else:
|
32 |
46
|
aaronmk
|
for child in xml_dom.NodeElemIter(parent):
|
33 |
16
|
aaronmk
|
child_name = name_of(child)
|
34 |
13
|
aaronmk
|
if is_ptr(child_name):
|
35 |
172
|
aaronmk
|
target = ptr_target(child)
|
36 |
|
|
if target.tagName == name: return target
|
37 |
13
|
aaronmk
|
elif child_name == name: return child
|
38 |
|
|
return None
|
39 |
|
|
|
40 |
1850
|
aaronmk
|
def get(db, node, limit=None, start=None):
|
41 |
|
|
def pkey(table): return sql.pkey(db, table)
|
42 |
126
|
aaronmk
|
|
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 |
1836
|
aaronmk
|
if id_ != None: conds[pkey(table)] = id_ # replace any existing pkey value
|
56 |
135
|
aaronmk
|
if fields == []: fields.append(pkey_)
|
57 |
133
|
aaronmk
|
|
58 |
864
|
aaronmk
|
return sql.select(db, table, fields, conds, limit, start)
|
59 |
126
|
aaronmk
|
|
60 |
1864
|
aaronmk
|
def put(db, node, row_ct_ref=None, on_error=exc.raise_, pool=None,
|
61 |
|
|
store_ids=False, parent_id=None):
|
62 |
138
|
aaronmk
|
'''store_ids enables searching the tree for missing fields'''
|
63 |
1850
|
aaronmk
|
def pkey(table): return sql.pkey(db, table, True)
|
64 |
15
|
aaronmk
|
|
65 |
461
|
aaronmk
|
def put_(node, parent_id=None):
|
66 |
1874
|
aaronmk
|
args = (db, node, row_ct_ref, on_error, pool, store_ids, parent_id)
|
67 |
|
|
if parent_id != None and pool != None: pool.apply_async(put, args)
|
68 |
|
|
else: return put(*args)
|
69 |
461
|
aaronmk
|
|
70 |
|
|
def on_error_(e):
|
71 |
446
|
aaronmk
|
exc.add_msg(e, 'node:\n'+str(node))
|
72 |
461
|
aaronmk
|
on_error(e)
|
73 |
446
|
aaronmk
|
|
74 |
48
|
aaronmk
|
table = name_of(node)
|
75 |
446
|
aaronmk
|
try: pkey_ = pkey(table)
|
76 |
461
|
aaronmk
|
except sql.DatabaseErrors, e: on_error_(e); return None
|
77 |
48
|
aaronmk
|
row = {}
|
78 |
|
|
children = []
|
79 |
13
|
aaronmk
|
|
80 |
48
|
aaronmk
|
# Divide children into fields and children with fkeys to parent
|
81 |
|
|
for child in xml_dom.NodeElemIter(node):
|
82 |
|
|
child_name = name_of(child)
|
83 |
463
|
aaronmk
|
if xml_dom.is_empty(child): row[child_name] = None
|
84 |
454
|
aaronmk
|
elif xml_dom.is_text(child):
|
85 |
84
|
aaronmk
|
row[child_name] = strings.to_unicode(xml_dom.value(child))
|
86 |
461
|
aaronmk
|
elif is_ptr(child_name): row[child_name] = put_(ptr_target(child))
|
87 |
48
|
aaronmk
|
else: children.append(child)
|
88 |
|
|
try: del row[pkey_]
|
89 |
|
|
except KeyError: pass
|
90 |
|
|
|
91 |
|
|
# Add fkey to parent
|
92 |
59
|
aaronmk
|
if parent_id != None:
|
93 |
|
|
parent_ptr = node.getAttribute('fkey')
|
94 |
|
|
if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
|
95 |
|
|
row[parent_ptr] = parent_id
|
96 |
48
|
aaronmk
|
|
97 |
|
|
# Insert node
|
98 |
446
|
aaronmk
|
try:
|
99 |
|
|
for try_num in xrange(2):
|
100 |
|
|
try:
|
101 |
472
|
aaronmk
|
id_ = sql.put(db, table, row, pkey_, row_ct_ref)
|
102 |
446
|
aaronmk
|
if store_ids: xml_dom.set_id(node, id_)
|
103 |
|
|
break
|
104 |
|
|
except sql.NullValueException, e:
|
105 |
468
|
aaronmk
|
col = e.cols[0]
|
106 |
446
|
aaronmk
|
if try_num > 0: raise # exception still raised after retry
|
107 |
468
|
aaronmk
|
if store_ids and is_ptr(col):
|
108 |
446
|
aaronmk
|
# Search for required column in ancestors and their children
|
109 |
468
|
aaronmk
|
target = find_by_name(node, ptr_type_guess(col))
|
110 |
446
|
aaronmk
|
if target == None: raise
|
111 |
468
|
aaronmk
|
row[col] = xml_dom.get_id(target)
|
112 |
446
|
aaronmk
|
else: raise
|
113 |
461
|
aaronmk
|
except sql.DatabaseErrors, e: on_error_(e); return None
|
114 |
48
|
aaronmk
|
|
115 |
|
|
# Insert children with fkeys to parent
|
116 |
1874
|
aaronmk
|
for child in children: put_(child, id_)
|
117 |
48
|
aaronmk
|
|
118 |
|
|
return id_
|
119 |
1996
|
aaronmk
|
|
120 |
2039
|
aaronmk
|
class ColRef:
|
121 |
|
|
'''A reference to a table column'''
|
122 |
|
|
def __init__(self, name, idx):
|
123 |
|
|
self.name = name
|
124 |
|
|
self.idx = idx
|
125 |
|
|
|
126 |
|
|
def __str__(self): return self.name
|
127 |
|
|
|
128 |
2052
|
aaronmk
|
def put_table(db, node, in_table, in_schema=None, commit=False,
|
129 |
|
|
row_ct_ref=None):
|
130 |
1996
|
aaronmk
|
'''
|
131 |
1998
|
aaronmk
|
@param node The XML tree that transforms the input to the output. Similar to
|
132 |
|
|
put()'s node param, but with the input column name prefixed by "$" in
|
133 |
|
|
place of the column value.
|
134 |
|
|
@param commit Whether to commit after each query
|
135 |
2133
|
aaronmk
|
@return (table, col) Where the pkeys (from INSERT RETURNING) are made
|
136 |
2067
|
aaronmk
|
available
|
137 |
1998
|
aaronmk
|
'''
|
138 |
2079
|
aaronmk
|
def esc_name(name): return sql.esc_name(db, name)
|
139 |
2060
|
aaronmk
|
def qual_name(table): return sql.qual_name(db, in_schema, table)
|
140 |
2005
|
aaronmk
|
def pkey(table): return sql.pkey(db, table, True)
|
141 |
|
|
|
142 |
2088
|
aaronmk
|
def put_table_(node):
|
143 |
|
|
return put_table(db, node, in_table, in_schema, commit, row_ct_ref)
|
144 |
|
|
|
145 |
2005
|
aaronmk
|
out_table = name_of(node)
|
146 |
|
|
row = {}
|
147 |
|
|
children = []
|
148 |
2129
|
aaronmk
|
in_tables = [qual_name(in_table)]
|
149 |
2005
|
aaronmk
|
|
150 |
|
|
# Divide children into fields and children with fkeys to parent
|
151 |
|
|
for child in xml_dom.NodeElemIter(node):
|
152 |
|
|
child_name = name_of(child)
|
153 |
|
|
if xml_dom.is_empty(child): row[child_name] = None
|
154 |
|
|
elif xml_dom.is_text(child):
|
155 |
|
|
row[child_name] = strings.to_unicode(xml_dom.value(child))
|
156 |
2113
|
aaronmk
|
else:
|
157 |
|
|
child_value = xml_dom.value_node(child)
|
158 |
|
|
if is_ptr(child_name) or xml_func.is_func(child_value):
|
159 |
2128
|
aaronmk
|
table, col = put_table_(child_value)
|
160 |
|
|
row[child_name] = '$'+col
|
161 |
2113
|
aaronmk
|
in_tables.append(table)
|
162 |
|
|
else: children.append(child)
|
163 |
2133
|
aaronmk
|
try: del row[pkey(out_table)]
|
164 |
2005
|
aaronmk
|
except KeyError: pass
|
165 |
|
|
|
166 |
2060
|
aaronmk
|
# Divide fields into input columns and literal values
|
167 |
|
|
for out_col, value in row.iteritems():
|
168 |
|
|
in_col = strings.remove_prefix('$', value)
|
169 |
|
|
if in_col != value: row[out_col] = in_col # value is input column
|
170 |
|
|
else: row[out_col] = (value, out_col) # value is literal value
|
171 |
|
|
|
172 |
|
|
# Insert node
|
173 |
2133
|
aaronmk
|
pkeys_loc = sql.put_table(db, esc_name(out_table), in_tables, row,
|
174 |
2134
|
aaronmk
|
row_ct_ref=row_ct_ref, table_is_esc=True)
|
175 |
2060
|
aaronmk
|
|
176 |
1998
|
aaronmk
|
if commit: db.db.commit()
|
177 |
2133
|
aaronmk
|
return pkeys_loc
|