1
|
# XML-database conversion
|
2
|
|
3
|
import re
|
4
|
import traceback
|
5
|
from xml.dom import Node
|
6
|
|
7
|
import exc
|
8
|
import sql
|
9
|
import strings
|
10
|
import util
|
11
|
import xml_dom
|
12
|
import xml_func
|
13
|
|
14
|
def name_of(node): return re.sub(r'^.*\.', r'', node.tagName)
|
15
|
|
16
|
ptr_suffix = '_id'
|
17
|
|
18
|
def is_ptr(node_name): return node_name.lower().endswith(ptr_suffix)
|
19
|
|
20
|
def ptr_type_guess(node_name):
|
21
|
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
|
return xml_dom.value_node(node)
|
27
|
|
28
|
def find_by_name(node, name):
|
29
|
for parent in xml_dom.NodeParentIter(node):
|
30
|
if name_of(parent) == name: return parent
|
31
|
else:
|
32
|
for child in xml_dom.NodeElemIter(parent):
|
33
|
child_name = name_of(child)
|
34
|
if is_ptr(child_name):
|
35
|
target = ptr_target(child)
|
36
|
if target.tagName == name: return target
|
37
|
elif child_name == name: return child
|
38
|
return None
|
39
|
|
40
|
def get(db, node, limit=None, start=None):
|
41
|
def pkey(table): return sql.pkey(db, table)
|
42
|
|
43
|
node = node.firstChild
|
44
|
table = name_of(node)
|
45
|
pkey_ = pkey(table)
|
46
|
|
47
|
fields = []
|
48
|
conds = {}
|
49
|
for child in xml_dom.NodeElemIter(node):
|
50
|
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_ != None: conds[pkey(table)] = id_ # replace any existing pkey value
|
56
|
if fields == []: fields.append(pkey_)
|
57
|
|
58
|
return sql.select(db, table, fields, conds, limit, start)
|
59
|
|
60
|
def put(db, node, row_ct_ref=None, on_error=exc.raise_, pool=None,
|
61
|
store_ids=False, parent_id=None):
|
62
|
'''store_ids enables searching the tree for missing fields'''
|
63
|
def pkey(table): return sql.pkey(db, table, True)
|
64
|
|
65
|
def put_(node, parent_id=None):
|
66
|
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
|
|
70
|
def on_error_(e):
|
71
|
exc.add_msg(e, 'node:\n'+str(node))
|
72
|
on_error(e)
|
73
|
|
74
|
table = name_of(node)
|
75
|
try: pkey_ = pkey(table)
|
76
|
except sql.DatabaseErrors, e: on_error_(e); return None
|
77
|
row = {}
|
78
|
children = []
|
79
|
|
80
|
# 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
|
if xml_dom.is_empty(child): row[child_name] = None
|
84
|
elif xml_dom.is_text(child):
|
85
|
row[child_name] = strings.to_unicode(xml_dom.value(child))
|
86
|
elif is_ptr(child_name): row[child_name] = put_(ptr_target(child))
|
87
|
else: children.append(child)
|
88
|
try: del row[pkey_]
|
89
|
except KeyError: pass
|
90
|
|
91
|
# Add fkey to parent
|
92
|
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
|
|
97
|
# Insert node
|
98
|
try:
|
99
|
for try_num in xrange(2):
|
100
|
try:
|
101
|
id_ = sql.put(db, table, row, pkey_, row_ct_ref)
|
102
|
if store_ids: xml_dom.set_id(node, id_)
|
103
|
break
|
104
|
except sql.NullValueException, e:
|
105
|
col = e.cols[0]
|
106
|
if try_num > 0: raise # exception still raised after retry
|
107
|
if store_ids and is_ptr(col):
|
108
|
# Search for required column in ancestors and their children
|
109
|
target = find_by_name(node, ptr_type_guess(col))
|
110
|
if target == None: raise
|
111
|
row[col] = xml_dom.get_id(target)
|
112
|
else: raise
|
113
|
except sql.DatabaseErrors, e: on_error_(e); return None
|
114
|
|
115
|
# Insert children with fkeys to parent
|
116
|
for child in children: put_(child, id_)
|
117
|
|
118
|
return id_
|
119
|
|
120
|
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
|
def put_table(db, node, in_table, in_schema=None, commit=False,
|
129
|
row_ct_ref=None):
|
130
|
'''
|
131
|
@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
|
@return (table, col) Where the pkeys (from INSERT RETURNING) are made
|
136
|
available
|
137
|
'''
|
138
|
def esc_name(name): return sql.esc_name(db, name)
|
139
|
def qual_name(table): return sql.qual_name(db, in_schema, table)
|
140
|
def pkey(table): return sql.pkey(db, table, True)
|
141
|
|
142
|
def put_table_(node):
|
143
|
return put_table(db, node, in_table, in_schema, commit, row_ct_ref)
|
144
|
|
145
|
out_table = name_of(node)
|
146
|
row = {}
|
147
|
children = []
|
148
|
in_tables = [qual_name(in_table)]
|
149
|
|
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
|
else:
|
157
|
child_value = xml_dom.value_node(child)
|
158
|
if is_ptr(child_name) or xml_func.is_func(child_value):
|
159
|
table, col = put_table_(child_value)
|
160
|
row[child_name] = '$'+col
|
161
|
in_tables.append(table)
|
162
|
else: children.append(child)
|
163
|
try: del row[pkey(out_table)]
|
164
|
except KeyError: pass
|
165
|
|
166
|
# 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
|
pkeys_loc = sql.put_table(db, esc_name(out_table), in_tables, row,
|
174
|
row_ct_ref=row_ct_ref, table_is_esc=True)
|
175
|
|
176
|
if commit: db.db.commit()
|
177
|
return pkeys_loc
|