Project

General

Profile

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 2177 aaronmk
input_col_prefix = '$'
129
130 2195 aaronmk
def put_table(db, node, in_table, in_schema=None, limit=None, start=0,
131
    commit=False, row_ct_ref=None, parent_ids_loc=None):
132 1996 aaronmk
    '''
133 1998 aaronmk
    @param node The XML tree that transforms the input to the output. Similar to
134 2177 aaronmk
        put()'s node param, but with the input column name prefixed by
135
        input_col_prefix in place of the column value.
136 1998 aaronmk
    @param commit Whether to commit after each query
137 2133 aaronmk
    @return (table, col) Where the pkeys (from INSERT RETURNING) are made
138 2067 aaronmk
        available
139 1998 aaronmk
    '''
140 2079 aaronmk
    def esc_name(name): return sql.esc_name(db, name)
141 2060 aaronmk
    def qual_name(table): return sql.qual_name(db, in_schema, table)
142 2005 aaronmk
    def pkey(table): return sql.pkey(db, table, True)
143
144 2177 aaronmk
    def put_table_(node, parent_ids_loc=None):
145 2195 aaronmk
        return put_table(db, node, in_table, in_schema, limit, start, commit,
146
            row_ct_ref, parent_ids_loc)
147 2088 aaronmk
148 2005 aaronmk
    out_table = name_of(node)
149
    row = {}
150
    children = []
151
152
    # Divide children into fields and children with fkeys to parent
153
    for child in xml_dom.NodeElemIter(node):
154
        child_name = name_of(child)
155
        if xml_dom.is_empty(child): row[child_name] = None
156
        elif xml_dom.is_text(child):
157
            row[child_name] = strings.to_unicode(xml_dom.value(child))
158 2113 aaronmk
        else:
159
            child_value = xml_dom.value_node(child)
160
            if is_ptr(child_name) or xml_func.is_func(child_value):
161 2157 aaronmk
                row[child_name] = put_table_(child_value)
162 2113 aaronmk
            else: children.append(child)
163 2133 aaronmk
    try: del row[pkey(out_table)]
164 2005 aaronmk
    except KeyError: pass
165
166 2177 aaronmk
    # Add fkey to parent
167
    if parent_ids_loc != None:
168
        parent_ptr = node.getAttribute('fkey')
169
        if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
170
        row[parent_ptr] = parent_ids_loc
171
172 2060 aaronmk
    # Divide fields into input columns and literal values
173 2177 aaronmk
    in_tables = [qual_name(in_table)]
174 2060 aaronmk
    for out_col, value in row.iteritems():
175 2177 aaronmk
        if isinstance(value, tuple): # value is temp table column
176
            in_tables.append(value[0])
177
        elif util.is_str(value) and value.startswith(input_col_prefix):
178
            # value is input column
179
            row[out_col] = strings.remove_prefix(input_col_prefix, value)
180
        else: # value is literal value; should only be string or None
181
            assert util.is_str(value) or value == None
182
            row[out_col] = (value,) # value is literal value
183 2060 aaronmk
184
    # Insert node
185 2196 aaronmk
    pkeys_loc = sql.put_table(db, esc_name(out_table), in_tables, row, limit,
186
        start, row_ct_ref=row_ct_ref, table_is_esc=True)
187 2182 aaronmk
    if commit: db.db.commit()
188 2060 aaronmk
189 2177 aaronmk
    # Insert children with fkeys to parent
190
    for child in children: put_table_(child, pkeys_loc)
191
192 2133 aaronmk
    return pkeys_loc