Project

General

Profile

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 46 aaronmk
import sql
9 2268 aaronmk
import sql_gen
10 84 aaronmk
import strings
11 133 aaronmk
import util
12 46 aaronmk
import xml_dom
13 2113 aaronmk
import xml_func
14 13 aaronmk
15 139 aaronmk
def name_of(node): return re.sub(r'^.*\.', r'', node.tagName)
16 16 aaronmk
17 13 aaronmk
ptr_suffix = '_id'
18
19 138 aaronmk
def is_ptr(node_name): return node_name.lower().endswith(ptr_suffix)
20 13 aaronmk
21 172 aaronmk
def ptr_type_guess(node_name):
22 13 aaronmk
    assert is_ptr(node_name)
23
    return node_name[:-len(ptr_suffix)]
24
25
def ptr_target(node):
26
    assert is_ptr(name_of(node))
27 2113 aaronmk
    return xml_dom.value_node(node)
28 13 aaronmk
29
def find_by_name(node, name):
30 46 aaronmk
    for parent in xml_dom.NodeParentIter(node):
31 13 aaronmk
        if name_of(parent) == name: return parent
32
        else:
33 46 aaronmk
            for child in xml_dom.NodeElemIter(parent):
34 16 aaronmk
                child_name = name_of(child)
35 13 aaronmk
                if is_ptr(child_name):
36 172 aaronmk
                    target = ptr_target(child)
37
                    if target.tagName == name: return target
38 13 aaronmk
                elif child_name == name: return child
39
    return None
40
41 1850 aaronmk
def get(db, node, limit=None, start=None):
42
    def pkey(table): return sql.pkey(db, table)
43 126 aaronmk
44 141 aaronmk
    node = node.firstChild
45 135 aaronmk
    table = name_of(node)
46
    pkey_ = pkey(table)
47
48
    fields = []
49
    conds = {}
50 133 aaronmk
    for child in xml_dom.NodeElemIter(node):
51 135 aaronmk
        child_name = name_of(child)
52
        if xml_dom.is_empty(child): fields.append(child_name)
53
        elif xml_dom.is_text(child): conds[child_name] = xml_dom.value(child)
54
        else: raise Exception('Joins not supported yet')
55
    id_ = xml_dom.get_id(node)
56 1836 aaronmk
    if id_ != None: conds[pkey(table)] = id_ # replace any existing pkey value
57 135 aaronmk
    if fields == []: fields.append(pkey_)
58 133 aaronmk
59 864 aaronmk
    return sql.select(db, table, fields, conds, limit, start)
60 126 aaronmk
61 1864 aaronmk
def put(db, node, row_ct_ref=None, on_error=exc.raise_, pool=None,
62
    store_ids=False, parent_id=None):
63 138 aaronmk
    '''store_ids enables searching the tree for missing fields'''
64 1850 aaronmk
    def pkey(table): return sql.pkey(db, table, True)
65 15 aaronmk
66 461 aaronmk
    def put_(node, parent_id=None):
67 1874 aaronmk
        args = (db, node, row_ct_ref, on_error, pool, store_ids, parent_id)
68
        if parent_id != None and pool != None: pool.apply_async(put, args)
69
        else: return put(*args)
70 461 aaronmk
71
    def on_error_(e):
72 446 aaronmk
        exc.add_msg(e, 'node:\n'+str(node))
73 461 aaronmk
        on_error(e)
74 446 aaronmk
75 48 aaronmk
    table = name_of(node)
76 446 aaronmk
    try: pkey_ = pkey(table)
77 461 aaronmk
    except sql.DatabaseErrors, e: on_error_(e); return None
78 48 aaronmk
    row = {}
79
    children = []
80 13 aaronmk
81 48 aaronmk
    # Divide children into fields and children with fkeys to parent
82
    for child in xml_dom.NodeElemIter(node):
83
        child_name = name_of(child)
84 463 aaronmk
        if xml_dom.is_empty(child): row[child_name] = None
85 454 aaronmk
        elif xml_dom.is_text(child):
86 84 aaronmk
            row[child_name] = strings.to_unicode(xml_dom.value(child))
87 461 aaronmk
        elif is_ptr(child_name): row[child_name] = put_(ptr_target(child))
88 48 aaronmk
        else: children.append(child)
89
    try: del row[pkey_]
90
    except KeyError: pass
91
92
    # Add fkey to parent
93 59 aaronmk
    if parent_id != None:
94
        parent_ptr = node.getAttribute('fkey')
95
        if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
96
        row[parent_ptr] = parent_id
97 48 aaronmk
98
    # Insert node
99 446 aaronmk
    try:
100
        for try_num in xrange(2):
101
            try:
102 472 aaronmk
                id_ = sql.put(db, table, row, pkey_, row_ct_ref)
103 446 aaronmk
                if store_ids: xml_dom.set_id(node, id_)
104
                break
105
            except sql.NullValueException, e:
