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 13 aaronmk
13 139 aaronmk
def name_of(node): return re.sub(r'^.*\.', r'', node.tagName)
14 16 aaronmk
15 13 aaronmk
ptr_suffix = '_id'
16
17 138 aaronmk
def is_ptr(node_name): return node_name.lower().endswith(ptr_suffix)
18 13 aaronmk
19 172 aaronmk
def ptr_type_guess(node_name):
20 13 aaronmk
    assert is_ptr(node_name)
21
    return node_name[:-len(ptr_suffix)]
22
23
def ptr_target(node):
24
    assert is_ptr(name_of(node))
25 46 aaronmk
    return xml_dom.first_elem(node)
26 13 aaronmk
27
def find_by_name(node, name):
28 46 aaronmk
    for parent in xml_dom.NodeParentIter(node):
29 13 aaronmk
        if name_of(parent) == name: return parent
30
        else:
31 46 aaronmk
            for child in xml_dom.NodeElemIter(parent):
32 16 aaronmk
                child_name = name_of(child)
33 13 aaronmk
                if is_ptr(child_name):
34 172 aaronmk
                    target = ptr_target(child)
35
                    if target.tagName == name: return target
36 13 aaronmk
                elif child_name == name: return child
37
    return None
38
39 1850 aaronmk
def get(db, node, limit=None, start=None):
40
    def pkey(table): return sql.pkey(db, table)
41 126 aaronmk
42 141 aaronmk
    node = node.firstChild
43 135 aaronmk
    table = name_of(node)
44
    pkey_ = pkey(table)
45
46
    fields = []
47
    conds = {}
48 133 aaronmk
    for child in xml_dom.NodeElemIter(node):
49 135 aaronmk
        child_name = name_of(child)
50
        if xml_dom.is_empty(child): fields.append(child_name)
51
        elif xml_dom.is_text(child): conds[child_name] = xml_dom.value(child)
52
        else: raise Exception('Joins not supported yet')
53
    id_ = xml_dom.get_id(node)
54 1836 aaronmk
    if id_ != None: conds[pkey(table)] = id_ # replace any existing pkey value
55 135 aaronmk
    if fields == []: fields.append(pkey_)
56 133 aaronmk
57 864 aaronmk
    return sql.select(db, table, fields, conds, limit, start)
58 126 aaronmk
59 1864 aaronmk
def put(db, node, row_ct_ref=None, on_error=exc.raise_, pool=None,
60
    store_ids=False, parent_id=None):
61 138 aaronmk
    '''store_ids enables searching the tree for missing fields'''
62 1850 aaronmk
    def pkey(table): return sql.pkey(db, table, True)
63 15 aaronmk
64 461 aaronmk
    def put_(node, parent_id=None):
65 1874 aaronmk
        args = (db, node, row_ct_ref, on_error, pool, store_ids, parent_id)
66
        if parent_id != None and pool != None: pool.apply_async(put, args)
67
        else: return put(*args)
68 461 aaronmk
69
    def on_error_(e):
70 446 aaronmk
        exc.add_msg(e, 'node:\n'+str(node))
71 461 aaronmk
        on_error(e)
72 446 aaronmk
73 48 aaronmk
    table = name_of(node)
74 446 aaronmk
    try: pkey_ = pkey(table)
75 461 aaronmk
    except sql.DatabaseErrors, e: on_error_(e); return None
76 48 aaronmk
    row = {}
77
    children = []
78 13 aaronmk
79 48 aaronmk
    # Divide children into fields and children with fkeys to parent
80
    for child in xml_dom.NodeElemIter(node):
81
        child_name = name_of(child)
82 463 aaronmk
        if xml_dom.is_empty(child): row[child_name] = None
83 454 aaronmk
        elif xml_dom.is_text(child):
84 84 aaronmk
            row[child_name] = strings.to_unicode(xml_dom.value(child))
85 461 aaronmk
        elif is_ptr(child_name): row[child_name] = put_(ptr_target(child))
86 48 aaronmk
        else: children.append(child)
87
    try: del row[pkey_]
88
    except KeyError: pass
89
90
    # Add fkey to parent
91 59 aaronmk
    if parent_id != None:
92
        parent_ptr = node.getAttribute('fkey')
93
        if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
94
        row[parent_ptr] = parent_id
95 48 aaronmk
96
    # Insert node
97 446 aaronmk
    try:
98
        for try_num in xrange(2):
99
            try:
100 472 aaronmk
                id_ = sql.put(db, table, row, pkey_, row_ct_ref)
101 446 aaronmk
                if store_ids: xml_dom.set_id(node, id_)
102
                break
103
            except sql.NullValueException, e:
104 468 aaronmk
                col = e.cols[0]
105 446 aaronmk
                if try_num > 0: raise # exception still raised after retry
106 468 aaronmk
                if store_ids and is_ptr(col):
107 446 aaronmk
                    # Search for required column in ancestors and their children
108 468 aaronmk
                    target = find_by_name(node, ptr_type_guess(col))
109 446 aaronmk
                    if target == None: raise
110 468 aaronmk
                    row[col] = xml_dom.get_id(target)
111 446 aaronmk
                else: raise
112 461 aaronmk
    except sql.DatabaseErrors, e: on_error_(e); return None
113 48 aaronmk
114
    # Insert children with fkeys to parent
115 1874 aaronmk
    for child in children: put_(child, id_)
116 48 aaronmk
117
    return id_
118 1996 aaronmk
119 2005 aaronmk
def put_table(db, node, in_table, commit=False, row_ct_ref=None,
120 1998 aaronmk
    table_is_esc=False):
121 1996 aaronmk
    '''
122 1998 aaronmk
    @param node The XML tree that transforms the input to the output. Similar to
123
        put()'s node param, but with the input column name prefixed by "$" in
124
        place of the column value.
125
    @param commit Whether to commit after each query
126
    '''
127 2005 aaronmk
    def pkey(table): return sql.pkey(db, table, True)
128
129
    out_table = name_of(node)
130
    pkey_ = pkey(out_table)
131
    row = {}
132
    children = []
133
134
    # Divide children into fields and children with fkeys to parent
135
    for child in xml_dom.NodeElemIter(node):
136
        child_name = name_of(child)
137
        if xml_dom.is_empty(child): row[child_name] = None
138
        elif xml_dom.is_text(child):
139
            row[child_name] = strings.to_unicode(xml_dom.value(child))
140
        elif is_ptr(child_name): pass#row[child_name] = put_(ptr_target(child))
141
        else: children.append(child)
142
    try: del row[pkey_]
143
    except KeyError: pass
144
145 1996 aaronmk
    import sys; sys.stderr.write(str(node))
146 1998 aaronmk
    if commit: db.db.commit()
147 1996 aaronmk
    raise NotImplementedError('By-column optimization not available yet')