Project

General

Profile

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

    
13
def name_of(node): return re.sub(r'^.*\.', r'', node.tagName)
14

    
15
ptr_suffix = '_id'
16

    
17
def value(node): return xml_dom.first_elem(node)
18

    
19
def is_ptr(node_name): return node_name.lower().endswith(ptr_suffix)
20

    
21
def ptr_type_guess(node_name):
22
    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
    return value(node)
28

    
29
def find_by_name(node, name):
30
    for parent in xml_dom.NodeParentIter(node):
31
        if name_of(parent) == name: return parent
32
        else:
33
            for child in xml_dom.NodeElemIter(parent):
34
                child_name = name_of(child)
35
                if is_ptr(child_name):
36
                    target = ptr_target(child)
37
                    if target.tagName == name: return target
38
                elif child_name == name: return child
39
    return None
40

    
41
def get(db, node, limit=None, start=None):
42
    def pkey(table): return sql.pkey(db, table)
43
    
44
    node = node.firstChild
45
    table = name_of(node)
46
    pkey_ = pkey(table)
47
    
48
    fields = []
49
    conds = {}
50
    for child in xml_dom.NodeElemIter(node):
51
        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
    if id_ != None: conds[pkey(table)] = id_ # replace any existing pkey value
57
    if fields == []: fields.append(pkey_)
58
    
59
    return sql.select(db, table, fields, conds, limit, start)
60

    
61
def put(db, node, row_ct_ref=None, on_error=exc.raise_, pool=None,
62
    store_ids=False, parent_id=None):
63
    '''store_ids enables searching the tree for missing fields'''
64
    def pkey(table): return sql.pkey(db, table, True)
65
    
66
    def put_(node, parent_id=None):
67
        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
    
71
    def on_error_(e):
72
        exc.add_msg(e, 'node:\n'+str(node))
73
        on_error(e)
74
    
75
    table = name_of(node)
76
    try: pkey_ = pkey(table)
77
    except sql.DatabaseErrors, e: on_error_(e); return None
78
    row = {}
79
    children = []
80
    
81
    # 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
        if xml_dom.is_empty(child): row[child_name] = None
85
        elif xml_dom.is_text(child):
86
            row[child_name] = strings.to_unicode(xml_dom.value(child))
87
        elif is_ptr(child_name): row[child_name] = put_(ptr_target(child))
88
        else: children.append(child)
89
    try: del row[pkey_]
90
    except KeyError: pass
91
    
92
    # Add fkey to parent
93
    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
    
98
    # Insert node
99
    try:
100
        for try_num in xrange(2):
101
            try:
102
                id_ = sql.put(db, table, row, pkey_, row_ct_ref)
103
                if store_ids: xml_dom.set_id(node, id_)
104
                break
105
            except sql.NullValueException, e:
106
                col = e.cols[0]
107
                if try_num > 0: raise # exception still raised after retry
108
                if store_ids and is_ptr(col):
109
                    # Search for required column in ancestors and their children
110
                    target = find_by_name(node, ptr_type_guess(col))
111
                    if target == None: raise
112
                    row[col] = xml_dom.get_id(target)
113
                else: raise
114
    except sql.DatabaseErrors, e: on_error_(e); return None
115
    
116
    # Insert children with fkeys to parent
117
    for child in children: put_(child, id_)
118
    
119
    return id_
120

    
121
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
def put_table(db, node, in_table, in_schema=None, commit=False,
130
    row_ct_ref=None):
131
    '''
132
    @param node The XML tree that transforms the input to the output. Similar to
133
        put()'s node param, but with the input column name prefixed by "$" in
134
        place of the column value.
135
    @param commit Whether to commit after each query
136
    @return tuple(table, col) Where the pkeys (from INSERT RETURNING) are made
137
        available
138
    '''
139
    def esc_name(name): return sql.esc_name(db, name)
140
    def qual_name(table): return sql.qual_name(db, in_schema, table)
141
    def pkey(table): return sql.pkey(db, table, True)
142
    
143
    def put_table_(node):
144
        return put_table(db, node, in_table, in_schema, commit, row_ct_ref)
145
    
146
    out_table = name_of(node)
147
    pkey_ = pkey(out_table)
148
    row = {}
149
    children = []
150
    in_tables = []
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
        elif is_ptr(child_name):
159
            table, row[child_name] = put_table_(ptr_target(child))
160
            in_tables.append(table)
161
        else: children.append(child)
162
    try: del row[pkey_]
163
    except KeyError: pass
164
    
165
    # Divide fields into input columns and literal values
166
    for out_col, value in row.iteritems():
167
        in_col = strings.remove_prefix('$', value)
168
        if in_col != value: row[out_col] = in_col # value is input column
169
        else: row[out_col] = (value, out_col) # value is literal value
170
    
171
    # Insert node
172
    in_tables.append(qual_name(in_table))
173
    pkeys_table = sql.put_table(db, esc_name(out_table), row.keys(),
174
        in_tables, row.values(), pkey_, row_ct_ref, table_is_esc=True)
175
    
176
    if commit: db.db.commit()
177
    return (pkeys_table, pkey_)
(9-9/33)