Project

General

Profile

1 13 aaronmk
# XML-database conversion
2
3 16 aaronmk
import re
4 13 aaronmk
from xml.dom import Node
5
6 46 aaronmk
import sql
7 84 aaronmk
import strings
8 133 aaronmk
import util
9 46 aaronmk
import xml_dom
10 13 aaronmk
11 139 aaronmk
def name_of(node): return re.sub(r'^.*\.', r'', node.tagName)
12 16 aaronmk
13 13 aaronmk
ptr_suffix = '_id'
14
15 138 aaronmk
def is_ptr(node_name): return node_name.lower().endswith(ptr_suffix)
16 13 aaronmk
17
def ptr_type(node_name):
18
    assert is_ptr(node_name)
19
    return node_name[:-len(ptr_suffix)]
20
21
def ptr_target(node):
22
    assert is_ptr(name_of(node))
23 46 aaronmk
    return xml_dom.first_elem(node)
24 13 aaronmk
25
def find_by_name(node, name):
26 46 aaronmk
    for parent in xml_dom.NodeParentIter(node):
27 13 aaronmk
        if name_of(parent) == name: return parent
28
        else:
29 46 aaronmk
            for child in xml_dom.NodeElemIter(parent):
30 16 aaronmk
                child_name = name_of(child)
31 13 aaronmk
                if is_ptr(child_name):
32
                    if ptr_type(child_name) == name: return ptr_target(child)
33
                elif child_name == name: return child
34
    return None
35
36 146 aaronmk
def get(db, node, pkeys=None, limit=None):
37 126 aaronmk
    if pkeys == None: pkeys = {}
38
    def pkey(table): return sql.pkey(db, pkeys, table)
39
40 141 aaronmk
    node = node.firstChild
41 135 aaronmk
    table = name_of(node)
42
    pkey_ = pkey(table)
43
44
    fields = []
45
    conds = {}
46 133 aaronmk
    for child in xml_dom.NodeElemIter(node):
47 135 aaronmk
        child_name = name_of(child)
48
        if xml_dom.is_empty(child): fields.append(child_name)
49
        elif xml_dom.is_text(child): conds[child_name] = xml_dom.value(child)
50
        else: raise Exception('Joins not supported yet')
51
    id_ = xml_dom.get_id(node)
52
    if id_ != '': conds[pkey(table)] = id_ # replace any existing pkey value
53
    if fields == []: fields.append(pkey_)
54 133 aaronmk
55 146 aaronmk
    return sql.select(db, table, fields, conds, limit)
56 126 aaronmk
57 48 aaronmk
def put(db, node, store_ids=False, row_ct_ref=None, pkeys=None, parent_id=None):
58 138 aaronmk
    '''store_ids enables searching the tree for missing fields'''
59 40 aaronmk
    if pkeys == None: pkeys = {}
60 126 aaronmk
    def pkey(table): return sql.pkey(db, pkeys, table)
61 15 aaronmk
62 48 aaronmk
    table = name_of(node)
63
    pkey_ = pkey(table)
64
    row = {}
65
    children = []
66 13 aaronmk
67 48 aaronmk
    # Divide children into fields and children with fkeys to parent
68
    for child in xml_dom.NodeElemIter(node):
69
        child_name = name_of(child)
70 84 aaronmk
        if xml_dom.is_text(child):
71
            row[child_name] = strings.to_unicode(xml_dom.value(child))
72 48 aaronmk
        elif is_ptr(child_name): row[child_name] = put(db, ptr_target(child),
73
            store_ids, row_ct_ref, pkeys)
74
        else: children.append(child)
75
    try: del row[pkey_]
76
    except KeyError: pass
77
78
    # Add fkey to parent
79 59 aaronmk
    if parent_id != None:
80
        parent_ptr = node.getAttribute('fkey')
81
        if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
82
        row[parent_ptr] = parent_id
83 48 aaronmk
84
    # Insert node
85 70 aaronmk
    for try_num in xrange(2):
86 48 aaronmk
        try:
87
            id_ = sql.get(db, table, row, pkey_, True, row_ct_ref)
88
            if store_ids: xml_dom.set_id(node, id_)
89
            break
90
        except sql.NullValueException, ex:
91
            if try_num > 0: raise # exception still raised after retry
92 138 aaronmk
            if store_ids and is_ptr(ex.col):
93 73 aaronmk
                # Search for required column in ancestors and their children
94
                target = find_by_name(node, ptr_type(ex.col))
95
                if target == None: raise
96
                row[ex.col] = xml_dom.get_id(target)
97
            else: raise
98 48 aaronmk
99
    # Insert children with fkeys to parent
100
    for child in children: put(db, child, store_ids, row_ct_ref, pkeys, id_)
101
102
    return id_
103 40 aaronmk
104
def xml2db(db, node, row_ct_ref=None):
105 48 aaronmk
    iter_ = xml_dom.NodeElemIter(node)
106 133 aaronmk
    util.skip(iter_, xml_dom.is_text) # skip metadata
107 138 aaronmk
    for child in iter_: put(db, child, False, row_ct_ref)