106 468 aaronmk
                col = e.cols[0]
107 446 aaronmk
                if try_num > 0: raise # exception still raised after retry
108 468 aaronmk
                if store_ids and is_ptr(col):
109 446 aaronmk
                    # Search for required column in ancestors and their children
110 468 aaronmk
                    target = find_by_name(node, ptr_type_guess(col))
111 446 aaronmk
                    if target == None: raise
112 468 aaronmk
                    row[col] = xml_dom.get_id(target)
113 446 aaronmk
                else: raise
114 461 aaronmk
    except sql.DatabaseErrors, e: on_error_(e); return None
115 48 aaronmk
116
    # Insert children with fkeys to parent
117 1874 aaronmk
    for child in children: put_(child, id_)
118 48 aaronmk
119
    return id_
120 1996 aaronmk
121 2039 aaronmk
class ColRef:
122
    '''A reference to a table column'''
123
    def __init__(self, name, idx):
124
        self.name = name
125
        self.idx = idx
126
127
    def __str__(self): return self.name
128
129 2177 aaronmk
input_col_prefix = '$'
130
131 2417 aaronmk
def put_table(db, node, in_table, commit=False, row_ct_ref=None, limit=None,
132 2418 aaronmk
    start=0, parent_ids_loc=None):
133 1996 aaronmk
    '''
134 1998 aaronmk
    @param node The XML tree that transforms the input to the output. Similar to
135 2177 aaronmk
        put()'s node param, but with the input column name prefixed by
136
        input_col_prefix in place of the column value.
137 1998 aaronmk
    @param commit Whether to commit after each query
138 2133 aaronmk
    @return (table, col) Where the pkeys (from INSERT RETURNING) are made
139 2067 aaronmk
        available
140 1998 aaronmk
    '''
141 2418 aaronmk
    in_table = sql_gen.as_Table(in_table)
142
143
    if limit != None or start != 0:
144
        in_table = copy.copy(in_table) # don't modify input!
145
        sql.run_query_into(db, *sql.mk_select(db, in_table, limit=limit,
146
            start=start, order_by=None), into=in_table)
147
            # in_table won't be overwritten because of automatic versioning
148
149 2005 aaronmk
    def pkey(table): return sql.pkey(db, table, True)
150
151 2177 aaronmk
    def put_table_(node, parent_ids_loc=None):
152 2417 aaronmk
        return put_table(db, node, in_table, commit, row_ct_ref,
153
            parent_ids_loc=parent_ids_loc)
154 2088 aaronmk
155 2005 aaronmk
    out_table = name_of(node)
156
    row = {}
157
    children = []
158
159
    # Divide children into fields and children with fkeys to parent
160
    for child in xml_dom.NodeElemIter(node):
161
        child_name = name_of(child)
162
        if xml_dom.is_empty(child): row[child_name] = None
163
        elif xml_dom.is_text(child):
164
            row[child_name] = strings.to_unicode(xml_dom.value(child))
165 2113 aaronmk
        else:
166
            child_value = xml_dom.value_node(child)
167
            if is_ptr(child_name) or xml_func.is_func(child_value):
168 2157 aaronmk
                row[child_name] = put_table_(child_value)
169 2113 aaronmk
            else: children.append(child)
170 2133 aaronmk
    try: del row[pkey(out_table)]
171 2005 aaronmk
    except KeyError: pass
172
173 2177 aaronmk
    # Add fkey to parent
174
    if parent_ids_loc != None:
175
        parent_ptr = node.getAttribute('fkey')
176
        if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
177
        row[parent_ptr] = parent_ids_loc
178
179 2060 aaronmk
    # Divide fields into input columns and literal values
180 2313 aaronmk
    in_tables = [in_table]
181 2060 aaronmk
    for out_col, value in row.iteritems():
182 2268 aaronmk
        if isinstance(value, sql_gen.Col): # value is temp table column
183
            in_tables.append(value.table)
184 2177 aaronmk
        elif util.is_str(value) and value.startswith(input_col_prefix):
185
            # value is input column
186 2272 aaronmk
            row[out_col] = sql_gen.Col(strings.remove_prefix(input_col_prefix,
187 2313 aaronmk
                value), in_table)
188 2177 aaronmk
        else: # value is literal value; should only be string or None
189
            assert util.is_str(value) or value == None
190 2323 aaronmk
            row[out_col] = sql_gen.NamedCol(out_col, value)
191 2060 aaronmk
192
    # Insert node
193 2346 aaronmk
    db.log_debug('Putting columns: '+str(row))
194 2416 aaronmk
    pkeys_loc = sql.put_table(db, out_table, in_tables, row, row_ct_ref)
195 2182 aaronmk
    if commit: db.db.commit()
196 2060 aaronmk
197 2177 aaronmk
    # Insert children with fkeys to parent
198
    for child in children: put_table_(child, pkeys_loc)
199
200 2133 aaronmk
    return pkeys_loc