1 |
13
|
aaronmk
|
# XML-database conversion
|
2 |
|
|
|
3 |
2418
|
aaronmk
|
import copy
|
4 |
16
|
aaronmk
|
import re
|
5 |
13
|
aaronmk
|
from xml.dom import Node
|
6 |
|
|
|
7 |
446
|
aaronmk
|
import exc
|
8 |
2436
|
aaronmk
|
import Parser
|
9 |
46
|
aaronmk
|
import sql
|
10 |
3077
|
aaronmk
|
import sql_io
|
11 |
2268
|
aaronmk
|
import sql_gen
|
12 |
84
|
aaronmk
|
import strings
|
13 |
133
|
aaronmk
|
import util
|
14 |
46
|
aaronmk
|
import xml_dom
|
15 |
2113
|
aaronmk
|
import xml_func
|
16 |
2436
|
aaronmk
|
import xpath
|
17 |
13
|
aaronmk
|
|
18 |
139
|
aaronmk
|
def name_of(node): return re.sub(r'^.*\.', r'', node.tagName)
|
19 |
16
|
aaronmk
|
|
20 |
13
|
aaronmk
|
ptr_suffix = '_id'
|
21 |
|
|
|
22 |
138
|
aaronmk
|
def is_ptr(node_name): return node_name.lower().endswith(ptr_suffix)
|
23 |
13
|
aaronmk
|
|
24 |
172
|
aaronmk
|
def ptr_type_guess(node_name):
|
25 |
13
|
aaronmk
|
assert is_ptr(node_name)
|
26 |
|
|
return node_name[:-len(ptr_suffix)]
|
27 |
|
|
|
28 |
|
|
def ptr_target(node):
|
29 |
|
|
assert is_ptr(name_of(node))
|
30 |
2113
|
aaronmk
|
return xml_dom.value_node(node)
|
31 |
13
|
aaronmk
|
|
32 |
|
|
def find_by_name(node, name):
|
33 |
46
|
aaronmk
|
for parent in xml_dom.NodeParentIter(node):
|
34 |
13
|
aaronmk
|
if name_of(parent) == name: return parent
|
35 |
|
|
else:
|
36 |
46
|
aaronmk
|
for child in xml_dom.NodeElemIter(parent):
|
37 |
16
|
aaronmk
|
child_name = name_of(child)
|
38 |
13
|
aaronmk
|
if is_ptr(child_name):
|
39 |
172
|
aaronmk
|
target = ptr_target(child)
|
40 |
|
|
if target.tagName == name: return target
|
41 |
13
|
aaronmk
|
elif child_name == name: return child
|
42 |
|
|
return None
|
43 |
|
|
|
44 |
1850
|
aaronmk
|
def get(db, node, limit=None, start=None):
|
45 |
|
|
def pkey(table): return sql.pkey(db, table)
|
46 |
126
|
aaronmk
|
|
47 |
141
|
aaronmk
|
node = node.firstChild
|
48 |
135
|
aaronmk
|
table = name_of(node)
|
49 |
|
|
pkey_ = pkey(table)
|
50 |
|
|
|
51 |
|
|
fields = []
|
52 |
|
|
conds = {}
|
53 |
133
|
aaronmk
|
for child in xml_dom.NodeElemIter(node):
|
54 |
135
|
aaronmk
|
child_name = name_of(child)
|
55 |
|
|
if xml_dom.is_empty(child): fields.append(child_name)
|
56 |
|
|
elif xml_dom.is_text(child): conds[child_name] = xml_dom.value(child)
|
57 |
|
|
else: raise Exception('Joins not supported yet')
|
58 |
|
|
id_ = xml_dom.get_id(node)
|
59 |
1836
|
aaronmk
|
if id_ != None: conds[pkey(table)] = id_ # replace any existing pkey value
|
60 |
135
|
aaronmk
|
if fields == []: fields.append(pkey_)
|
61 |
133
|
aaronmk
|
|
62 |
864
|
aaronmk
|
return sql.select(db, table, fields, conds, limit, start)
|
63 |
126
|
aaronmk
|
|
64 |
1864
|
aaronmk
|
def put(db, node, row_ct_ref=None, on_error=exc.raise_, pool=None,
|
65 |
|
|
store_ids=False, parent_id=None):
|
66 |
138
|
aaronmk
|
'''store_ids enables searching the tree for missing fields'''
|
67 |
1850
|
aaronmk
|
def pkey(table): return sql.pkey(db, table, True)
|
68 |
15
|
aaronmk
|
|
69 |
461
|
aaronmk
|
def put_(node, parent_id=None):
|
70 |
1874
|
aaronmk
|
args = (db, node, row_ct_ref, on_error, pool, store_ids, parent_id)
|
71 |
|
|
if parent_id != None and pool != None: pool.apply_async(put, args)
|
72 |
|
|
else: return put(*args)
|
73 |
461
|
aaronmk
|
|
74 |
|
|
def on_error_(e):
|
75 |
446
|
aaronmk
|
exc.add_msg(e, 'node:\n'+str(node))
|
76 |
461
|
aaronmk
|
on_error(e)
|
77 |
446
|
aaronmk
|
|
78 |
48
|
aaronmk
|
table = name_of(node)
|
79 |
446
|
aaronmk
|
try: pkey_ = pkey(table)
|
80 |
461
|
aaronmk
|
except sql.DatabaseErrors, e: on_error_(e); return None
|
81 |
48
|
aaronmk
|
row = {}
|
82 |
|
|
children = []
|
83 |
13
|
aaronmk
|
|
84 |
48
|
aaronmk
|
# Divide children into fields and children with fkeys to parent
|
85 |
|
|
for child in xml_dom.NodeElemIter(node):
|
86 |
|
|
child_name = name_of(child)
|
87 |
463
|
aaronmk
|
if xml_dom.is_empty(child): row[child_name] = None
|
88 |
454
|
aaronmk
|
elif xml_dom.is_text(child):
|
89 |
84
|
aaronmk
|
row[child_name] = strings.to_unicode(xml_dom.value(child))
|
90 |
461
|
aaronmk
|
elif is_ptr(child_name): row[child_name] = put_(ptr_target(child))
|
91 |
48
|
aaronmk
|
else: children.append(child)
|
92 |
|
|
try: del row[pkey_]
|
93 |
|
|
except KeyError: pass
|
94 |
|
|
|
95 |
|
|
# Add fkey to parent
|
96 |
59
|
aaronmk
|
if parent_id != None:
|
97 |
|
|
parent_ptr = node.getAttribute('fkey')
|
98 |
|
|
if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
|
99 |
|
|
row[parent_ptr] = parent_id
|
100 |
48
|
aaronmk
|
|
101 |
|
|
# Insert node
|
102 |
446
|
aaronmk
|
try:
|
103 |
|
|
for try_num in xrange(2):
|
104 |
|
|
try:
|
105 |
3077
|
aaronmk
|
id_ = sql_io.put(db, table, row, pkey_, row_ct_ref)
|
106 |
446
|
aaronmk
|
if store_ids: xml_dom.set_id(node, id_)
|
107 |
|
|
break
|
108 |
|
|
except sql.NullValueException, e:
|
109 |
468
|
aaronmk
|
col = e.cols[0]
|
110 |
446
|
aaronmk
|
if try_num > 0: raise # exception still raised after retry
|
111 |
468
|
aaronmk
|
if store_ids and is_ptr(col):
|
112 |
446
|
aaronmk
|
# Search for required column in ancestors and their children
|
113 |
468
|
aaronmk
|
target = find_by_name(node, ptr_type_guess(col))
|
114 |
446
|
aaronmk
|
if target == None: raise
|
115 |
468
|
aaronmk
|
row[col] = xml_dom.get_id(target)
|
116 |
446
|
aaronmk
|
else: raise
|
117 |
461
|
aaronmk
|
except sql.DatabaseErrors, e: on_error_(e); return None
|
118 |
48
|
aaronmk
|
|
119 |
|
|
# Insert children with fkeys to parent
|
120 |
1874
|
aaronmk
|
for child in children: put_(child, id_)
|
121 |
48
|
aaronmk
|
|
122 |
|
|
return id_
|
123 |
1996
|
aaronmk
|
|
124 |
2039
|
aaronmk
|
class ColRef:
|
125 |
|
|
'''A reference to a table column'''
|
126 |
|
|
def __init__(self, name, idx):
|
127 |
|
|
self.name = name
|
128 |
|
|
self.idx = idx
|
129 |
|
|
|
130 |
|
|
def __str__(self): return self.name
|
131 |
|
|
|
132 |
2177
|
aaronmk
|
input_col_prefix = '$'
|
133 |
|
|
|
134 |
2549
|
aaronmk
|
put_table_special_funcs = set(['_simplifyPath'])
|
135 |
|
|
|
136 |
2928
|
aaronmk
|
def put_table(db, node, in_table, in_row_ct_ref=None,
|
137 |
2806
|
aaronmk
|
row_ins_ct_ref=None, limit=None, start=0, on_error=exc.raise_,
|
138 |
|
|
parent_ids_loc=None, next=None):
|
139 |
1996
|
aaronmk
|
'''
|
140 |
1998
|
aaronmk
|
@param node The XML tree that transforms the input to the output. Similar to
|
141 |
2177
|
aaronmk
|
put()'s node param, but with the input column name prefixed by
|
142 |
|
|
input_col_prefix in place of the column value.
|
143 |
2133
|
aaronmk
|
@return (table, col) Where the pkeys (from INSERT RETURNING) are made
|
144 |
2067
|
aaronmk
|
available
|
145 |
1998
|
aaronmk
|
'''
|
146 |
2418
|
aaronmk
|
in_table = sql_gen.as_Table(in_table)
|
147 |
2714
|
aaronmk
|
in_table.set_srcs(sql_gen.src_self, overwrite=False)
|
148 |
2418
|
aaronmk
|
|
149 |
2423
|
aaronmk
|
# Subset in_table
|
150 |
2418
|
aaronmk
|
if limit != None or start != 0:
|
151 |
2714
|
aaronmk
|
full_in_table = in_table
|
152 |
2418
|
aaronmk
|
in_table = copy.copy(in_table) # don't modify input!
|
153 |
2714
|
aaronmk
|
in_table.set_srcs([full_in_table])
|
154 |
2786
|
aaronmk
|
sql.run_query_into(db, sql.mk_select(db, in_table, limit=limit,
|
155 |
2605
|
aaronmk
|
start=start), into=in_table)
|
156 |
2469
|
aaronmk
|
# in_table will be shadowed (hidden) by the created temp table
|
157 |
2780
|
aaronmk
|
sql.add_pkey(db, in_table)
|
158 |
2418
|
aaronmk
|
|
159 |
2005
|
aaronmk
|
def pkey(table): return sql.pkey(db, table, True)
|
160 |
|
|
|
161 |
2506
|
aaronmk
|
def put_table_(node):
|
162 |
2928
|
aaronmk
|
return put_table(db, node, in_table, None, row_ins_ct_ref,
|
163 |
2806
|
aaronmk
|
on_error=on_error, parent_ids_loc=parent_ids_loc, next=next)
|
164 |
2088
|
aaronmk
|
|
165 |
2551
|
aaronmk
|
is_func = xml_func.is_func(node)
|
166 |
2005
|
aaronmk
|
out_table = name_of(node)
|
167 |
2432
|
aaronmk
|
|
168 |
|
|
# Divide children into fields and children with fkeys to parent
|
169 |
2005
|
aaronmk
|
row = {}
|
170 |
|
|
children = []
|
171 |
|
|
for child in xml_dom.NodeElemIter(node):
|
172 |
|
|
child_name = name_of(child)
|
173 |
|
|
if xml_dom.is_empty(child): row[child_name] = None
|
174 |
|
|
elif xml_dom.is_text(child):
|
175 |
|
|
row[child_name] = strings.to_unicode(xml_dom.value(child))
|
176 |
2113
|
aaronmk
|
else:
|
177 |
|
|
child_value = xml_dom.value_node(child)
|
178 |
2434
|
aaronmk
|
if is_func or is_ptr(child_name) or xml_func.is_func(child_value):
|
179 |
2432
|
aaronmk
|
row[child_name] = child_value
|
180 |
2113
|
aaronmk
|
else: children.append(child)
|
181 |
2434
|
aaronmk
|
|
182 |
|
|
# Special handling for structural XML functions
|
183 |
|
|
if out_table == '_simplifyPath':
|
184 |
2436
|
aaronmk
|
# Parse args
|
185 |
|
|
def wrap_e(e): raise xml_func.SyntaxError(e)
|
186 |
2434
|
aaronmk
|
try:
|
187 |
2506
|
aaronmk
|
next = row['next'] # modifies outer next var used by put_table_()
|
188 |
2434
|
aaronmk
|
require = row['require']
|
189 |
|
|
path = row['path']
|
190 |
2436
|
aaronmk
|
except KeyError, e: wrap_e(e)
|
191 |
|
|
try: next = xpath.parse(next)
|
192 |
|
|
except Parser.SyntaxError, e: wrap_e(e)
|
193 |
|
|
try: next = next[0].name
|
194 |
|
|
except IndexError, e: wrap_e(e)
|
195 |
2434
|
aaronmk
|
|
196 |
2506
|
aaronmk
|
return put_table_(path)
|
197 |
2434
|
aaronmk
|
|
198 |
2966
|
aaronmk
|
no_empty = set([in_table])
|
199 |
|
|
|
200 |
2434
|
aaronmk
|
# Remove any explicit pkey
|
201 |
2133
|
aaronmk
|
try: del row[pkey(out_table)]
|
202 |
2005
|
aaronmk
|
except KeyError: pass
|
203 |
|
|
|
204 |
2177
|
aaronmk
|
# Add fkey to parent
|
205 |
|
|
if parent_ids_loc != None:
|
206 |
2966
|
aaronmk
|
no_empty.add(parent_ids_loc.table)
|
207 |
2177
|
aaronmk
|
parent_ptr = node.getAttribute('fkey')
|
208 |
|
|
if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
|
209 |
|
|
row[parent_ptr] = parent_ids_loc
|
210 |
|
|
|
211 |
2060
|
aaronmk
|
# Divide fields into input columns and literal values
|
212 |
2506
|
aaronmk
|
parent_ids_loc = None # applies to this section
|
213 |
2313
|
aaronmk
|
in_tables = [in_table]
|
214 |
2060
|
aaronmk
|
for out_col, value in row.iteritems():
|
215 |
2432
|
aaronmk
|
# Handle forward pointers
|
216 |
|
|
if xml_dom.is_node(value): row[out_col] = value = put_table_(value)
|
217 |
|
|
|
218 |
|
|
# Translate values
|
219 |
2268
|
aaronmk
|
if isinstance(value, sql_gen.Col): # value is temp table column
|
220 |
|
|
in_tables.append(value.table)
|
221 |
2177
|
aaronmk
|
elif util.is_str(value) and value.startswith(input_col_prefix):
|
222 |
|
|
# value is input column
|
223 |
2272
|
aaronmk
|
row[out_col] = sql_gen.Col(strings.remove_prefix(input_col_prefix,
|
224 |
2313
|
aaronmk
|
value), in_table)
|
225 |
2177
|
aaronmk
|
else: # value is literal value; should only be string or None
|
226 |
|
|
assert util.is_str(value) or value == None
|
227 |
2323
|
aaronmk
|
row[out_col] = sql_gen.NamedCol(out_col, value)
|
228 |
2060
|
aaronmk
|
|
229 |
|
|
# Insert node
|
230 |
3077
|
aaronmk
|
pkeys_loc = sql_io.put_table(db, out_table, in_tables, row, row_ins_ct_ref,
|
231 |
2813
|
aaronmk
|
None, next, is_func, on_error)
|
232 |
2060
|
aaronmk
|
|
233 |
2975
|
aaronmk
|
sql.empty_temp(db, set(in_tables) - no_empty)
|
234 |
2966
|
aaronmk
|
|
235 |
2177
|
aaronmk
|
# Insert children with fkeys to parent
|
236 |
2506
|
aaronmk
|
parent_ids_loc = pkeys_loc # applies to this section
|
237 |
|
|
for child in children: put_table_(child)
|
238 |
2177
|
aaronmk
|
|
239 |
2423
|
aaronmk
|
# Count # rows and update in_row_ct_ref once all columns have been processed
|
240 |
|
|
if in_row_ct_ref != None:
|
241 |
2427
|
aaronmk
|
in_row_ct_ref[0] += sql.table_row_count(db, in_table)
|
242 |
2423
|
aaronmk
|
|
243 |
2133
|
aaronmk
|
return pkeys_loc
|